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 .

This commit is contained in:
Jukka Jylänki 2013-09-09 21:30:06 +03:00
Родитель c72022ee37
Коммит d0b297107e
9 изменённых файлов: 133 добавлений и 7 удалений

Просмотреть файл

@ -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()

Просмотреть файл

@ -1,11 +1,15 @@
cmake_minimum_required(VERSION 2.8)
project(hello_world)
project(test_cmake)
file(GLOB sourceFiles ../../hello_world.cpp)
file(GLOB sourceFiles main.cpp)
file(GLOB preJsFiles pre*.js)
file(GLOB postJsFiles post*.js)
file(GLOB libraryJsFiles jslibrary*.js)
if (CMAKE_BUILD_TYPE STREQUAL Debug)
SET(linkFlags "")
SET(linkFlags "-g4")
else() # Either MinSizeRel, RelWithDebInfo or Release, all which run with optimizations enabled.
SET(linkFlags "-O2")
endif()
@ -28,5 +32,17 @@ if (NOT CMAKE_C_SIZEOF_DATA_PTR)
message(FATAL_ERROR "CMAKE_C_SIZEOF_DATA_PTR was not defined!")
endif()
add_executable(hello_world ${sourceFiles})
set_target_properties(hello_world PROPERTIES LINK_FLAGS "${linkFlags}")
add_executable(test_cmake ${sourceFiles})
# GOTCHA: If your project has custom link flags, these must be set *before* calling any of the em_link_xxx functions!
set_target_properties(test_cmake PROPERTIES LINK_FLAGS "${linkFlags}")
message(STATUS "js libs '${libraryJsFiles}'")
# To link .js files using the --js-library flag, use the following helper function.
em_link_js_library(test_cmake ${libraryJsFiles})
# To link .js files using the --pre-js flag, use the following helper function.
em_link_pre_js(test_cmake ${preJsFiles})
# To link .js files using the --post-js flag, use the following helper function.
em_link_post_js(test_cmake ${postJsFiles})

Просмотреть файл

@ -0,0 +1,7 @@
var mylib = {};
mylib.lib_function = function() {
console.log('lib_function');
}
mergeInto(LibraryManager.library, mylib);

Просмотреть файл

@ -0,0 +1,7 @@
var mylib = {};
mylib.lib_function2 = function() {
console.log('lib_function2');
}
mergeInto(LibraryManager.library, mylib);

Просмотреть файл

@ -0,0 +1,10 @@
extern "C" {
void lib_function();
void lib_function2();
}
int main()
{
lib_function();
lib_function2();
}

Просмотреть файл

@ -0,0 +1,4 @@
prejs executed
lib_function
lib_function2
postjs executed

Просмотреть файл

@ -0,0 +1 @@
console.log('postjs executed');

Просмотреть файл

@ -0,0 +1 @@
console.log('prejs executed');

Просмотреть файл

@ -295,7 +295,7 @@ f.close()
make = make_commands[generator]
cmake_cases = ['target_js', 'target_html']
cmake_outputs = ['hello_world.js', 'hello_world_gles.html']
cmake_outputs = ['test_cmake.js', 'hello_world_gles.html']
for i in range(0, 2):
for configuration in ['Debug', 'Release']:
# CMake can be invoked in two ways, using 'emconfigure cmake', or by directly running 'cmake'.
@ -342,7 +342,7 @@ f.close()
# Run through node, if CMake produced a .js file.
if cmake_outputs[i].endswith('.js'):
ret = Popen(listify(NODE_JS) + [tempdirname + '/' + cmake_outputs[i]], stdout=PIPE).communicate()[0]
assert 'hello, world!' in ret, 'Running cmake-based .js application failed!'
self.assertTextDataIdentical(open(cmakelistsdir + '/out.txt', 'r').read().strip(), ret.strip())
finally:
os.chdir(path_from_root('tests')) # Move away from the directory we are about to remove.
shutil.rmtree(tempdirname)