From c90cc1b11fd9a519e5b2d7a7039030376a99c96d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 15 Jul 2024 12:50:28 -0700 Subject: [PATCH 1/4] Update build of toolchain/sysroot to not touch installation root This changes everything to ensure that only the `install` step actually tries to install things. Everything is staged into temporary `./install` folders inside of the build directory and then running the build system's `install` target will actually copy out everything using CMake builtins. Closes #442 --- cmake/wasi-sdk-sysroot.cmake | 20 +++++++----- cmake/wasi-sdk-toolchain.cmake | 56 +++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/cmake/wasi-sdk-sysroot.cmake b/cmake/wasi-sdk-sysroot.cmake index 5c23fe610..ab505806d 100644 --- a/cmake/wasi-sdk-sysroot.cmake +++ b/cmake/wasi-sdk-sysroot.cmake @@ -10,7 +10,8 @@ find_program(MAKE make REQUIRED) option(WASI_SDK_DEBUG_PREFIX_MAP "Pass `-fdebug-prefix-map` for built artifacts" ON) option(WASI_SDK_INCLUDE_TESTS "Whether or not to build tests by default" OFF) -set(wasi_sysroot ${CMAKE_INSTALL_PREFIX}/share/wasi-sysroot) +set(wasi_tmp_install ${CMAKE_CURRENT_BINARY_DIR}/install) +set(wasi_sysroot ${wasi_tmp_install}/share/wasi-sysroot) if(WASI_SDK_DEBUG_PREFIX_MAP) add_compile_options( @@ -44,7 +45,7 @@ endif() # compiler-rt build logic # ============================================================================= -set(compiler_rt_dst ${CMAKE_INSTALL_PREFIX}/lib/clang/${clang_version}) +set(compiler_rt_dst ${wasi_tmp_install}/lib/clang/${clang_version}) ExternalProject_Add(compiler-rt-build SOURCE_DIR "${llvm_proj_dir}/compiler-rt" CMAKE_ARGS @@ -234,6 +235,10 @@ endforeach() # misc build logic # ============================================================================= +install(DIRECTORY ${wasi_tmp_install}/lib ${wasi_tmp_install}/share + USE_SOURCE_PERMISSIONS + DESTINATION ${CMAKE_INSTALL_PREFIX}) + # Add a top-level `build` target as well as `build-$target` targets. add_custom_target(build ALL) foreach(target IN LISTS WASI_SDK_TARGETS) @@ -244,14 +249,13 @@ endforeach() # Install a `VERSION` file in the output prefix with a dump of version # information. -set(version_file_tmp ${CMAKE_CURRENT_BINARY_DIR}/VERSION) +set(version_file_tmp ${wasi_tmp_install}/VERSION) execute_process( COMMAND ${PYTHON} ${version_script} dump WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_FILE ${version_file_tmp}) -install( - FILES ${version_file_tmp} - DESTINATION ${CMAKE_INSTALL_PREFIX}) +install(FILES ${wasi_tmp_install}/VERSION + DESTINATION ${CMAKE_INSTALL_PREFIX}) if(WASI_SDK_INCLUDE_TESTS) add_subdirectory(tests) @@ -264,13 +268,13 @@ set(dist_dir ${CMAKE_CURRENT_BINARY_DIR}/dist) # Tarball with just `compiler-rt` builtins within it wasi_sdk_add_tarball(dist-compiler-rt ${dist_dir}/libclang_rt.builtins-wasm32-wasi-${wasi_sdk_version}.tar.gz - ${CMAKE_INSTALL_PREFIX}/lib/clang/${clang_version}/lib/wasi) + ${wasi_tmp_install}/lib/clang/${clang_version}/lib/wasi) add_dependencies(dist-compiler-rt compiler-rt) # Tarball with the whole sysroot wasi_sdk_add_tarball(dist-sysroot ${dist_dir}/wasi-sysroot-${wasi_sdk_version}.tar.gz - ${CMAKE_INSTALL_PREFIX}/share/wasi-sysroot) + ${wasi_tmp_install}/share/wasi-sysroot) add_dependencies(dist-sysroot build install) add_custom_target(dist DEPENDS dist-compiler-rt dist-sysroot) diff --git a/cmake/wasi-sdk-toolchain.cmake b/cmake/wasi-sdk-toolchain.cmake index 1da2ebf3f..92943defb 100644 --- a/cmake/wasi-sdk-toolchain.cmake +++ b/cmake/wasi-sdk-toolchain.cmake @@ -7,6 +7,8 @@ set(WASI_SDK_ARTIFACT "" CACHE STRING "Name of the wasi-sdk artifact being produ string(REGEX REPLACE "[ ]+" ";" llvm_cmake_flags_list "${LLVM_CMAKE_FLAGS}") +set(wasi_tmp_install ${CMAKE_CURRENT_BINARY_DIR}/install) + if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE MinSizeRel) endif() @@ -16,7 +18,7 @@ set(default_cmake_args -DCMAKE_AR=${CMAKE_AR} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} - -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}) + -DCMAKE_INSTALL_PREFIX=${wasi_tmp_install}) if(CMAKE_C_COMPILER_LAUNCHER) list(APPEND default_cmake_args -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER}) @@ -106,6 +108,14 @@ ExternalProject_Add(llvm-build USES_TERMINAL_INSTALL ON ) +add_custom_target(build ALL DEPENDS llvm-build) + +# Installation target for this outer project for installing the toolchain to the +# system. +install(DIRECTORY ${wasi_tmp_install}/bin ${wasi_tmp_install}/lib ${wasi_tmp_install}/share + USE_SOURCE_PERMISSIONS + DESTINATION ${CMAKE_INSTALL_PREFIX}) + # Build logic for `wasm-component-ld` installed from Rust code. set(wasm_component_ld_root ${CMAKE_CURRENT_BINARY_DIR}/wasm-component-ld) set(wasm_component_ld ${wasm_component_ld_root}/bin/wasm-component-ld${CMAKE_EXECUTABLE_SUFFIX}) @@ -118,24 +128,34 @@ add_custom_command( COMMAND cargo install --root ${wasm_component_ld_root} ${rust_target_flag} wasm-component-ld@${wasm_component_ld_version} + COMMAND + cmake -E make_directory ${wasi_tmp_install}/bin + COMMAND + cmake -E copy ${wasm_component_ld} ${wasi_tmp_install}/bin COMMENT "Building `wasm-component-ld` ...") - -add_custom_target(wasm-component-ld ALL DEPENDS ${wasm_component_ld}) - -install( - PROGRAMS ${wasm_component_ld} - DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) +add_custom_target(wasm-component-ld DEPENDS ${wasm_component_ld}) +add_dependencies(build wasm-component-ld) # Setup installation logic for CMake support files. -install( - PROGRAMS src/config/config.sub src/config/config.guess - DESTINATION ${CMAKE_INSTALL_PREFIX}/share/misc) -install( - FILES wasi-sdk.cmake wasi-sdk-pthread.cmake wasi-sdk-p2.cmake - DESTINATION ${CMAKE_INSTALL_PREFIX}/share/cmake) -install( - DIRECTORY cmake/Platform - DESTINATION ${CMAKE_INSTALL_PREFIX}/share/cmake) +add_custom_target(misc-files) +add_dependencies(build misc-files) + +function(copy_misc_file src dst_folder) + cmake_path(GET src FILENAME src_filename) + set(dst ${wasi_tmp_install}/share/${dst_folder}/${src_filename}) + add_custom_command( + OUTPUT ${dst} + COMMAND cmake -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${src} ${dst}) + add_custom_target(copy-${src_filename} DEPENDS ${dst}) + add_dependencies(misc-files copy-${src_filename}) +endfunction() + +copy_misc_file(src/config/config.sub misc) +copy_misc_file(src/config/config.guess misc) +copy_misc_file(wasi-sdk.cmake cmake) +copy_misc_file(wasi-sdk-pthread.cmake cmake) +copy_misc_file(wasi-sdk-p2.cmake cmake) +copy_misc_file(cmake/Platform/WASI.cmake cmake/Platform) include(wasi-sdk-dist) @@ -156,6 +176,6 @@ endif() set(dist_dir ${CMAKE_CURRENT_BINARY_DIR}/dist) wasi_sdk_add_tarball(dist-toolchain ${dist_dir}/wasi-toolchain-${wasi_sdk_version}-${wasi_sdk_artifact}.tar.gz - ${CMAKE_INSTALL_PREFIX}) -add_dependencies(dist-toolchain llvm-build install) + ${wasi_tmp_install}) +add_dependencies(dist-toolchain build) add_custom_target(dist DEPENDS dist-toolchain) From 7daf6f71e998994a818f3950d6f54d33afe70d06 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 15 Jul 2024 13:29:29 -0700 Subject: [PATCH 2/4] Better integrate generating a version file --- cmake/wasi-sdk-sysroot.cmake | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cmake/wasi-sdk-sysroot.cmake b/cmake/wasi-sdk-sysroot.cmake index ab505806d..51f01b06c 100644 --- a/cmake/wasi-sdk-sysroot.cmake +++ b/cmake/wasi-sdk-sysroot.cmake @@ -249,12 +249,15 @@ endforeach() # Install a `VERSION` file in the output prefix with a dump of version # information. -set(version_file_tmp ${wasi_tmp_install}/VERSION) execute_process( COMMAND ${PYTHON} ${version_script} dump WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - OUTPUT_FILE ${version_file_tmp}) -install(FILES ${wasi_tmp_install}/VERSION + OUTPUT_VARIABLE version_dump) +set(version_file_tmp ${CMAKE_CURRENT_BINARY_DIR}/VERSION) +file(GENERATE OUTPUT ${version_file_tmp} CONTENT ${version_dump}) +add_custom_target(version-file DEPENDS ${version_file_tmp}) +add_dependencies(build version-file) +install(FILES ${version_file_tmp} DESTINATION ${CMAKE_INSTALL_PREFIX}) if(WASI_SDK_INCLUDE_TESTS) @@ -275,6 +278,6 @@ add_dependencies(dist-compiler-rt compiler-rt) wasi_sdk_add_tarball(dist-sysroot ${dist_dir}/wasi-sysroot-${wasi_sdk_version}.tar.gz ${wasi_tmp_install}/share/wasi-sysroot) -add_dependencies(dist-sysroot build install) +add_dependencies(dist-sysroot build) add_custom_target(dist DEPENDS dist-compiler-rt dist-sysroot) From ca475951a8c7ce83a83fd4ab805c0baae01b180f Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 15 Jul 2024 14:08:09 -0700 Subject: [PATCH 3/4] Try to avoid path length limits on Windows --- .github/workflows/main.yml | 8 ++++++-- ci/build.sh | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0b3abe693..5445d021a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -119,9 +119,13 @@ jobs: if: runner.os == 'Linux' # Use a shorter build directory than the default on Windows to avoid - # hitting path length and command line length limits. + # hitting path length and command line length limits. See + # WebAssembly/wasi-libc#514 - name: Build and test (Windows) - run: ./ci/build.sh C:/wasi-sdk + run: | + ./ci/build.sh C:/wasi-sdk + mkdir build + cp -r C:/wasi-sdk/build/dist build shell: bash if: runner.os == 'Windows' diff --git a/ci/build.sh b/ci/build.sh index f391e8ab4..6dc187c4a 100755 --- a/ci/build.sh +++ b/ci/build.sh @@ -11,19 +11,19 @@ set -ex # Optionally allow the first argument to this script to be the install # location. if [ "$1" = "" ]; then - install_dir=`pwd`/build/install + build_dir=`pwd`/build else - install_dir="$1" + build_dir="$1" fi -cmake -G Ninja -B build/toolchain -S . \ +cmake -G Ninja -B $build_dir/toolchain -S . \ -DWASI_SDK_BUILD_TOOLCHAIN=ON \ - "-DCMAKE_INSTALL_PREFIX=$install_dir" \ + "-DCMAKE_INSTALL_PREFIX=$build_dir/install" \ $WASI_SDK_CI_TOOLCHAIN_CMAKE_ARGS \ "-DLLVM_CMAKE_FLAGS=$WASI_SDK_CI_TOOLCHAIN_LLVM_CMAKE_ARGS" -ninja -C build/toolchain install dist -v +ninja -C $build_dir/toolchain install dist -v -mv build/toolchain/dist build/dist +mv $build_dir/toolchain/dist $build_dir/dist if [ "$WASI_SDK_CI_SKIP_SYSROOT" = "1" ]; then exit 0 @@ -31,19 +31,19 @@ fi # Use the just-built toolchain and its `CMAKE_TOOLCHAIN_FILE` to build a # sysroot. -cmake -G Ninja -B build/sysroot -S . \ - "-DCMAKE_TOOLCHAIN_FILE=$install_dir/share/cmake/wasi-sdk.cmake" \ +cmake -G Ninja -B $build_dir/sysroot -S . \ + "-DCMAKE_TOOLCHAIN_FILE=$build_dir/install/share/cmake/wasi-sdk.cmake" \ -DCMAKE_C_COMPILER_WORKS=ON \ -DCMAKE_CXX_COMPILER_WORKS=ON \ -DWASI_SDK_INCLUDE_TESTS=ON \ - "-DCMAKE_INSTALL_PREFIX=$install_dir" -ninja -C build/sysroot install dist -v + "-DCMAKE_INSTALL_PREFIX=$build_dir/install" +ninja -C $build_dir/sysroot install dist -v -mv build/sysroot/dist/* build/dist +mv $build_dir/sysroot/dist/* $build_dir/dist if [ "$WASI_SDK_CI_SKIP_TESTS" = "1" ]; then exit 0 fi # Run tests to ensure that the sysroot works. -ctest --output-on-failure --parallel 10 --test-dir build/sysroot/tests +ctest --output-on-failure --parallel 10 --test-dir $build_dir/sysroot/tests From 8c86febf580a5ed13c56bd5d7674e40016ae55c0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 16 Jul 2024 07:29:17 -0700 Subject: [PATCH 4/4] Fix CI script --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5445d021a..a23beb1f8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -125,7 +125,7 @@ jobs: run: | ./ci/build.sh C:/wasi-sdk mkdir build - cp -r C:/wasi-sdk/build/dist build + cp -r C:/wasi-sdk/dist build shell: bash if: runner.os == 'Windows'