Skip to content

Commit b88d479

Browse files
authored
Support cmake LINK_LIBRARY:WHOLE_ARCHIVE (#22975)
Related: https://discourse.cmake.org/t/error-when-crosscompiling-with-whole-archive-target-link/9394 whole-archive support needs to be explicitly declared in toolchain file, see cmake official [GNU](https://github.com/Kitware/CMake/blob/6be01c932e077306ebea15cef4b8befb29c5130e/Modules/Platform/Linker/GNU.cmake#L35-L44).
1 parent e42ca23 commit b88d479

File tree

7 files changed

+34
-6
lines changed

7 files changed

+34
-6
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ jobs:
799799
EMTEST_SKIP_NODE_CANARY: "1"
800800
EMTEST_SKIP_RUST: "1"
801801
EMTEST_SKIP_WASM64: "1"
802+
EMTEST_SKIP_NEW_CMAKE: "1"
802803
steps:
803804
- install-rust
804805
- run: apt-get install -q -y ninja-build scons ccache

cmake/Modules/Platform/Emscripten.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_INCLUDES 1)
279279
set(CMAKE_C_RESPONSE_FILE_LINK_FLAG "@")
280280
set(CMAKE_CXX_RESPONSE_FILE_LINK_FLAG "@")
281281

282+
# Enable $<LINK_LIBRARY:WHOLE_ARCHIVE,static_lib> for CMake 3.24+
283+
set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE "-Wl,--whole-archive" "<LINK_ITEM>" "-Wl,--no-whole-archive")
284+
set(CMAKE_LINK_LIBRARY_USING_WHOLE_ARCHIVE_SUPPORTED True)
285+
282286
# Set a global EMSCRIPTEN variable that can be used in client CMakeLists.txt to
283287
# detect when building using Emscripten.
284288
set(EMSCRIPTEN 1 CACHE INTERNAL "If true, we are targeting Emscripten output.")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.24)
2+
3+
project(whole)
4+
5+
add_library(whole_lib STATIC lib.c)
6+
add_executable(whole main.c)
7+
target_link_libraries(whole $<LINK_LIBRARY:WHOLE_ARCHIVE,whole_lib>)

test/cmake/whole_archive/lib.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <stdio.h>
2+
3+
__attribute__((constructor)) void init(void) {
4+
printf("init\n");
5+
}

test/cmake/whole_archive/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
printf("main\n");
5+
}

test/cmake/whole_archive/out.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
init
2+
main

test/test_other.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -913,14 +913,18 @@ def test_emstrip(self):
913913
# would take 10 minutes+ to finish (CMake feature detection is slow), so
914914
# combine multiple features into one to try to cover as much as possible
915915
# while still keeping this test in sensible time limit.
916-
'js': ('target_js', 'test_cmake.js', ['-DCMAKE_BUILD_TYPE=Debug']),
917-
'html': ('target_html', 'hello_world_gles.html', ['-DCMAKE_BUILD_TYPE=Release']),
918-
'library': ('target_library', 'libtest_cmake.a', ['-DCMAKE_BUILD_TYPE=MinSizeRel']),
919-
'static_cpp': ('target_library', 'libtest_cmake.a', ['-DCMAKE_BUILD_TYPE=RelWithDebInfo', '-DCPP_LIBRARY_TYPE=STATIC']),
920-
'stdproperty': ('stdproperty', 'helloworld.js', []),
921-
'post_build': ('post_build', 'hello.js', []),
916+
'js': ('target_js', 'test_cmake.js', ['-DCMAKE_BUILD_TYPE=Debug']),
917+
'html': ('target_html', 'hello_world_gles.html', ['-DCMAKE_BUILD_TYPE=Release']),
918+
'library': ('target_library', 'libtest_cmake.a', ['-DCMAKE_BUILD_TYPE=MinSizeRel']),
919+
'static_cpp': ('target_library', 'libtest_cmake.a', ['-DCMAKE_BUILD_TYPE=RelWithDebInfo', '-DCPP_LIBRARY_TYPE=STATIC']),
920+
'whole_archive': ('whole_archive', 'whole.js', []),
921+
'stdproperty': ('stdproperty', 'helloworld.js', []),
922+
'post_build': ('post_build', 'hello.js', []),
922923
})
923924
def test_cmake(self, test_dir, output_file, cmake_args):
925+
if test_dir == 'whole_archive' and 'EMTEST_SKIP_NEW_CMAKE' in os.environ:
926+
self.skipTest('EMTEST_SKIP_NEW_CMAKE set')
927+
924928
# Test all supported generators.
925929
if WINDOWS:
926930
generators = ['MinGW Makefiles', 'NMake Makefiles']

0 commit comments

Comments
 (0)