diff options
| author | ruki <[email protected]> | 2021-04-13 22:42:22 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-04-13 22:42:22 +0800 |
| commit | 84b64c4ecffe6ed4a498efa8116271ecba78e52d (patch) | |
| tree | 4f03fff7f04d6fd648b9bfce26348463b4bc7423 | |
| parent | 317745583da0e5c940f6e300c5f6163eb800efbf (diff) | |
improve envs
| -rw-r--r-- | CHANGELOG.md | 8 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 17 | ||||
| -rw-r--r-- | xmake/core/package/package.lua | 28 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/os.lua | 1 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/check.lua | 3 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/actions/install.lua | 13 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/environment.lua | 6 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/install_packages.lua | 3 |
8 files changed, 37 insertions, 42 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f7cf627c..bf8a4c0c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ * [#1323](https://github.com/xmake-io/xmake/issues/1323): Support find and install package from `apt`, `add_requires("apt::zlib1g-dev")` +### Change + +* Improve `find_package` and add `package:find_package` for xmake package + ### v2.5.3 ### New features @@ -972,6 +976,10 @@ * [#1323](https://github.com/xmake-io/xmake/issues/1323): 支持从 apt 查找安装包,`add_requires("apt::zlib1g-dev")` +### 改进 + +* 改进 `find_package` 并且添加 `package:find_package` 接口在包定义中方便查找包 + ## v2.5.3 ### 新特性 diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index f9444d4b3..44b373a66 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -998,6 +998,23 @@ function os.getenvs() return envs end +-- set all current environment variables +function os.setenvs(envs) + if envs then + -- remove new added values + local curenvs = os.getenvs() + for name, _ in pairs(curenvs) do + if not envs[name] then + os.setenv(name, nil) + end + end + -- change values + for name, values in pairs(envs) do + os.setenv(name, values) + end + end +end + -- set values to environment variable function os.setenv(name, ...) local values = {...} diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 3c2f36e75..c4db97454 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -591,18 +591,8 @@ end -- enter the package environments function _instance:envs_enter() - - -- save the old environments - local oldenvs = self._OLDENVS - if not oldenvs then - oldenvs = {} - self._OLDENVS = oldenvs - end - - -- add the new environments local installdir = self:installdir() for name, values in pairs(self:envs()) do - oldenvs[name] = oldenvs[name] or os.getenv(name) if name == "PATH" or name == "LD_LIBRARY_PATH" or name == "DYLD_LIBRARY_PATH" then for _, value in ipairs(values) do if path.is_absolute(value) then @@ -617,24 +607,6 @@ function _instance:envs_enter() end end --- leave the package environments -function _instance:envs_leave() - local oldenvs = self._OLDENVS - if oldenvs then - -- remove new added values - for name, _ in pairs(self:envs()) do - if not oldenvs[name] then - os.setenv(name, nil) - end - end - -- restore old values - for name, values in pairs(oldenvs) do - os.setenv(name, values) - end - self._OLDENVS = nil - end -end - -- get the given environment variable function _instance:getenv(name) return self:envs()[name] diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index 2436c175c..9700aebb8 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -58,6 +58,7 @@ sandbox_os.addenv = os.addenv sandbox_os.setenvp = os.setenvp sandbox_os.addenvp = os.addenvp sandbox_os.getenvs = os.getenvs +sandbox_os.setenvs = os.setenvs sandbox_os.pbpaste = os.pbpaste sandbox_os.pbcopy = os.pbcopy sandbox_os.cpuinfo = os.cpuinfo diff --git a/xmake/modules/private/action/require/check.lua b/xmake/modules/private/action/require/check.lua index 5d7dc103c..9b20b1310 100644 --- a/xmake/modules/private/action/require/check.lua +++ b/xmake/modules/private/action/require/check.lua @@ -43,9 +43,10 @@ function main(requires_raw) runjobs("fetch_packages", function (index) local instance = packages[index] if instance and (not option.get("force") or (option.get("shallow") and not instance:is_toplevel())) then + local oldenvs = os.getenvs() instance:envs_enter() instance:fetch() - instance:envs_leave() + os.setenvs(oldenvs) end end, {total = #packages}) diff --git a/xmake/modules/private/action/require/impl/actions/install.lua b/xmake/modules/private/action/require/impl/actions/install.lua index 752b80725..81f93681e 100644 --- a/xmake/modules/private/action/require/impl/actions/install.lua +++ b/xmake/modules/private/action/require/impl/actions/install.lua @@ -132,6 +132,7 @@ function main(package) end -- install it + local oldenvs = os.getenvs() try { function () @@ -158,7 +159,6 @@ function main(package) patch_sources(package) -- enter the environments of all package dependencies - local oldenvs = os.getenvs() for _, dep in ipairs(package:orderdeps()) do dep:envs_enter() end @@ -172,9 +172,7 @@ function main(package) end -- leave the environments of all package dependencies - for _, dep in irpairs(package:orderdeps()) do - dep:envs_leave() - end + os.setenvs(oldenvs) -- save the package info to the manifest file package:manifest_save() @@ -206,10 +204,7 @@ function main(package) end -- leave the package environments - package:envs_leave() - for _, dep in irpairs(package:orderdeps()) do - dep:envs_leave() - end + os.setenvs(oldenvs) -- trace tty.erase_line_to_start().cr() @@ -235,7 +230,7 @@ function main(package) cprint("${yellow} => ${clear}install %s %s .. ${color.failure}${text.failure}", package:displayname(), package:version_str() or "") -- leave the package environments - package:envs_leave() + os.setenvs(oldenvs) -- copy the invalid package directory to cache local installdir = package:installdir() diff --git a/xmake/modules/private/action/require/impl/environment.lua b/xmake/modules/private/action/require/impl/environment.lua index ae6950de3..115f263aa 100644 --- a/xmake/modules/private/action/require/impl/environment.lua +++ b/xmake/modules/private/action/require/impl/environment.lua @@ -54,6 +54,7 @@ function enter() end -- enter the environments of installed packages + _g._OLDENVS = os.getenvs() for _, instance in ipairs(packages) do instance:envs_enter() end @@ -64,9 +65,8 @@ end function leave() -- leave the environments of installed packages - for _, instance in irpairs(_g._PACKAGES) do - instance:envs_leave() - end + os.setenvs(_g._OLDENVS) + _g._OLDENVS = nil _g._PACKAGES = nil -- leave the environments of git diff --git a/xmake/modules/private/action/require/impl/install_packages.lua b/xmake/modules/private/action/require/impl/install_packages.lua index efc0c73a9..8bec808c6 100644 --- a/xmake/modules/private/action/require/impl/install_packages.lua +++ b/xmake/modules/private/action/require/impl/install_packages.lua @@ -429,9 +429,10 @@ function main(requires, opt) runjobs("fetch_packages", function (index) local instance = packages[index] if instance and (not option.get("force") or (option.get("shallow") and not instance:is_toplevel())) then + local oldenvs = os.getenvs() instance:envs_enter() instance:fetch() - instance:envs_leave() + os.setenvs(oldenvs) end end, {total = #packages}) |
