diff options
author | Jukka Jylänki <jujjyl@gmail.com> | 2013-09-09 21:30:06 +0300 |
---|---|---|
committer | Jukka Jylänki <jujjyl@gmail.com> | 2013-09-18 16:27:51 +0300 |
commit | d0b297107e15592fc9b39786947d120690ec6ac6 (patch) | |
tree | 75a40e378bb76971e92cc3eb93a35b9e3bc181f2 /cmake | |
parent | c72022ee3788281dd764782eb51395e322efb1bc (diff) |
Add functions em_link_js_library(), em_link_pre_js() and em_link_post_js() to support linking .js files to executables, while doing dependency tracking on filestamp modifications. For more info, see https://groups.google.com/forum/#!topic/emscripten-discuss/uRbTIB62V7s .
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/Platform/Emscripten.cmake | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/cmake/Platform/Emscripten.cmake b/cmake/Platform/Emscripten.cmake index ec3f5383..5cddc22d 100644 --- a/cmake/Platform/Emscripten.cmake +++ b/cmake/Platform/Emscripten.cmake @@ -139,3 +139,83 @@ set(CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO "-O2" CACHE STRING "Emscripten-over function(em_validate_asmjs_after_build target) add_custom_command(TARGET ${target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E echo Validating build output for asm.js... COMMAND "python" ARGS "${EMSCRIPTEN_ROOT_PATH}/tools/validate_asmjs.py" "$<TARGET_FILE:${target}>") endfunction() + +# A global counter to guarantee unique names for js library files. +set(link_js_counter 1) + +# This function links a (list of ) .js library file(s) to the given CMake project. +# Example: em_link_js_library(my_executable "lib1.js" "lib2.js") +# will result in emcc passing --js-library lib1.js --js-library lib2.js to the emscripten linker, as well as +# tracking the modification timestamp between the linked .js files and the main project, so that editing the .js file +# will cause the target project to be relinked. +function(em_link_js_library target) + get_target_property(props ${target} LINK_FLAGS) + # User can input list of JS files either as a single list, or as variable arguments to this function, so iterate over varargs, and treat each + # item in varargs as a list itself, to support both syntax forms. + foreach(jsFileList ${ARGN}) + foreach(jsfile ${jsFileList}) + # Add link command to the given JS file. + set(props "${props} --js-library \"${jsfile}\"") + + # If the user edits the JS file, we want to relink the emscripten application, but unfortunately it is not possible to make a link step + # depend directly on a source file. Instead, we must make a dummy no-op build target on that source file, and make the project depend on + # that target. + + # Sanitate the source .js filename to a good symbol name to use as a dummy filename. + get_filename_component(jsname "${jsfile}" NAME) + string(REGEX REPLACE "[/:\\\\.\ ]" "_" dummy_js_target ${jsname}) + set(dummy_lib_name ${target}_${link_js_counter}_${dummy_js_target}) + set(dummy_c_name "${CMAKE_BINARY_DIR}/${dummy_js_target}_library.c") + + # Create a new static library target that with a single dummy .c file. + add_library(${dummy_lib_name} STATIC ${dummy_c_name}) + # Make the dummy .c file depend on the .js file we are linking, so that if the .js file is edited, the dummy .c file, and hence the static library will be rebuild (no-op). This causes the main application to be relinked, which is what we want. + # This approach was recommended by http://www.cmake.org/pipermail/cmake/2010-May/037206.html + add_custom_command(OUTPUT ${dummy_c_name} COMMAND ${CMAKE_COMMAND} -E touch ${dummy_c_name} DEPENDS ${jsfile}) + target_link_libraries(${target} ${dummy_lib_name}) + + math(EXPR link_js_counter "${link_js_counter} + 1") + endforeach() + endforeach() + set_target_properties(${target} PROPERTIES LINK_FLAGS "${props}") +endfunction() + +# This function is identical to em_link_js_library(), except the .js files will be added with '--pre-js file.js' command line flag, +# which is generally used to add some preamble .js code to a generated output file. +function(em_link_pre_js target) + get_target_property(props ${target} LINK_FLAGS) + foreach(jsFileList ${ARGN}) + foreach(jsfile ${jsFileList}) + set(props "${props} --pre-js \"${jsfile}\"") + get_filename_component(jsname "${jsfile}" NAME) + string(REGEX REPLACE "[/:\\\\.\ ]" "_" dummy_js_target ${jsname}) + set(dummy_lib_name ${target}_${link_js_counter}_${dummy_js_target}) + set(dummy_c_name "${CMAKE_BINARY_DIR}/${dummy_js_target}_prejs.c") + add_library(${dummy_lib_name} STATIC ${dummy_c_name}) + add_custom_command(OUTPUT ${dummy_c_name} COMMAND ${CMAKE_COMMAND} -E touch ${dummy_c_name} DEPENDS ${jsfile}) + target_link_libraries(${target} ${dummy_lib_name}) + math(EXPR link_js_counter "${link_js_counter} + 1") + endforeach() + endforeach() + set_target_properties(${target} PROPERTIES LINK_FLAGS "${props}") +endfunction() + +# This function is identical to em_link_js_library(), except the .js files will be added with '--post-js file.js' command line flag, +# which is generally used to add some postamble .js code to a generated output file. +function(em_link_post_js target) + get_target_property(props ${target} LINK_FLAGS) + foreach(jsFileList ${ARGN}) + foreach(jsfile ${jsFileList}) + set(props "${props} --post-js \"${jsfile}\"") + get_filename_component(jsname "${jsfile}" NAME) + string(REGEX REPLACE "[/:\\\\.\ ]" "_" dummy_js_target ${jsname}) + set(dummy_lib_name ${target}_${link_js_counter}_${dummy_js_target}) + set(dummy_c_name "${CMAKE_BINARY_DIR}/${dummy_js_target}_postjs.c") + add_library(${dummy_lib_name} STATIC ${dummy_c_name}) + add_custom_command(OUTPUT ${dummy_c_name} COMMAND ${CMAKE_COMMAND} -E touch ${dummy_c_name} DEPENDS ${jsfile}) + target_link_libraries(${target} ${dummy_lib_name}) + math(EXPR link_js_counter "${link_js_counter} + 1") + endforeach() + endforeach() + set_target_properties(${target} PROPERTIES LINK_FLAGS "${props}") +endfunction() |