diff options
| author | ruki <[email protected]> | 2020-05-16 00:03:19 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-05-16 00:03:19 +0800 |
| commit | c6c71612e034c2e741add2b9deb69be4c93a8388 (patch) | |
| tree | 0b7d15fffb3bb620ffcb93f45c50f002262874e3 | |
| parent | 53f9e8e305a0cc51f4021a957cfdbc5c37784b9b (diff) | |
add xcode toolchain
| -rw-r--r-- | xmake/core/platform/platform.lua | 6 | ||||
| -rw-r--r-- | xmake/core/tool/toolchain.lua | 231 | ||||
| -rw-r--r-- | xmake/platforms/macosx/xmake.lua | 3 | ||||
| -rw-r--r-- | xmake/toolchains/xcode/xmake.lua | 23 |
4 files changed, 263 insertions, 0 deletions
diff --git a/xmake/core/platform/platform.lua b/xmake/core/platform/platform.lua index bf6734373..b6574584e 100644 --- a/xmake/core/platform/platform.lua +++ b/xmake/core/platform/platform.lua @@ -103,6 +103,11 @@ function _instance:archs() return self._INFO:get("archs") end +-- get the toolchains +function _instance:toolchains() + return self._INFO:get("toolchains") +end + -- get the platform script function _instance:script(name) return self._INFO:get(name) @@ -175,6 +180,7 @@ function platform._apis() , "platform.set_hosts" , "platform.set_archs" , "platform.set_installdir" + , "platform.set_toolchains" } , script = { diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua new file mode 100644 index 000000000..ba5640126 --- /dev/null +++ b/xmake/core/tool/toolchain.lua @@ -0,0 +1,231 @@ +--!A cross-toolchain 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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file toolchain.lua +-- + +-- define module +local toolchain = toolchain or {} +local _instance = _instance or {} + +-- load modules +local os = require("base/os") +local path = require("base/path") +local utils = require("base/utils") +local table = require("base/table") +local interpreter = require("base/interpreter") +local language = require("language/language") +local sandbox = require("sandbox/sandbox") + +-- new an instance +function _instance.new(name, info, rootdir) + local instance = table.inherit(_instance) + instance._NAME = name + instance._INFO = info + instance._ROOTDIR = rootdir + return instance +end + +-- get toolchain name +function _instance:name() + return self._NAME +end + +-- set the value to the toolchain configuration +function _instance:set(name, ...) + self._INFO:apival_set(name, ...) +end + +-- add the value to the toolchain configuration +function _instance:add(name, ...) + self._INFO:apival_add(name, ...) +end + +-- get the toolchain configuration +function _instance:get(name) + + -- attempt to get the static configure value + local value = self._INFO:get(name) + if value ~= nil then + return value + end + + -- lazy loading toolchain if get other configuration + if not self._LOADED and not self:_is_builtin_conf(name) then + local on_load = self._INFO:get("load") + if on_load then + local ok, errors = sandbox.load(on_load, self) + if not ok then + os.raise(errors) + end + end + self._LOADED = true + end + + -- get other toolchain info + return self._INFO:get(name) +end + +-- get the toolchain script +function _instance:script(name) + return self._INFO:get(name) +end + +-- is builtin configuration? +function _instance:_is_builtin_conf(name) + + local builtin_configs = self._BUILTIN_CONFIGS + if not builtin_configs then + builtin_configs = {} + for apiname, _ in pairs(toolchain._interpreter():apis("toolchain")) do + local pos = apiname:find('_', 1, true) + if pos then + builtin_configs[apiname:sub(pos + 1)] = true + end + end + self._BUILTIN_CONFIGS = builtin_configs + end + return builtin_configs[name] +end + +-- the interpreter +function toolchain._interpreter() + + -- the interpreter has been initialized? return it directly + if toolchain._INTERPRETER then + return toolchain._INTERPRETER + end + + -- init interpreter + local interp = interpreter.new() + assert(interp) + + -- define apis + interp:api_define(toolchain._apis()) + + -- define apis for language + interp:api_define(language.apis()) + + -- save interpreter + toolchain._INTERPRETER = interp + + -- ok? + return interp +end + +-- get toolchain apis +function toolchain._apis() + return + { + values = + { + -- toolchain.set_xxx + "toolchain.set_cc" + , "toolchain.set_cxx" + , "toolchain.set_ld" + , "toolchain.set_sh" + } + , script = + { + -- toolchain.on_xxx + "toolchain.on_load" + } + , dictionary = + { + -- toolchain.set_xxx + , "toolchain.set_formats" + } + } +end + +-- get toolchain directories +function toolchain.directories() + + -- init directories + local dirs = toolchain._DIRS or { path.join(global.directory(), "toolchains") + , path.join(os.programdir(), "toolchains") + } + + -- save directories to cache + toolchain._DIRS = dirs + return dirs +end + +-- load the given toolchain +function toolchain.load(name, plat) + + -- get toolchain name + plat = plat or config.get("plat") or os.host() + if not plat then + return nil, string.format("unknown toolchain!") + end + + -- get cache key + local cachekey = plat .. "_" .. (config.get("arch") or os.arch()) + + -- get it directly from cache dirst + toolchain._TOOLCHAINS = toolchain._TOOLCHAINS or {} + if toolchain._TOOLCHAINS[cachekey] then + return toolchain._TOOLCHAINS[cachekey] + end + + -- find the toolchain script path + local scriptpath = nil + for _, dir in ipairs(toolchain.directories()) do + scriptpath = path.join(dir, name, "xmake.lua") + if os.isfile(scriptpath) then + break + end + end + if not scriptpath or not os.isfile(scriptpath) then + return nil, string.format("the toolchain %s not found!", name) + end + + -- get interpreter + local interp = toolchain._interpreter() + + -- load script + local ok, errors = interp:load(scriptpath) + if not ok then + return nil, errors + end + + -- load toolchain + local results, errors = interp:make("toolchain", true, false) + if not results and os.isfile(scriptpath) then + return nil, errors + end + + -- check the toolchain name + local result = results[name] + if not result then + return nil, string.format("the toolchain %s not found!", name) + end + + -- new an instance + local instance, errors = _instance.new(name, result, interp:rootdir()) + if not instance then + return nil, errors + end + + -- save instance to the cache + toolchain._TOOLCHAINS[cachekey] = instance + return instance +end + +-- return module +return toolchain diff --git a/xmake/platforms/macosx/xmake.lua b/xmake/platforms/macosx/xmake.lua index 452c528db..d65c03a9e 100644 --- a/xmake/platforms/macosx/xmake.lua +++ b/xmake/platforms/macosx/xmake.lua @@ -45,6 +45,9 @@ platform("macosx") -- on load on_load("load") + -- set toolchains + set_toolchains("envs", "xcode", "clang", "gcc", "dlang", "rust", "go", "cuda") + -- set menu set_menu { config = diff --git a/xmake/toolchains/xcode/xmake.lua b/xmake/toolchains/xcode/xmake.lua new file mode 100644 index 000000000..633f5a45d --- /dev/null +++ b/xmake/toolchains/xcode/xmake.lua @@ -0,0 +1,23 @@ +--!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-2020, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- define toolchain +toolchain("xcode") + |
