diff options
| author | ruki <[email protected]> | 2024-04-18 17:19:13 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-04-18 17:19:13 +0800 |
| commit | e170a82a9f79f0331d22e66fc4ec35d05a784d8a (patch) | |
| tree | c704874b6789cc27fa634a41b6f4722eed3a72a7 | |
| parent | 78627bc254c1c9c798f802fa15ddda4cff37da87 (diff) | |
| parent | f00427931ec4125ba42325e03d306b377f3d9b1d (diff) | |
Merge pull request #4986 from xmake-io/runenvs
Improve runenvs
| -rw-r--r-- | xmake/actions/run/main.lua | 3 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 32 | ||||
| -rw-r--r-- | xmake/modules/devel/debugger/run.lua | 33 | ||||
| -rw-r--r-- | xmake/modules/private/action/run/runenvs.lua | 53 |
4 files changed, 61 insertions, 60 deletions
diff --git a/xmake/actions/run/main.lua b/xmake/actions/run/main.lua index 217f87c66..0c519473b 100644 --- a/xmake/actions/run/main.lua +++ b/xmake/actions/run/main.lua @@ -54,8 +54,7 @@ function _do_run_target(target) if option.get("debug") then debugger.run(targetfile, args, {curdir = rundir, addenvs = addenvs, setenvs = setenvs}) else - local envs = runenvs.join(addenvs, setenvs) - os.execv(targetfile, args, {curdir = rundir, detach = option.get("detach"), envs = envs}) + os.execv(targetfile, args, {curdir = rundir, detach = option.get("detach"), addenvs = addenvs, setenvs = setenvs}) end end diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 2d7023ac4..b0545afc8 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -830,20 +830,36 @@ function os.execv(program, argv, opt) -- uses the given environments? local envs = nil - if opt.envs then + local setenvs = opt.setenvs or opt.envs + local addenvs = opt.addenvs + if setenvs or addenvs then local envars = os.getenvs() - for k, v in pairs(opt.envs) do - if type(v) == "table" then - v = path.joinenv(v) + if setenvs then + for k, v in pairs(setenvs) do + if type(v) == "table" then + v = path.joinenv(v) + end + envars[k] = v end - -- we try to fix too long value before running process - if type(v) == "string" and #v > 4096 and os.host() == "windows" then - v = os._deduplicate_pathenv(v) + end + if addenvs then + for k, v in pairs(addenvs) do + if type(v) == "table" then + v = path.joinenv(v) + end + local o = envars[k] + if o then + v = v .. path.envsep() .. o + end + envars[k] = v end - envars[k] = v end envs = {} for k, v in pairs(envars) do + -- we try to fix too long value before running process + if type(v) == "string" and #v > 4096 and os.host() == "windows" then + v = os._deduplicate_pathenv(v) + end table.insert(envs, k .. '=' .. v) end end diff --git a/xmake/modules/devel/debugger/run.lua b/xmake/modules/devel/debugger/run.lua index 6befa017e..29c3f8c45 100644 --- a/xmake/modules/devel/debugger/run.lua +++ b/xmake/modules/devel/debugger/run.lua @@ -50,9 +50,6 @@ function _run_gdb(program, argv, opt) table.insert(argv, 1, program) table.insert(argv, 1, "--args") - -- handle envs - opt.envs = runenvs.join(opt.addenvs, opt.setenvs) - -- run it os.execv(gdb, argv, table.join(opt, {exclusive = true})) return true @@ -73,9 +70,6 @@ function _run_cudagdb(program, argv, opt) table.insert(argv, 1, program) table.insert(argv, 1, "--args") - -- handle envs - opt.envs = runenvs.join(opt.addenvs, opt.setenvs) - -- run it os.execv(gdb, argv, table.join(opt, {exclusive = true})) return true @@ -103,9 +97,6 @@ function _run_lldb(program, argv, opt) table.insert(argv, 1, names[i]) end - -- handle envs - opt.envs = runenvs.join(opt.addenvs, opt.setenvs) - -- run it os.execv(names[1], argv, table.join(opt, {exclusive = true})) return true @@ -124,9 +115,6 @@ function _run_windbg(program, argv, opt) argv = argv or {} table.insert(argv, 1, program) - -- handle envs - opt.envs = runenvs.join(opt.addenvs, opt.setenvs) - -- run it opt.detach = true os.execv(windbg, argv, opt) @@ -146,9 +134,6 @@ function _run_cudamemcheck(program, argv, opt) argv = argv or {} table.insert(argv, 1, program) - -- handle envs - opt.envs = runenvs.join(opt.addenvs, opt.setenvs) - -- run it os.execv(cudamemcheck, argv, opt) return true @@ -167,9 +152,6 @@ function _run_x64dbg(program, argv, opt) argv = argv or {} table.insert(argv, 1, program) - -- handle envs - opt.envs = runenvs.join(opt.addenvs, opt.setenvs) - -- run it opt.detach = true os.execv(x64dbg, argv, opt) @@ -189,9 +171,6 @@ function _run_ollydbg(program, argv, opt) argv = argv or {} table.insert(argv, 1, program) - -- handle envs - opt.envs = runenvs.join(opt.addenvs, opt.setenvs) - -- run it opt.detach = true os.execv(ollydbg, argv, opt) @@ -211,9 +190,6 @@ function _run_vsjitdebugger(program, argv, opt) argv = argv or {} table.insert(argv, 1, program) - -- handle envs - opt.envs = runenvs.join(opt.addenvs, opt.setenvs) - -- run it opt.detach = true os.execv(vsjitdebugger, argv, opt) @@ -234,9 +210,6 @@ function _run_devenv(program, argv, opt) table.insert(argv, 1, "/DebugExe") table.insert(argv, 2, program) - -- handle envs - opt.envs = runenvs.join(opt.addenvs, opt.setenvs) - -- run it opt.detach = true os.execv(devenv, argv, opt) @@ -332,9 +305,6 @@ function _run_gede(program, argv, opt) table.insert(argv, 1, "--args") table.insert(argv, 1, "--no-show-config") - -- handle envs - opt.envs = runenvs.join(opt.addenvs, opt.setenvs) - -- run it os.execv(gede.program, argv, table.join(opt, {exclusive = true})) return true @@ -355,9 +325,6 @@ function _run_seergdb(program, argv, opt) table.insert(argv, 1, program) table.insert(argv, 1, "--start") - -- handle envs - opt.envs = runenvs.join(opt.addenvs, opt.setenvs) - -- run it os.execv(seergdb.program, argv, table.join(opt, {exclusive = true})) return true diff --git a/xmake/modules/private/action/run/runenvs.lua b/xmake/modules/private/action/run/runenvs.lua index 1a7ba05ac..da22f6c72 100644 --- a/xmake/modules/private/action/run/runenvs.lua +++ b/xmake/modules/private/action/run/runenvs.lua @@ -23,7 +23,6 @@ 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) @@ -68,9 +67,7 @@ function _make_runpath_on_windows(target) end end end - insert_target(target) - return pathenv end @@ -92,41 +89,63 @@ function join(addenvs, setenvs) return envs end -function make(target) +-- recursively add package envs +function _add_target_pkgenvs(addenvs, target, targets_added) + if targets_added[target:name()] then + return + end + targets_added[target:name()] = true + local pkgenvs = target:pkgenvs() + if pkgenvs then + for name, values in pairs(pkgenvs) do + values = path.splitenv(values) + local oldenvs = addenvs[name] + if oldenvs then + table.join2(oldenvs, values) + else + addenvs[name] = values + end + end + end + for _, dep in ipairs(target:orderdeps()) do + _add_target_pkgenvs(addenvs, dep, targets_added) + end +end - -- check - assert(target) +function make(target) -- add run environments - local set = {} - local add = {} + local setenvs = {} + local addenvs = {} local runenvs = target:get("runenvs") if runenvs then for name, values in pairs(runenvs) do - add[name] = table.wrap(values) + addenvs[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 + setenvs[name] = table.wrap(value) + if addenvs[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 + addenvs[name] = nil end end end + -- add package run environments + _add_target_pkgenvs(addenvs, target, {}) + -- add search directories for all dependent shared libraries on windows if target:is_plat("windows") or (target:is_plat("mingw") and is_host("windows")) then - -- get PATH table - local pathenv = add["PATH"] or set["PATH"] + local pathenv = addenvs["PATH"] or setenvs["PATH"] local runpath = _make_runpath_on_windows(target) if pathenv == nil then - add["PATH"] = runpath + addenvs["PATH"] = runpath else - table.append(pathenv, table.unpack(runpath)) + table.join2(pathenv, runpath) end end - return add, set + return addenvs, setenvs end |
