Skip to content

Commit

Permalink
Merge pull request #4047 from xmake-io/envs
Browse files Browse the repository at this point in the history
improve target envs #4033
  • Loading branch information
waruqi authored Aug 7, 2023
2 parents 5ea087b + 2abd761 commit e8dbb83
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions xmake/actions/build/build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ function _add_batchjobs_for_target(batchjobs, rootjob, target)
end

-- add after_build job for target
local oldenvs
local pkgenvs = _g.pkgenvs or {}
_g.pkgenvs = pkgenvs
local job_build_after = batchjobs:addjob(target:name() .. "/after_build", function (index, total)

-- do after_build
Expand All @@ -140,8 +141,15 @@ function _add_batchjobs_for_target(batchjobs, rootjob, target)
end

-- restore environments
if oldenvs then
os.setenvs(oldenvs)
if target:pkgenvs() then
pkgenvs.oldenvs = pkgenvs.oldenvs or os.getenvs()
pkgenvs.newenvs = pkgenvs.newenvs or {}
pkgenvs.newenvs[target] = nil
local newenvs = pkgenvs.oldenvs
for _, envs in pairs(pkgenvs.newenvs) do
newenvs = os.joinenvs(envs, newenvs)
end
os.setenvs(newenvs)
end

end, {rootjob = rootjob})
Expand All @@ -153,7 +161,21 @@ function _add_batchjobs_for_target(batchjobs, rootjob, target)
local job_build_before = batchjobs:addjob(target:name() .. "/before_build", function (index, total)

-- enter package environments
oldenvs = os.addenvs(target:pkgenvs())
-- https://github.com/xmake-io/xmake/issues/4033
--
-- maybe mixing envs isn't a great solution,
-- but it's the most efficient compromise compared to setting envs in every on_build_file.
--
if target:pkgenvs() then
pkgenvs.oldenvs = pkgenvs.oldenvs or os.getenvs()
pkgenvs.newenvs = pkgenvs.newenvs or {}
pkgenvs.newenvs[target] = target:pkgenvs()
local newenvs = pkgenvs.oldenvs
for _, envs in pairs(pkgenvs.newenvs) do
newenvs = os.joinenvs(envs, newenvs)
end
os.setenvs(newenvs)
end

-- clean target if rebuild
if option.get("rebuild") and not option.get("dry-run") then
Expand Down Expand Up @@ -266,6 +288,3 @@ function main(targetname, group_pattern)
end
end




0 comments on commit e8dbb83

Please sign in to comment.