Skip to content

Commit 9482950

Browse files
committed
Support multiple targets for package
1 parent 87630c8 commit 9482950

File tree

3 files changed

+52
-58
lines changed

3 files changed

+52
-58
lines changed

xmake/modules/package/tools/cmake.lua

+28-32
Original file line numberDiff line numberDiff line change
@@ -1002,12 +1002,13 @@ end
10021002
-- do build for make
10031003
function _build_for_make(package, configs, opt)
10041004
local argv = {}
1005-
if opt.target then
1006-
table.insert(argv, opt.target)
1005+
local target = table.wrap(opt.target)
1006+
if #target ~= 0 then
1007+
table.join2(argv, target)
10071008
end
10081009
local jobs = _get_parallel_njobs(opt)
10091010
table.insert(argv, "-j" .. jobs)
1010-
if option.get("diagnosis") then
1011+
if option.get("verbose") then
10111012
table.insert(argv, "VERBOSE=1")
10121013
end
10131014
if is_host("bsd") then
@@ -1047,9 +1048,19 @@ function _build_for_cmakebuild(package, configs, opt)
10471048
table.insert(argv, "--config")
10481049
table.insert(argv, opt.config)
10491050
end
1050-
if opt.target then
1051+
local target = table.wrap(opt.target)
1052+
if #target ~= 0 then
10511053
table.insert(argv, "--target")
1052-
table.insert(argv, opt.target)
1054+
if #target > 1 then
1055+
-- https://stackoverflow.com/questions/47553569/how-can-i-build-multiple-targets-using-cmake-build
1056+
if _get_cmake_version():ge("3.15") then
1057+
table.join2(argv, target)
1058+
else
1059+
raise("Build multiple targets need cmake >=3.15")
1060+
end
1061+
else
1062+
table.insert(argv, target[1])
1063+
end
10531064
end
10541065
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package)})
10551066
end
@@ -1082,7 +1093,7 @@ end
10821093
function _install_for_make(package, configs, opt)
10831094
local jobs = _get_parallel_njobs(opt)
10841095
local argv = {"-j" .. jobs}
1085-
if option.get("diagnosis") then
1096+
if option.get("verbose") then
10861097
table.insert(argv, "VERBOSE=1")
10871098
end
10881099
if is_host("bsd") then
@@ -1160,10 +1171,8 @@ function _get_cmake_generator(package, opt)
11601171
return cmake_generator
11611172
end
11621173

1163-
-- build package
1164-
function build(package, configs, opt)
1174+
function configure(package, configs, opt)
11651175
opt = opt or {}
1166-
local cmake_generator = _get_cmake_generator(package, opt)
11671176

11681177
-- enter build directory
11691178
local buildir = opt.buildir or package:buildir()
@@ -1188,6 +1197,15 @@ function build(package, configs, opt)
11881197
local cmake = assert(find_tool("cmake"), "cmake not found!")
11891198
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package, opt)})
11901199

1200+
return oldir
1201+
end
1202+
1203+
-- build package
1204+
function build(package, configs, opt)
1205+
opt = opt or {}
1206+
local cmake_generator = _get_cmake_generator(package, opt)
1207+
local oldir = configure(package, configs, opt)
1208+
11911209
-- do build
11921210
if opt.cmake_build then
11931211
_build_for_cmakebuild(package, configs, opt)
@@ -1215,29 +1233,7 @@ end
12151233
function install(package, configs, opt)
12161234
opt = opt or {}
12171235
local cmake_generator = _get_cmake_generator(package, opt)
1218-
1219-
-- enter build directory
1220-
local buildir = opt.buildir or package:buildir()
1221-
os.mkdir(path.join(buildir, "install"))
1222-
local oldir = os.cd(buildir)
1223-
1224-
-- pass configurations
1225-
local argv = {}
1226-
for name, value in pairs(_get_configs(package, configs, opt)) do
1227-
value = tostring(value):trim()
1228-
if type(name) == "number" then
1229-
if value ~= "" then
1230-
table.insert(argv, value)
1231-
end
1232-
else
1233-
table.insert(argv, "-D" .. name .. "=" .. value)
1234-
end
1235-
end
1236-
table.insert(argv, oldir)
1237-
1238-
-- generate build file
1239-
local cmake = assert(find_tool("cmake"), "cmake not found!")
1240-
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package, opt)})
1236+
local oldir = configure(package, configs, opt)
12411237

12421238
-- do build and install
12431239
if opt.cmake_build then

xmake/modules/package/tools/ninja.lua

+18-22
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,44 @@
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 target = table.wrap(opt.target)
32+
if #target ~= 0 then
33+
table.join2(argv, target)
3434
end
3535
table.insert(argv, "-C")
3636
table.insert(argv, buildir)
37-
if option.get("diagnosis") then
37+
if option.get("verbose") then
3838
table.insert(argv, "-v")
3939
end
4040
table.insert(argv, "-j")
4141
table.insert(argv, njob)
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 target = table.wrap(opt.target)
530+
if #target ~= 0 then
531+
table.join2(argv, target)
530532
end
531533
os.vrunv(os.programfile(), argv, {envs = envs})
532534
end

0 commit comments

Comments
 (0)