Skip to content

Commit 30c720a

Browse files
authored
cmake: Use LINK_DEPENDS for em_link_js_library et al. NFC (#19071)
Fixes: #19070
1 parent 8274bc4 commit 30c720a

File tree

3 files changed

+22
-52
lines changed

3 files changed

+22
-52
lines changed

cmake/Modules/Platform/Emscripten.cmake

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -306,50 +306,21 @@ function(em_validate_asmjs_after_build target)
306306
message(WARNING "em_validate_asmjs_after_build no longer exists")
307307
endfunction()
308308

309-
# A global counter to guarantee unique names for js library files.
310-
set(link_js_counter 1)
311-
312309
# Internal function: Do not call from user CMakeLists.txt files. Use one of
313310
# em_link_js_library()/em_link_pre_js()/em_link_post_js() instead.
314-
function(em_add_tracked_link_flag target flagname)
315-
311+
function(em_add_link_deps target flagname)
316312
# User can input list of JS files either as a single list, or as variable
317313
# arguments to this function, so iterate over varargs, and treat each item in
318314
# varargs as a list itself, to support both syntax forms.
319315
foreach(jsFileList ${ARGN})
320316
foreach(jsfile ${jsFileList})
321-
# If the user edits the JS file, we want to relink the emscripten
322-
# application, but unfortunately it is not possible to make a link step
323-
# depend directly on a source file. Instead, we must make a dummy no-op
324-
# build target on that source file, and make the project depend on
325-
# that target.
326-
327-
# Sanitate the source .js filename to a good symbol name to use as a dummy
328-
# filename.
329-
get_filename_component(jsname "${jsfile}" NAME)
330-
string(REGEX REPLACE "[/:\\\\.\ ]" "_" dummy_js_target ${jsname})
331-
set(dummy_lib_name ${target}_${link_js_counter}_${dummy_js_target})
332-
set(dummy_c_name "${CMAKE_BINARY_DIR}/${dummy_js_target}_tracker.c")
333-
334-
# Create a new static library target that with a single dummy .c file.
335-
add_library(${dummy_lib_name} STATIC ${dummy_c_name})
336-
# Make the dummy .c file depend on the .js file we are linking, so that if
337-
# the .js file is edited, the dummy .c file, and hence the static library
338-
# will be rebuild (no-op). This causes the main application to be
339-
# relinked, which is what we want. This approach was recommended by
340-
# http://www.cmake.org/pipermail/cmake/2010-May/037206.html
341-
add_custom_command(OUTPUT ${dummy_c_name} COMMAND ${CMAKE_COMMAND} -E touch ${dummy_c_name} DEPENDS ${jsfile})
342-
target_link_libraries(${target} ${dummy_lib_name})
343-
344-
# Link the js-library to the target
345-
# When a linked library starts with a "-" cmake will just add it to the
346-
# linker command line as it is. The advantage of doing it this way is
347-
# that the js-library will also be automatically linked to targets that
348-
# depend on this target.
349-
get_filename_component(js_file_absolute_path "${jsfile}" ABSOLUTE )
350-
target_link_libraries(${target} "${flagname} \"${js_file_absolute_path}\"")
351-
352-
math(EXPR link_js_counter "${link_js_counter} + 1")
317+
get_target_property(linkdeps ${target} LINK_DEPENDS)
318+
if(linkdeps STREQUAL "linkdeps-NOTFOUND")
319+
set(linkdeps "")
320+
endif()
321+
get_filename_component(jsfile_abs "${jsfile}" ABSOLUTE )
322+
set_target_properties(${target} PROPERTIES LINK_DEPENDS "${linkdeps};${jsfile_abs}")
323+
target_link_libraries(${target} "${flagname} \"${jsfile_abs}\"")
353324
endforeach()
354325
endforeach()
355326
endfunction()
@@ -361,21 +332,21 @@ endfunction()
361332
# between the linked .js files and the main project, so that editing the .js
362333
# file will cause the target project to be relinked.
363334
function(em_link_js_library target)
364-
em_add_tracked_link_flag(${target} "--js-library" ${ARGN})
335+
em_add_link_deps(${target} "--js-library" ${ARGN})
365336
endfunction()
366337

367338
# This function is identical to em_link_js_library(), except the .js files will
368339
# be added with '--pre-js file.js' command line flag, which is generally used to
369340
# add some preamble .js code to a generated output file.
370341
function(em_link_pre_js target)
371-
em_add_tracked_link_flag(${target} "--pre-js" ${ARGN})
342+
em_add_link_deps(${target} "--pre-js" ${ARGN})
372343
endfunction()
373344

374345
# This function is identical to em_link_js_library(), except the .js files will
375346
# be added with '--post-js file.js' command line flag, which is generally used
376347
# to add some postamble .js code to a generated output file.
377348
function(em_link_post_js target)
378-
em_add_tracked_link_flag(${target} "--post-js" ${ARGN})
349+
em_add_link_deps(${target} "--post-js" ${ARGN})
379350
endfunction()
380351

381352
# Experimental support for targeting generation of Visual Studio project files

test/cmake/target_js/CMakeLists.txt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@ project(test_cmake)
44

55
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../cpp_lib ${CMAKE_CURRENT_BINARY_DIR}/cpp_lib)
66

7-
file(GLOB sourceFiles main.cpp)
8-
9-
file(GLOB preJsFiles pre*.js)
10-
file(GLOB postJsFiles post*.js)
11-
file(GLOB libraryJsFiles jslibrary*.js)
12-
137
if (CMAKE_BUILD_TYPE STREQUAL Debug)
14-
set(linkFlags "-g4 -sNO_EXIT_RUNTIME")
8+
set(linkFlags "-g")
159
else()
1610
# Either MinSizeRel, RelWithDebInfo or Release, all which run with optimizations enabled.
17-
set(linkFlags "-O2 -sNO_EXIT_RUNTIME")
11+
set(linkFlags "-O2")
1812
endif()
1913

2014
# Ensure synchronous startup for this test, whose output expects it
@@ -82,19 +76,23 @@ if (NOT ${endian_result} EQUAL 0)
8276
message(FATAL_ERROR "TEST_BIG_ENDIAN did not return 0!")
8377
endif()
8478

85-
add_executable(test_cmake ${sourceFiles})
79+
add_executable(test_cmake "main.cpp")
8680
target_link_libraries( test_cmake cpp_lib)
8781

8882
# GOTCHA: If your project has custom link flags, these must be set *before*
8983
# calling any of the em_link_xxx functions!
9084
set_target_properties(test_cmake PROPERTIES LINK_FLAGS "${linkFlags}")
9185

86+
file(GLOB libraryJsFiles jslibrary*.js)
9287
message(STATUS "js libs '${libraryJsFiles}'")
9388
# To link .js files using the --js-library flag, use the following helper function.
9489
em_link_js_library(test_cmake ${libraryJsFiles})
9590

9691
# To link .js files using the --pre-js flag, use the following helper function.
97-
em_link_pre_js(test_cmake ${preJsFiles})
92+
em_link_pre_js(test_cmake "prejs.js")
93+
94+
# Ensure that calling em_link_pre_js multiple times works as expected.
95+
em_link_pre_js(test_cmake "prejs.js")
9896

9997
# To link .js files using the --post-js flag, use the following helper function.
100-
em_link_post_js(test_cmake ${postJsFiles})
98+
em_link_post_js(test_cmake "postjs.js")

test/cmake/target_js/out.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
prejs executed
2+
prejs executed
23
lib_function
34
lib_function2
4-
postjs executed
5+
postjs executed

0 commit comments

Comments
 (0)