From 94829508d57ac49bcd0cecc3a6de6f5afcdf921c Mon Sep 17 00:00:00 2001 From: star9029 Date: Thu, 29 Aug 2024 16:50:29 +0800 Subject: [PATCH 1/5] Support multiple targets for package --- xmake/modules/package/tools/cmake.lua | 60 +++++++++++++-------------- xmake/modules/package/tools/ninja.lua | 40 ++++++++---------- xmake/modules/package/tools/xmake.lua | 10 +++-- 3 files changed, 52 insertions(+), 58 deletions(-) diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index 99dfe0436a3..c9cfc8c2aa2 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -1002,12 +1002,13 @@ end -- do build for make function _build_for_make(package, configs, opt) local argv = {} - if opt.target then - table.insert(argv, opt.target) + local target = table.wrap(opt.target) + if #target ~= 0 then + table.join2(argv, target) end local jobs = _get_parallel_njobs(opt) table.insert(argv, "-j" .. jobs) - if option.get("diagnosis") then + if option.get("verbose") then table.insert(argv, "VERBOSE=1") end if is_host("bsd") then @@ -1047,9 +1048,19 @@ function _build_for_cmakebuild(package, configs, opt) table.insert(argv, "--config") table.insert(argv, opt.config) end - if opt.target then + local target = table.wrap(opt.target) + if #target ~= 0 then table.insert(argv, "--target") - table.insert(argv, opt.target) + if #target > 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, target) + else + raise("Build multiple targets need cmake >=3.15") + end + else + table.insert(argv, target[1]) + end end os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package)}) end @@ -1082,7 +1093,7 @@ end function _install_for_make(package, configs, opt) local jobs = _get_parallel_njobs(opt) local argv = {"-j" .. jobs} - if option.get("diagnosis") then + if option.get("verbose") then table.insert(argv, "VERBOSE=1") end if is_host("bsd") then @@ -1160,10 +1171,8 @@ 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() @@ -1188,6 +1197,15 @@ function build(package, configs, opt) local cmake = assert(find_tool("cmake"), "cmake not found!") os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package, opt)}) + return oldir +end + +-- build package +function build(package, configs, opt) + opt = opt or {} + local cmake_generator = _get_cmake_generator(package, opt) + local oldir = configure(package, configs, opt) + -- do build if opt.cmake_build then _build_for_cmakebuild(package, configs, opt) @@ -1215,29 +1233,7 @@ end 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)}) + local oldir = configure(package, configs, opt) -- do build and install if opt.cmake_build then diff --git a/xmake/modules/package/tools/ninja.lua b/xmake/modules/package/tools/ninja.lua index 42dc73ed626..f900a8d1f0f 100644 --- a/xmake/modules/package/tools/ninja.lua +++ b/xmake/modules/package/tools/ninja.lua @@ -22,19 +22,19 @@ 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 target = table.wrap(opt.target) + if #target ~= 0 then + table.join2(argv, target) end table.insert(argv, "-C") table.insert(argv, buildir) - if option.get("diagnosis") then + if option.get("verbose") then table.insert(argv, "-v") end table.insert(argv, "-j") @@ -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 diff --git a/xmake/modules/package/tools/xmake.lua b/xmake/modules/package/tools/xmake.lua index dbe417a8001..b9cf605ce1a 100644 --- a/xmake/modules/package/tools/xmake.lua +++ b/xmake/modules/package/tools/xmake.lua @@ -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 target = table.wrap(opt.target) + if #target ~= 0 then + table.join2(argv, target) end os.vrunv(os.programfile(), argv, {envs = envs}) end From 2292b165497b0f2c560899c2436a52b779ee043b Mon Sep 17 00:00:00 2001 From: star9029 Date: Thu, 29 Aug 2024 20:58:24 +0800 Subject: [PATCH 2/5] log output verbose -> diagnosis --- xmake/modules/package/tools/cmake.lua | 4 ++-- xmake/modules/package/tools/ninja.lua | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index c9cfc8c2aa2..27268613d8c 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -1008,7 +1008,7 @@ function _build_for_make(package, configs, opt) end local jobs = _get_parallel_njobs(opt) table.insert(argv, "-j" .. jobs) - if option.get("verbose") then + if option.get("diagnosis") then table.insert(argv, "VERBOSE=1") end if is_host("bsd") then @@ -1093,7 +1093,7 @@ end function _install_for_make(package, configs, opt) local jobs = _get_parallel_njobs(opt) local argv = {"-j" .. jobs} - if option.get("verbose") then + if option.get("diagnosis") then table.insert(argv, "VERBOSE=1") end if is_host("bsd") then diff --git a/xmake/modules/package/tools/ninja.lua b/xmake/modules/package/tools/ninja.lua index f900a8d1f0f..df5ba21da16 100644 --- a/xmake/modules/package/tools/ninja.lua +++ b/xmake/modules/package/tools/ninja.lua @@ -34,7 +34,7 @@ function _default_argv(package, configs, opt) end table.insert(argv, "-C") table.insert(argv, buildir) - if option.get("verbose") then + if option.get("diagnosis") then table.insert(argv, "-v") end table.insert(argv, "-j") From 7d2b49214caa15642cbdff28bf8797692e14768c Mon Sep 17 00:00:00 2001 From: star9029 Date: Thu, 29 Aug 2024 21:00:10 +0800 Subject: [PATCH 3/5] target -> targets --- xmake/modules/package/tools/cmake.lua | 16 ++++++++-------- xmake/modules/package/tools/ninja.lua | 6 +++--- xmake/modules/package/tools/xmake.lua | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index 27268613d8c..2dabe8b8607 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -1002,9 +1002,9 @@ end -- do build for make function _build_for_make(package, configs, opt) local argv = {} - local target = table.wrap(opt.target) - if #target ~= 0 then - table.join2(argv, 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) @@ -1048,18 +1048,18 @@ function _build_for_cmakebuild(package, configs, opt) table.insert(argv, "--config") table.insert(argv, opt.config) end - local target = table.wrap(opt.target) - if #target ~= 0 then + local targets = table.wrap(opt.target) + if #targets ~= 0 then table.insert(argv, "--target") - if #target > 1 then + 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, target) + table.join2(argv, targets) else raise("Build multiple targets need cmake >=3.15") end else - table.insert(argv, target[1]) + table.insert(argv, targets[1]) end end os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package)}) diff --git a/xmake/modules/package/tools/ninja.lua b/xmake/modules/package/tools/ninja.lua index df5ba21da16..2d2fb734d5c 100644 --- a/xmake/modules/package/tools/ninja.lua +++ b/xmake/modules/package/tools/ninja.lua @@ -28,9 +28,9 @@ function _default_argv(package, configs, opt) local njob = opt.jobs or option.get("jobs") or tostring(os.default_njob()) local argv = {} - local target = table.wrap(opt.target) - if #target ~= 0 then - table.join2(argv, target) + local targets = table.wrap(opt.target) + if #targets ~= 0 then + table.join2(argv, targets) end table.insert(argv, "-C") table.insert(argv, buildir) diff --git a/xmake/modules/package/tools/xmake.lua b/xmake/modules/package/tools/xmake.lua index b9cf605ce1a..c779a9ff56b 100644 --- a/xmake/modules/package/tools/xmake.lua +++ b/xmake/modules/package/tools/xmake.lua @@ -526,9 +526,9 @@ function install(package, configs, opt) -- do install argv = {"install", "-y", "--nopkgs", "-o", package:installdir()} _set_builtin_argv(package, argv) - local target = table.wrap(opt.target) - if #target ~= 0 then - table.join2(argv, target) + local targets = table.wrap(opt.target) + if #targets ~= 0 then + table.join2(argv, targets) end os.vrunv(os.programfile(), argv, {envs = envs}) end From 3ee57aa5c9297e37027108fd1756f93eef9da196 Mon Sep 17 00:00:00 2001 From: star9029 Date: Thu, 29 Aug 2024 23:43:35 +0800 Subject: [PATCH 4/5] improve cmake configure --- xmake/modules/package/tools/cmake.lua | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index 2dabe8b8607..f7556dd8ec8 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -1171,14 +1171,9 @@ function _get_cmake_generator(package, opt) return cmake_generator end -function configure(package, configs, opt) +function configure(package, configs, opt, sourcedir) opt = opt or {} - -- 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 @@ -1191,20 +1186,23 @@ function configure(package, configs, opt) table.insert(argv, "-D" .. name .. "=" .. value) end end - table.insert(argv, oldir) + table.insert(argv, sourcedir) -- do configure local cmake = assert(find_tool("cmake"), "cmake not found!") os.vrunv(cmake.program, argv, {envs = opt.envs or buildenvs(package, opt)}) - - return oldir end -- build package function build(package, configs, opt) opt = opt or {} local cmake_generator = _get_cmake_generator(package, opt) - local oldir = configure(package, configs, opt) + + -- enter build directory + local buildir = opt.buildir or package:buildir() + os.mkdir(path.join(buildir, "install")) + local oldir = os.cd(buildir) + configure(package, configs, opt, oldir) -- do build if opt.cmake_build then @@ -1233,7 +1231,12 @@ end function install(package, configs, opt) opt = opt or {} local cmake_generator = _get_cmake_generator(package, opt) - local oldir = configure(package, configs, opt) + + -- enter build directory + local buildir = opt.buildir or package:buildir() + os.mkdir(path.join(buildir, "install")) + local oldir = os.cd(buildir) + configure(package, configs, opt, oldir) -- do build and install if opt.cmake_build then From 412be797d41b3524eb37e71e17a791e03bc226ab Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 30 Aug 2024 10:38:22 +0800 Subject: [PATCH 5/5] Update cmake.lua --- xmake/modules/package/tools/cmake.lua | 29 ++++++++++++++++----------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index f7556dd8ec8..3a2caadbd8e 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -939,6 +939,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) @@ -1171,8 +1178,9 @@ function _get_cmake_generator(package, opt) return cmake_generator end -function configure(package, configs, opt, sourcedir) +function configure(package, configs, opt) opt = opt or {} + local oldir = _enter_buildir(package, opt) -- pass configurations local argv = {} @@ -1186,11 +1194,12 @@ function configure(package, configs, opt, sourcedir) table.insert(argv, "-D" .. name .. "=" .. value) end end - table.insert(argv, sourcedir) + table.insert(argv, oldir) -- 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 @@ -1198,13 +1207,11 @@ function build(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) - configure(package, configs, opt, oldir) + -- 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 @@ -1232,13 +1239,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) - configure(package, configs, opt, oldir) + -- 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