From 42786255ed8b8ba449415185eba6a765a5084513 Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 18 Apr 2024 00:00:35 +0800 Subject: improve os.execv and runenvs #4980 --- xmake/core/base/os.lua | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'xmake/core/base/os.lua') diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 2d7023ac4..967c02097 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -830,17 +830,37 @@ 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 + -- 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 + 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 + if type(v) == "string" and #v > 4096 and os.host() == "windows" then + v = os._deduplicate_pathenv(v) + end + local o = envars[k] + if o then + envars[k] = v .. path.envsep() .. o + else + envars[k] = v + end end - envars[k] = v end envs = {} for k, v in pairs(envars) do -- cgit v1.3.1 From f00427931ec4125ba42325e03d306b377f3d9b1d Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 18 Apr 2024 23:45:14 +0800 Subject: improve to make runenvs --- xmake/core/base/os.lua | 16 ++++------ xmake/modules/private/action/run/runenvs.lua | 47 +++++++++++++++++++++------- 2 files changed, 42 insertions(+), 21 deletions(-) (limited to 'xmake/core/base/os.lua') diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 967c02097..b0545afc8 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -839,10 +839,6 @@ function os.execv(program, argv, opt) if type(v) == "table" then v = path.joinenv(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 envars[k] = v end end @@ -851,19 +847,19 @@ function os.execv(program, argv, opt) if type(v) == "table" then v = path.joinenv(v) end - if type(v) == "string" and #v > 4096 and os.host() == "windows" then - v = os._deduplicate_pathenv(v) - end local o = envars[k] if o then - envars[k] = v .. path.envsep() .. o - else - envars[k] = v + v = v .. path.envsep() .. o end + envars[k] = v end 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/private/action/run/runenvs.lua b/xmake/modules/private/action/run/runenvs.lua index 7349b1d69..da22f6c72 100644 --- a/xmake/modules/private/action/run/runenvs.lua +++ b/xmake/modules/private/action/run/runenvs.lua @@ -89,38 +89,63 @@ function join(addenvs, setenvs) return envs end +-- 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 + 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 -- cgit v1.3.1