Skip to content

Commit

Permalink
Merge pull request #5537 from star-hengxing/multiple-target
Browse files Browse the repository at this point in the history
Support multiple targets for package
  • Loading branch information
waruqi committed Aug 30, 2024
2 parents f11f39b + 412be79 commit 2f3f1a0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 59 deletions.
72 changes: 38 additions & 34 deletions xmake/modules/package/tools/cmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,13 @@ function _fix_pdbdir_for_ninja(package)
end
end

-- enter build directory
function _enter_buildir(package, opt)
local buildir = opt.buildir or package:buildir()
os.mkdir(path.join(buildir, "install"))
return os.cd(buildir)
end

-- get build environments
function buildenvs(package, opt)

Expand Down Expand Up @@ -1010,8 +1017,9 @@ end
-- do build for make
function _build_for_make(package, configs, opt)
local argv = {}
if opt.target then
table.insert(argv, opt.target)
local targets = table.wrap(opt.target)
if #targets ~= 0 then
table.join2(argv, targets)
end
local jobs = _get_parallel_njobs(opt)
table.insert(argv, "-j" .. jobs)
Expand Down Expand Up @@ -1055,9 +1063,19 @@ function _build_for_cmakebuild(package, configs, opt)
table.insert(argv, "--config")
table.insert(argv, opt.config)
end
if opt.target then
local targets = table.wrap(opt.target)
if #targets ~= 0 then
table.insert(argv, "--target")
table.insert(argv, opt.target)
if #targets > 1 then
-- https://stackoverflow.com/questions/47553569/how-can-i-build-multiple-targets-using-cmake-build
if _get_cmake_version():ge("3.15") then
table.join2(argv, targets)
else
raise("Build multiple targets need cmake >=3.15")
end
else
table.insert(argv, targets[1])
end
end
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package)})
end
Expand Down Expand Up @@ -1168,15 +1186,9 @@ function _get_cmake_generator(package, opt)
return cmake_generator
end

-- build package
function build(package, configs, opt)
function configure(package, configs, opt)
opt = opt or {}
local cmake_generator = _get_cmake_generator(package, opt)

-- enter build directory
local buildir = opt.buildir or package:buildir()
os.mkdir(path.join(buildir, "install"))
local oldir = os.cd(buildir)
local oldir = _enter_buildir(package, opt)

-- pass configurations
local argv = {}
Expand All @@ -1195,8 +1207,19 @@ function build(package, configs, opt)
-- do configure
local cmake = assert(find_tool("cmake"), "cmake not found!")
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package, opt)})
os.cd(oldir)
end

-- build package
function build(package, configs, opt)
opt = opt or {}
local cmake_generator = _get_cmake_generator(package, opt)

-- do configure
configure(package, configs, opt)

-- do build
local oldir = _enter_buildir(package, opt)
if opt.cmake_build then
_build_for_cmakebuild(package, configs, opt)
elseif cmake_generator then
Expand Down Expand Up @@ -1224,30 +1247,11 @@ function install(package, configs, opt)
opt = opt or {}
local cmake_generator = _get_cmake_generator(package, opt)

-- enter build directory
local buildir = opt.buildir or package:buildir()
os.mkdir(path.join(buildir, "install"))
local oldir = os.cd(buildir)

-- pass configurations
local argv = {}
for name, value in pairs(_get_configs(package, configs, opt)) do
value = tostring(value):trim()
if type(name) == "number" then
if value ~= "" then
table.insert(argv, value)
end
else
table.insert(argv, "-D" .. name .. "=" .. value)
end
end
table.insert(argv, oldir)

-- generate build file
local cmake = assert(find_tool("cmake"), "cmake not found!")
os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package, opt)})
-- do configure
configure(package, configs, opt)

-- do build and install
local oldir = _enter_buildir(package, opt)
if opt.cmake_build then
_install_for_cmakebuild(package, configs, opt)
elseif cmake_generator then
Expand Down
38 changes: 17 additions & 21 deletions xmake/modules/package/tools/ninja.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
import("core.base.option")
import("lib.detect.find_tool")

-- build package
function build(package, configs, opt)
function _default_argv(package, configs, opt)
opt = opt or {}
local buildir = opt.buildir or os.curdir()
local njob = opt.jobs or option.get("jobs") or tostring(os.default_njob())
local ninja = assert(find_tool("ninja"), "ninja not found!")

local argv = {}
if opt.target then
table.insert(argv, opt.target)
local targets = table.wrap(opt.target)
if #targets ~= 0 then
table.join2(argv, targets)
end
table.insert(argv, "-C")
table.insert(argv, buildir)
Expand All @@ -42,28 +42,24 @@ function build(package, configs, opt)
if configs then
table.join2(argv, configs)
end

return argv
end

-- build package
function build(package, configs, opt)
opt = opt or {}
local argv = {}
local ninja = assert(find_tool("ninja"), "ninja not found!")
table.join2(argv, _default_argv(package, configs, opt))
os.vrunv(ninja.program, argv, {envs = opt.envs})
end

-- install package
function install(package, configs, opt)
opt = opt or {}
local buildir = opt.buildir or os.curdir()
local njob = opt.jobs or option.get("jobs") or tostring(os.default_njob())
local ninja = assert(find_tool("ninja"), "ninja not found!")
local argv = {"install"}
if opt.target then
table.insert(argv, opt.target)
end
table.insert(argv, "-C")
table.insert(argv, buildir)
if option.get("verbose") then
table.insert(argv, "-v")
end
table.insert(argv, "-j")
table.insert(argv, njob)
if configs then
table.join2(argv, configs)
end
local ninja = assert(find_tool("ninja"), "ninja not found!")
table.join2(argv, _default_argv(package, configs, opt))
os.vrunv(ninja.program, argv, {envs = opt.envs})
end
10 changes: 6 additions & 4 deletions xmake/modules/package/tools/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,18 @@ function install(package, configs, opt)
if njob then
table.insert(argv, "--jobs=" .. njob)
end
if opt.target then
table.insert(argv, opt.target)
local target = table.wrap(opt.target)
if #target ~= 0 then
table.join2(argv, target)
end
os.vrunv(os.programfile(), argv, {envs = envs})

-- do install
argv = {"install", "-y", "--nopkgs", "-o", package:installdir()}
_set_builtin_argv(package, argv)
if opt.target then
table.insert(argv, opt.target)
local targets = table.wrap(opt.target)
if #targets ~= 0 then
table.join2(argv, targets)
end
os.vrunv(os.programfile(), argv, {envs = envs})
end

0 comments on commit 2f3f1a0

Please sign in to comment.