From 82079a5382230da5fe1b1cfbc0c1a4f66f34b98f Mon Sep 17 00:00:00 2001 From: Junhao Liao Date: Thu, 31 Oct 2024 20:57:52 -0400 Subject: [PATCH] Enable exception catching; Separate build targets for `node` and `worker` environments. (#11) --- CMakeLists.txt | 123 +++++++++++++++++++++++++++++-------------------- Taskfile.yml | 20 +++++--- package.json | 15 ++++-- 3 files changed, 99 insertions(+), 59 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 80568fe2..0fab4c3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,59 +58,28 @@ message(STATUS "Fetching Boost.") FetchContent_MakeAvailable(Boost) message("Boost sources successfully fetched into ${boost_SOURCE_DIR}") -set(CLP_FFI_JS_BIN_NAME - "ClpFfiJs" - CACHE STRING - "Binary name for the generated .js and .wasm files." -) -add_executable(${CLP_FFI_JS_BIN_NAME}) - -target_compile_features(${CLP_FFI_JS_BIN_NAME} PRIVATE cxx_std_20) - set(CMAKE_EXECUTABLE_SUFFIX ".js" CACHE STRING "Binary type to be generated by Emscripten.") +# Set up common compile and link options to be merged with other options as necessary. +set(CLP_FFI_JS_COMMON_COMPILE_OPTIONS + -fwasm-exceptions +) +set(CLP_FFI_JS_COMMON_LINK_OPTIONS + -fwasm-exceptions + -sALLOW_MEMORY_GROWTH + -sEXPORT_ES6 + -sMODULARIZE + -sWASM_BIGINT +) if(CMAKE_BUILD_TYPE MATCHES "Release") - set(CLP_FFI_JS_EXTRA_LINKER_FLAGS + list(APPEND CLP_FFI_JS_COMMON_COMPILE_OPTIONS + -flto + ) + list(APPEND CLP_FFI_JS_COMMON_LINK_OPTIONS -flto --closure=1 - -sENVIRONMENT=worker ) -else() - set(CLP_FFI_JS_EXTRA_LINKER_FLAGS -sENVIRONMENT=node) endif() -message( - "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}: Extra linker flags: ${CLP_FFI_JS_EXTRA_LINKER_FLAGS}" -) -target_link_options( - ${CLP_FFI_JS_BIN_NAME} - PRIVATE - ${CLP_FFI_JS_EXTRA_LINKER_FLAGS} - -sALLOW_MEMORY_GROWTH - -sEXPORT_ES6 - -sMODULARIZE - -sWASM_BIGINT - --emit-tsd ${CLP_FFI_JS_BIN_NAME}.d.ts -) -target_link_libraries(${CLP_FFI_JS_BIN_NAME} PRIVATE embind) - -target_compile_definitions(${CLP_FFI_JS_BIN_NAME} PUBLIC SPDLOG_FMT_EXTERNAL=1) - -# NOTE: We mark the include directories below as system headers so that the compiler (including -# `clang-tidy`) doesn't generate warnings from them. -target_include_directories( - ${CLP_FFI_JS_BIN_NAME} - SYSTEM - PRIVATE - ${boost_SOURCE_DIR} - src/submodules/clp/components/core/src - src/submodules/clp/components/core/src/clp - src/submodules/clp/components/core/submodules - src/submodules/fmt/include - src/submodules/spdlog/include - src/submodules/zstd/lib -) - -target_include_directories(${CLP_FFI_JS_BIN_NAME} PRIVATE src/) set(CLP_FFI_JS_SRC_MAIN src/clp_ffi_js/ir/decoding_methods.cpp @@ -142,11 +111,65 @@ set(CLP_FFI_JS_SRC_ZSTD src/submodules/zstd/lib/decompress/zstd_decompress.c ) -target_sources( - ${CLP_FFI_JS_BIN_NAME} - PRIVATE +set(CLP_FFI_JS_SUPPORTED_ENVIRONMENTS + node + worker + CACHE INTERNAL + "List of supported environments." +) + +foreach(env ${CLP_FFI_JS_SUPPORTED_ENVIRONMENTS}) + set(CLP_FFI_JS_BIN_NAME "ClpFfiJs-${env}") + add_executable(${CLP_FFI_JS_BIN_NAME}) + + # Set up compile options + target_compile_features(${CLP_FFI_JS_BIN_NAME} PRIVATE cxx_std_20) + target_compile_definitions(${CLP_FFI_JS_BIN_NAME} PUBLIC SPDLOG_FMT_EXTERNAL=1) + target_compile_options(${CLP_FFI_JS_BIN_NAME} PRIVATE ${CLP_FFI_JS_COMMON_COMPILE_OPTIONS}) + + # Set up link options + target_link_libraries(${CLP_FFI_JS_BIN_NAME} PRIVATE embind) + set(CLP_FFI_JS_LINK_OPTIONS + ${CLP_FFI_JS_COMMON_LINK_OPTIONS} + --emit-tsd=${CLP_FFI_JS_BIN_NAME}.d.ts + -sENVIRONMENT=${env} + ) + target_link_options( + ${CLP_FFI_JS_BIN_NAME} + PRIVATE + ${CLP_FFI_JS_LINK_OPTIONS} + ) + + message( + "CLP_FFI_JS_BIN_NAME=\"${CLP_FFI_JS_BIN_NAME}\". \ +CMAKE_BUILD_TYPE=\"${CMAKE_BUILD_TYPE}\". \ +Compile options: ${CLP_FFI_JS_COMMON_COMPILE_OPTIONS}. \ +Link options: ${CLP_FFI_JS_LINK_OPTIONS}." + ) + + # NOTE: We mark the include directories below as system headers so that the compiler (including + # `clang-tidy`) doesn't generate warnings from them. + target_include_directories( + ${CLP_FFI_JS_BIN_NAME} + SYSTEM + PRIVATE + ${boost_SOURCE_DIR} + src/submodules/clp/components/core/src + src/submodules/clp/components/core/src/clp + src/submodules/clp/components/core/submodules + src/submodules/fmt/include + src/submodules/spdlog/include + src/submodules/zstd/lib + ) + + target_include_directories(${CLP_FFI_JS_BIN_NAME} PRIVATE src/) + + target_sources( + ${CLP_FFI_JS_BIN_NAME} + PRIVATE ${CLP_FFI_JS_SRC_MAIN} ${CLP_FFI_JS_SRC_CLP_CORE} ${CLP_FFI_JS_SRC_FMT} ${CLP_FFI_JS_SRC_ZSTD} -) + ) +endforeach() diff --git a/Taskfile.yml b/Taskfile.yml index 13b600a5..a4623251 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -8,7 +8,8 @@ vars: G_BUILD_DIR: "{{.ROOT_DIR}}/build" G_CLP_FFI_JS_BUILD_DIR: "{{.G_BUILD_DIR}}/clp-ffi-js" G_CLP_FFI_JS_CHECKSUM: "{{.G_BUILD_DIR}}/clp-ffi-js.md5" - G_CLP_FFI_JS_TARGET_NAME: "ClpFfiJs" + G_CLP_FFI_JS_ENV_NAMES: ["node", "worker"] + G_CLP_FFI_JS_TARGET_PREFIX: "ClpFfiJs-" G_DIST_DIR: "{{.ROOT_DIR}}/dist" G_EMSDK_DIR: "{{.G_BUILD_DIR}}/emsdk" G_EMSDK_CHECKSUM: "{{.G_BUILD_DIR}}/emsdk.md5" @@ -50,7 +51,11 @@ tasks: DATA_DIR: "{{.OUTPUT_DIR}}" cmds: - task: "config-cmake-project" - - "cmake --build '{{.OUTPUT_DIR}}' --parallel --target '{{.G_CLP_FFI_JS_TARGET_NAME}}'" + - >- + cmake + --build '{{.OUTPUT_DIR}}' + --parallel + --target {{range .G_CLP_FFI_JS_ENV_NAMES}}"{{$.G_CLP_FFI_JS_TARGET_PREFIX}}{{.}}" {{end}} # This command must be last - task: "utils:compute-checksum" vars: @@ -115,10 +120,12 @@ tasks: DATA_DIR: "{{.OUTPUT_DIR}}" cmds: - "rm -rf {{.OUTPUT_DIR}}" - - >- - rsync -a - "{{.G_CLP_FFI_JS_BUILD_DIR}}/{{.G_CLP_FFI_JS_TARGET_NAME}}."{d.ts,js,wasm} - "{{.OUTPUT_DIR}}/" + - for: + var: "G_CLP_FFI_JS_ENV_NAMES" + cmd: >- + rsync -a + "{{.G_CLP_FFI_JS_BUILD_DIR}}/{{.G_CLP_FFI_JS_TARGET_PREFIX}}{{.ITEM}}."{d.ts,js,wasm} + "{{.OUTPUT_DIR}}/" - "npm pack" # This command must be last - task: "utils:compute-checksum" @@ -138,6 +145,7 @@ tasks: deps: ["emsdk"] cmd: |- cmake \ + -DCLP_FFI_JS_SUPPORTED_ENVIRONMENTS="{{.G_CLP_FFI_JS_ENV_NAMES | join ";"}}" \ -DCMAKE_TOOLCHAIN_FILE="{{.G_EMSDK_DIR}}/upstream/emscripten/cmake/Modules/Platform/\ Emscripten.cmake" \ -S "{{.ROOT_DIR}}" \ diff --git a/package.json b/package.json index 0eff9bf8..8f11d7e8 100644 --- a/package.json +++ b/package.json @@ -15,13 +15,22 @@ "scripts": { "release": "git diff --exit-code && npm publish" }, + "type": "module", "files": [ - "./dist" + "dist" ], "exports": { ".": { - "import": "./dist/ClpFfiJs.js", - "types": "./dist/ClpFfiJs.d.ts" + "import": "./dist/ClpFfiJs-worker.js", + "types": "./dist/ClpFfiJs-worker.d.ts" + }, + "./node": { + "import": "./dist/ClpFfiJs-node.js", + "types": "./dist/ClpFfiJs-node.d.ts" + }, + "./worker": { + "import": "./dist/ClpFfiJs-worker.js", + "types": "./dist/ClpFfiJs-worker.d.ts" } } }