Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve runenvs #4986

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions xmake/actions/run/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 24 additions & 8 deletions xmake/core/base/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 0 additions & 33 deletions xmake/modules/devel/debugger/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
53 changes: 36 additions & 17 deletions xmake/modules/private/action/run/runenvs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -68,9 +67,7 @@ function _make_runpath_on_windows(target)
end
end
end

insert_target(target)

return pathenv
end

Expand All @@ -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
Loading