Skip to content

Commit

Permalink
Enable exception catching; Separate build targets for node and `wor…
Browse files Browse the repository at this point in the history
…ker` environments. (#11)
  • Loading branch information
junhaoliao authored Nov 1, 2024
1 parent 9e82372 commit 82079a5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 59 deletions.
123 changes: 73 additions & 50 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
20 changes: 14 additions & 6 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand All @@ -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}}" \
Expand Down
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}

0 comments on commit 82079a5

Please sign in to comment.