emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.92k stars 3.32k forks source link

support cmake LINK_LIBRARY:WHOLE_ARCHIVE #22975

Closed eagleoflqj closed 1 day ago

eagleoflqj commented 1 week ago

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.

Test:

CMakeLists.txt

cmake_minimum_required(VERSION 3.24)

project(whole)

add_library(whole_lib STATIC lib.cpp)
add_executable(whole main.cpp)
target_link_libraries(whole $<LINK_LIBRARY:WHOLE_ARCHIVE,whole_lib>)

lib.cpp

#include <iostream>

static void init(void) __attribute__((constructor));
static void init(void) {
    std::cout << "init" << std::endl;
}

main.cpp

#include <iostream>

int main() {
    std::cout << "main" << std::endl;
}

With latest emsdk, emcmake cmake -B build complains

configure: cmake -B build -DCMAKE_TOOLCHAIN_FILE=/path/to/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR=/Users/liumeo/github/emsdk/node/20.18.0_64bit/bin/node
-- Configuring done (2.2s)
CMake Error at CMakeLists.txt:6 (add_executable):
  Feature 'WHOLE_ARCHIVE', specified through generator-expression
  '$<LINK_LIBRARY>' to link target 'whole', is not supported for the 'CXX'
  link language.

After overriding emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake, it passes, and cmake --build build passes, node build/whole.js outputs

init
main

as expected.

dschuff commented 1 day ago

This seems to be failing on the Chromium CI: https://logs.chromium.org/logs/emscripten-releases/buildbucket/cr-buildbucket/8729603731668921633/+/u/Emscripten_testsuite__other_/stdout

sbc100 commented 1 day ago

Yes we need to add EMTEST_SKIP_NEW_CMAKE there too.

dschuff commented 1 day ago

I guess the other option would be to just update the install of CMake we have on those builders.

sbc100 commented 1 day ago

I guess the other option would be to just update the install of CMake we have on those builders.

Maybe .. although then we loose coverage of older cmake versions