From 8512b8200ac982c4075c4707c3b2971c73159c9f Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 17 Dec 2021 00:51:02 +0800 Subject: modify package configurations --- tests/projects/package/conan/.gitignore | 8 ++++ tests/projects/package/conan/src/main.cpp | 9 +++++ tests/projects/package/conan/xmake.lua | 9 +++++ xmake/core/package/package.lua | 9 +++-- .../package/manager/cargo/configurations.lua | 28 ++++++++++++++ .../package/manager/cargo/install_package.lua | 15 ++------ .../package/manager/clib/configurations.lua | 30 +++++++++++++++ .../package/manager/clib/install_package.lua | 21 ++++------- .../package/manager/cmake/configurations.lua | 33 ++++++++++++++++ .../modules/package/manager/cmake/find_package.lua | 9 +++-- .../package/manager/conan/configurations.lua | 33 ++++++++++++++++ .../package/manager/conan/install_package.lua | 44 +++++++++------------- 12 files changed, 189 insertions(+), 59 deletions(-) create mode 100644 tests/projects/package/conan/.gitignore create mode 100644 tests/projects/package/conan/src/main.cpp create mode 100644 tests/projects/package/conan/xmake.lua create mode 100644 xmake/modules/package/manager/cargo/configurations.lua create mode 100644 xmake/modules/package/manager/clib/configurations.lua create mode 100644 xmake/modules/package/manager/cmake/configurations.lua create mode 100644 xmake/modules/package/manager/conan/configurations.lua 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 + +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/xmake/core/package/package.lua b/xmake/core/package/package.lua index 3b778d617..602b0e58d 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -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.pkgconfigs = 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..dd2fde7e9 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 pkgconfigs = opt.pkgconfigs 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 = pkgconfigs.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, "\", \""), pkgconfigs.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..8ebac7829 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/bytes@0.4.0 -- @param opt the options, e.g. { verbose = true, --- settings = {outputdir = "clib", save = false, save_dev = false}} +-- pkgconfigs = {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 pkgconfigs = opt.pkgconfigs or {} local argv = {"install", name} - local abs_out = path.join(os.projectdir(), opt.outputdir) + local abs_out = path.join(os.projectdir(), pkgconfigs.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 pkgconfigs.save then table.insert(argv, "--save") end - if opt.save_dev then + if pkgconfigs.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..ce9b21e17 --- /dev/null +++ b/xmake/modules/package/manager/cmake/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/cmake/find_package.lua b/xmake/modules/package/manager/cmake/find_package.lua index 22ebc711b..e0e767145 100644 --- a/xmake/modules/package/manager/cmake/find_package.lua +++ b/xmake/modules/package/manager/cmake/find_package.lua @@ -243,10 +243,11 @@ end -- -- @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"}) +-- pkgconfigs = { +-- 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..ce9b21e17 --- /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..d7f730e14 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, pkgconfigs) -- 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(pkgconfigs.options) + local imports = table.wrap(pkgconfigs.imports) + local build_requires = table.wrap(pkgconfigs.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"}} +-- pkgconfigs = { +-- 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 pkgconfigs + opt = opt or {} + local pkgconfigs = opt.pkgconfigs 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, pkgconfigs) -- install package local argv = {"install", "."} - if opt.build then - if opt.build == "all" then + if pkgconfigs.build then + if pkgconfigs.build == "all" then table.insert(argv, "--build") else - table.insert(argv, "--build=" .. opt.build) + table.insert(argv, "--build=" .. pkgconfigs.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(pkgconfigs.settings) do table.insert(argv, "-s") table.insert(argv, setting) end -- set remote - if opt.remote then + if pkgconfigs.remote then table.insert(argv, "-r") - table.insert(argv, opt.remote) + table.insert(argv, pkgconfigs.remote) end -- TODO set environments -- cgit v1.3.1 From 1bb127c6ddb9ca7928f827f04160dde828e54925 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 17 Dec 2021 00:53:13 +0800 Subject: improve cmake find_pacakge --- tests/projects/package/cmake/.gitignore | 8 ++++++ tests/projects/package/cmake/src/main.cpp | 9 ++++++ tests/projects/package/cmake/xmake.lua | 11 ++++++++ xmake/core/package/package.lua | 4 +-- .../package/manager/cargo/install_package.lua | 6 ++-- .../package/manager/clib/install_package.lua | 10 +++---- .../package/manager/cmake/configurations.lua | 10 +++---- .../modules/package/manager/cmake/find_package.lua | 33 +++++++++++++++------- .../package/manager/conan/configurations.lua | 2 +- .../package/manager/conan/install_package.lua | 28 +++++++++--------- .../modules/package/manager/vcpkg/find_package.lua | 6 ++-- 11 files changed, 83 insertions(+), 44 deletions(-) create mode 100644 tests/projects/package/cmake/.gitignore create mode 100644 tests/projects/package/cmake/src/main.cpp create mode 100644 tests/projects/package/cmake/xmake.lua 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 + +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/bytes@0.4.0 -- @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 -- cgit v1.3.1 From 0644c09d6c75a669f976f8d4bf67041c83c3f1d4 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 17 Dec 2021 00:54:44 +0800 Subject: update cmake tests --- tests/projects/package/cmake/xmake.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/projects/package/cmake/xmake.lua b/tests/projects/package/cmake/xmake.lua index 48aa74660..b6490032d 100644 --- a/tests/projects/package/cmake/xmake.lua +++ b/tests/projects/package/cmake/xmake.lua @@ -1,11 +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") + add_packages("cmake::ZLIB", "cmake::Boost", "cmake::LibXml2") -- cgit v1.3.1 From 0fdd42e56f478591a4876866af833273f2cc244f Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 17 Dec 2021 00:55:21 +0800 Subject: add vcpkg tests --- tests/projects/package/vcpkg/.gitignore | 8 ++++++++ tests/projects/package/vcpkg/src/main.cpp | 9 +++++++++ tests/projects/package/vcpkg/xmake.lua | 8 ++++++++ xmake/modules/package/manager/vcpkg/find_package.lua | 4 ++-- 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 tests/projects/package/vcpkg/.gitignore create mode 100644 tests/projects/package/vcpkg/src/main.cpp create mode 100644 tests/projects/package/vcpkg/xmake.lua 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 + +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/modules/package/manager/vcpkg/find_package.lua b/xmake/modules/package/manager/vcpkg/find_package.lua index 1547da40a..24500cc2a 100644 --- a/xmake/modules/package/manager/vcpkg/find_package.lua +++ b/xmake/modules/package/manager/vcpkg/find_package.lua @@ -82,8 +82,8 @@ 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 configs = opt.configs - if plat == "windows" and configs and configs.shared ~= true then + local configs = opt.configs or {} + if plat == "windows" and configs.shared ~= true then triplet = triplet .. "-static" if configs.vs_runtime and configs.vs_runtime:startswith("MD") then triplet = triplet .. "-md" -- cgit v1.3.1 From 0c977557c4566c6aff800320998c9b3aea0c8f1d Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 17 Dec 2021 00:55:39 +0800 Subject: update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) 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 修复 -- cgit v1.3.1