Skip to content

Commit 566acc6

Browse files
committed
improve cmake for ninja
1 parent 014838d commit 566acc6

File tree

7 files changed

+220
-11
lines changed

7 files changed

+220
-11
lines changed

tests/projects/c/library_with_cmakelists/xmake.lua

+170
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,175 @@
11
add_rules("mode.debug", "mode.release")
22

3+
package("cmake")
4+
set_kind("binary")
5+
set_homepage("https://cmake.org")
6+
set_description("A cross-platform family of tool designed to build, test and package software")
7+
8+
if is_host("macosx") then
9+
add_urls("https://cmake.org/files/v$(version).tar.gz", {version = function (version)
10+
return table.concat(table.slice((version):split('%.'), 1, 2), '.') .. "/cmake-" .. version .. (version:ge("3.20") and "-macos-universal" or "-Darwin-x86_64")
11+
end})
12+
add_urls("https://github.com/Kitware/CMake/releases/download/v$(version).tar.gz", {version = function (version)
13+
return version .. "/cmake-" .. version .. (version:ge("3.20") and "-macos-universal" or "-Darwin-x86_64")
14+
end})
15+
add_versions("3.11.4", "2b5eb705f036b1906a5e0bce996e9cd56d43d73bdee8318ece3e5ce31657b812")
16+
add_versions("3.15.4", "adfbf611d21daa83b9bf6d85ab06a455e481b63a38d6e1270d563b03d4e5f829")
17+
add_versions("3.18.4", "9d27049660474cf134ab46fa0e0db771b263313fcb8ba82ee8b2d1a1a62f8f20")
18+
add_versions("3.21.0", "c1c6f19dfc9c658a48b5aed22806595b2337bb3aedb71ab826552f74f568719f")
19+
add_versions("3.22.1", "9ba46ce69d524f5bcdf98076a6b01f727604fb31cf9005ec03dea1cf16da9514")
20+
add_versions("3.24.1", "71bb8db69826d74c395a3c3bbf8b773dbe9f54a2c7331266ba70da303e9c97a1")
21+
add_versions("3.24.2", "efb11a78c064dd7c54a50b8da247254d252112c402c6e48cb7db3f9c84a4e5ad")
22+
add_versions("3.26.4", "5417fb979c1f82aaffe4420112e2c84562c024b6683161afb520c9e378161340")
23+
add_versions("3.28.1", "0e0942bb5ed7ee1aeda0c00b3cb7738f2590865f1d69fe1d5212cbc26fc040a5")
24+
add_versions("3.28.3", "d9e2c22fec920a4d1f6b0d0683c035d799475c179c91e41e1a7fbfab610a0305")
25+
add_versions("3.29.2", "0d670b59dddd064d24cf8c386abf3590bda2642bb169e11534cf1e3d1ae3a76a")
26+
add_versions("3.30.1", "51e12618829b811bba6f033ee8f39f6192da1b6abb20d82a7899d5134e879a4c")
27+
add_versions("3.30.2", "c6fdda745f9ce69bca048e91955c7d043ba905d6388a62e0ff52b681ac17183c")
28+
elseif is_host("linux") then
29+
if os.arch():find("arm64.*") then
30+
add_urls("https://cmake.org/files/v$(version)-aarch64.tar.gz", {version = function (version)
31+
return table.concat(table.slice((version):split('%.'), 1, 2), '.') .. "/cmake-" .. version .. (version:ge("3.20") and "-linux" or "-Linux")
32+
end})
33+
add_urls("https://github.com/Kitware/CMake/releases/download/v$(version)-aarch64.tar.gz", {version = function (version)
34+
return version .. "/cmake-" .. version .. (version:ge("3.20") and "-linux" or "-Linux")
35+
end})
36+
add_versions("3.24.2", "5f1c0d49bac89915b5c68811c2430e5de6c8e606785b9f2919eabee86c2f12b4")
37+
add_versions("3.26.4", "1c9843c92f40bee1a16baa12871693d3e190c9a222259a89e406d4d9aae6cf74")
38+
add_versions("3.28.1", "e84d88e46ed8c85fbe259bcd4ca07df7a928df87e84013e0da34d91b01a25d71")
39+
add_versions("3.28.3", "bbf023139f944cefe731d944f2864d8ea3ea0c4f9310b46ac72b3cb4e314b023")
40+
add_versions("3.29.2", "ca883c6dc3ce9eebd833804f0f940ecbbff603520cfd169ee58916dbbc23c2b8")
41+
add_versions("3.30.1", "ad234996f8750f11d7bd0d17b03f55c434816adf1f1671aab9e8bab21a43286a")
42+
add_versions("3.30.2", "d18f50f01b001303d21f53c6c16ff12ee3aa45df5da1899c2fe95be7426aa026")
43+
else
44+
add_urls("https://cmake.org/files/v$(version)-x86_64.tar.gz", {version = function (version)
45+
return table.concat(table.slice((version):split('%.'), 1, 2), '.') .. "/cmake-" .. version .. (version:ge("3.20") and "-linux" or "-Linux")
46+
end})
47+
add_urls("https://github.com/Kitware/CMake/releases/download/v$(version)-x86_64.tar.gz", {version = function (version)
48+
return version .. "/cmake-" .. version .. (version:ge("3.20") and "-linux" or "-Linux")
49+
end})
50+
add_versions("3.11.4", "6dab016a6b82082b8bcd0f4d1e53418d6372015dd983d29367b9153f1a376435")
51+
add_versions("3.15.4", "7c2b17a9be605f523d71b99cc2e5b55b009d82cf9577efb50d4b23056dee1109")
52+
add_versions("3.18.4", "149e0cee002e59e0bb84543cf3cb099f108c08390392605e944daeb6594cbc29")
53+
add_versions("3.21.0", "d54ef6909f519740bc85cec07ff54574cd1e061f9f17357d9ace69f61c6291ce")
54+
add_versions("3.22.1", "73565c72355c6652e9db149249af36bcab44d9d478c5546fd926e69ad6b43640")
55+
add_versions("3.24.1", "827bf068cfaa23a9fb95f990c9f8a7ed8f2caeb3af62b5c0a2fed7a8dd6dde3e")
56+
add_versions("3.24.2", "71a776b6a08135092b5beb00a603b60ca39f8231c01a0356e205e0b4631747d9")
57+
add_versions("3.26.4", "ba1e0dcc710e2f92be6263f9617510b3660fa9dc409ad2fb8190299563f952a0")
58+
add_versions("3.28.1", "f76398c24362ad87bad1a3d6f1e8f4377632b5b1c360c4ba1fd7cd205fd9d8d4")
59+
add_versions("3.28.3", "804d231460ab3c8b556a42d2660af4ac7a0e21c98a7f8ee3318a74b4a9a187a6")
60+
add_versions("3.29.2", "0416c70cf88e8f92efcbfe292e181bc09ead7d70e29ab37b697522c01121eab5")
61+
add_versions("3.30.1", "ac31f077ef3378641fa25a3cb980d21b2f083982d3149a8f2eb9154f2b53696b")
62+
add_versions("3.30.2", "cdd7fb352605cee3ae53b0e18b5929b642900e33d6b0173e19f6d4f2067ebf16")
63+
end
64+
elseif is_host("windows") then
65+
if os.arch() == "x64" then
66+
add_urls("https://cmake.org/files/v$(version).zip", {excludes = {"*/doc/*"}, version = function (version)
67+
return table.concat(table.slice((version):split('%.'), 1, 2), '.') .. "/cmake-" .. version .. (version:ge("3.20") and "-windows-x86_64" or "-win64-x64")
68+
end})
69+
add_urls("https://github.com/Kitware/CMake/releases/download/v$(version).zip", {excludes = {"*/doc/*"}, version = function (version)
70+
return version .. "/cmake-" .. version .. (version:ge("3.20") and "-windows-x86_64" or "-win64-x64")
71+
end})
72+
add_versions("3.11.4", "d3102abd0ded446c898252b58857871ee170312d8e7fd5cbff01fbcb1068a6e5")
73+
add_versions("3.15.4", "5bb49c0274800c38833e515a01af75a7341db68ea82c71856bb3cf171d2068be")
74+
add_versions("3.18.4", "a932bc0c8ee79f1003204466c525b38a840424d4ae29f9e5fb88959116f2407d")
75+
add_versions("3.21.0", "c7b88c907a753f4ec86e43ddc89f91f70bf1b011859142f7f29e6d51ea4abb3c")
76+
add_versions("3.22.1", "35fbbb7d9ffa491834bbc79cdfefc6c360088a3c9bf55c29d111a5afa04cdca3")
77+
add_versions("3.24.1", "c1b17431a16337d517f7ba78c7067b6f143a12686cb8087f3dd32f3fa45f5aae")
78+
add_versions("3.24.2", "6af30354eecbb7113b0f0142d13c03d21abbc9f4dbdcddaf88df1f9ca1bc4d6f")
79+
add_versions("3.26.4", "62c35427104a4f8205226f72708d71334bd36a72cf72c60d0e3a766d71dcc78a")
80+
add_versions("3.28.1", "671332249bc7cc7424523d6c2b5edd3e3de90a43b8b82e8782f42da4fe4c562d")
81+
add_versions("3.28.3", "cac7916f7e1e73a25de857704c94fd5b72ba9fe2f055356b5602d2f960e50e5b")
82+
add_versions("3.29.2", "86b5de51f60a0e9d62be4d8ca76ea467d154083d356fcc9af1409606be341cd8")
83+
add_versions("3.30.1", "cf7788ff9d92812da194847d4ec874fc576f34079987d0f20c96cd09e2a16220")
84+
add_versions("3.30.2", "48bf4b3dc2d668c578e0884cac7878e146b036ca6b5ce4f8b5572f861b004c25")
85+
elseif os.arch() == "x86" then
86+
add_urls("https://cmake.org/files/v$(version).zip", {excludes = {"*/doc/*"}, version = function (version)
87+
return table.concat(table.slice((version):split('%.'), 1, 2), '.') .. "/cmake-" .. version .. (version:ge("3.20") and "-windows-i386" or "-win32-x86")
88+
end})
89+
add_urls("https://github.com/Kitware/CMake/releases/download/v$(version).zip", {excludes = {"*/doc/*"}, version = function (version)
90+
return version .. "/cmake-" .. version .. (version:ge("3.20") and "-windows-i386" or "-win32-x86")
91+
end})
92+
add_versions("3.11.4", "b068001ff879f86e704977c50a8c5917e4b4406c66242366dba2674abe316579")
93+
add_versions("3.15.4", "19c2bfd26c4de4d8046dd5ad6de95b57a2556559ec81b13b94e63ea4ae49b3f2")
94+
add_versions("3.18.4", "4c519051853686927f87df99669ada3ff15a3086535a7131892febd7c6e2f122")
95+
add_versions("3.21.0", "11ee86b7f9799724fc16664c63e308bfe3fbc22c9df8ef4955ad4b248f3e680b")
96+
add_versions("3.22.1", "f53494e3b35e5a1177ad55c28763eb5bb45772c1d80778c0f96c45ce4376b6e8")
97+
add_versions("3.24.1", "a0b894e2a814d2353f1e581eb6ca3c878a39c071624495729dbcf9978e1579f2")
98+
add_versions("3.24.2", "52f174dc7f52a9c496c7a49ee35456466c07c8ce29aa2092f4b4536ce5d7ed57")
99+
add_versions("3.26.4", "342ca44f494985f8ef43676eb8a0404b2c68321036e28aa221ceab51d377b158")
100+
add_versions("3.28.1", "e9591cfdb1d394eee84acdecf880cbd91cf0707dfd0d58bf3796b88475f46cb9")
101+
add_versions("3.28.3", "411812b6b29ac793faf69bdbd36c612f72659363c5491b9f0a478915db3fc58c")
102+
add_versions("3.29.2", "e51b281c9dfd1498834729b33bf49fc668ad1dadbc2eaba7b693d0f7d748450d")
103+
add_versions("3.30.1", "f5fb1d93b82e9a5fbd5853d4b17a130605f0b4ed13a655d1371c2d6d55f9261d")
104+
add_versions("3.30.2", "d01f7ea52097dd58aa225884b1ecc543827e9ef99d36dac2898609a0d5e60eb6")
105+
elseif os.arch() == "arm64" then
106+
add_urls("https://cmake.org/files/v$(version).zip", {excludes = {"*/doc/*"}, version = function (version)
107+
return table.concat(table.slice((version):split('%.'), 1, 2), '.') .. "/cmake-" .. version .. "-windows-arm64"
108+
end})
109+
add_urls("https://github.com/Kitware/CMake/releases/download/v$(version).zip", {excludes = {"*/doc/*"}, version = function (version)
110+
return version .. "/cmake-" .. version .. "-windows-arm64"
111+
end})
112+
add_versions("3.28.1", "a839b8d32c11b24f078142b5b8c3361a955ebc65788f0f0353b2121fe2f74e49")
113+
add_versions("3.28.3", "cfe023b7e82812ef802fb1ec619f6cfa2fdcb58ee61165fc315086286fe9cdcc")
114+
add_versions("3.29.2", "5b16a0db4966c04582c40131038de49d5b0161fcd950dc9e955753dfab858882")
115+
add_versions("3.30.1", "02b433f70aa549449be2d53046d0179590bf3b6290d9fda3fbbb23f96a4f2802")
116+
add_versions("3.30.2", "c0cef52e8f60eb1c3058f8bc0b3803c27d79f066b7d7d94f46a2c689bbd36f22")
117+
end
118+
else
119+
add_urls("https://github.com/Kitware/CMake/releases/download/v$(version)/cmake-$(version).tar.gz")
120+
add_versions("3.18.4", "597c61358e6a92ecbfad42a9b5321ddd801fc7e7eca08441307c9138382d4f77")
121+
add_versions("3.21.0", "4a42d56449a51f4d3809ab4d3b61fd4a96a469e56266e896ce1009b5768bd2ab")
122+
add_versions("3.22.1", "0e998229549d7b3f368703d20e248e7ee1f853910d42704aa87918c213ea82c0")
123+
add_versions("3.24.1", "4931e277a4db1a805f13baa7013a7757a0cbfe5b7932882925c7061d9d1fa82b")
124+
add_versions("3.24.2", "0d9020f06f3ddf17fb537dc228e1a56c927ee506b486f55fe2dc19f69bf0c8db")
125+
add_versions("3.26.4", "313b6880c291bd4fe31c0aa51d6e62659282a521e695f30d5cc0d25abbd5c208")
126+
add_versions("3.28.1", "15e94f83e647f7d620a140a7a5da76349fc47a1bfed66d0f5cdee8e7344079ad")
127+
add_versions("3.28.3", "72b7570e5c8593de6ac4ab433b73eab18c5fb328880460c86ce32608141ad5c1")
128+
add_versions("3.29.2", "36db4b6926aab741ba6e4b2ea2d99c9193222132308b4dc824d4123cb730352e")
129+
add_versions("3.30.1", "df9b3c53e3ce84c3c1b7c253e5ceff7d8d1f084ff0673d048f260e04ccb346e1")
130+
add_versions("3.30.2", "46074c781eccebc433e98f0bbfa265ca3fd4381f245ca3b140e7711531d60db2")
131+
end
132+
133+
if is_plat("mingw") and is_subhost("msys") then
134+
add_extsources("pacman::cmake")
135+
elseif is_plat("linux") then
136+
add_extsources("pacman::cmake", "apt::cmake")
137+
elseif is_plat("macosx") then
138+
add_extsources("brew::cmake")
139+
end
140+
141+
on_load(function (package)
142+
print("cmake.on_load")
143+
-- xmake v3.x will enable this ninja policy by default
144+
import("core.project.project")
145+
print("111111", project.policy("package.cmake_generator.ninja"))
146+
if xmake.version():ge("2.9.0") and project.policy("package.cmake_generator.ninja") then
147+
print("2222", "add ninja deps")
148+
package:add("deps", "ninja", {public = true})
149+
end
150+
end)
151+
152+
on_install("@macosx", function (package)
153+
os.cp("CMake.app/Contents/bin", package:installdir())
154+
os.cp("CMake.app/Contents/share", package:installdir())
155+
end)
156+
157+
on_install("@linux", "@windows", "@msys", "@cygwin", function (package)
158+
print("install cmake")
159+
os.cp("bin", package:installdir())
160+
os.cp("share", package:installdir())
161+
end)
162+
163+
on_install("@bsd", function (package)
164+
import("core.base.option")
165+
os.vrunv("sh", {"./bootstrap", "--parallel=" .. (option.get("jobs") or tostring(os.default_njob())), "--prefix=" .. package:installdir()})
166+
import("package.tools.make").install(package)
167+
end)
168+
169+
on_test(function (package)
170+
os.vrun("cmake --version")
171+
end)
172+
3173
package("foo")
4174
add_deps("cmake")
5175
set_sourcedir(path.join(os.scriptdir(), "foo"))

tests/test_utils/test_build.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ function test_build:build(argv)
44
os.exec("xmake f -c -D -y")
55
os.exec("xmake")
66
os.rm("build")
7-
os.exec("xmake f -c -D -y --policies=compatibility.version:3.0")
8-
os.exec("xmake -r")
7+
print("xmake f -c -D -y --policies=compatibility.version:3.0")
8+
os.exec("xmake f -c -D -y --policies=compatibility.version:3.0,package.precompiled:n")
9+
os.exec("xmake -rvD")
910
end
1011

1112
function main()

xmake/core/package/package.lua

+22
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,26 @@ function _instance:_configs_for_buildhash()
16681668
configs[name] = value
16691669
end
16701670
end
1671+
1672+
--[[
1673+
-- Fix 3.0 buildhash (with runtimes) to be compatible with existing build artifacts
1674+
-- v2.x: MT: configs.runtimes = nil MD: configs.runtimes = "MD"
1675+
-- v3.x: MD: configs.runtimes = nil MT: configs.runtimes = "MT"
1676+
local project = package._project()
1677+
if project then
1678+
local runtime_default = project.policy("build.c++.msvc.runtime")
1679+
print("runtime_default", runtime_default)
1680+
if runtime_default and runtime_default == "MD" then
1681+
local runtimes = configs.runtimes
1682+
if runtimes == nil then
1683+
configs.runtimes = "MD"
1684+
elseif runtimes == "MT" then
1685+
configs.runtimes = nil
1686+
end
1687+
print("runtimes", runtimes)
1688+
print("configs.runtimes", configs.runtimes)
1689+
end
1690+
end]]
16711691
else
16721692
configs = false
16731693
end
@@ -1798,6 +1818,8 @@ function _instance:buildhash()
17981818
buildhash = _get_buildhash(self:_configs_for_buildhash())
17991819
end
18001820
self._BUILDHASH = buildhash
1821+
print("buildhash", buildhash)
1822+
utils.dump(self:_configs_for_buildhash())
18011823
end
18021824
return buildhash
18031825
end

xmake/core/project/project.lua

+2
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,12 @@ end
869869
-- @see https://github.com/xmake-io/xmake/issues/5527
870870
function project._init_default_policies()
871871
local compatibility_version = project.policy("compatibility.version")
872+
print("_init_default_policies", "compatibility_version", compatibility_version)
872873
if compatibility_version then
873874
if semver.compare(compatibility_version, "3.0") >= 0 then
874875
policy.set_default("package.cmake_generator.ninja", true)
875876
policy.set_default("build.c++.msvc.runtime", "MD")
877+
print("set ninja default true")
876878
else
877879
policy.set_default("package.cmake_generator.ninja", false)
878880
policy.set_default("build.c++.msvc.runtime", "MT")

xmake/modules/package/tools/cmake.lua

+9-9
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,6 @@ function _get_configs_for_mingw(package, configs, opt)
601601
if is_subhost("windows") and opt.cmake_generator ~= "Ninja" then
602602
envs.CMAKE_MAKE_PROGRAM = _get_mingw32_make(package)
603603
end
604-
if opt.cmake_generator == "Ninja" then
605-
envs.CMAKE_MAKE_PROGRAM = "ninja"
606-
end
607604
_fix_cxx_compiler_cmake(package, envs)
608605
_insert_configs_from_envs(configs, envs, opt)
609606
end
@@ -618,12 +615,7 @@ function _get_configs_for_wasm(package, configs, opt)
618615
assert(emscripten_cmakefile, "Emscripten.cmake not found!")
619616
table.insert(configs, "-DCMAKE_TOOLCHAIN_FILE=" .. emscripten_cmakefile)
620617
if is_subhost("windows") then
621-
if opt.cmake_generator == "Ninja" then
622-
local ninja = _get_ninja(package)
623-
if ninja then
624-
table.insert(configs, "-DCMAKE_MAKE_PROGRAM=" .. ninja)
625-
end
626-
else
618+
if opt.cmake_generator ~= "Ninja" then
627619
local mingw_make = _get_mingw32_make(package)
628620
if mingw_make then
629621
table.insert(configs, "-DCMAKE_MAKE_PROGRAM=" .. mingw_make)
@@ -783,6 +775,10 @@ function _get_configs_for_generator(package, configs, opt)
783775
table.insert(configs, "-DCMAKE_JOB_POOL_LINK:STRING=link")
784776
table.insert(configs, ("-DCMAKE_JOB_POOLS:STRING=compile=%s;link=%s"):format(jobs, linkjobs))
785777
end
778+
local ninja = _get_ninja(package)
779+
if ninja then
780+
table.insert(configs, "-DCMAKE_MAKE_PROGRAM=" .. ninja)
781+
end
786782
end
787783
elseif package:is_plat("mingw") and is_subhost("msys") then
788784
table.insert(configs, "-G")
@@ -964,6 +960,7 @@ function _get_configs(package, configs, opt)
964960
table.insert(configs, "-DCMAKE_CXX_COMPILER_LAUNCHER=" .. ccache)
965961
end
966962

963+
print("configs", configs)
967964
return configs
968965
end
969966

@@ -1312,13 +1309,16 @@ function configure(package, configs, opt)
13121309
table.insert(argv, "-D" .. name .. "=" .. value)
13131310
end
13141311
end
1312+
print("before shrink", argv)
13151313
-- shrink cmake arguments, fix too long arguments
13161314
-- @see https://github.com/xmake-io/xmake-repo/pull/5247#discussion_r1780302212
13171315
_shrink_cmake_arguments(argv, oldir, opt)
13181316
table.insert(argv, oldir)
13191317

13201318
-- do configure
13211319
local cmake = assert(find_tool("cmake"), "cmake not found!")
1320+
print(argv)
1321+
print(package:name(), package:plat(), package:arch(), opt)
13221322
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package, opt)})
13231323
os.cd(oldir)
13241324
end

xmake/modules/private/action/require/impl/package.lua

+4
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,10 @@ function _must_depend_on(package, dep)
14371437
local librarydeps = hashset.from(manifest.librarydeps)
14381438
return librarydeps:has(dep:name())
14391439
end
1440+
print(package:name(), package:extraconf("deps", dep:name()))
1441+
if package:is_binary() and package:extraconf("deps", dep:name(), "public") then
1442+
return true
1443+
end
14401444
end
14411445

14421446
-- compatible with all previous link dependencies?

xmake/rules/c++/xmake.lua

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ rule("c.build")
2323
add_deps("c.build.pcheader", "c.build.optimization", "c.build.sanitizer")
2424
on_build_files("private.action.build.object", {batch = true, distcc = true})
2525
on_config(function (target)
26+
-- enable vs runtime as MD by default
27+
if target:is_plat("windows") and not target:get("runtimes") then
28+
local vs_runtime_default = target:policy("build.c++.msvc.runtime")
29+
if vs_runtime_default and target:has_tool("cxx", "cl", "clang_cl") then
30+
if is_mode("debug") then
31+
vs_runtime_default = vs_runtime_default .. "d"
32+
end
33+
target:set("runtimes", vs_runtime_default)
34+
end
35+
end
2636
-- https://github.com/xmake-io/xmake/issues/4621
2737
if target:is_plat("windows") and target:is_static() and target:has_tool("cc", "tcc") then
2838
target:set("extension", ".a")

0 commit comments

Comments
 (0)