diff options
| author | ruki <[email protected]> | 2020-05-20 23:17:55 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-05-20 13:47:33 +0800 |
| commit | addd29396de0952b78cc69c4510b7be0f7886c1f (patch) | |
| tree | bf6ae81193a92ac1c2082183eb8a19fab05e9a2f | |
| parent | eff77a9a787016605baf133bc1a582ef8437a7fa (diff) | |
improve envs
| -rw-r--r-- | xmake/core/platform/environment.lua | 92 | ||||
| -rw-r--r-- | xmake/core/platform/platform.lua | 20 | ||||
| -rw-r--r-- | xmake/core/tool/toolchain.lua | 25 | ||||
| -rw-r--r-- | xmake/platforms/windows/environment/leave.lua | 44 | ||||
| -rw-r--r-- | xmake/platforms/windows/xmake.lua | 6 | ||||
| -rw-r--r-- | xmake/toolchains/vs/check.lua | 11 | ||||
| -rw-r--r-- | xmake/toolchains/vs/load.lua (renamed from xmake/platforms/windows/environment/enter.lua) | 61 | ||||
| -rw-r--r-- | xmake/toolchains/vs/xmake.lua | 17 |
8 files changed, 97 insertions, 179 deletions
diff --git a/xmake/core/platform/environment.lua b/xmake/core/platform/environment.lua index acc383f19..f76568cb7 100644 --- a/xmake/core/platform/environment.lua +++ b/xmake/core/platform/environment.lua @@ -23,6 +23,7 @@ local environment = environment or {} -- load modules local os = require("base/os") +local table = require("base/table") local global = require("base/global") local platform_core = require("platform/platform") local sandbox = require("sandbox/sandbox") @@ -32,97 +33,80 @@ local import = require("sandbox/modules/import") -- enter the toolchains environment function environment._enter_toolchains() - -- save the toolchains environment - environment._PATH = os.getenv("PATH") + -- get the current platform + local platform, errors = platform_core.load() + if not platform then + return false, errors + end -- add $programdir/winenv/bin to $path + local oldenvs = {} + oldenvs.PATH = os.getenv("PATH") if os.host() == "windows" then os.addenv("PATH", path.join(os.programdir(), "winenv", "bin")) end + + -- add the runenvs of toolchains + for name, values in pairs(platform:runenvs()) do + if not oldenvs[name] then + oldenvs[name] = os.getenv(name) + end + os.addenv(name, table.unpack(table.wrap(values))) + end + environment._OLDENVS_TOOLCHAINS = oldenvs + return true end -- leave the toolchains environment function environment._leave_toolchains() - - -- leave the toolchains environment - os.setenv("PATH", environment._PATH) + local oldenvs = environment._OLDENVS_TOOLCHAINS + for name, values in pairs(oldenvs) do + os.setenv(name, values) + end + return true end -- enter the running environment function environment._enter_run() - - -- save the running environment - environment._PATH = os.getenv("PATH") - environment._LD_LIBRARY_PATH = os.getenv("LD_LIBRARY_PATH") + local oldenvs = {} + oldenvs.PATH = os.getenv("PATH") + oldenvs.LD_LIBRARY_PATH = os.getenv("LD_LIBRARY_PATH") + environment._OLDENVS_RUN = oldenvs + return true end -- leave the running environment function environment._leave_run() - - -- leave the running environment - os.setenv("PATH", environment._PATH) - os.setenv("LD_LIBRARY_PATH", environment._LD_LIBRARY_PATH) + local oldenvs = environment._OLDENVS_RUN + for name, values in pairs(oldenvs) do + os.setenv(name, values) + end + return true end -- enter the environment for the current platform function environment.enter(name) - - -- get the current platform - local platform, errors = platform_core.load() - if not platform then - return false, errors - end - - -- the maps local maps = {toolchains = environment._enter_toolchains, run = environment._enter_run} - - -- enter the common environment local func = maps[name] if func then - func() - end - - -- enter the environment of the given platform - local on_enter = platform:script("environment_enter") - if on_enter then - local ok, errors = sandbox.load(on_enter, platform, name) + local ok, errors = func() if not ok then return false, errors end end - - -- ok return true end -- leave the environment for the current platform function environment.leave(name) - - -- get the current platform - local platform, errors = platform_core.load() - if not platform then - return false, errors - end - - -- leave the environment of the given platform - local on_leave = platform:script("environment_leave") - if on_leave then - local ok, errors = sandbox.load(on_leave, platform, name) - if not ok then - return false, errors - end - end - - -- the maps local maps = {toolchains = environment._leave_toolchains, run = environment._leave_run} - - -- leave the common environment local func = maps[name] if func then - func() + local ok, errors = func() + if not ok then + return false, errors + end end - - -- ok return true end diff --git a/xmake/core/platform/platform.lua b/xmake/core/platform/platform.lua index 328bcce75..c20da127e 100644 --- a/xmake/core/platform/platform.lua +++ b/xmake/core/platform/platform.lua @@ -103,6 +103,24 @@ function _instance:archs() return self._INFO:get("archs") end +-- get runenvs of toolchains +function _instance:runenvs() + local runenvs = self._RUNENVS + if not runenvs then + runenvs = {} + for _, toolchain_inst in ipairs(self:toolchains()) do + local toolchain_runenvs = toolchain_inst:get("runenvs") + if toolchain_runenvs then + for name, values in pairs(toolchain_runenvs) do + runenvs[name] = table.join2(table.wrap(runenvs[name]), values) + end + end + end + self._RUNENVS = runenvs + end + return runenvs +end + -- get the toolchains function _instance:toolchains() local toolchains = self._TOOLCHAINS @@ -284,8 +302,6 @@ function platform._apis() -- platform.on_xxx "platform.on_load" , "platform.on_check" - , "platform.on_environment_enter" - , "platform.on_environment_leave" } , dictionary = { diff --git a/xmake/core/tool/toolchain.lua b/xmake/core/tool/toolchain.lua index f5d6ada4f..97fb512a1 100644 --- a/xmake/core/tool/toolchain.lua +++ b/xmake/core/tool/toolchain.lua @@ -68,10 +68,8 @@ function _instance:get(name) return value end - -- lazy loading platform if get other configuration - if not self:_is_builtin_conf(name) then - self:_load() - end + -- lazy loading platform + self:_load() -- get other platform info return self._INFO:get(name) @@ -233,23 +231,6 @@ function _instance:_checktool(toolkind, toolpath) return program, toolname 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() @@ -289,6 +270,8 @@ function toolchain._apis() { -- toolchain.set_xxx "toolchain.set_toolsets" + -- toolchain.add_xxx + , "toolchain.add_runenvs" } , script = { diff --git a/xmake/platforms/windows/environment/leave.lua b/xmake/platforms/windows/environment/leave.lua deleted file mode 100644 index 8547cfd66..000000000 --- a/xmake/platforms/windows/environment/leave.lua +++ /dev/null @@ -1,44 +0,0 @@ ---!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 leave.lua --- - --- leave the given environment -function _leave(platform, name) - local old = platform:data("windows.environment." .. name) - if old then - os.setenv(name, old) - end -end - --- leave the toolchains environment -function _leave_toolchains(platform) - _leave(platform, "path") - _leave(platform, "lib") - _leave(platform, "include") - _leave(platform, "libpath") -end - --- leave environment -function main(platform, name) - local maps = {toolchains = _leave_toolchains} - local func = maps[name] - if func then - func(platform) - end -end diff --git a/xmake/platforms/windows/xmake.lua b/xmake/platforms/windows/xmake.lua index dd0c37679..dd4e4da6f 100644 --- a/xmake/platforms/windows/xmake.lua +++ b/xmake/platforms/windows/xmake.lua @@ -36,12 +36,6 @@ platform("windows") -- on check on_check("check") - -- on environment enter - on_environment_enter("environment.enter") - - -- on environment leave - on_environment_leave("environment.leave") - -- set toolchains set_toolchains("vs", "yasm", "nasm", "cuda", "dlang", "rust", "go") diff --git a/xmake/toolchains/vs/check.lua b/xmake/toolchains/vs/check.lua index 1e9a97d48..57220f73b 100644 --- a/xmake/toolchains/vs/check.lua +++ b/xmake/toolchains/vs/check.lua @@ -23,7 +23,6 @@ import("core.base.option") import("core.project.config") import("detect.sdks.find_vstudio") import("lib.detect.find_tool") -import("core.platform.environment") -- attempt to check vs environment function _check_vsenv() @@ -60,13 +59,19 @@ function _check_vsenv() config.set("__vcvarsall", vcvarsall) -- check compiler - environment.enter("toolchains") + local oldenvs = {} + oldenvs.PATH = os.getenv("PATH") + oldenvs.LIB = os.getenv("LIB") + os.setenv("PATH", vsenv.path .. ';' .. (oldenvs.PATH or "")) + os.setenv("LIB", vsenv.lib .. ';' .. (oldenvs.LIB or "")) local program = nil local tool = find_tool("cl.exe", {force = true}) if tool then program = tool.program end - environment.leave("toolchains") + for name, values in pairs(oldenvs) do + os.setenv(name, values) + end -- ok? if program then diff --git a/xmake/platforms/windows/environment/enter.lua b/xmake/toolchains/vs/load.lua index 827898222..855869a0b 100644 --- a/xmake/platforms/windows/environment/enter.lua +++ b/xmake/toolchains/vs/load.lua @@ -1,4 +1,4 @@ ---!A cross-platform build utility based on Lua +--!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. @@ -15,25 +15,25 @@ -- Copyright (C) 2015-2020, TBOOX Open Source Group. -- -- @author ruki --- @file enter.lua +-- @file load.lua -- -- imports +import("core.base.option") import("core.project.config") -import("core.base.global") import("detect.sdks.find_vstudio") --- enter the given environment -function _enter(platform, name) +-- add the given vs environment +function _add_vsenv(toolchain, name) -- get vcvarsall - local vcvarsall = config.get("__vcvarsall") or global.get("__vcvarsall") + local vcvarsall = config.get("__vcvarsall") if not vcvarsall then return end -- get arch - local arch = config.get("arch") or global.get("arch") or "" + local arch = config.get("arch") or "" -- get vs environment for the current arch local vsenv = vcvarsall[arch] or {} @@ -58,38 +58,33 @@ function _enter(platform, name) end -- get the pathes for the vs environment - local old = nil local new = vsenv[name] if new then - - -- get the current pathes - old = os.getenv(name) or "" - - -- append the current pathes - new = new .. ";" .. old - - -- update the pathes for the environment - os.setenv(name, new) + toolchain:add("runenvs", name:upper(), path.splitenv(new)) end - - -- save the previous environment - platform:data_set("windows.environment." .. name, old) end --- enter the toolchains environment -function _enter_toolchains(platform) - _enter(platform, "path") - _enter(platform, "lib") - _enter(platform, "include") - _enter(platform, "libpath") -end +-- main entry +function main(toolchain) --- enter environment -function main(platform, name) - local maps = {toolchains = _enter_toolchains} - local func = maps[name] - if func then - func(platform) + -- set toolsets + toolchain:set("toolsets", "cc", "cl.exe") + toolchain:set("toolsets", "cxx", "cl.exe") + toolchain:set("toolsets", "mrc", "rc.exe") + if is_arch("x64") then + toolchain:set("toolsets", "as", "ml64.exe") + else + toolchain:set("toolsets", "as", "ml.exe") end + toolchain:set("toolsets", "ld", "link.exe") + toolchain:set("toolsets", "sh", "link.exe -dll") + toolchain:set("toolsets", "ar", "link.exe -lib") + toolchain:set("toolsets", "ex", "lib.exe") + + -- add vs environments + _add_vsenv(toolchain, "path") + _add_vsenv(toolchain, "lib") + _add_vsenv(toolchain, "include") + _add_vsenv(toolchain, "libpath") end diff --git a/xmake/toolchains/vs/xmake.lua b/xmake/toolchains/vs/xmake.lua index a2b1c71db..cc717de9f 100644 --- a/xmake/toolchains/vs/xmake.lua +++ b/xmake/toolchains/vs/xmake.lua @@ -28,19 +28,4 @@ toolchain("vs") on_check("check") -- load toolchain - on_load(function (toolchain) - - -- set toolsets - toolchain:set("toolsets", "cc", "cl.exe") - toolchain:set("toolsets", "cxx", "cl.exe") - toolchain:set("toolsets", "mrc", "rc.exe") - if is_arch("x64") then - toolchain:set("toolsets", "as", "ml64.exe") - else - toolchain:set("toolsets", "as", "ml.exe") - end - toolchain:set("toolsets", "ld", "link.exe") - toolchain:set("toolsets", "sh", "link.exe -dll") - toolchain:set("toolsets", "ar", "link.exe -lib") - toolchain:set("toolsets", "ex", "lib.exe") - end) + on_load("load") |
