summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-02-09 23:43:27 +0800
committerruki <[email protected]>2021-02-09 23:43:27 +0800
commitbc620153c3b7b7de9f62612339788ff98689a403 (patch)
treefc4a9c70d94589f217539917db648021a007b00f
parentb4420e51c372cd46367852a354855555b044b21c (diff)
pass toolchains to package
-rw-r--r--tests/projects/package/toolchain_muslcc/src/main.c2
-rw-r--r--tests/projects/package/toolchain_muslcc/test.lua12
-rw-r--r--tests/projects/package/toolchain_muslcc/xmake.lua17
-rw-r--r--xmake/actions/require/impl/package.lua8
-rw-r--r--xmake/core/package/package.lua66
-rw-r--r--xmake/core/project/target.lua1
-rw-r--r--xmake/modules/package/tools/xmake.lua69
7 files changed, 138 insertions, 37 deletions
diff --git a/tests/projects/package/toolchain_muslcc/src/main.c b/tests/projects/package/toolchain_muslcc/src/main.c
index 9ec3e9c6d..c8b1209a0 100644
--- a/tests/projects/package/toolchain_muslcc/src/main.c
+++ b/tests/projects/package/toolchain_muslcc/src/main.c
@@ -1,11 +1,9 @@
#include <stdio.h>
#include <zlib.h>
-#include <pcre2.h>
int main(int argc, char** argv)
{
printf("hello world!\n");
inflate(0, 0);
- pcre2_compile(0, 0, 0, 0, 0, 0);
return 0;
}
diff --git a/tests/projects/package/toolchain_muslcc/test.lua b/tests/projects/package/toolchain_muslcc/test.lua
new file mode 100644
index 000000000..4964fecaa
--- /dev/null
+++ b/tests/projects/package/toolchain_muslcc/test.lua
@@ -0,0 +1,12 @@
+function main(t)
+
+ -- freebsd ci is slower
+ if is_host("bsd") then
+ return
+ end
+
+ -- only for x86/x64, because it will take too long time on ci with arm/mips
+ if os.subarch():startswith("x") or os.subarch() == "i386" then
+ t:build()
+ end
+end
diff --git a/tests/projects/package/toolchain_muslcc/xmake.lua b/tests/projects/package/toolchain_muslcc/xmake.lua
index 688cb241c..bd0b1245c 100644
--- a/tests/projects/package/toolchain_muslcc/xmake.lua
+++ b/tests/projects/package/toolchain_muslcc/xmake.lua
@@ -1,16 +1,19 @@
add_rules("mode.debug", "mode.release")
--- set muslcc as global toolchain
+-- set cross-compliation platform
+set_plat("cross")
+set_arch("arm")
+
+-- add toolchains package
add_requires("muslcc")
-set_toolchains("@muslcc")
--- explicitly specify the package toolchain
-add_requires("zlib", {configs = {toolchains = "@muslcc"}})
+-- add library packages
+add_requires("zlib", {system = false})
--- use global toolchain: muslcc
-add_requires("pcre2")
+-- set global toolchains for target and packages
+set_toolchains("@muslcc")
target("test")
set_kind("binary")
add_files("src/*.c")
- add_packages("zlib", "pcre2")
+ add_packages("zlib", "openssl")
diff --git a/xmake/actions/require/impl/package.lua b/xmake/actions/require/impl/package.lua
index 16a0acb4d..58386d140 100644
--- a/xmake/actions/require/impl/package.lua
+++ b/xmake/actions/require/impl/package.lua
@@ -225,8 +225,12 @@ end
-- add some builtin configurations to package
function _add_package_configurations(package)
+ local toolchains
+ if package:is_plat("cross") and package:is_library() then
+ -- we only can set toolchains to library package
+ toolchains = project.get("target.toolchains") or get_config("toolchain")
+ end
local vs_runtime = project.get("target.runtimes") or get_config("vs_runtime") or "MT"
- local toolchains = project.get("target.toolchains") or get_config("toolchain")
package:add("configs", "debug", {builtin = true, description = "Enable debug symbols.", default = false, type = "boolean"})
package:add("configs", "shared", {builtin = true, description = "Enable shared library.", default = false, type = "boolean"})
package:add("configs", "cflags", {builtin = true, description = "Set the C compiler flags."})
@@ -235,7 +239,7 @@ function _add_package_configurations(package)
package:add("configs", "asflags", {builtin = true, description = "Set the assembler flags."})
package:add("configs", "pic", {builtin = true, description = "Enable the position independent code.", default = true, type = "boolean"})
package:add("configs", "vs_runtime", {builtin = true, description = "Set vs compiler runtime.", default = vs_runtime, values = {"MT", "MTd", "MD", "MDd"}})
- package:add("configs", "toolchains", {builtin = true, description = "Set package toolchains.", default = toolchains})
+ package:add("configs", "toolchains", {builtin = true, description = "Set package toolchains only for cross-compilation.", default = toolchains})
end
-- select package version
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index 242892271..b18119b6c 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -106,10 +106,10 @@ function _instance:plat()
return os.host()
end
local requireinfo = self:requireinfo()
- if requireinfo and requireinfo.plat then
+ if not plat and requireinfo and requireinfo.plat then
return requireinfo.plat
end
- return self:get("plat") or config.get("plat") or os.host()
+ return self:get("plat") or package._target_plat()
end
-- get the architecture of package
@@ -118,11 +118,13 @@ function _instance:arch()
if self:is_binary() then
return os.arch()
end
- local requireinfo = self:requireinfo()
- if requireinfo and requireinfo.arch then
- return requireinfo.arch
+ if not arch then
+ local requireinfo = self:requireinfo()
+ if requireinfo and requireinfo.arch then
+ return requireinfo.arch
+ end
end
- return self:get("arch") or config.get("arch") or os.arch()
+ return self:get("arch") or package._target_arch()
end
-- get the target os
@@ -1333,12 +1335,22 @@ end
-- the current platform is belong to the given platforms?
function package._api_is_plat(interp, ...)
- return config.is_plat(...)
+ local plat = package._target_plat()
+ for _, v in ipairs(table.join(...)) do
+ if v and plat == v then
+ return true
+ end
+ end
end
-- the current platform is belong to the given architectures?
function package._api_is_arch(interp, ...)
- return config.is_arch(...)
+ local arch = package._target_arch()
+ for _, v in ipairs(table.join(...)) do
+ if v and arch:find("^" .. v:gsub("%-", "%%-") .. "$") then
+ return true
+ end
+ end
end
-- the current host is belong to the given hosts?
@@ -1376,6 +1388,44 @@ function package._memcache()
return memcache.cache("core.base.package")
end
+-- get global target platform of package
+function package._target_plat()
+ local plat = package._PLAT
+ if plat == nil then
+ if not plat and os.isfile(os.projectfile()) then
+ local project = require("project/project")
+ local target_root_plat = project.get("target.plat")
+ if target_root_plat then
+ plat = target_root_plat
+ end
+ end
+ if not plat then
+ plat = config.get("plat") or os.host()
+ end
+ package._PLAT = plat
+ end
+ return plat
+end
+
+-- get global target architecture of pacakge
+function package._target_arch()
+ local arch = package._ARCH
+ if arch == nil then
+ if not arch then
+ local project = require("project/project")
+ local target_root_arch = project.get("target.arch")
+ if target_root_arch then
+ arch = target_root_arch
+ end
+ end
+ if not arch then
+ arch = config.get("arch") or os.arch()
+ end
+ package._ARCH = arch
+ end
+ return arch
+end
+
-- get package apis
function package.apis()
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index 66360213e..17951dbef 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -1890,6 +1890,7 @@ function target.apis()
, "target.add_imports"
, "target.add_languages"
, "target.add_vectorexts"
+ , "target.add_toolchains"
}
, keyvalues =
{
diff --git a/xmake/modules/package/tools/xmake.lua b/xmake/modules/package/tools/xmake.lua
index 7c2e86327..55cbb47e6 100644
--- a/xmake/modules/package/tools/xmake.lua
+++ b/xmake/modules/package/tools/xmake.lua
@@ -20,6 +20,7 @@
-- imports
import("core.base.option")
+import("core.tool.toolchain")
import("core.package.repository")
-- get configs
@@ -30,13 +31,22 @@ function _get_configs(package, configs)
local cxxflags = table.join(table.wrap(package:config("cxxflags")), get_config("cxxflags"))
local asflags = table.join(table.wrap(package:config("asflags")), get_config("asflags"))
local ldflags = table.join(table.wrap(package:config("ldflags")), get_config("ldflags"))
+ table.insert(configs, "--plat=" .. package:plat())
+ table.insert(configs, "--arch=" .. package:arch())
+ table.insert(configs, "--mode=" .. (package:debug() and "debug" or "release"))
if package:is_plat("windows") then
local vs_runtime = package:config("vs_runtime")
if vs_runtime then
table.insert(configs, "--vs_runtime=" .. vs_runtime)
end
end
- table.insert(configs, "--mode=" .. (package:debug() and "debug" or "release"))
+ local names = {"ndk", "ndk_sdkver", "vs", "mingw", "sdk", "bin", "cross", "ld", "sh", "ar", "cc", "cxx", "mm", "mxx"}
+ for _, name in ipairs(names) do
+ local value = get_config(name)
+ if value ~= nil then
+ table.insert(configs, "--" .. name .. "=" .. tostring(value))
+ end
+ end
if cflags then
table.insert(configs, "--cflags=" .. table.concat(cflags, ' '))
end
@@ -56,7 +66,7 @@ function _get_configs(package, configs)
end
-- init arguments and inherit some global options from the parent xmake
-function _init_argv(...)
+function _init_argv(package, ...)
local argv = {...}
for _, name in ipairs({"diagnosis", "verbose", "quiet", "yes", "confirm", "root"}) do
local value = option.get(name)
@@ -69,25 +79,43 @@ function _init_argv(...)
return argv
end
+-- get the build environments
+function buildenvs(package, opt)
+ opt = opt or {}
+ local envs = {}
+ -- pass toolchains
+ local toolchains = package:config("toolchains")
+ if toolchains then
+ local rcfile_path = os.tmpfile() .. ".lua"
+ local rcfile = io.open(rcfile_path, 'w')
+ local toolchain_packages = {}
+ for _, name in ipairs(toolchains) do
+ local toolchain_inst = toolchain.load(name, {plat = package:plat(), arch = package:arch()})
+ if toolchain_inst then
+ table.join2(toolchain_packages, toolchain_inst:config("packages"))
+ end
+ end
+ rcfile:print("add_requires(\"%s\")", table.concat(toolchain_packages, '", "'))
+ rcfile:print("add_toolchains(\"%s\")", table.concat(table.wrap(toolchains), '", "'))
+ rcfile:close()
+ envs.XMAKE_RCFILES = {}
+ table.insert(envs.XMAKE_RCFILES, rcfile_path)
+ table.join2(envs.XMAKE_RCFILES, os.getenv("XMAKE_RCFILES"))
+ end
+ return envs
+end
+
-- install package
-function install(package, configs)
+function install(package, configs, opt)
-- pass local repositories
+ opt = opt or {}
for _, repo in ipairs(repository.repositories()) do
os.vrunv("xmake", {"repo", "--add", repo:name(), repo:url(), repo:branch()})
end
- -- inherit builtin configs
- local argv = _init_argv("f", "-y", "-c")
- local names = {"plat", "arch", "ndk", "ndk_sdkver", "vs", "mingw", "sdk", "toolchain", "bin", "cross", "ld", "sh", "ar", "cc", "cxx", "mm", "mxx"}
- for _, name in ipairs(names) do
- local value = get_config(name)
- if value ~= nil then
- table.insert(argv, "--" .. name .. "=" .. tostring(value))
- end
- end
-
-- pass configurations
+ local argv = _init_argv(package, "f", "-y", "-c")
for name, value in pairs(_get_configs(package, configs)) do
value = tostring(value):trim()
if type(name) == "number" then
@@ -98,13 +126,18 @@ function install(package, configs)
table.insert(argv, "--" .. name .. "=" .. value)
end
end
- os.vrunv("xmake", argv)
+
+ -- get build environments
+ local envs = opt.envs or buildenvs(package)
+
+ -- do configure
+ os.vrunv("xmake", argv, {envs = envs})
-- do build
- argv = _init_argv()
- os.vrunv("xmake", argv)
+ argv = _init_argv(package)
+ os.vrunv("xmake", argv, {envs = envs})
-- do install
- argv = _init_argv("install", "-y", "-o", package:installdir())
- os.vrunv("xmake", argv)
+ argv = _init_argv(package, "install", "-y", "-o", package:installdir())
+ os.vrunv("xmake", argv, {envs = envs})
end