diff options
| author | OpportunityLiu <[email protected]> | 2019-08-21 09:49:29 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2019-08-21 09:53:15 +0800 |
| commit | c4dbeb54523b8773c030fd23e227fcc28c84f778 (patch) | |
| tree | 0ee9addbd249f2cb231594446ba2ea77e2c26029 | |
| parent | 2c891f5a18aec7d47d3458ef39b9294d7f3b4d59 (diff) | |
move to common file
| -rw-r--r-- | xmake/actions/run/main.lua | 52 | ||||
| -rw-r--r-- | xmake/modules/private/action/run/make_runenvs.lua | 92 | ||||
| -rw-r--r-- | xmake/plugins/project/vsxmake/getinfo.lua | 48 |
3 files changed, 108 insertions, 84 deletions
diff --git a/xmake/actions/run/main.lua b/xmake/actions/run/main.lua index 0ec951d41..acea817a2 100644 --- a/xmake/actions/run/main.lua +++ b/xmake/actions/run/main.lua @@ -21,43 +21,13 @@ -- imports import("core.base.option") import("core.base.task") -import("core.base.hashset") import("core.project.config") import("core.base.global") import("core.project.project") import("core.platform.platform") import("core.platform.environment") import("devel.debugger") - --- add search directories for all dependent shared libraries on windows -function _make_runpath_on_windows(target) - - local pathenv = {} - local searchdirs = hashset.new() - local function insert(dir) - if not path.is_absolute(dir) then - dir = path.absolute(dir, os.projectdir()) - end - if searchdirs:insert(dir) then - table.insert(pathenv, dir) - end - end - - for _, linkdir in ipairs(target:get("linkdirs")) do - insert(linkdir) - end - for _, opt in ipairs(target:orderopts()) do - for _, linkdir in ipairs(opt:get("linkdirs")) do - insert(linkdir) - end - end - for _, dep in ipairs(target:orderdeps()) do - if dep:targetkind() == "shared" then - insert(dep:targetdir()) - end - end - return pathenv -end +import("private.action.run.make_runenvs") -- run target function _do_run_target(target) @@ -77,22 +47,12 @@ function _do_run_target(target) local oldir = os.cd(rundir) -- add run environments - local runenvs = target:get("runenvs") - if runenvs then - for name, values in pairs(runenvs) do - os.addenv(name, unpack(table.wrap(values))) - end - end - local runenv = target:get("runenv") - if runenv then - for name, value in pairs(runenv) do - os.setenv(name, unpack(table.wrap(value))) - end + local addrunenvs, setrunenvs = make_runenvs(target) + for name, values in pairs(addrunenvs) do + os.addenv(name, unpack(table.wrap(values))) end - - -- add search directories for all dependent shared libraries on windows - if is_plat("windows") or (is_plat("mingw") and is_host("windows")) then - os.addenv("PATH", table.unpack(_make_runpath_on_windows(target))) + for name, value in pairs(setrunenvs) do + os.setenv(name, unpack(table.wrap(value))) end -- debugging? diff --git a/xmake/modules/private/action/run/make_runenvs.lua b/xmake/modules/private/action/run/make_runenvs.lua new file mode 100644 index 000000000..c42df29b4 --- /dev/null +++ b/xmake/modules/private/action/run/make_runenvs.lua @@ -0,0 +1,92 @@ +--!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 - 2019, TBOOX Open Source Group. +-- +-- @author ruki, OpportunityLiu +-- @file make_runenvs.lua +-- + +-- imports +import("core.base.hashset") + +-- add search directories for all dependent shared libraries on windows +function _make_runpath_on_windows(target) + + local pathenv = {} + local searchdirs = hashset.new() + local function insert(dir) + if not path.is_absolute(dir) then + dir = path.absolute(dir, os.projectdir()) + end + if searchdirs:insert(dir) then + table.insert(pathenv, dir) + end + end + + for _, linkdir in ipairs(target:get("linkdirs")) do + insert(linkdir) + end + for _, opt in ipairs(target:orderopts()) do + for _, linkdir in ipairs(opt:get("linkdirs")) do + insert(linkdir) + end + end + for _, dep in ipairs(target:orderdeps()) do + if dep:targetkind() == "shared" then + insert(dep:targetdir()) + end + end + return pathenv +end + +function main(target) + + assert(target) + + local set = {} + local add = {} + + -- add run environments + local runenvs = target:get("runenvs") + if runenvs then + for name, values in pairs(runenvs) do + add[name] = table.wrap(values) + end + end + local runenv = target:get("runenv") + if runenv then + for name, value in pairs(runenv) do + set[name] = table.wrap(value) + if add[name] then + utils.warning(format("both add_runenvs and set_runenv called on environment variable \"%s\", the former one will be ignored.", name)) + add[name] = nil + end + end + end + + if is_plat("windows") or (is_plat("mingw") and is_host("windows")) then + -- get PATH table + local path = add["PATH"] or set["PATH"] + if path == nil then + path = {} + add["PATH"] = path + end + + -- add search directories for all dependent shared libraries on windows + table.append(path, table.unpack(_make_runpath_on_windows(target))) + end + + return add, set +end diff --git a/xmake/plugins/project/vsxmake/getinfo.lua b/xmake/plugins/project/vsxmake/getinfo.lua index b0d24b04e..f433cb157 100644 --- a/xmake/plugins/project/vsxmake/getinfo.lua +++ b/xmake/plugins/project/vsxmake/getinfo.lua @@ -30,6 +30,7 @@ import("core.platform.environment") import("core.tool.compiler") import("core.tool.linker") import("lib.detect.find_tool") +import("private.action.run.make_runenvs") import("actions.config.configheader", {alias = "generate_configheader", rootdir = os.programdir()}) import("actions.config.configfiles", {alias = "generate_configfiles", rootdir = os.programdir()}) @@ -130,37 +131,6 @@ function _get_values(target, name) return table.unique(values) end --- add search directories for all dependent shared libraries on windows -function _make_runpath(target) - - local searchdirs = hashset.new() - local pathenv = {} - - local function insert(dir) - if not path.is_absolute(dir) then - dir = path.absolute(dir, os.projectdir()) - end - if searchdirs:insert(dir) then - table.insert(pathenv, dir) - end - end - - for _, linkdir in ipairs(target:get("linkdirs")) do - insert(linkdir) - end - for _, opt in ipairs(target:orderopts()) do - for _, linkdir in ipairs(opt:get("linkdirs")) do - insert(linkdir) - end - end - for _, dep in ipairs(target:orderdeps()) do - if dep:targetkind() == "shared" then - insert(dep:targetdir()) - end - end - return pathenv -end - -- make target info function _make_targetinfo(mode, arch, target) @@ -207,15 +177,17 @@ function _make_targetinfo(mode, arch, target) -- save runenvs local runenvs = {} - for k, v in pairs(target:get("runenvs")) do - local defs = table.imap(table.wrap(v), function(_, v) return vformat(v) end) - runenvs[k] = format("%s;$([System.Environment]::GetEnvironmentVariable('%s'))", path.joinenv(defs), k) + local addrunenvs, setrunenvs = make_runenvs(target) + for k, v in pairs(addrunenvs) do + if k:upper() == "PATH" then + runenvs[k] = format("%s;$([System.Environment]::GetEnvironmentVariable('%s'))", _make_dirs(v), k) + else + runenvs[k] = format("%s;$([System.Environment]::GetEnvironmentVariable('%s'))", path.joinenv(v), k) + end end - for k, v in pairs(target:get("runenv")) do - local defs = table.imap(table.wrap(v), function(_, v) return vformat(v) end) - runenvs[k] = path.joinenv(defs) + for k, v in pairs(setrunenvs) do + runenvs[k] = path.joinenv(v) end - runenvs["PATH"] = _make_dirs(_make_runpath(target)) .. ";" .. (runenvs["PATH"] or "$([System.Environment]::GetEnvironmentVariable('PATH'))") local runenvstr = {} for k, v in pairs(runenvs) do table.insert(runenvstr, k .. "=" .. v) |
