diff options
| author | ruki <[email protected]> | 2021-12-17 00:53:13 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-12-17 00:53:13 +0800 |
| commit | 1bb127c6ddb9ca7928f827f04160dde828e54925 (patch) | |
| tree | 89eba30db8ec8dd415c8616b7823da720958776c | |
| parent | 8512b8200ac982c4075c4707c3b2971c73159c9f (diff) | |
improve cmake find_pacakge
| -rw-r--r-- | tests/projects/package/cmake/.gitignore | 8 | ||||
| -rw-r--r-- | tests/projects/package/cmake/src/main.cpp | 9 | ||||
| -rw-r--r-- | tests/projects/package/cmake/xmake.lua | 11 | ||||
| -rw-r--r-- | xmake/core/package/package.lua | 4 | ||||
| -rw-r--r-- | xmake/modules/package/manager/cargo/install_package.lua | 6 | ||||
| -rw-r--r-- | xmake/modules/package/manager/clib/install_package.lua | 10 | ||||
| -rw-r--r-- | xmake/modules/package/manager/cmake/configurations.lua | 10 | ||||
| -rw-r--r-- | xmake/modules/package/manager/cmake/find_package.lua | 33 | ||||
| -rw-r--r-- | xmake/modules/package/manager/conan/configurations.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/package/manager/conan/install_package.lua | 28 | ||||
| -rw-r--r-- | xmake/modules/package/manager/vcpkg/find_package.lua | 6 |
11 files changed, 83 insertions, 44 deletions
diff --git a/tests/projects/package/cmake/.gitignore b/tests/projects/package/cmake/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/projects/package/cmake/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/projects/package/cmake/src/main.cpp b/tests/projects/package/cmake/src/main.cpp new file mode 100644 index 000000000..7c435d251 --- /dev/null +++ b/tests/projects/package/cmake/src/main.cpp @@ -0,0 +1,9 @@ +#include <iostream> + +using namespace std; + +int main(int argc, char** argv) +{ + cout << "hello world!" << endl; + return 0; +} diff --git a/tests/projects/package/cmake/xmake.lua b/tests/projects/package/cmake/xmake.lua new file mode 100644 index 000000000..48aa74660 --- /dev/null +++ b/tests/projects/package/cmake/xmake.lua @@ -0,0 +1,11 @@ +add_rules("mode.debug", "mode.release") + +add_requires("cmake::ZLIB", {system = true}) +add_requires("cmake::Boost", {system = true, + configs = {components = {"regex", "system"}, presets = {Boost_USE_STATIC_LIB = true}}}) +target("test") + set_kind("binary") + add_files("src/*.cpp") + add_packages("cmake::ZLIB", "cmake::Boost") + + diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 602b0e58d..e255fa1ec 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -1316,7 +1316,7 @@ function _instance:find_package(name, opt) mode = self:mode(), plat = self:plat(), arch = self:arch(), - pkgconfigs = self:configs(), + configs = self:configs(), buildhash = self:buildhash(), -- for xmake package or 3rd package manager, e.g. go:: .. cachekey = opt.cachekey or "fetch_package_system", external = opt.external, @@ -1896,7 +1896,7 @@ function package.load_from_system(packagename) -- on install script local on_install = function (pkg) local opt = {} - opt.pkgconfigs = pkg:configs() + opt.configs = pkg:configs() opt.mode = pkg:is_debug() and "debug" or "release" opt.plat = pkg:plat() opt.arch = pkg:arch() diff --git a/xmake/modules/package/manager/cargo/install_package.lua b/xmake/modules/package/manager/cargo/install_package.lua index dd2fde7e9..9ea709dd7 100644 --- a/xmake/modules/package/manager/cargo/install_package.lua +++ b/xmake/modules/package/manager/cargo/install_package.lua @@ -45,7 +45,7 @@ function main(name, opt) -- get required version opt = opt or {} - local pkgconfigs = opt.pkgconfigs or {} + local configs = opt.configs or {} local require_version = opt.require_version if not require_version or require_version == "latest" then require_version = "*" @@ -62,10 +62,10 @@ function main(name, opt) tomlfile:print("edition = \"2018\"") tomlfile:print("") tomlfile:print("[dependencies]") - local features = pkgconfigs.features + local features = configs.features if features then features = table.wrap(features) - tomlfile:print("%s = {version = \"%s\", features = [\"%s\"], default-features = %s}", name, require_version, table.concat(features, "\", \""), pkgconfigs.default_features) + tomlfile:print("%s = {version = \"%s\", features = [\"%s\"], default-features = %s}", name, require_version, table.concat(features, "\", \""), configs.default_features) else tomlfile:print("%s = \"%s\"", name, require_version) end diff --git a/xmake/modules/package/manager/clib/install_package.lua b/xmake/modules/package/manager/clib/install_package.lua index 8ebac7829..0257ff8fe 100644 --- a/xmake/modules/package/manager/clib/install_package.lua +++ b/xmake/modules/package/manager/clib/install_package.lua @@ -26,7 +26,7 @@ import("lib.detect.find_tool") -- install package -- @param name the package name, e.g. clib::clibs/[email protected] -- @param opt the options, e.g. { verbose = true, --- pkgconfigs = {outputdir = "clib", save = false, save_dev = false}} +-- configs = {outputdir = "clib", save = false, save_dev = false}} -- -- @return true or false -- @@ -39,19 +39,19 @@ function main(name, opt) end opt = opt or {} - local pkgconfigs = opt.pkgconfigs or {} + local configs = opt.configs or {} local argv = {"install", name} - local abs_out = path.join(os.projectdir(), pkgconfigs.outputdir) + local abs_out = path.join(os.projectdir(), configs.outputdir) dprint("installing %s to %s", name, abs_out) table.insert(argv, "-o " .. abs_out) if not option.get("verbose") then table.insert(argv, "-q") end - if pkgconfigs.save then + if configs.save then table.insert(argv, "--save") end - if pkgconfigs.save_dev then + if configs.save_dev then table.insert(argv, "--save-dev") end diff --git a/xmake/modules/package/manager/cmake/configurations.lua b/xmake/modules/package/manager/cmake/configurations.lua index ce9b21e17..e65525f6d 100644 --- a/xmake/modules/package/manager/cmake/configurations.lua +++ b/xmake/modules/package/manager/cmake/configurations.lua @@ -22,12 +22,10 @@ function main() return { - build = {description = "use it to choose if you want to build from sources.", default = "missing", values = {"all", "never", "missing", "outdated"}}, - remote = {description = "Set the conan remote server."}, - options = {description = "Set the options values, e.g. OpenSSL:shared=True"}, - imports = {description = "Set the imports for conan."}, - settings = {description = "Set the build settings for conan."}, - build_requires = {description = "Set the build requires for conan.", default = "xmake_generator/0.1.0@bincrafters/testing"} + components = {description = "Set the cmake package components, e.g. {\"regex\", \"system\"}"}, + moduledirs = {description = "Set the cmake modules directories."}, + presets = {description = "Set the preset values, e.g. {Boost_USE_STATIC_LIB = true}"}, + envs = {description = "Set the run environments of cmake, e.g. {CMAKE_PREFIX_PATH = \"xxx\"}"}, } end diff --git a/xmake/modules/package/manager/cmake/find_package.lua b/xmake/modules/package/manager/cmake/find_package.lua index e0e767145..b78b784c5 100644 --- a/xmake/modules/package/manager/cmake/find_package.lua +++ b/xmake/modules/package/manager/cmake/find_package.lua @@ -41,23 +41,28 @@ function _find_package(cmake, name, opt) -- e.g. OpenCV 4.1.1, Boost COMPONENTS regex system local requirestr = name + local configs = opt.configs or {} if opt.required_version then requirestr = requirestr .. " " .. opt.required_version end - if opt.components then + -- use opt.components is for backward compatibility + local components = configs.components or opt.components + if components then requirestr = requirestr .. " COMPONENTS" - for _, component in ipairs(opt.components) do + for _, component in ipairs(components) do requirestr = requirestr .. " " .. component end end - if opt.moduledirs then - for _, moduledir in ipairs(opt.moduledirs) do + local moduledirs = configs.moduledirs or opt.moduledirs + if moduledirs then + for _, moduledir in ipairs(moduledirs) do cmakefile:print("add_cmake_modules(%s)", moduledir) end end -- e.g. set(Boost_USE_STATIC_LIB ON) - if opt.presets then - for k, v in pairs(opt.presets) do + local presets = configs.presets or opt.presets + if presets then + for k, v in pairs(presets) do if type(v) == "boolean" then cmakefile:print("set(%s %s)", k, v and "ON" or "OFF") else @@ -82,7 +87,8 @@ function _find_package(cmake, name, opt) cmakefile:close() -- run cmake - try {function() return os.vrunv(cmake.program, {workdir}, {curdir = workdir, envs = opt.envs}) end} + local envs = configs.envs or opt.envs + try {function() return os.vrunv(cmake.program, {workdir}, {curdir = workdir, envs = envs}) end} -- pares defines and includedirs for macosx/linux local links @@ -238,12 +244,19 @@ end -- -- find_package("cmake::ZLIB") -- find_package("cmake::OpenCV", {required_version = "4.1.1"}) --- find_package("cmake::Boost", {components = {"regex", "system"}, presets = {Boost_USE_STATIC_LIB = true}}) --- find_package("cmake::Foo", {moduledirs = "xxx"}) +-- find_package("cmake::Boost", {configs = {components = {"regex", "system"}, presets = {Boost_USE_STATIC_LIB = true}}}) +-- find_package("cmake::Foo", {configs = {moduledirs = "xxx"}}) +-- +-- we can use add_requires with {system = true} +-- +-- add_requires("cmake::ZLIB", {system = true}) +-- add_requires("cmake::OpenCV 4.1.1", {system = true}) +-- add_requires("cmake::Boost", {configs = {components = {"regex", "system"}, presets = {Boost_USE_STATIC_LIB = true}}}) +-- add_requires("cmake::Foo", {configs = {moduledirs = "xxx"}}) -- -- @param name the package name -- @param opt the options, e.g. {verbose = true, required_version = "1.0", --- pkgconfigs = { +-- configs = { -- components = {"regex", "system"}, -- moduledirs = "xxx", -- presets = {Boost_USE_STATIC_LIB = true}, diff --git a/xmake/modules/package/manager/conan/configurations.lua b/xmake/modules/package/manager/conan/configurations.lua index ce9b21e17..b4ed38fc3 100644 --- a/xmake/modules/package/manager/conan/configurations.lua +++ b/xmake/modules/package/manager/conan/configurations.lua @@ -22,7 +22,7 @@ function main() return { - build = {description = "use it to choose if you want to build from sources.", default = "missing", values = {"all", "never", "missing", "outdated"}}, + build = {description = "Use it to choose if you want to build from sources.", default = "missing", values = {"all", "never", "missing", "outdated"}}, remote = {description = "Set the conan remote server."}, options = {description = "Set the options values, e.g. OpenSSL:shared=True"}, imports = {description = "Set the imports for conan."}, diff --git a/xmake/modules/package/manager/conan/install_package.lua b/xmake/modules/package/manager/conan/install_package.lua index d7f730e14..9f5a52a18 100644 --- a/xmake/modules/package/manager/conan/install_package.lua +++ b/xmake/modules/package/manager/conan/install_package.lua @@ -49,15 +49,15 @@ function _conan_get_build_directory(name) end -- generate conanfile.txt -function _conan_generate_conanfile(name, pkgconfigs) +function _conan_generate_conanfile(name, configs) -- trace dprint("generate %s ..", path.join(_conan_get_build_directory(name), "conanfile.txt")) -- get conan options, imports and build_requires - local options = table.wrap(pkgconfigs.options) - local imports = table.wrap(pkgconfigs.imports) - local build_requires = table.wrap(pkgconfigs.build_requires) + local options = table.wrap(configs.options) + local imports = table.wrap(configs.imports) + local build_requires = table.wrap(configs.build_requires) -- @see https://docs.conan.io/en/latest/systems_cross_building/cross_building.html -- generate it @@ -113,7 +113,7 @@ end -- -- @param name the package name, e.g. conan::OpenSSL/1.0.2n@conan/stable -- @param opt the options, e.g. { verbose = true, mode = "release", plat = , arch = , --- pkgconfigs = { +-- configs = { -- remote = "", build = "all", options = {}, imports = {}, build_requires = {}, -- settings = {"compiler=Visual Studio", "compiler.version=10", "compiler.runtime=MD"}}} -- @@ -121,9 +121,9 @@ end -- function main(name, opt) - -- get pkgconfigs + -- get configs opt = opt or {} - local pkgconfigs = opt.pkgconfigs or {} + local configs = opt.configs or {} -- find conan local conan = find_tool("conan") @@ -147,15 +147,15 @@ function main(name, opt) _conan_install_xmake_generator(conan) -- generate conanfile.txt - _conan_generate_conanfile(name, pkgconfigs) + _conan_generate_conanfile(name, configs) -- install package local argv = {"install", "."} - if pkgconfigs.build then - if pkgconfigs.build == "all" then + if configs.build then + if configs.build == "all" then table.insert(argv, "--build") else - table.insert(argv, "--build=" .. pkgconfigs.build) + table.insert(argv, "--build=" .. configs.build) end end @@ -233,15 +233,15 @@ function main(name, opt) end -- set custom settings - for _, setting in ipairs(pkgconfigs.settings) do + for _, setting in ipairs(configs.settings) do table.insert(argv, "-s") table.insert(argv, setting) end -- set remote - if pkgconfigs.remote then + if configs.remote then table.insert(argv, "-r") - table.insert(argv, pkgconfigs.remote) + table.insert(argv, configs.remote) end -- TODO set environments diff --git a/xmake/modules/package/manager/vcpkg/find_package.lua b/xmake/modules/package/manager/vcpkg/find_package.lua index 4b587be0a..1547da40a 100644 --- a/xmake/modules/package/manager/vcpkg/find_package.lua +++ b/xmake/modules/package/manager/vcpkg/find_package.lua @@ -82,10 +82,10 @@ function main(name, opt) -- find the package info file, e.g. zlib_1.2.11-3_x86-windows[-static].list local triplet = arch .. "-" .. plat - local pkgconfigs = opt.pkgconfigs - if plat == "windows" and pkgconfigs and pkgconfigs.shared ~= true then + local configs = opt.configs + if plat == "windows" and configs and configs.shared ~= true then triplet = triplet .. "-static" - if pkgconfigs.vs_runtime and pkgconfigs.vs_runtime:startswith("MD") then + if configs.vs_runtime and configs.vs_runtime:startswith("MD") then triplet = triplet .. "-md" end end |
