Skip to content

Commit

Permalink
#877 api: wasm: indigo-ketcher: prepare 2-files wasm+js package (#1921)
Browse files Browse the repository at this point in the history
  • Loading branch information
even1024 authored Jun 25, 2024
1 parent 99c10b8 commit ee45082
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .ci/version.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1.22.0
dev
1
2

53ffd477e36c5f4ee52621c2fbd8583f64abe9111e098b6226c73fbd6e409670
65 changes: 46 additions & 19 deletions api/wasm/indigo-ketcher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,59 @@ project(indigo-ketcher)
add_library(${PROJECT_NAME}-object OBJECT indigo-ketcher.cpp)
target_link_libraries(${PROJECT_NAME}-object indigo-static indigo-inchi-static indigo-renderer-static cppcodec)

set(EMCC_FLAGS
if (CMAKE_BUILD_TYPE STREQUAL Debug)
set(EMCC_FLAGS
-g
)
else ()
set(EMCC_FLAGS
-Oz
)
endif ()

set(EMCC_COMMON_FLAGS
--bind
-s INITIAL_MEMORY=32mb
-s ALLOW_MEMORY_GROWTH=1
-s DISABLE_EXCEPTION_CATCHING=0
-s SINGLE_FILE=1
-s MODULARIZE=1
-s FILESYSTEM=1
-s ASSERTIONS=1
-s USE_SDL=0 -s USE_SDL_IMAGE=0 -s USE_SDL_TTF=0 -s USE_SDL_NET=0
--no-entry)
if (CMAKE_BUILD_TYPE STREQUAL Debug)
list(APPEND EMCC_FLAGS
-g
)
elseif (CMAKE_BUILD_TYPE STREQUAL Release)
list(APPEND EMCC_FLAGS
-Oz
)
endif ()
--no-entry
${EMCC_FLAGS}
)

set(EMCC_FLAGS_SEPARATE
${EMCC_COMMON_FLAGS}
-s SINGLE_FILE=0
)

set(EMCC_FLAGS
${EMCC_COMMON_FLAGS}
-s SINGLE_FILE=1
)

set(TARGET_FILES
$<TARGET_OBJECTS:${PROJECT_NAME}-object>
$<TARGET_FILE:indigo-static> $<TARGET_FILE:indigo-inchi-static> $<TARGET_FILE:indigo-renderer-static> $<TARGET_FILE:render2d> $<TARGET_FILE:cairo> $<TARGET_FILE:freetype> $<TARGET_FILE:png> $<TARGET_FILE:pixman> $<TARGET_FILE:indigo-core> $<TARGET_FILE:inchi> $<TARGET_FILE:tinyxml2> $<TARGET_FILE:z>
)

add_custom_target(${PROJECT_NAME}-separate
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
COMMAND emcc
-o ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/indigo-ketcher-separate-wasm.js
${EMCC_FLAGS_SEPARATE}
${TARGET_FILES}
DEPENDS ${PROJECT_NAME}-object
)

add_custom_target(${PROJECT_NAME}
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
COMMAND emcc
-o ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/indigo-ketcher.js
${EMCC_FLAGS}
$<TARGET_OBJECTS:${PROJECT_NAME}-object>
$<TARGET_FILE:indigo-static> $<TARGET_FILE:indigo-inchi-static> $<TARGET_FILE:indigo-renderer-static> $<TARGET_FILE:render2d> $<TARGET_FILE:cairo> $<TARGET_FILE:freetype> $<TARGET_FILE:png> $<TARGET_FILE:pixman> $<TARGET_FILE:indigo-core> $<TARGET_FILE:inchi> $<TARGET_FILE:tinyxml2> $<TARGET_FILE:z>
${TARGET_FILES}
DEPENDS ${PROJECT_NAME}-object
)

Expand All @@ -43,12 +68,13 @@ if (NOT RENDER_ENABLE_CJK)
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/LICENSE ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/
COMMAND npm install
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/test/ ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/
WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_DIR}/patch.cmake
COMMAND npm test
COMMAND npm pack
COMMAND ${CMAKE_COMMAND} -E make_directory ${DIST_DIRECTORY}
COMMAND ${COPY_COMMAND} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}${SEP}${PROJECT_NAME}*.tgz ${NATIVE_DIST_DIRECTORY}${SEP}
WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
DEPENDS ${PROJECT_NAME}
DEPENDS ${PROJECT_NAME} ${PROJECT_NAME}-separate
)
else ()
add_custom_target(${PROJECT_NAME}-package
Expand All @@ -58,12 +84,13 @@ else ()
COMMAND npm install
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/test/ ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/test-cjk/ ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/
WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_DIR}/patch.cmake
COMMAND npm test
COMMAND npm run test_cjk
COMMAND npm pack
COMMAND ${CMAKE_COMMAND} -E make_directory ${DIST_DIRECTORY}
COMMAND ${COPY_COMMAND} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}${SEP}${PROJECT_NAME}*.tgz ${NATIVE_DIST_DIRECTORY}${SEP}
WORKING_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
DEPENDS ${PROJECT_NAME}
DEPENDS ${PROJECT_NAME} ${PROJECT_NAME}-separate
)
endif ()
endif ()
13 changes: 12 additions & 1 deletion api/wasm/indigo-ketcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@
"name": "indigo-ketcher",
"version": "1.22.0-dev.1-2",
"description": "WASM implementation of Indigo organic chemistry toolkit web service for Ketcher",
"main": "indigo-ketcher.js",
"exports": {
".": {
"import": "./indigo-ketcher.js",
"require": "./indigo-ketcher.js"
},
"./binaryWasm": {
"import": "./indigo-ketcher-separate-wasm.js",
"require": "./indigo-ketcher-separate-wasm.js"
}
},
"files": [
"indigo-ketcher-separate-wasm.js",
"indigo-ketcher.js",
"indigo-ketcher-separate-wasm.wasm",
"README.md",
"LICENSE"
],
Expand Down
17 changes: 17 additions & 0 deletions api/wasm/indigo-ketcher/patch.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
file(READ package.json packageJson)

string(REGEX REPLACE ".*\"version\": \"([^\"]*)\".*" "\\1" version ${packageJson})
message("Version is: ${version}")
# patch package.json
string(REPLACE "indigo-ketcher-separate-wasm.wasm" "indigo-ketcher-${version}.wasm" modifiedPackageJson ${packageJson})
file(WRITE package.json "${modifiedPackageJson}")

# patch indigo-ketcher-separate-wasm.js
file(READ indigo-ketcher-separate-wasm.js content)
string(REPLACE "indigo-ketcher-separate-wasm.wasm" "/indigo-ketcher-${version}.wasm" content "${content}")
string(REPLACE "fetch(binaryFile" "fetch(new URL(binaryFile, self.location.origin)" content "${content}")

file(WRITE "indigo-ketcher-separate-wasm.js" "${content}")

# rename indigo-ketcher.wasm
file(RENAME indigo-ketcher-separate-wasm.wasm indigo-ketcher-${version}.wasm)

0 comments on commit ee45082

Please sign in to comment.