[Clang][CMake] Support MinGW in CLANG_ENABLE_BOOTSTRAP #242849
<!--LLVM NEW CONTRIBUTOR COMMENT-->
Hello @EmilAskerov :wave:
Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.
All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.
---
Frequently asked questions
How do I add reviewers?
This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.
You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.
What if there are no comments?
If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.
Are any special GitHub settings required to contribute to LLVM?
We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.
---
If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.
Thank you,
The LLVM Community
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Emil Askerov (EmilAskerov)
<details>
<summary>Changes</summary>
When bootstrapping Clang with a MinGW toolchain on Windows (WIN32 and MINGW are both true), clang/CMakeLists.txt currently assumes WIN32 always implies an MSVC-like toolchain (clang-cl.exe, llvm-lib, lld-link.exe):
1. Compiler selection: clang-cl.exe was selected for all WIN32 hosts, failing when building with a MinGW toolchain/sysroot. Select clang${CMAKE_EXECUTABLE_SUFFIX} / clang++${CMAKE_EXECUTABLE_SUFFIX} by default and reserve clang-cl.exe for WIN32 AND NOT MINGW.
2. LTO archiver selection: The elseif(MSVC) / elseif(NOT WIN32) branches skipped MinGW entirely, leaving llvm-ar / llvm-ranlib unset and omitted from clang-bootstrap-deps. Add an elseif(MINGW) branch to depend on llvm-ar and llvm-ranlib and set CMAKE_AR / CMAKE_RANLIB with ${CMAKE_EXECUTABLE_SUFFIX}.
3. LLD linker selection: BOOTSTRAP_LLVM_ENABLE_LLD forced lld-link.exe on all Windows hosts, which fails with GNU-driver flags passed by clang.exe on MinGW. Select ld.lld${CMAKE_EXECUTABLE_SUFFIX} when MINGW is set (and targeting Windows).
Tested locally on Windows using llvm-mingw (x86_64-w64-windows-gnu) with:
The stage2-configure target completed successfully.
---
Full diff: https://github.com/llvm/llvm-project/pull/242849.diff
1 Files Affected:
````````````diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index fbb4cfaf09005..51e883d8eb972 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -768,6 +768,10 @@ if (CLANG_ENABLE_BOOTSTRAP)
elseif(MSVC)
add_dependencies(clang-bootstrap-deps llvm-lib)
set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-lib)
+ elseif(MINGW)
+ add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
+ set(${CLANG_STAGE}_AR -DCMAKE_AR=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ar${CMAKE_EXECUTABLE_SUFFIX})
+ set(${CLANG_STAGE}_RANLIB -DCMAKE_RANLIB=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-ranlib${CMAKE_EXECUTABLE_SUFFIX})
elseif(NOT WIN32)
add_dependencies(clang-bootstrap-deps llvm-ar llvm-ranlib)
if(NOT BOOTSTRAP_LLVM_ENABLE_LLD AND LLVM_BINUTILS_INCDIR)
@@ -833,9 +837,9 @@ if (CLANG_ENABLE_BOOTSTRAP)
endif()
endif()
- set(C_COMPILER "clang")
- set(CXX_COMPILER "clang++")
- if(WIN32)
+ set(C_COMPILER "clang${CMAKE_EXECUTABLE_SUFFIX}")
+ set(CXX_COMPILER "clang++${CMAKE_EXECUTABLE_SUFFIX}")
+ if(WIN32 AND NOT MINGW)
set(C_COMPILER "clang-cl.exe")
set(CXX_COMPILER "clang-cl.exe")
endif()
@@ -849,7 +853,9 @@ if (CLANG_ENABLE_BOOTSTRAP)
# cmake requires CMAKE_LINKER to be specified if the compiler is MSVC-like,
# otherwise it defaults the linker to be link.exe.
if(BOOTSTRAP_LLVM_ENABLE_LLD)
- if((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
+ if(MINGW AND (NOT BOOTSTRAP_CMAKE_SYSTEM_NAME OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows"))
+ set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/ld.lld${CMAKE_EXECUTABLE_SUFFIX})
+ elseif((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(${CLANG_STAGE}_LINKER -DCMAKE_LINKER=${LLVM_RUNTIME_OUTPUT_INTDIR}/lld-link${CMAKE_EXECUTABLE_SUFFIX})
endif()
endif()
</details>
Hello @EmilAskerov 👋
> Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.
> All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
* Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.
> Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.
> ### Frequently asked questions
How do I add reviewers?
> This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.
> You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.> What if there are no comments?
> If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.
> Are any special GitHub settings required to contribute to LLVM?
> We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.
> If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.
> Thank you, The LLVM Community
I have read the LLVM AI Tool Use Policy, Code-Review Policy and Practices, and Developer Policy.
@mstorsjo Could you please review the MinGW-specific CMake changes in this PR?
Problem
When configuring the Clang bootstrap on Windows with a MinGW toolchain, both WIN32 and MINGW are true. However, clang/CMakeLists.txt currently treats WIN32 as an indication that the host uses an MSVC-style toolchain.
This causes several problems during Stage2 configuration:
clang-cl.exe and uses the MSVC command-line interface instead of clang.exe and clang++.exe. As a result, GNU-style compiler flags such as -ffile-prefix-map are reported as unknown arguments and ignored.BOOTSTRAP_LLVM_ENABLE_LLD=ON, the configuration selects lld-link.exe instead of ld.lld.exe. CMake consequently invokes the MSVC-style vs_link_exe workflow, passing MSVC-specific linker arguments.-Wl,/Brepro and -flto=thin, are passed to lld-link.exe, which reports them as unknown arguments.kernel32.lib, user32.lib, msvcrtd.lib, and oldnames.lib. These libraries are not available in the MinGW environment, causing the link step to fail.MSVC and NOT WIN32 branches do not cover the MinGW case. Consequently, llvm-ar and llvm-ranlib are not added to clang-bootstrap-deps or configured for the bootstrap stage.The failure occurs during CMake's initial C compiler test (CMakeTestCCompiler.cmake), before the Stage2 build can be generated:
The C compiler
".../build/bin/clang-cl.exe"
is not able to compile a simple test program.
lld-link: error: could not open 'kernel32.lib': No such file or directory
lld-link: error: could not open 'user32.lib': No such file or directory
lld-link: error: could not open 'msvcrtd.lib': No such file or directory
Solution
Handle MinGW explicitly in the bootstrap configuration:
clang.exe and clang++.exe for MinGW builds, while preserving clang-cl.exe for non-MinGW Windows builds.llvm-ar.exe and llvm-ranlib.exe.ld.lld.exe for MinGW bootstrap builds targeting Windows, while retaining lld-link.exe for the existing MSVC configuration.This allows the bootstrap configuration to use the appropriate GNU-style tools on MinGW without changing the existing MSVC behavior.
I'm unsure about this part here. We don't set any -DCMAKE_LINKER= at all for other platforms, and the comment above says we only need to do this when the compiler is MSVC_like. So I would rather limit the existing conditional by adding an (MSVC AND ...()) to it, or ((NOT MINGW) AND (...)) or so. Otherwise I'd like to know why we should do this for mingw but not for unixy targets.
Couldn't this be made into elseif((NOT WIN32) OR MINGW), if you'd add ${CMAKE_EXECUTABLE_SUFFIX} where needed?
Good point, thank you! Updated to restrict the lld-link CMAKE_LINKER setting with NOT MINGW instead of setting ld.lld here, and verified locally that stage2 still configures and links with -fuse-ld=lld.
Done, combined into elseif((NOT WIN32) OR MINGW) with ${CMAKE_EXECUTABLE_SUFFIX}.
Won't the BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows" case hit on Windows too?
I wonder if keeping this case would be benign? On the other hand, gold won't do much for windows anyway, so there's no need for keeping it either...
Moved (NOT MINGW) outside to wrap both conditions: if((NOT MINGW) AND ((WIN32 AND NOT BOOTSTRAP_CMAKE_SYSTEM_NAME) OR BOOTSTRAP_CMAKE_SYSTEM_NAME STREQUAL "Windows")).
Yes, LLVMgold isn't relevant on Windows/MinGW anyway, so guarding it with NOT WIN32 avoids pulling it in if LLVM_BINUTILS_INCDIR happens to be set.
LGTM
I can merge this later today if nobody else objects (and if I remember - if not, remind me in a day or two).
LGTM
> I can merge this later today if nobody else objects (and if I remember - if not, remind me in a day or two).
Thanks for the review and approval! I appreciate your help. I'll wait for the merge.
Oh, also, for this to be mergeable: Please turn off Keep my email addresses private setting in your account. See LLVM Developer Policy and LLVM Discourse for more information.
Oh, also, for this to be mergeable: Please turn off Keep my email addresses private setting in your account. See LLVM Developer Policy and LLVM Discourse for more information.
I've turned off the Keep my email addresses private setting in my GitHub account. Thanks for letting me know!
LGTM
> I can merge this later today if nobody else objects (and if I remember - if not, remind me in a day or two).
@mstorsjo just a quick remind regarding this PR. All checks are passed. Thanks again for the review!
<!--LLVM BUILDBOT INFORMATION COMMENT-->
@EmilAskerov Congratulations on having your first Pull Request (PR) merged into the LLVM Project!
Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.
Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.
How to do this, and the rest of the post-merge process, is covered in detail here.
If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.
If you don't get any reports, no action is required from you. Your changes are working as expected, well done!
mstorsjo
EmilAskerov
When bootstrapping Clang with a MinGW toolchain on Windows (
WIN32andMINGWare both true),clang/CMakeLists.txtcurrently assumesWIN32implies an MSVC-like toolchain:clang${CMAKE_EXECUTABLE_SUFFIX}andclang++${CMAKE_EXECUTABLE_SUFFIX}instead ofclang-cl.exewhenMINGWis set.elseif(NOT WIN32)LTO archiver branch toelseif((NOT WIN32) OR MINGW)with${CMAKE_EXECUTABLE_SUFFIX}(keepingLLVMgoldrestricted toNOT WIN32).lld-linkCMAKE_LINKERoverride underBOOTSTRAP_LLVM_ENABLE_LLDtoWIN32 AND NOT MINGW(or cross-compiling toWindows), since MinGW uses the GNU driver like Unix targets.Tested locally on Windows with llvm-mingw (
x86_64-w64-windows-gnu),CLANG_ENABLE_BOOTSTRAP=ON,BOOTSTRAP_LLVM_ENABLE_LLD=ON, andBOOTSTRAP_LLVM_ENABLE_LTO=Thin(stage2-configureand stage2llvm-tblgenbuild succeed).