Skip to content

Commit 2f3f1a0

Browse files
authored
Merge pull request #5537 from star-hengxing/multiple-target
Support multiple targets for package
2 parents f11f39b + 412be79 commit 2f3f1a0

File tree

3 files changed

+61
-59
lines changed

3 files changed

+61
-59
lines changed

xmake/modules/package/tools/cmake.lua

+38-34
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,13 @@ function _fix_pdbdir_for_ninja(package)
947947
end
948948
end
949949

950+
-- enter build directory
951+
function _enter_buildir(package, opt)
952+
local buildir = opt.buildir or package:buildir()
953+
os.mkdir(path.join(buildir, "install"))
954+
return os.cd(buildir)
955+
end
956+
950957
-- get build environments
951958
function buildenvs(package, opt)
952959

@@ -1010,8 +1017,9 @@ end
10101017
-- do build for make
10111018
function _build_for_make(package, configs, opt)
10121019
local argv = {}
1013-
if opt.target then
1014-
table.insert(argv, opt.target)
1020+
local targets = table.wrap(opt.target)
1021+
if #targets ~= 0 then
1022+
table.join2(argv, targets)
10151023
end
10161024
local jobs = _get_parallel_njobs(opt)
10171025
table.insert(argv, "-j" .. jobs)
@@ -1055,9 +1063,19 @@ function _build_for_cmakebuild(package, configs, opt)
10551063
table.insert(argv, "--config")
10561064
table.insert(argv, opt.config)
10571065
end
1058-
if opt.target then
1066+
local targets = table.wrap(opt.target)
1067+
if #targets ~= 0 then
10591068
table.insert(argv, "--target")
1060-
table.insert(argv, opt.target)
1069+
if #targets > 1 then
1070+
-- https://stackoverflow.com/questions/47553569/how-can-i-build-multiple-targets-using-cmake-build
1071+
if _get_cmake_version():ge("3.15") then
1072+
table.join2(argv, targets)
1073+
else
1074+
raise("Build multiple targets need cmake >=3.15")
1075+
end
1076+
else
1077+
table.insert(argv, targets[1])
1078+
end
10611079
end
10621080
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package)})
10631081
end
@@ -1168,15 +1186,9 @@ function _get_cmake_generator(package, opt)
11681186
return cmake_generator
11691187
end
11701188

1171-
-- build package
1172-
function build(package, configs, opt)
1189+
function configure(package, configs, opt)
11731190
opt = opt or {}
1174-
local cmake_generator = _get_cmake_generator(package, opt)
1175-
1176-
-- enter build directory
1177-
local buildir = opt.buildir or package:buildir()
1178-
os.mkdir(path.join(buildir, "install"))
1179-
local oldir = os.cd(buildir)
1191+
local oldir = _enter_buildir(package, opt)
11801192

11811193
-- pass configurations
11821194
local argv = {}
@@ -1195,8 +1207,19 @@ function build(package, configs, opt)
11951207
-- do configure
11961208
local cmake = assert(find_tool("cmake"), "cmake not found!")
11971209
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package, opt)})
1210+
os.cd(oldir)
1211+
end
1212+
1213+
-- build package
1214+
function build(package, configs, opt)
1215+
opt = opt or {}
1216+
local cmake_generator = _get_cmake_generator(package, opt)
1217+
1218+
-- do configure
1219+
configure(package, configs, opt)
11981220

11991221
-- do build
1222+
local oldir = _enter_buildir(package, opt)
12001223
if opt.cmake_build then
12011224
_build_for_cmakebuild(package, configs, opt)
12021225
elseif cmake_generator then
@@ -1224,30 +1247,11 @@ function install(package, configs, opt)
12241247
opt = opt or {}
12251248
local cmake_generator = _get_cmake_generator(package, opt)
12261249

1227-
-- enter build directory
1228-
local buildir = opt.buildir or package:buildir()
1229-
os.mkdir(path.join(buildir, "install"))
1230-
local oldir = os.cd(buildir)
1231-
1232-
-- pass configurations
1233-
local argv = {}
1234-
for name, value in pairs(_get_configs(package, configs, opt)) do
1235-
value = tostring(value):trim()
1236-
if type(name) == "number" then
1237-
if value ~= "" then
1238-
table.insert(argv, value)
1239-
end
1240-
else
1241-
table.insert(argv, "-D" .. name .. "=" .. value)
1242-
end
1243-
end
1244-
table.insert(argv, oldir)
1245-
1246-
-- generate build file
1247-
local cmake = assert(find_tool("cmake"), "cmake not found!")
1248-
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package, opt)})
1250+
-- do configure
1251+
configure(package, configs, opt)
12491252

12501253
-- do build and install
1254+
local oldir = _enter_buildir(package, opt)
12511255
if opt.cmake_build then
12521256
_install_for_cmakebuild(package, configs, opt)
12531257
elseif cmake_generator then

xmake/modules/package/tools/ninja.lua

+17-21
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
import("core.base.option")
2323
import("lib.detect.find_tool")
2424

25-
-- build package
26-
function build(package, configs, opt)
25+
function _default_argv(package, configs, opt)
2726
opt = opt or {}
2827
local buildir = opt.buildir or os.curdir()
2928
local njob = opt.jobs or option.get("jobs") or tostring(os.default_njob())
30-
local ninja = assert(find_tool("ninja"), "ninja not found!")
29+
3130
local argv = {}
32-
if opt.target then
33-
table.insert(argv, opt.target)
31+
local targets = table.wrap(opt.target)
32+
if #targets ~= 0 then
33+
table.join2(argv, targets)
3434
end
3535
table.insert(argv, "-C")
3636
table.insert(argv, buildir)
@@ -42,28 +42,24 @@ function build(package, configs, opt)
4242
if configs then
4343
table.join2(argv, configs)
4444
end
45+
46+
return argv
47+
end
48+
49+
-- build package
50+
function build(package, configs, opt)
51+
opt = opt or {}
52+
local argv = {}
53+
local ninja = assert(find_tool("ninja"), "ninja not found!")
54+
table.join2(argv, _default_argv(package, configs, opt))
4555
os.vrunv(ninja.program, argv, {envs = opt.envs})
4656
end
4757

4858
-- install package
4959
function install(package, configs, opt)
5060
opt = opt or {}
51-
local buildir = opt.buildir or os.curdir()
52-
local njob = opt.jobs or option.get("jobs") or tostring(os.default_njob())
53-
local ninja = assert(find_tool("ninja"), "ninja not found!")
5461
local argv = {"install"}
55-
if opt.target then
56-
table.insert(argv, opt.target)
57-
end
58-
table.insert(argv, "-C")
59-
table.insert(argv, buildir)
60-
if option.get("verbose") then
61-
table.insert(argv, "-v")
62-
end
63-
table.insert(argv, "-j")
64-
table.insert(argv, njob)
65-
if configs then
66-
table.join2(argv, configs)
67-
end
62+
local ninja = assert(find_tool("ninja"), "ninja not found!")
63+
table.join2(argv, _default_argv(package, configs, opt))
6864
os.vrunv(ninja.program, argv, {envs = opt.envs})
6965
end

xmake/modules/package/tools/xmake.lua

+6-4
Original file line numberDiff line numberDiff line change
@@ -517,16 +517,18 @@ function install(package, configs, opt)
517517
if njob then
518518
table.insert(argv, "--jobs=" .. njob)
519519
end
520-
if opt.target then
521-
table.insert(argv, opt.target)
520+
local target = table.wrap(opt.target)
521+
if #target ~= 0 then
522+
table.join2(argv, target)
522523
end
523524
os.vrunv(os.programfile(), argv, {envs = envs})
524525

525526
-- do install
526527
argv = {"install", "-y", "--nopkgs", "-o", package:installdir()}
527528
_set_builtin_argv(package, argv)
528-
if opt.target then
529-
table.insert(argv, opt.target)
529+
local targets = table.wrap(opt.target)
530+
if #targets ~= 0 then
531+
table.join2(argv, targets)
530532
end
531533
os.vrunv(os.programfile(), argv, {envs = envs})
532534
end

0 commit comments

Comments
 (0)