aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmake/Platform/Emscripten.cmake80
-rw-r--r--tests/cmake/target_js/CMakeLists.txt26
-rw-r--r--tests/cmake/target_js/jslibrary.js7
-rw-r--r--tests/cmake/target_js/jslibrary2.js7
-rw-r--r--tests/cmake/target_js/main.cpp10
-rw-r--r--tests/cmake/target_js/out.txt4
-rw-r--r--tests/cmake/target_js/postjs.js1
-rw-r--r--tests/cmake/target_js/prejs.js1
-rw-r--r--tests/test_other.py4
9 files changed, 133 insertions, 7 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()
diff --git a/tests/cmake/target_js/CMakeLists.txt b/tests/cmake/target_js/CMakeLists.txt
index cee5fc42..244cc70a 100644
--- a/tests/cmake/target_js/CMakeLists.txt
+++ b/tests/cmake/target_js/CMakeLists.txt
@@ -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})
diff --git a/tests/cmake/target_js/jslibrary.js b/tests/cmake/target_js/jslibrary.js
new file mode 100644
index 00000000..63375d8f
--- /dev/null
+++ b/tests/cmake/target_js/jslibrary.js
@@ -0,0 +1,7 @@
+var mylib = {};
+
+mylib.lib_function = function() {
+ console.log('lib_function');
+}
+
+mergeInto(LibraryManager.library, mylib);
diff --git a/tests/cmake/target_js/jslibrary2.js b/tests/cmake/target_js/jslibrary2.js
new file mode 100644
index 00000000..5d322e2d
--- /dev/null
+++ b/tests/cmake/target_js/jslibrary2.js
@@ -0,0 +1,7 @@
+var mylib = {};
+
+mylib.lib_function2 = function() {
+ console.log('lib_function2');
+}
+
+mergeInto(LibraryManager.library, mylib);
diff --git a/tests/cmake/target_js/main.cpp b/tests/cmake/target_js/main.cpp
new file mode 100644
index 00000000..4b61dbf7
--- /dev/null
+++ b/tests/cmake/target_js/main.cpp
@@ -0,0 +1,10 @@
+extern "C" {
+ void lib_function();
+ void lib_function2();
+}
+
+int main()
+{
+ lib_function();
+ lib_function2();
+}
diff --git a/tests/cmake/target_js/out.txt b/tests/cmake/target_js/out.txt
new file mode 100644
index 00000000..76135df7
--- /dev/null
+++ b/tests/cmake/target_js/out.txt
@@ -0,0 +1,4 @@
+prejs executed
+lib_function
+lib_function2
+postjs executed \ No newline at end of file
diff --git a/tests/cmake/target_js/postjs.js b/tests/cmake/target_js/postjs.js
new file mode 100644
index 00000000..5a1b44ce
--- /dev/null
+++ b/tests/cmake/target_js/postjs.js
@@ -0,0 +1 @@
+console.log('postjs executed');
diff --git a/tests/cmake/target_js/prejs.js b/tests/cmake/target_js/prejs.js
new file mode 100644
index 00000000..87beb770
--- /dev/null
+++ b/tests/cmake/target_js/prejs.js
@@ -0,0 +1 @@
+console.log('prejs executed');
diff --git a/tests/test_other.py b/tests/test_other.py
index b11f22e5..56c13650 100644
--- a/tests/test_other.py
+++ b/tests/test_other.py
@@ -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)