diff options
| author | ruki <[email protected]> | 2022-02-10 07:07:09 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-02-10 07:07:09 +0800 |
| commit | c62d50f692935b7369869ac72dfb7b99f6e19941 (patch) | |
| tree | 4409bd927200ab34ca8a2d4cb19d56bd2898e9e8 | |
| parent | 53f8b1561ae2a7bbdd5c1b210b1f0dad544a611e (diff) | |
| parent | 266c575bbcb9a678188d83678baebc682e871502 (diff) | |
Merge pull request #2045 from xmake-io/toolchain
pass toolchains in tools/xmake
| -rw-r--r-- | tests/projects/package/toolchain_muslcc/xmake.lua | 21 | ||||
| -rw-r--r-- | xmake/core/base/io.lua | 27 | ||||
| -rw-r--r-- | xmake/core/project/project.lua | 16 | ||||
| -rw-r--r-- | xmake/core/tool/toolchain.lua | 50 | ||||
| -rw-r--r-- | xmake/modules/package/tools/xmake.lua | 29 | ||||
| -rw-r--r-- | xmake/toolchains/muslcc/xmake.lua | 4 |
6 files changed, 103 insertions, 44 deletions
diff --git a/tests/projects/package/toolchain_muslcc/xmake.lua b/tests/projects/package/toolchain_muslcc/xmake.lua index b111d5976..829ca8b9e 100644 --- a/tests/projects/package/toolchain_muslcc/xmake.lua +++ b/tests/projects/package/toolchain_muslcc/xmake.lua @@ -7,6 +7,22 @@ set_arch("arm") -- lock requires set_policy("package.requires_lock", true) +-- custom toolchain +toolchain("my_muslcc") + set_homepage("https://musl.cc/") + set_description("The musl-based cross-compilation toolchains") + set_kind("cross") + on_load(function (toolchain) + toolchain:load_cross_toolchain() + if toolchain:is_arch("arm") then + toolchain:add("cxflags", "-march=armv7-a", "-msoft-float", {force = true}) + toolchain:add("ldflags", "-march=armv7-a", "-msoft-float", {force = true}) + toolchain:add("syslinks", "atomic") -- fix undefined reference to `__atomic_fetch_add_8' + end + toolchain:add("syslinks", "gcc", "c") + end) +toolchain_end() + -- add library packages -- for testing zlib/xmake, libplist/autoconf, libogg/cmake add_requires("zlib", "libogg", {system = false}) @@ -18,7 +34,10 @@ end add_requires("muslcc") -- set global toolchains for target and packages -set_toolchains("@muslcc") +set_toolchains("my_muslcc@muslcc") + +-- use the builltin toolchain("muslcc") instead of "my_muslcc" +--set_toolchains("@muslcc") target("test") set_kind("binary") diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua index 38df841a1..2f7fed671 100644 --- a/xmake/core/base/io.lua +++ b/xmake/core/base/io.lua @@ -601,66 +601,41 @@ end -- save object the the given filepath function io.save(filepath, object, opt) - - -- check assert(filepath and object) - -- init option opt = opt or {} - - -- open the file local file, err = io.open(filepath, "wb", opt) if err then - -- error return false, err end - -- save object to file local ok, errors = file:save(object, opt) - -- close file file:close() if not ok then - -- error return false, string.format("save %s failed, %s!", filepath, errors) end - - -- ok return true end -- load object from the given file function io.load(filepath, opt) - - -- check assert(filepath) - -- init option opt = opt or {} - - -- open the file local file, err = io.open(filepath, "rb", opt) if err then - -- error return nil, err end - - -- load object local result, errors = file:load() - - -- close file file:close() - - -- ok? return result, errors end -- gsub the given file and return replaced data function io.gsub(filepath, pattern, replace, opt) - -- init option - opt = opt or {} - -- read all data from file + opt = opt or {} local data, errors = io.readfile(filepath, opt) if not data then return nil, 0, errors end diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua index cd5171599..c40cfbac0 100644 --- a/xmake/core/project/project.lua +++ b/xmake/core/project/project.lua @@ -628,6 +628,22 @@ function project._toolchains() if not toolchains then os.raise(errors) end + -- load toolchains from data file from "package.tools.xmake" module + local toolchain_datafiles = os.getenv("XMAKE_TOOLCHAIN_DATAFILES") + if toolchain_datafiles then + toolchain_datafiles = path.splitenv(toolchain_datafiles) + if toolchain_datafiles and #toolchain_datafiles > 0 then + for _, toolchain_datafile in ipairs(toolchain_datafiles) do + local toolchain_inst, errors = toolchain.load_fromfile(toolchain_datafile) + if toolchain_inst then + -- @note we use this passed toolchain configuration first if this toolchain has been defined in current project + toolchains[toolchain_inst:name()] = toolchain_inst + else + os.raise(errors) + end + end + end + end project._memcache():set("toolchains", toolchains) end return toolchains diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index f4a511f0f..848668d8a 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -29,6 +29,7 @@ local utils = require("base/utils") local table = require("base/table") local global = require("base/global") local option = require("base/option") +local scopeinfo = require("base/scopeinfo") local interpreter = require("base/interpreter") local config = require("project/config") local memcache = require("cache/memcache") @@ -38,13 +39,14 @@ local sandbox = require("sandbox/sandbox") local sandbox_module = require("sandbox/modules/import/core/sandbox/module") -- new an instance -function _instance.new(name, info, cachekey, configs) - local instance = table.inherit(_instance) - instance._NAME = name - instance._INFO = info - instance._CACHE = toolchain._localcache() - instance._CACHEKEY = cachekey - instance._CONFIGS = instance._CACHE:get(cachekey) or {} +function _instance.new(name, info, cachekey, is_builtin, configs) + local instance = table.inherit(_instance) + instance._NAME = name + instance._INFO = info + instance._IS_BUILTIN = is_builtin + instance._CACHE = toolchain._localcache() + instance._CACHEKEY = cachekey + instance._CONFIGS = instance._CACHE:get(cachekey) or {} for k, v in pairs(configs) do instance._CONFIGS[k] = v end @@ -162,6 +164,11 @@ function _instance:is_global() return self:config("__global") end +-- is builtin toolchain? it's not from local project +function _instance:is_builtin() + return self._IS_BUILTIN +end + -- get the run environments function _instance:runenvs() local runenvs = self._RUNENVS @@ -292,6 +299,11 @@ function _instance:packages() return packages or nil end +-- save toolchain to file +function _instance:savefile(filepath) + return io.save(filepath, {name = self:name(), info = self:info():info(), cachekey = self:cachekey(), configs = self._CONFIGS}) +end + -- on check (builtin) function _instance:_on_check() local on_check = self:info():get("check") @@ -603,7 +615,7 @@ function toolchain.load(name, opt) end -- save instance to the cache - instance = _instance.new(name, result, cachekey, opt) + instance = _instance.new(name, result, cachekey, true, opt) cache:set(cachekey, instance) return instance end @@ -630,11 +642,31 @@ function toolchain.load_withinfo(name, info, opt) end -- save instance to the cache - instance = _instance.new(name, info, cachekey, opt) + instance = _instance.new(name, info, cachekey, false, opt) cache:set(cachekey, instance) return instance end +-- load toolchain from file +function toolchain.load_fromfile(filepath, opt) + local fileinfo, errors = io.load(filepath) + if not fileinfo then + return nil, errors + end + if not fileinfo.name or not fileinfo.info then + return nil, string.format("%s is invalid toolchain info file!", filepath) + end + opt = table.join(opt or {}, fileinfo.configs) + opt.cachekey = fileinfo.cachekey + local scope_opt = {interpreter = toolchain._interpreter(), deduplicate = true, enable_filter = true} + local info = scopeinfo.new("toolchain", fileinfo.info, scope_opt) + local instance = toolchain.load_withinfo(fileinfo.name, info, opt) + -- we need skip check + instance._CHECKED = true + return instance +end + + -- get the program and name of the given tool kind function toolchain.tool(toolchains, toolkind, opt) diff --git a/xmake/modules/package/tools/xmake.lua b/xmake/modules/package/tools/xmake.lua index 9bfcffaff..53f977911 100644 --- a/xmake/modules/package/tools/xmake.lua +++ b/xmake/modules/package/tools/xmake.lua @@ -138,25 +138,29 @@ function _get_package_requireinfo(packagename) end end --- get the build environments -function buildenvs(package, opt) +-- get package toolchains envs +function _get_package_toolchains_envs(package, opt) opt = opt or {} local envs = {} - -- pass toolchains local toolchains = package:config("toolchains") if toolchains then + + -- pass toolchains name and their package configurations local toolchain_packages = {} + local toolchains_custom = {} 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")) + if not toolchain_inst:is_builtin() then + table.insert(toolchains_custom, toolchain_inst) + end end end local rcfile_path = os.tmpfile() .. ".lua" local rcfile = io.open(rcfile_path, 'w') if #toolchain_packages > 0 then for _, packagename in ipairs(toolchain_packages) do - -- pass package configurations, {configs = {}} local requireinfo = _get_package_requireinfo(packagename) if requireinfo then requireinfo.originstr = nil @@ -171,10 +175,27 @@ function buildenvs(package, opt) envs.XMAKE_RCFILES = {} table.insert(envs.XMAKE_RCFILES, rcfile_path) table.join2(envs.XMAKE_RCFILES, os.getenv("XMAKE_RCFILES")) + + -- pass custom toolchains definition in project + for _, toolchain_inst in ipairs(toolchains_custom) do + local toolchains_file = os.tmpfile() + dprint("passing toolchain(%s) to %s", toolchain_inst:name(), toolchains_file) + local ok, errors = toolchain_inst:savefile(toolchains_file) + if not ok then + raise("save toolchain failed, %s", errors or "unknown") + end + envs.XMAKE_TOOLCHAIN_DATAFILES = envs.XMAKE_TOOLCHAIN_DATAFILES or {} + table.insert(envs.XMAKE_TOOLCHAIN_DATAFILES, toolchains_file) + end end return envs end +-- get the build environments +function buildenvs(package, opt) + return _get_package_toolchains_envs(package, opt) +end + -- install package function install(package, configs, opt) diff --git a/xmake/toolchains/muslcc/xmake.lua b/xmake/toolchains/muslcc/xmake.lua index 4c8de24ac..bfb57ecc1 100644 --- a/xmake/toolchains/muslcc/xmake.lua +++ b/xmake/toolchains/muslcc/xmake.lua @@ -18,17 +18,13 @@ -- @file xmake.lua -- --- define toolchain toolchain("muslcc") - - -- set homepage set_homepage("https://musl.cc/") set_description("The musl-based cross-compilation toolchains") -- mark as cross-compilation toolchain set_kind("cross") - -- on load on_load(function (toolchain) -- load basic configuration of cross toolchain |
