diff options
| author | ruki <[email protected]> | 2021-05-11 00:41:32 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-05-11 10:28:37 +0800 |
| commit | 0b9b15fb9cd681831d3964474f5935e4e38e0e60 (patch) | |
| tree | 41e7b63ea7b72811aa38eba5937d9212eb51a52f | |
| parent | 1a1b4ddb49c43103f3c498d74dbee267a210fad7 (diff) | |
improve target:pkgenvs
| -rw-r--r-- | xmake/actions/build/build.lua | 11 | ||||
| -rw-r--r-- | xmake/actions/clean/main.lua | 10 | ||||
| -rw-r--r-- | xmake/actions/install/install.lua | 10 | ||||
| -rw-r--r-- | xmake/actions/package/main.lua | 10 | ||||
| -rw-r--r-- | xmake/actions/run/main.lua | 19 | ||||
| -rw-r--r-- | xmake/actions/uninstall/uninstall.lua | 10 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 24 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 15 | ||||
| -rw-r--r-- | xmake/plugins/project/vsxmake/getinfo.lua | 4 |
9 files changed, 40 insertions, 73 deletions
diff --git a/xmake/actions/build/build.lua b/xmake/actions/build/build.lua index a8029e24c..aa8805c62 100644 --- a/xmake/actions/build/build.lua +++ b/xmake/actions/build/build.lua @@ -105,7 +105,6 @@ function _add_batchjobs_for_target(batchjobs, rootjob, target) end -- add after_build job for target - local oldenvs = {} local job_after_build = batchjobs:addjob(target:name() .. "/after_build", function (index, total) -- do after_build @@ -120,11 +119,6 @@ function _add_batchjobs_for_target(batchjobs, rootjob, target) after_build(target, {progress = progress}) end end - - -- leave the environments of the target packages - for name, values in pairs(oldenvs) do - os.setenv(name, values) - end end, rootjob) -- add batch jobs for target, @note only on_build script support batch jobs @@ -134,10 +128,7 @@ function _add_batchjobs_for_target(batchjobs, rootjob, target) local job_build_before = batchjobs:addjob(target:name() .. "/before_build", function (index, total) -- enter the environments of the target packages - for name, values in pairs(target:pkgenvs()) do - oldenvs[name] = os.getenv(name) - os.addenv(name, unpack(values)) - end + os.addenvs(target:pkgenvs()) -- clean target if rebuild if option.get("rebuild") and not option.get("dry-run") then diff --git a/xmake/actions/clean/main.lua b/xmake/actions/clean/main.lua index 4dc42437a..e9e574545 100644 --- a/xmake/actions/clean/main.lua +++ b/xmake/actions/clean/main.lua @@ -56,11 +56,7 @@ function _clean_target(target) end -- enter the environments of the target packages - local oldenvs = {} - for name, values in pairs(target:pkgenvs()) do - oldenvs[name] = os.getenv(name) - os.addenv(name, unpack(values)) - end + local oldenvs = os.addenvs(target:pkgenvs()) -- the target scripts local scripts = @@ -95,9 +91,7 @@ function _clean_target(target) end -- leave the environments of the target packages - for name, values in pairs(oldenvs) do - os.setenv(name, values) - end + os.setenvs(oldenvs) end -- clean the given targets diff --git a/xmake/actions/install/install.lua b/xmake/actions/install/install.lua index 1a71bda40..b4b0a6628 100644 --- a/xmake/actions/install/install.lua +++ b/xmake/actions/install/install.lua @@ -57,11 +57,7 @@ function _install_target(target) local oldir = os.cd(project.directory()) -- enter the environments of the target packages - local oldenvs = {} - for name, values in pairs(target:pkgenvs()) do - oldenvs[name] = os.getenv(name) - os.addenv(name, unpack(values)) - end + local oldenvs = os.addenvs(target:pkgenvs()) -- the target scripts local scripts = @@ -96,9 +92,7 @@ function _install_target(target) end -- leave the environments of the target packages - for name, values in pairs(oldenvs) do - os.setenv(name, values) - end + os.setenvs(oldenvs) -- leave project directory os.cd(oldir) diff --git a/xmake/actions/package/main.lua b/xmake/actions/package/main.lua index 39078392b..a1fcab5ed 100644 --- a/xmake/actions/package/main.lua +++ b/xmake/actions/package/main.lua @@ -147,11 +147,7 @@ function _package_target(target) local oldir = os.cd(project.directory()) -- enter the environments of the target packages - local oldenvs = {} - for name, values in pairs(target:pkgenvs()) do - oldenvs[name] = os.getenv(name) - os.addenv(name, unpack(values)) - end + local oldenvs = os.addenvs(target:pkgenvs()) -- the target scripts local scripts = @@ -186,9 +182,7 @@ function _package_target(target) end -- leave the environments of the target packages - for name, values in pairs(oldenvs) do - os.setenv(name, values) - end + os.setenvs(oldenvs) -- leave project directory os.cd(oldir) diff --git a/xmake/actions/run/main.lua b/xmake/actions/run/main.lua index 04b0d76d4..a74845f95 100644 --- a/xmake/actions/run/main.lua +++ b/xmake/actions/run/main.lua @@ -84,19 +84,14 @@ function _on_run_target(target) end -- recursively target add env -function _add_target_pkgenvs(target, oldenvs, targets_added) +function _add_target_pkgenvs(target, targets_added) if targets_added[target:name()] then return end targets_added[target:name()] = true - for name, values in pairs(target:pkgenvs()) do - if not oldenvs[name] then - oldenvs[name] = os.getenv(name) - end - os.addenv(name, unpack(values)) - end + os.addenvs(target:pkgenvs()) for _, dep in ipairs(target:orderdeps()) do - _add_target_pkgenvs(dep, oldenvs, targets_added) + _add_target_pkgenvs(dep, targets_added) end end @@ -109,8 +104,8 @@ function _run(target) end -- enter the environments of the target packages - local oldenvs = {} - _add_target_pkgenvs(target, oldenvs, {}) + local oldenvs = os.getenvs() + _add_target_pkgenvs(target, {}) -- the target scripts local scripts = @@ -145,9 +140,7 @@ function _run(target) end -- leave the environments of the target packages - for name, values in pairs(oldenvs) do - os.setenv(name, values) - end + os.setenvs(oldenvs) end -- check targets diff --git a/xmake/actions/uninstall/uninstall.lua b/xmake/actions/uninstall/uninstall.lua index 08b5067ba..8ce5346a6 100644 --- a/xmake/actions/uninstall/uninstall.lua +++ b/xmake/actions/uninstall/uninstall.lua @@ -57,11 +57,7 @@ function _uninstall_target(target) local oldir = os.cd(project.directory()) -- enter the environments of the target packages - local oldenvs = {} - for name, values in pairs(target:pkgenvs()) do - oldenvs[name] = os.getenv(name) - os.addenv(name, unpack(values)) - end + local oldenvs = os.addenvs(target:pkgenvs()) -- the target scripts local scripts = @@ -96,9 +92,7 @@ function _uninstall_target(target) end -- leave the environments of the target packages - for name, values in pairs(oldenvs) do - os.setenv(name, values) - end + os.setenvs(oldenvs) -- leave project directory os.cd(oldir) diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index accc6c7dd..2182a91de 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -1012,11 +1012,11 @@ end -- set all current environment variables -- e.g. envs["PATH"] = "/xxx:/yyy/foo" function os.setenvs(envs) + local oldenvs = os.getenvs() if envs then local changed = false -- remove new added values - local curenvs = os.getenvs() - for name, _ in pairs(curenvs) do + for name, _ in pairs(oldenvs) do if not envs[name] then if os._setenv(name, "") then changed = true @@ -1025,7 +1025,7 @@ function os.setenvs(envs) end -- change values for name, values in pairs(envs) do - if curenvs[name] ~= values then + if oldenvs[name] ~= values then if os._setenv(name, values) then changed = true end @@ -1036,22 +1036,25 @@ function os.setenvs(envs) os._SCHED_CHENVS(envs) end end + return oldenvs end -- add environment variables -- e.g. envs["PATH"] = "/xxx:/yyy/foo" function os.addenvs(envs) + local oldenvs = os.getenvs() if envs then - local changed = false + local newenvs = {} for name, values in pairs(envs) do - local ok - local oldenv = os.getenv(name) - if oldenv == "" or oldenv == nil then - ok = os._setenv(name, values) + if newenvs[name] then + newenvs[name] = values .. path.envsep() .. newenvs[name] else - ok = os._setenv(name, values .. path.envsep() .. oldenv) + newenvs[name] = oldenvs[name] end - if ok then + end + local changed = false + for name, values in pairs(newenvs) do + if os._setenv(name, values) then changed = true end end @@ -1060,6 +1063,7 @@ function os.addenvs(envs) os._SCHED_CHENVS() end end + return oldenvs end -- set values to environment variable diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index f4bf69efb..3b5c67427 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -140,10 +140,7 @@ end function _instance:_load_after() -- enter the environments of the target packages - local oldenvs = os.getenvs() - for name, values in pairs(self:pkgenvs()) do - os.addenv(name, unpack(values)) - end + local oldenvs = os.addenvs(self:pkgenvs()) -- do after_load with target rules local ok, errors = self:_load_rules("after") @@ -792,8 +789,14 @@ function _instance:pkgenvs() local envs = pkg:get("envs") if envs then for name, values in pairs(envs) do - pkgenvs[name] = pkgenvs[name] or {} - table.join2(pkgenvs[name], values) + if type(values) == "table" then + values = path.joinenv(values) + end + if pkgenvs[name] then + pkgenvs[name] = pkgenvs[name] .. path.envsep() .. values + else + pkgenvs[name] = values + end end end end diff --git a/xmake/plugins/project/vsxmake/getinfo.lua b/xmake/plugins/project/vsxmake/getinfo.lua index 97803a115..0c96a70ad 100644 --- a/xmake/plugins/project/vsxmake/getinfo.lua +++ b/xmake/plugins/project/vsxmake/getinfo.lua @@ -180,12 +180,12 @@ function _make_targetinfo(mode, arch, target) local addrunenvs, setrunenvs = make_runenvs(target) for k, v in pairs(target:pkgenvs()) do addrunenvs = addrunenvs or {} - addrunenvs[k] = table.join(table.wrap(addrunenvs[k]), v) + addrunenvs[k] = table.join(table.wrap(addrunenvs[k]), path.splitenv(v)) end for _, dep in ipairs(target:orderdeps()) do for k, v in pairs(dep:pkgenvs()) do addrunenvs = addrunenvs or {} - addrunenvs[k] = table.join(table.wrap(addrunenvs[k]), v) + addrunenvs[k] = table.join(table.wrap(addrunenvs[k]), path.splitenv(v)) end end for k, v in pairs(addrunenvs) do |
