Clarify host/target separation when cross-building lld for Windows with Linux clang and MinGW/MSYS2
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
## Background
I am building LLVM/lld in two stages:
```text
stage1:
Build native clang/lld on Linux with GCC.
stage2:
Use the stage1 Linux clang/clang++.
Use a MinGW/MSYS2 sysroot.
Cross-build Windows lld.exe.
```
The stage2 environment is effectively:
```text
host:
Linux x86_64
stage1 clang / clang++ / llvm-tblgen / ld.lld
target:
x86_64-w64-windows-gnu
MinGW/MSYS2 runtime
Windows PE/COFF output
```
## Problem
The current CMake configuration does not clearly separate host tools from the target runtime/sysroot.
A toolchain file can describe the target like this:
```cmake
set(CMAKE_SYSTEM_NAME "Windows")
set(CMAKE_C_COMPILER ".../clang")
set(CMAKE_CXX_COMPILER ".../clang++")
set(CMAKE_C_COMPILER_TARGET "x86_64-w64-windows-gnu")
set(CMAKE_CXX_COMPILER_TARGET "x86_64-w64-windows-gnu")
set(CMAKE_SYSROOT ".../mingw64")
```
However, during configuration, some checks and link steps still appear to mix host and target assumptions.
The main issues are:
```text
1. winpthread is not linked automatically.
The target uses the MinGW/MSYS2 runtime.
When std::thread, pthread, or libstdc++ threading support is involved, the target side needs winpthread.
If this dependency is not added automatically, linking can fail.
2. ASM compiler detection is not always aligned with the C/C++ target configuration.
In this stage2 build, the ASM compiler should also be the stage1 clang.
It should inherit the same target triple, sysroot, and linker configuration as C/C++.
Since CMake detects ASM separately, the detected assembler version, target format, or flags can become inconsistent with the C/C++ configuration.
```
## Root cause
This is not a clang cross-compilation problem, and it is not a MinGW/MSYS2 runtime problem.
The problem is that the build configuration does not clearly model the different roles of:
```text
host compiler
host tools
target compiler
target sysroot
target runtime
target C++ standard library
target ASM compiler
```
As a result, a Linux-hosted clang cross-compiling to Windows through a MinGW/MSYS2 sysroot requires manual fixes in places that should ideally be expressed by the toolchain configuration.
## Current workaround
I currently work around this by constructing a complete local MSYS2 mingw64 sysroot:
```text
/ae/0_sysroot/msys2/mingw64
```
Then I point the stage2 toolchain file to that sysroot:
```cmake
set(CMAKE_SYSROOT "/ae/0_sysroot/msys2/mingw64")
set(CMAKE_FIND_ROOT_PATH "/ae/0_sysroot/msys2/mingw64")
set(CMAKE_C_COMPILER_TARGET "x86_64-w64-windows-gnu")
set(CMAKE_CXX_COMPILER_TARGET "x86_64-w64-windows-gnu")
```
In some cases, target include and link paths still need to be added explicitly.
## Expected behavior
It would be helpful if the LLVM/lld CMake cross-build configuration made the host/target boundary clearer, especially for:
```text
separating host tools from the target compiler
describing the target runtime/sysroot explicitly
linking target runtime dependencies such as winpthread correctly
making ASM compiler detection inherit the same target configuration as C/C++
```
This would make it easier to build Windows lld.exe from Linux using clang and a MinGW/MSYS2 sysroot without manually patching target runtime and ASM configuration details.
Contributor guide
Research direction
Start with the stage2 CMake toolchain configuration described in the issue and trace how host tools, target compiler settings, sysroot, runtime dependencies, and ASM detection are handled. No source files or tests are named; done means the host/target boundary is explicit, target dependencies such as winpthread are handled, and ASM inherits the C/C++ target configuration without manual fixes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake
- Domain
- build-system, compilers, operating-systems
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100