Files
basis_universal/webgl/encoder/CMakeLists.txt
T
Richard GeldreichandClaude Opus 5.5 9459482681 Build: compile Wuffs in its own translation unit (encoder/3rdparty/basisu_wuffs.cpp)
basisu_wuffs.cpp defines WUFFS_IMPLEMENTATION and #includes the unmodified
encoder/3rdparty/wuffs-v0.4.c with all warnings silenced in that TU only
(MSVC #pragma warning(push, 0)/pop; gcc/clang diagnostic push/ignored/pop).
basisu_wuffs_config.h holds the Wuffs configuration (modules BASE core/
interfaces/pixconv, ADLER32, CRC32, DEFLATE, ZLIB, PNG, JPEG, TARGA, QOI;
RGBA_NONPREMUL destination only; WUFFS_CONFIG__AVOID_CPU_ARCH = no SIMD), shared
with basisu_enc.cpp's declarations-only include (next commit). With
BASISU_USE_WUFFS_LDR_LOADER=0 the TU compiles to nothing.

Added to every build that compiles the encoder: root CMakeLists.txt
(native, -m32, WASI, Python module) with a BASISU_USE_WUFFS_LDR_LOADER option
(default ON), webgl/encoder/CMakeLists.txt (all Emscripten variants, same
option), and encoder_lib.vcxproj/.filters (basisu.sln: basisu, examples).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 16:55:17 -04:00

206 lines
8.9 KiB
CMake

cmake_minimum_required(VERSION 3.5)
project(basisu_encoder_js)
# Toggle Zstd support for KTX2 (ON by default).
option(KTX2_ZSTANDARD "Enable KTX2 Zstandard support" TRUE)
option(SUPPORT_ASTCENC "Enable astcenc support" FALSE)
option(BASISU_USE_WUFFS_LDR_LOADER "Load .PNG/.JPG/.QOI source images with Wuffs; OFF = the older built-in decoders" TRUE)
# Build JavaScript glue that never calls eval() or new Function(), so the module
# loads under a Content-Security-Policy without 'unsafe-eval' (OFF by default).
option(NO_DYNAMIC_EXECUTION "Disallow eval()/new Function() in the JS glue" FALSE)
# With NO_DYNAMIC_EXECUTION, generate the embind call wrappers at build time (fast)
# instead of using the slower generic wrappers. No effect unless NO_DYNAMIC_EXECUTION is ON.
option(EMBIND_AOT "Generate embind wrappers at build time" TRUE)
message("KTX2_ZSTANDARD=${KTX2_ZSTANDARD}")
message("SUPPORT_ASTCENC=${SUPPORT_ASTCENC}")
message("BASISU_USE_WUFFS_LDR_LOADER=${BASISU_USE_WUFFS_LDR_LOADER}")
message("NO_DYNAMIC_EXECUTION=${NO_DYNAMIC_EXECUTION}")
message("EMBIND_AOT=${EMBIND_AOT}")
# Only for Emscripten builds.
if(EMSCRIPTEN)
set(CMAKE_CXX_STANDARD 17)
# ---------- Pick config once (single-config generators) ----------
# Supports: Release (default), Debug, SAN
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
string(TOUPPER "${CMAKE_BUILD_TYPE}" BUILD_MODE)
# Per-config compile/link flags
set(CONFIG_CFLAGS "")
set(CONFIG_DEFS "")
set(CONFIG_LINK "")
if(BUILD_MODE STREQUAL "RELEASE")
set(CONFIG_CFLAGS "-O3")
set(CONFIG_DEFS "NDEBUG")
set(CONFIG_LINK "-O3 -s ASSERTIONS=0")
elseif(BUILD_MODE STREQUAL "DEBUG")
set(CONFIG_CFLAGS "-g -O0")
set(CONFIG_DEFS "DEBUG")
set(CONFIG_LINK "-g -s ASSERTIONS=2")
elseif(BUILD_MODE STREQUAL "SAN")
set(CONFIG_CFLAGS "-g -O1 -fsanitize=undefined -fsanitize=address")
set(CONFIG_DEFS "DEBUG")
set(CONFIG_LINK "-g -s ASSERTIONS=2 -fsanitize=undefined -fsanitize=address")
else()
message(WARNING "Unknown CMAKE_BUILD_TYPE='${CMAKE_BUILD_TYPE}', defaulting to Release-like flags.")
set(CONFIG_CFLAGS "-O3")
set(CONFIG_DEFS "NDEBUG")
set(CONFIG_LINK "-O3 -s ASSERTIONS=0")
endif()
# ---------- Sources (shared) ----------
set(SRC_LIST
../transcoder/basis_wrappers.cpp
../../transcoder/basisu_transcoder.cpp
../../encoder/basisu_backend.cpp
../../encoder/basisu_basis_file.cpp
../../encoder/basisu_comp.cpp
../../encoder/basisu_enc.cpp
../../encoder/basisu_etc.cpp
../../encoder/basisu_frontend.cpp
../../encoder/basisu_gpu_texture.cpp
../../encoder/basisu_pvrtc1_4.cpp
../../encoder/basisu_resampler.cpp
../../encoder/basisu_resample_filters.cpp
../../encoder/basisu_ssim.cpp
../../encoder/basisu_uastc_enc.cpp
../../encoder/basisu_bc7e_scalar.cpp
../../encoder/basisu_dds_export.cpp
../../encoder/basisu_bc7enc.cpp
../../encoder/basisu_kernels_sse.cpp
../../encoder/basisu_opencl.cpp
../../encoder/pvpngreader.cpp
../../encoder/jpgd.cpp
../../encoder/3rdparty/basisu_wuffs.cpp
../../encoder/3rdparty/android_astc_decomp.cpp
../../encoder/basisu_uastc_hdr_4x4_enc.cpp
../../encoder/basisu_astc_hdr_6x6_enc.cpp
../../encoder/basisu_astc_hdr_common.cpp
../../encoder/basisu_astc_ldr_common.cpp
../../encoder/basisu_astc_ldr_encode.cpp
../../encoder/basisu_astc_ldr_fencode.cpp
../../encoder/basisu_tinyexr.cpp
../../encoder/basisu_xbc7_encode.cpp
)
if(KTX2_ZSTANDARD)
list(APPEND SRC_LIST ../../zstd/zstd.c)
set(ZSTD_DEFINITION BASISD_SUPPORT_KTX2_ZSTD=1)
else()
set(ZSTD_DEFINITION BASISD_SUPPORT_KTX2_ZSTD=0)
endif()
if(SUPPORT_ASTCENC)
list(APPEND SRC_LIST
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_averages_and_directions.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_block_sizes.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_color_quantize.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_color_unquantize.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_compress_symbolic.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_compute_variance.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_decompress_symbolic.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_diagnostic_trace.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_entry.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_find_best_partitioning.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_ideal_endpoints_and_weights.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_image.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_integer_sequence.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_mathlib.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_mathlib_softfloat.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_partition_tables.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_percentile_tables.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_pick_best_endpoint_format.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_quantization.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_symbolic_physical.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_weight_align.cpp
../../encoder/3rdparty/astc-encoder-main/Source/astcenc_weight_quant_xfer_tables.cpp
)
set(ASTCENC_DEFINITION BASISU_SUPPORT_ASTCENC=1)
else()
set(ASTCENC_DEFINITION BASISU_SUPPORT_ASTCENC=0)
endif()
if(BASISU_USE_WUFFS_LDR_LOADER)
set(WUFFS_LDR_DEFINITION BASISU_USE_WUFFS_LDR_LOADER=1)
else()
set(WUFFS_LDR_DEFINITION BASISU_USE_WUFFS_LDR_LOADER=0)
endif()
# Common preprocessor defines (same as your original)
set(COMMON_DEFS
BASISD_SUPPORT_UASTC=1
BASISD_SUPPORT_BC7=1
BASISD_SUPPORT_ATC=0
BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY=0
BASISD_SUPPORT_PVRTC2=0
BASISD_SUPPORT_FXT1=0
BASISD_SUPPORT_ETC2_EAC_RG11=0
BASISU_SUPPORT_ENCODING=1
BASISU_SUPPORT_SSE=0
BASISD_SUPPORT_XUASTC=1
${ZSTD_DEFINITION}
${ASTCENC_DEFINITION}
${WUFFS_LDR_DEFINITION}
)
# Base link flags
set(LINK_BASE "--bind -s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=134217728 -s STACK_SIZE=2097152 -s MODULARIZE=1 -s EXPORT_NAME=BASIS -s EXPORTED_RUNTIME_METHODS=['HEAP8']")
if(NO_DYNAMIC_EXECUTION)
set(LINK_BASE "${LINK_BASE} -s DYNAMIC_EXECUTION=0")
if(EMBIND_AOT)
set(LINK_BASE "${LINK_BASE} -s EMBIND_AOT=1")
endif()
endif()
# INCOMING_MODULE_JS_API: Emscripten's default list plus mainScriptUrlOrBlob, which newer Emscripten (6.0.x) no longer accepts by default.
# ktx2_studio/encode-worker.js loads the threaded encoder via importScripts() and must pass mainScriptUrlOrBlob, otherwise the pthread
# pool is spawned from the wrong script and the encoder module never finishes loading (silent hang).
set(LINK_THREADS "-s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=32 -s ENVIRONMENT=web,worker -s INCOMING_MODULE_JS_API=['ENVIRONMENT','arguments','canvas','dynamicLibraries','elementPointerLock','instantiateWasm','locateFile','monitorRunDependencies','noExitRuntime','noInitialRun','onAbort','onExit','onRuntimeInitialized','postRun','preInit','preRun','print','printErr','setStatus','statusMessage','stderr','stdin','stdout','thisProgram','wasm','websocket','mainScriptUrlOrBlob']")
set(LINK_WASM64 "-s MEMORY64=1 -sWASM_BIGINT=1 --profiling-funcs")
# Helper to avoid repetition
function(add_encoder target out_name use_threads use_wasm64)
add_executable(${target} ${SRC_LIST})
set_target_properties(${target} PROPERTIES OUTPUT_NAME "${out_name}" SUFFIX ".js")
target_include_directories(${target} PRIVATE ../../transcoder)
# Compile defs and options
target_compile_definitions(${target} PRIVATE ${COMMON_DEFS} ${CONFIG_DEFS})
target_compile_options(${target} PRIVATE -fno-strict-aliasing ${CONFIG_CFLAGS})
if(${use_threads})
target_compile_options(${target} PRIVATE -matomics -mbulk-memory)
target_compile_definitions(${target} PRIVATE WASM_THREADS_ENABLED=1)
endif()
if(${use_wasm64})
target_compile_options(${target} PRIVATE -s MEMORY64=1)
endif()
# Link flags (no generator expressions)
set(_lf "${LINK_BASE} ${CONFIG_LINK}")
if(${use_threads})
set(_lf "${_lf} ${LINK_THREADS}")
endif()
if(${use_wasm64})
set(_lf "${_lf} ${LINK_WASM64} -s INITIAL_MEMORY=134217728 -s MAXIMUM_MEMORY=16106127360")
endif()
set_target_properties(${target} PROPERTIES LINK_FLAGS "${_lf}")
endfunction()
# The three outputs (names unchanged)
add_encoder(basis_encoder.js "basis_encoder" OFF OFF) # wasm32
add_encoder(basis_encoder_threads.js "basis_encoder_threads" ON OFF) # wasm32 + threads
add_encoder(basis_encoder_threads_wasm64.js "basis_encoder_threads_wasm64" ON ON ) # wasm64 + threads
endif()