Skip to content

Commit 9dfe769

Browse files
committed
fix(cmake): Set correct c++ compiler and detect ninja for MinGW
1. Set clang++ and g++ first since we use the first c++ compiler in the toolchain to build the package 2. Try to detect ninja when use a toolchain without mingw 3. see #5518
1 parent 8dd37a6 commit 9dfe769

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

xmake/modules/package/tools/cmake.lua

+9
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,15 @@ function _get_cmake_generator(package, opt)
11421142
if not cmake_generator then
11431143
if package:has_tool("cc", "clang_cl") or package:has_tool("cxx", "clang_cl") then
11441144
cmake_generator = "Ninja"
1145+
elseif is_subhost("windows") and package:is_plat("mingw") then
1146+
-- When we are using a standalone clang/gcc toolchain without mingw, try to detect ninja automatically.
1147+
-- see:https://github.com/xmake-io/xmake/issues/5518
1148+
local mingw = package:build_getenv("mingw") or package:build_getenv("sdk") or ""
1149+
mingw = find_tool(mingw, {paths = mingw})
1150+
if not mingw then
1151+
assert(find_tool("ninja"), "No mingw or ninja found!")
1152+
cmake_generator = "Ninja"
1153+
end
11451154
end
11461155
end
11471156
local cmake_generator_env = os.getenv("CMAKE_GENERATOR")

xmake/toolchains/clang/xmake.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ toolchain("clang" .. suffix)
3030
set_description("A C language family frontend for LLVM" .. (version and (" (" .. version .. ")") or ""))
3131
set_runtimes("c++_static", "c++_shared", "stdc++_static", "stdc++_shared")
3232

33+
-- set clang++ first, then package:build_getenv("cxx") is always clang++ instead of clang
34+
-- see:https://github.com/xmake-io/xmake/issues/5518
3335
set_toolset("cc", "clang" .. suffix)
34-
set_toolset("cxx", "clang" .. suffix, "clang++" .. suffix)
36+
set_toolset("cxx", "clang++" .. suffix, "clang" .. suffix)
3537
set_toolset("ld", "clang++" .. suffix, "clang" .. suffix)
3638
set_toolset("sh", "clang++" .. suffix, "clang" .. suffix)
3739
set_toolset("ar", "ar", "llvm-ar")
3840
set_toolset("strip", "strip", "llvm-strip")
3941
set_toolset("ranlib", "ranlib", "llvm-ranlib")
4042
set_toolset("objcopy", "objcopy", "llvm-objcopy")
4143
set_toolset("mm", "clang" .. suffix)
42-
set_toolset("mxx", "clang" .. suffix, "clang++" .. suffix)
44+
set_toolset("mxx", "clang++" .. suffix, "clang" .. suffix)
4345
set_toolset("as", "clang" .. suffix)
4446
set_toolset("mrc", "llvm-rc")
4547

xmake/toolchains/gcc/xmake.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ toolchain("gcc" .. suffix)
3030
set_description("GNU Compiler Collection" .. (version and (" (" .. version .. ")") or ""))
3131
set_runtimes("stdc++_static", "stdc++_shared")
3232

33+
-- set g++ first, then package:build_getenv("cxx") is always g++ instead of gcc
34+
-- see:https://github.com/xmake-io/xmake/issues/5518
3335
set_toolset("cc", "gcc" .. suffix)
34-
set_toolset("cxx", "gcc" .. suffix, "g++" .. suffix)
36+
set_toolset("cxx", "g++" .. suffix, "gcc" .. suffix)
3537
set_toolset("ld", "g++" .. suffix, "gcc" .. suffix)
3638
set_toolset("sh", "g++" .. suffix, "gcc" .. suffix)
3739
set_toolset("ar", "ar")
3840
set_toolset("strip", "strip")
3941
set_toolset("objcopy", "objcopy")
4042
set_toolset("ranlib", "ranlib")
4143
set_toolset("mm", "gcc" .. suffix)
42-
set_toolset("mxx", "gcc" .. suffix, "g++" .. suffix)
44+
set_toolset("mxx", "g++" .. suffix, "gcc" .. suffix)
4345
set_toolset("as", "gcc" .. suffix)
4446

4547
on_check(function (toolchain)

0 commit comments

Comments
 (0)