From 708d5964ee9cf1325892dff5f1c1c47f2a202bb1 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Sep 2025 23:45:06 +0800 Subject: improve os.getenvs --- xmake/core/base/os.lua | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index cecf38dd2..f65f47edf 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -199,6 +199,7 @@ end -- notify envs have been changed function os._notify_envs_changed(envs) + os._ENVS = nil if os._SCHED_CHENVS then os._SCHED_CHENVS(envs) end @@ -1292,21 +1293,25 @@ end -- get all current environment variables -- e.g. envs["PATH"] = "/xxx:/yyy/foo" function os.getenvs() - local envs = {} - for _, line in ipairs(os._getenvs()) do - local p = line:find('=', 1, true) - if p then - local key = line:sub(1, p - 1):trim() - -- only translate Path to PATH on windows - -- @see https://github.com/xmake-io/xmake/issues/3752 - if os.host() == "windows" and key:lower() == "path" then - key = key:upper() - end - local values = line:sub(p + 1):trim() - if #key > 0 then - envs[key] = values + local envs = os._ENVS + if envs == nil then + envs = {} + for _, line in ipairs(os._getenvs()) do + local p = line:find('=', 1, true) + if p then + local key = line:sub(1, p - 1):trim() + -- only translate Path to PATH on windows + -- @see https://github.com/xmake-io/xmake/issues/3752 + if os.host() == "windows" and key:lower() == "path" then + key = key:upper() + end + local values = line:sub(p + 1):trim() + if #key > 0 then + envs[key] = values + end end end + os._ENVS = envs end return envs end -- cgit v1.3.1 From 80473173e753bc989f0b6ae216df160eff4e3911 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Sep 2025 23:56:42 +0800 Subject: improve to cache envs for scheduler --- xmake/core/base/scheduler.lua | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index 9084ad6ba..fc6666c77 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -296,22 +296,15 @@ function scheduler:_co_curenvs_update(envs) return end - -- save the current directory hash - local envs_hash = "" + -- save the current environments envs = envs or os.getenvs() - for _, key in ipairs(table.orderkeys(envs)) do - envs_hash = envs_hash .. key:upper() .. envs[key] - end - envs_hash = hash.uuid4(envs_hash):sub(1, 8) - self._CO_CURENVS_HASH = envs_hash - - -- save the current directory for each coroutine local co_curenvs = self._CO_CURENVS if not co_curenvs then co_curenvs = {} self._CO_CURENVS = co_curenvs end - co_curenvs[running] = {envs_hash, envs} + co_curenvs[running] = envs + self._CO_CURENVS_CURRENT = envs end -- resume it's waiting coroutine if all coroutines are dead in group @@ -451,10 +444,10 @@ function scheduler:co_resume(co, ...) end -- has the current environments been changed? restore it - local curenvs = self._CO_CURENVS_HASH + local curenvs = self._CO_CURENVS_CURRENT local oldenvs = self._CO_CURENVS and self._CO_CURENVS[running] or nil - if oldenvs and curenvs ~= oldenvs[1] and running:is_isolated() then -- hash changed? - os.setenvs(oldenvs[2]) + if oldenvs and curenvs ~= oldenvs and running:is_isolated() then -- hash changed? + os.setenvs(oldenvs) end end @@ -476,10 +469,10 @@ function scheduler:co_suspend(...) end -- has the current environments been changed? restore it - local curenvs = self._CO_CURENVS_HASH + local curenvs = self._CO_CURENVS_CURRENT local oldenvs = self._CO_CURENVS and self._CO_CURENVS[running] or nil - if oldenvs and curenvs ~= oldenvs[1] and running:is_isolated() then -- hash changed? - os.setenvs(oldenvs[2]) + if oldenvs and curenvs ~= oldenvs and running:is_isolated() then -- hash changed? + os.setenvs(oldenvs) end -- return results -- cgit v1.3.1 From 91b87415d0666c388d5d2f8fada1164c095db098 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 26 Sep 2025 22:37:30 +0800 Subject: improve to change directory --- xmake/core/base/os.lua | 45 ++++++++++++++++++++++++++---------------- xmake/core/base/scheduler.lua | 46 +++++++++++++++++++++++-------------------- 2 files changed, 53 insertions(+), 38 deletions(-) diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index f65f47edf..99b702ce8 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -39,6 +39,7 @@ os._mkdir = os._mkdir or os.mkdir os._rmdir = os._rmdir or os.rmdir os._touch = os._touch or os.touch os._tmpdir = os._tmpdir or os.tmpdir +os._curdir = os._curdir or os.curdir os._fscase = os._fscase or os.fscase os._setenv = os._setenv or os.setenv os._getenvs = os._getenvs or os.getenvs @@ -197,6 +198,14 @@ function os._sched_chdir_set(chdir) os._SCHED_CHDIR = chdir end +-- notify the current directory have been changed +function os._notify_curdir_changed() + os._CURDIR = nil + if os._SCHED_CHDIR then + os._SCHED_CHDIR(os.curdir()) + end +end + -- notify envs have been changed function os._notify_envs_changed(envs) os._ENVS = nil @@ -606,41 +615,33 @@ function os.cd(dir) -- support path instance dir = tostring(dir) - -- the previous directory - local oldir = os.curdir() - -- change to the previous directory? + local oldir = os.curdir() if dir == "-" then - -- exists the previous directory? if os._PREDIR then dir = os._PREDIR os._PREDIR = nil else - -- error return nil, string.format("not found the previous directory %s", os.strerror()) end end - -- is directory? - if os.isdir(dir) then + -- no changed? + if dir == oldir then + return oldir + end - -- change to directory + -- do change directory + if os.isdir(dir) then if not os.chdir(dir) then return nil, string.format("cannot change directory %s %s", dir, os.strerror()) end - - -- save the previous directory os._PREDIR = oldir - - -- not exists? else return nil, string.format("cannot change directory %s, not found this directory %s", dir, os.strerror()) end - -- do chdir callback for scheduler - if os._SCHED_CHDIR then - os._SCHED_CHDIR(os.curdir()) - end + os._notify_curdir_changed() return oldir end @@ -696,6 +697,16 @@ function os.rmdir(dir) return true end +-- get the current directory +function os.curdir() + local curdir = os._CURDIR + if curdir == nil then + curdir = os._curdir() + os._CURDIR = curdir + end + return curdir +end + -- get the temporary directory function os.tmpdir(opt) @@ -1293,7 +1304,7 @@ end -- get all current environment variables -- e.g. envs["PATH"] = "/xxx:/yyy/foo" function os.getenvs() - local envs = os._ENVS + local envs -- = os._ENVS if envs == nil then envs = {} for _, line in ipairs(os._getenvs()) do diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index fc6666c77..c96b32441 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -273,18 +273,15 @@ function scheduler:_co_curdir_update(curdir) return end - -- save the current directory hash + -- save the current directory curdir = curdir or os.curdir() - local curdir_hash = hash.uuid4(path.absolute(curdir)):sub(1, 8) - self._CO_CURDIR_HASH = curdir_hash - - -- save the current directory for each coroutine local co_curdirs = self._CO_CURDIRS if not co_curdirs then co_curdirs = {} self._CO_CURDIRS = co_curdirs end - co_curdirs[running] = {curdir_hash, curdir} + co_curdirs[running] = curdir + self._CO_CURDIR_CURRENT = curdir end -- update the current environments hash of current coroutine @@ -296,15 +293,22 @@ function scheduler:_co_curenvs_update(envs) return end - -- save the current environments + -- save the current directory hash + local envs_hash = "" envs = envs or os.getenvs() + for _, key in ipairs(table.orderkeys(envs)) do + envs_hash = envs_hash .. key:upper() .. envs[key] + end + envs_hash = hash.uuid4(envs_hash):sub(1, 8) + self._CO_CURENVS_HASH = envs_hash + + -- save the current directory for each coroutine local co_curenvs = self._CO_CURENVS if not co_curenvs then co_curenvs = {} self._CO_CURENVS = co_curenvs end - co_curenvs[running] = envs - self._CO_CURENVS_CURRENT = envs + co_curenvs[running] = {envs_hash, envs} end -- resume it's waiting coroutine if all coroutines are dead in group @@ -437,17 +441,17 @@ function scheduler:co_resume(co, ...) if running then -- has the current directory been changed? restore it - local curdir = self._CO_CURDIR_HASH + local curdir = self._CO_CURDIR_CURRENT local olddir = self._CO_CURDIRS and self._CO_CURDIRS[running] or nil - if olddir and curdir ~= olddir[1] then -- hash changed? - os.cd(olddir[2]) + if olddir and curdir ~= olddir then -- hash changed? + os.cd(olddir) end -- has the current environments been changed? restore it - local curenvs = self._CO_CURENVS_CURRENT + local curenvs = self._CO_CURENVS_HASH local oldenvs = self._CO_CURENVS and self._CO_CURENVS[running] or nil - if oldenvs and curenvs ~= oldenvs and running:is_isolated() then -- hash changed? - os.setenvs(oldenvs) + if oldenvs and curenvs ~= oldenvs[1] and running:is_isolated() then -- hash changed? + os.setenvs(oldenvs[2]) end end @@ -462,17 +466,17 @@ function scheduler:co_suspend(...) -- has the current directory been changed? restore it local running = assert(self:co_running()) - local curdir = self._CO_CURDIR_HASH + local curdir = self._CO_CURDIR_CURRENT local olddir = self._CO_CURDIRS and self._CO_CURDIRS[running] or nil - if olddir and curdir ~= olddir[1] then -- hash changed? - os.cd(olddir[2]) + if olddir and curdir ~= olddir then -- hash changed? + os.cd(olddir) end -- has the current environments been changed? restore it - local curenvs = self._CO_CURENVS_CURRENT + local curenvs = self._CO_CURENVS_HASH local oldenvs = self._CO_CURENVS and self._CO_CURENVS[running] or nil - if oldenvs and curenvs ~= oldenvs and running:is_isolated() then -- hash changed? - os.setenvs(oldenvs) + if oldenvs and curenvs ~= oldenvs[1] and running:is_isolated() then -- hash changed? + os.setenvs(oldenvs[2]) end -- return results -- cgit v1.3.1 From 947491d0df87eafcbf8f560a1377f49a7b60a2db Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 27 Sep 2025 00:45:48 +0800 Subject: update envs --- xmake/core/base/os.lua | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 99b702ce8..57046813e 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -208,7 +208,6 @@ end -- notify envs have been changed function os._notify_envs_changed(envs) - os._ENVS = nil if os._SCHED_CHENVS then os._SCHED_CHENVS(envs) end @@ -1304,25 +1303,21 @@ end -- get all current environment variables -- e.g. envs["PATH"] = "/xxx:/yyy/foo" function os.getenvs() - local envs -- = os._ENVS - if envs == nil then - envs = {} - for _, line in ipairs(os._getenvs()) do - local p = line:find('=', 1, true) - if p then - local key = line:sub(1, p - 1):trim() - -- only translate Path to PATH on windows - -- @see https://github.com/xmake-io/xmake/issues/3752 - if os.host() == "windows" and key:lower() == "path" then - key = key:upper() - end - local values = line:sub(p + 1):trim() - if #key > 0 then - envs[key] = values - end + local envs = {} + for _, line in ipairs(os._getenvs()) do + local p = line:find('=', 1, true) + if p then + local key = line:sub(1, p - 1):trim() + -- only translate Path to PATH on windows + -- @see https://github.com/xmake-io/xmake/issues/3752 + if os.host() == "windows" and key:lower() == "path" then + key = key:upper() + end + local values = line:sub(p + 1):trim() + if #key > 0 then + envs[key] = values end end - os._ENVS = envs end return envs end -- cgit v1.3.1