diff options
| author | ruki <[email protected]> | 2021-12-17 00:03:26 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-12-17 00:03:26 +0800 |
| commit | 369afb0809747329524839edbeab813ad3cf0de8 (patch) | |
| tree | 15883de9c7855bab819ad58209836273921f67a2 | |
| parent | 9af42bcb37bde188b60d64d36c4e86873c2ece0b (diff) | |
| parent | 0c977557c4566c6aff800320998c9b3aea0c8f1d (diff) | |
Merge pull request #1917 from xmake-io/package
Improve to find package and configurations
20 files changed, 269 insertions, 72 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cb2bf1d4..78c70ba9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ * [#1904](https://github.com/xmake-io/xmake/pull/1904): Improve vs201x generator * Add `XMAKE_THEME` envirnoment variable to switch theme * [#1907](https://github.com/xmake-io/xmake/issues/1907): Add `-f/--force` to force to create project in a non-empty directory +* [#1917](https://github.com/xmake-io/xmake/pull/1917): Improve to find_package and configurations ### Bugs fixed @@ -1178,6 +1179,7 @@ * [#1904](https://github.com/xmake-io/xmake/pull/1904): 改进 vs201x 工程生成器 * 添加 `XMAKE_THEME` 环境变量去切换主题配置 * [#1907](https://github.com/xmake-io/xmake/issues/1907): 添加 `-f/--force` 参数使得 `xmake create` 可以在费控目录被强制创建 +* [#1917](https://github.com/xmake-io/xmake/pull/1917): 改进 find_package 和配置 ### Bugs 修复 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..b6490032d --- /dev/null +++ b/tests/projects/package/cmake/xmake.lua @@ -0,0 +1,12 @@ +add_rules("mode.debug", "mode.release") + +add_requires("cmake::ZLIB", {system = true}) +add_requires("cmake::LibXml2", {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", "cmake::LibXml2") + + diff --git a/tests/projects/package/conan/.gitignore b/tests/projects/package/conan/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/projects/package/conan/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/projects/package/conan/src/main.cpp b/tests/projects/package/conan/src/main.cpp new file mode 100644 index 000000000..7c435d251 --- /dev/null +++ b/tests/projects/package/conan/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/conan/xmake.lua b/tests/projects/package/conan/xmake.lua new file mode 100644 index 000000000..69f7d2052 --- /dev/null +++ b/tests/projects/package/conan/xmake.lua @@ -0,0 +1,9 @@ +add_requires("conan::zlib/1.2.11", {alias = "zlib", debug = true}) +add_requires("conan::openssl/1.1.1g", {alias = "openssl", + configs = {options = "OpenSSL:shared=True"}}) + +target("test") + set_kind("binary") + add_files("src/*.cpp") + add_packages("openssl", "zlib") + diff --git a/tests/projects/package/vcpkg/.gitignore b/tests/projects/package/vcpkg/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/projects/package/vcpkg/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/projects/package/vcpkg/src/main.cpp b/tests/projects/package/vcpkg/src/main.cpp new file mode 100644 index 000000000..7c435d251 --- /dev/null +++ b/tests/projects/package/vcpkg/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/vcpkg/xmake.lua b/tests/projects/package/vcpkg/xmake.lua new file mode 100644 index 000000000..3f5dbcf55 --- /dev/null +++ b/tests/projects/package/vcpkg/xmake.lua @@ -0,0 +1,8 @@ +add_requires("vcpkg::zlib", "vcpkg::pcre2") +add_requires("vcpkg::boost[core]", {alias = "boost"}) + +target("test") + set_kind("binary") + add_files("src/*.cpp") + add_packages("vcpkg::zlib", "vcpkg::pcre2", "boost") + diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 3b778d617..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, @@ -1895,7 +1895,8 @@ function package.load_from_system(packagename) -- on install script local on_install = function (pkg) - local opt = table.copy(pkg:configs()) + local opt = {} + opt.configs = pkg:configs() opt.mode = pkg:is_debug() and "debug" or "release" opt.plat = pkg:plat() opt.arch = pkg:arch() @@ -1930,9 +1931,9 @@ function package.load_from_system(packagename) if is_thirdparty then -- add configurations for the 3rd package - local install_package = sandbox_module.import("package.manager." .. packagename:split("::")[1]:lower() .. ".install_package", {try = true, anonymous = true}) - if install_package and install_package.configurations then - for name, conf in pairs(install_package.configurations()) do + local configurations = sandbox_module.import("package.manager." .. packagename:split("::")[1]:lower() .. ".configurations", {try = true, anonymous = true}) + if configurations then + for name, conf in pairs(configurations()) do instance:add("configs", name, conf) end end diff --git a/xmake/modules/package/manager/cargo/configurations.lua b/xmake/modules/package/manager/cargo/configurations.lua new file mode 100644 index 000000000..a3f85a0ea --- /dev/null +++ b/xmake/modules/package/manager/cargo/configurations.lua @@ -0,0 +1,28 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file configurations.lua +-- + +-- get configurations +function main() + return + { + features = {description = "set the features of dependency."}, + default_features = {description = "enables or disables any defaults provided by the dependency.", default = true}, + } +end diff --git a/xmake/modules/package/manager/cargo/install_package.lua b/xmake/modules/package/manager/cargo/install_package.lua index 0f06a8f6b..9ea709dd7 100644 --- a/xmake/modules/package/manager/cargo/install_package.lua +++ b/xmake/modules/package/manager/cargo/install_package.lua @@ -23,15 +23,6 @@ import("core.base.option") import("core.project.config") import("lib.detect.find_tool") --- get configurations -function configurations() - return - { - features = {description = "set the features of dependency."}, - default_features = {description = "enables or disables any defaults provided by the dependency.", default = true}, - } -end - -- install package -- -- e.g. @@ -53,6 +44,8 @@ function main(name, opt) end -- get required version + opt = opt or {} + local configs = opt.configs or {} local require_version = opt.require_version if not require_version or require_version == "latest" then require_version = "*" @@ -69,10 +62,10 @@ function main(name, opt) tomlfile:print("edition = \"2018\"") tomlfile:print("") tomlfile:print("[dependencies]") - local features = opt.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, "\", \""), opt.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/configurations.lua b/xmake/modules/package/manager/clib/configurations.lua new file mode 100644 index 000000000..4f59ea70d --- /dev/null +++ b/xmake/modules/package/manager/clib/configurations.lua @@ -0,0 +1,30 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author Adel Vilkov (aka RaZeR-RBI) +-- @file configurations.lua +-- + +-- get configurations +function main() + return + { + save = {description = "save dependency in project's package.json", default = false, type = "boolean"}, + save_dev = {description = "save as development dependency in project's package.json", default = false, type = "boolean"}, + outputdir = {description = "package installation directory relative to project root", default = "clib"}, + } +end + diff --git a/xmake/modules/package/manager/clib/install_package.lua b/xmake/modules/package/manager/clib/install_package.lua index 1b61af5b4..0257ff8fe 100644 --- a/xmake/modules/package/manager/clib/install_package.lua +++ b/xmake/modules/package/manager/clib/install_package.lua @@ -23,42 +23,35 @@ import("core.base.option") import("core.project.config") import("lib.detect.find_tool") --- get configurations -function configurations() - return - { - save = {description = "save dependency in project's package.json", default = false, type = "boolean"}, - save_dev = {description = "save as development dependency in project's package.json", default = false, type = "boolean"}, - outputdir = {description = "package installation directory relative to project root", default = "clib"}, - } -end - -- install package -- @param name the package name, e.g. clib::clibs/[email protected] -- @param opt the options, e.g. { verbose = true, --- settings = {outputdir = "clib", save = false, save_dev = false}} +-- configs = {outputdir = "clib", save = false, save_dev = false}} -- -- @return true or false -- function main(name, opt) + -- find clib local clib = find_tool("clib") if not clib then raise("clib not found!") end + opt = opt or {} + local configs = opt.configs or {} local argv = {"install", name} - local abs_out = path.join(os.projectdir(), opt.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 opt.save then + if configs.save then table.insert(argv, "--save") end - if opt.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 new file mode 100644 index 000000000..e65525f6d --- /dev/null +++ b/xmake/modules/package/manager/cmake/configurations.lua @@ -0,0 +1,31 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file configurations.lua +-- + +-- get configurations +function main() + return + { + 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 22ebc711b..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,15 +244,23 @@ 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", --- components = {"regex", "system"}, --- moduledirs = "xxx", --- presets = {Boost_USE_STATIC_LIB = true}, --- envs = {CMAKE_PREFIX_PATH = "xxx"}) +-- configs = { +-- components = {"regex", "system"}, +-- moduledirs = "xxx", +-- presets = {Boost_USE_STATIC_LIB = true}, +-- envs = {CMAKE_PREFIX_PATH = "xxx"}}) -- function main(name, opt) opt = opt or {} diff --git a/xmake/modules/package/manager/conan/configurations.lua b/xmake/modules/package/manager/conan/configurations.lua new file mode 100644 index 000000000..b4ed38fc3 --- /dev/null +++ b/xmake/modules/package/manager/conan/configurations.lua @@ -0,0 +1,33 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file configurations.lua +-- + +-- get configurations +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"} + } +end + diff --git a/xmake/modules/package/manager/conan/install_package.lua b/xmake/modules/package/manager/conan/install_package.lua index 97af3b4b8..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, opt) +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(opt.options) - local imports = table.wrap(opt.imports) - local build_requires = table.wrap(opt.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 @@ -109,30 +109,22 @@ function _conan_install_xmake_generator(conan) end end --- get configurations -function configurations() - 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"} - } -end - -- install package -- -- @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 = , --- remote = "", build = "all", options = {}, imports = {}, build_requires = {}, --- settings = {"compiler=Visual Studio", "compiler.version=10", "compiler.runtime=MD"}} +-- configs = { +-- remote = "", build = "all", options = {}, imports = {}, build_requires = {}, +-- settings = {"compiler=Visual Studio", "compiler.version=10", "compiler.runtime=MD"}}} -- -- @return true or false -- function main(name, opt) + -- get configs + opt = opt or {} + local configs = opt.configs or {} + -- find conan local conan = find_tool("conan") if not conan then @@ -155,15 +147,15 @@ function main(name, opt) _conan_install_xmake_generator(conan) -- generate conanfile.txt - _conan_generate_conanfile(name, opt) + _conan_generate_conanfile(name, configs) -- install package local argv = {"install", "."} - if opt.build then - if opt.build == "all" then + if configs.build then + if configs.build == "all" then table.insert(argv, "--build") else - table.insert(argv, "--build=" .. opt.build) + table.insert(argv, "--build=" .. configs.build) end end @@ -241,15 +233,15 @@ function main(name, opt) end -- set custom settings - for _, setting in ipairs(opt.settings) do + for _, setting in ipairs(configs.settings) do table.insert(argv, "-s") table.insert(argv, setting) end -- set remote - if opt.remote then + if configs.remote then table.insert(argv, "-r") - table.insert(argv, opt.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..24500cc2a 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 or {} + if plat == "windows" 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 |
