Skip to content

Commit

Permalink
Merge pull request #1917 from xmake-io/package
Browse files Browse the repository at this point in the history
Improve to find package and configurations
  • Loading branch information
waruqi committed Dec 16, 2021
2 parents 9af42bc + 0c97755 commit 369afb0
Show file tree
Hide file tree
Showing 20 changed files with 269 additions and 72 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 修复

Expand Down
8 changes: 8 additions & 0 deletions tests/projects/package/cmake/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


9 changes: 9 additions & 0 deletions tests/projects/package/cmake/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
cout << "hello world!" << endl;
return 0;
}
12 changes: 12 additions & 0 deletions tests/projects/package/cmake/xmake.lua
Original file line number Diff line number Diff line change
@@ -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")


8 changes: 8 additions & 0 deletions tests/projects/package/conan/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


9 changes: 9 additions & 0 deletions tests/projects/package/conan/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
cout << "hello world!" << endl;
return 0;
}
9 changes: 9 additions & 0 deletions tests/projects/package/conan/xmake.lua
Original file line number Diff line number Diff line change
@@ -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")

8 changes: 8 additions & 0 deletions tests/projects/package/vcpkg/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


9 changes: 9 additions & 0 deletions tests/projects/package/vcpkg/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
cout << "hello world!" << endl;
return 0;
}
8 changes: 8 additions & 0 deletions tests/projects/package/vcpkg/xmake.lua
Original file line number Diff line number Diff line change
@@ -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")

11 changes: 6 additions & 5 deletions xmake/core/package/package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions xmake/modules/package/manager/cargo/configurations.lua
Original file line number Diff line number Diff line change
@@ -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
15 changes: 4 additions & 11 deletions xmake/modules/package/manager/cargo/install_package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = "*"
Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions xmake/modules/package/manager/clib/configurations.lua
Original file line number Diff line number Diff line change
@@ -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

21 changes: 7 additions & 14 deletions xmake/modules/package/manager/clib/install_package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
-- 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

Expand Down
31 changes: 31 additions & 0 deletions xmake/modules/package/manager/cmake/configurations.lua
Original file line number Diff line number Diff line change
@@ -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

40 changes: 27 additions & 13 deletions xmake/modules/package/manager/cmake/find_package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {}
Expand Down
Loading

0 comments on commit 369afb0

Please sign in to comment.