This is an archive of the discontinued LLVM Phabricator instance.

Add initial support for cross compile Windows runtimes under Linux when building Fuchsia clang toolchain
ClosedPublic

Authored by haowei on Jan 13 2023, 4:54 PM.

Diff Detail

Event Timeline

haowei created this revision.Jan 13 2023, 4:54 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 13 2023, 4:54 PM
Herald added a subscriber: abrachet. · View Herald Transcript
haowei requested review of this revision.Jan 13 2023, 4:54 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJan 13 2023, 4:54 PM
haowei updated this revision to Diff 489177.Jan 13 2023, 6:09 PM

Correct C, CXX flag issue.

phosek added inline comments.Jan 14 2023, 7:03 PM
clang/cmake/caches/Fuchsia-stage2.cmake
73

I'd drop this part altogether, let's build these anytime when WINDOWS_SDK_DIR is set.

96–99

I think we should introduce a new LLVM CMake option for setting this akin to LLVM_WINSYSROOT (for example LLVM_VFSOVERLAY).

100–101

Can we instead set BUILTINS_${target}_LLVM_WINSYSROOT to ${WINDOWS_SDK_DIR}?

105–108

These should be set automatically if you set /winsysroot (through LLVM_WINSYSROOT), see https://github.com/llvm/llvm-project/blob/a64846bee0bb4b4912c8cf6bf018ba5d892065d1/clang/lib/Driver/ToolChains/MSVC.cpp#L107.

142–143

Can we instead set RUNTIMES_${target}_LLVM_WINSYSROOT to ${WINDOWS_SDK_DIR}?

llvm/cmake/modules/LLVMExternalProjectUtils.cmake
172

This should be in a separate block just like other tools below guarded by a llvm-rc IN_LIST TOOLCHAIN_TOOLS condition.

173

This should be in a separate block just like other tools below guarded by a llvm-mt IN_LIST TOOLCHAIN_TOOLS condition.

llvm/runtimes/CMakeLists.txt
359

We should fix the build so that this isn't needed, it should be possible to use LLVM_USE_LINKER=lld when targeting Windows.

haowei updated this revision to Diff 490679.Jan 19 2023, 3:49 PM
haowei retitled this revision from [WIP] Add initial support for cross compile Windows runtimes under Linux when building Fuchsia clang toolchain to Add initial support for cross compile Windows runtimes under Linux when building Fuchsia clang toolchain.
haowei edited the summary of this revision. (Show Details)
haowei marked 2 inline comments as done.Jan 19 2023, 3:54 PM
haowei added inline comments.
clang/cmake/caches/Fuchsia-stage2.cmake
73

We need to do a soft transition if we drop the "WIN32" since the bots are not passing the WINSYSROOT flag yet. Let's keep the WIN32 here.

105–108

There is an issue that "CLANG_CL" variable is not correctly set when clang kicks runtime build. When CLANG_CL is not set, the /winsysroot will not be added by the LLVM_WINSYSROOT flag. I have some thoughts about it and prefer to address this issue in a follow up patch instead of put everything here.

142–143

As stated in an earlier comment, this will require fixing the setting the "CLANG_CL" variable in compiler version detection.

llvm/runtimes/CMakeLists.txt
359

I made some other changes and now the /fuse-ld=lld will be passed to lld-link.exe if we leave this line unchanged.
It will through a warning but won't stop the build.
Once I fix the CLANG_CL issue, I can prevent it from inserting /fuse-ld=lld when using CLANG_CL

haowei updated this revision to Diff 490921.Jan 20 2023, 11:28 AM

Correct an issue llvm-rc and llvm-mt are not part of TOOLCHAIN_TOOLS

phosek added inline comments.Jan 20 2023, 3:27 PM
clang/cmake/caches/Fuchsia-stage2.cmake
79

I think we'll want to use these flags even when not cross-compiling so I'd change the name to reflect that.

81–115

You can set these unconditionally (unset variables in CMake evaluate to empty string).

90

I think we'll want to use these flags even when not cross-compiling so I'd change the name to reflect that.

123–132

You can set these unconditionally.

haowei updated this revision to Diff 490994.Jan 20 2023, 3:39 PM
haowei marked 4 inline comments as done.
phosek accepted this revision.Jan 20 2023, 3:51 PM

LGTM

This revision is now accepted and ready to land.Jan 20 2023, 3:51 PM

Thanks for the review. I will land the change once Fuchsia clang builders are green.

This revision was landed with ongoing or failed builds.Jan 23 2023, 2:09 PM
This revision was automatically updated to reflect the committed changes.
thakis added a subscriber: thakis.Jan 23 2023, 2:14 PM
thakis added inline comments.
clang/cmake/caches/Fuchsia-stage2.cmake
95

You can tell cmake to invoke lld-link, which has a /winsysroot: flag.

haowei added inline comments.Jan 23 2023, 3:20 PM
clang/cmake/caches/Fuchsia-stage2.cmake
95

What would be a good way to make CMake invoke lld-link (or through clang-cl) instead of using cmake vs_link in this situation?
We thought about using

set(CMAKE_${LANG}_SIMULATE_ID "MSVC")
set(CMAKE_${LANG}_COMPILER_FRONTEND_VARIANT "GNU")

but I don't feel great about changing variables that are not suppose to change.

phosek added inline comments.Jan 23 2023, 4:28 PM
clang/cmake/caches/Fuchsia-stage2.cmake
95

Wouldn't CMake use lld-link if you set CMAKE_LINKER=lld-link. We already do this in https://github.com/llvm/llvm-project/blob/0e09bb8b143c80426c497a924ee4fa57a26af6b5/llvm/cmake/modules/LLVMExternalProjectUtils.cmake#L179 which is used by the bootstrapping build so you should be able to use /winsysroot: here.

haowei updated this revision to Diff 493766.Jan 31 2023, 4:11 PM

Correct dependency issues on llvm-mt

This revision is now accepted and ready to land.Jan 31 2023, 4:11 PM
phosek added inline comments.Jan 31 2023, 11:38 PM
clang/cmake/caches/Fuchsia-stage2.cmake
95

@haowei have tried this suggestion?

haowei added inline comments.Feb 2 2023, 1:31 PM
clang/cmake/caches/Fuchsia-stage2.cmake
95

I tried:

FAILED: runtimes/runtimes-x86_64-pc-windows-msvc-stamps/runtimes-x86_64-pc-windows-msvc-configure /mnt/nvme_sec/SRC/llvm-project/build-lldlink/runtimes/runtimes-x86_64-pc-windows-msvc-stamps/runtimes-x86_64-pc-windows-msvc-configure 
cd /mnt/nvme_sec/SRC/llvm-project/build-lldlink/runtimes/runtimes-x86_64-pc-windows-msvc-bins && /mnt/nvme_sec/SRC/llvm-prebuilts/cmake/linux-amd64/bin/cmake \
-DCMAKE_C_COMPILER=/mnt/nvme_sec/SRC/llvm-project/build-lldlink/./bin/clang-cl \
-DCMAKE_CXX_COMPILER=/mnt/nvme_sec/SRC/llvm-project/build-lldlink/./bin/clang-cl \
-DCMAKE_ASM_COMPILER=/mnt/nvme_sec/SRC/llvm-project/build-lldlink/./bin/clang-cl \
-DCMAKE_LINKER=/mnt/nvme_sec/SRC/llvm-project/build-lldlink/./bin/lld-link \
....

But cmake is still using vs_link, see:

FAILED: cmTC_a05d1.exe 
        : && /mnt/nvme_sec/SRC/llvm-prebuilts/cmake/linux-amd64/bin/cmake -E vs_link_exe --intdir=CMakeFiles/cmTC_a05d1.dir --rc=/mnt/nvme_sec/SRC/llvm-project/build-lldlink/./bin/llvm-rc --mt=/mnt/nvme_sec/SRC/llvm-project/build-lldlink/bin/llvm-mt --manifests  -- /mnt/nvme_sec/SRC/llvm-project/build-lldlink/bin/lld-link /nologo CMakeFiles/cmTC_a05d1.dir/CMakeCCompilerABI.c.obj  /out:cmTC_a05d1.exe /implib:cmTC_a05d1.lib /pdb:cmTC_a05d1.pdb /version:0.0 /vfsoverlay:/mnt/nvme_sec/SRC/WinSDK/winsdk-cipd/llvm-vfsoverlay.yaml  /debug /INCREMENTAL /subsystem:console
phosek accepted this revision.Feb 3 2023, 10:22 AM

LGTM

clang/cmake/caches/Fuchsia-stage2.cmake
95

Thanks for checking, we should investigate this further and perhaps reach out to CMake maintainers but that can be done separately.