summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-02-08 23:04:27 +0800
committerruki <[email protected]>2023-02-08 23:04:27 +0800
commit13c187a5bf48fa0470e153ae863c2c2931f5a576 (patch)
treec57f4a10a28114641ec14d9d6136fb5544b19563
parentf38629bf5c5748719a02fae9afebd7ce83a64d0a (diff)
improve to load projectdir
-rw-r--r--xmake/actions/config/main.lua5
-rw-r--r--xmake/core/main.lua23
-rw-r--r--xmake/core/project/config.lua23
3 files changed, 44 insertions, 7 deletions
diff --git a/xmake/actions/config/main.lua b/xmake/actions/config/main.lua
index 1a392d33f..59bfe2b05 100644
--- a/xmake/actions/config/main.lua
+++ b/xmake/actions/config/main.lua
@@ -301,7 +301,10 @@ function main(opt)
end
-- check the working directory
- if not option.get("project") and not option.get("file") and os.isdir(os.projectdir()) then
+ if not option.get("project") and not option.get("file") and -- no given project path
+ not localcache.get("project", "projectdir") and -- no cached project path
+ not localcache.get("project", "projectfile") and
+ os.isdir(os.projectdir()) then
if path.translate(os.projectdir()) ~= path.translate(os.workingdir()) then
wprint([[You are working in the project directory(%s) and you can also
force to build in current directory via run `xmake -P .`]], os.projectdir())
diff --git a/xmake/core/main.lua b/xmake/core/main.lua
index e6c3fbfd5..18ab0f62e 100644
--- a/xmake/core/main.lua
+++ b/xmake/core/main.lua
@@ -117,6 +117,25 @@ function main._basicparse()
return option.parse(xmake._COMMAND_ARGV, task.common_options(), { allow_unknown = true })
end
+-- get the project configuration from cache if we are in the independent working directory
+-- @see https://github.com/xmake-io/xmake/issues/3342
+--
+function main._projectconf(name)
+ local rootdir = os.getenv("XMAKE_CONFIGDIR")
+ -- we switch to independent working directory
+ -- @see https://github.com/xmake-io/xmake/issues/820
+ if not rootdir and os.isdir(path.join(os.workingdir(), "." .. xmake._NAME)) then
+ rootdir = os.workingdir()
+ end
+ local cachefile = path.join(rootdir, "." .. xmake._NAME, os.host(), os.arch(), "cache", "project")
+ if os.isfile(cachefile) then
+ local cacheinfo = io.load(cachefile)
+ if cacheinfo then
+ return cacheinfo[name]
+ end
+ end
+end
+
-- the init function for main
function main._init()
@@ -139,7 +158,7 @@ function main._init()
local opt_projectdir, opt_projectfile = options.project, options.file
-- init the project directory
- local projectdir = opt_projectdir or localcache.get("project", "projectdir") or xmake._PROJECT_DIR
+ local projectdir = opt_projectdir or main._projectconf("projectdir") or xmake._PROJECT_DIR
if projectdir and not path.is_absolute(projectdir) then
projectdir = path.absolute(projectdir)
elseif projectdir then
@@ -149,7 +168,7 @@ function main._init()
assert(projectdir)
-- init the xmake.lua file path
- local projectfile = opt_projectfile or localcache.get("project", "projectfile") or xmake._PROJECT_FILE
+ local projectfile = opt_projectfile or main._projectconf("projectfile") or xmake._PROJECT_FILE
if projectfile and not path.is_absolute(projectfile) then
projectfile = path.absolute(projectfile, projectdir)
end
diff --git a/xmake/core/project/config.lua b/xmake/core/project/config.lua
index 3f9f02341..1220fbc79 100644
--- a/xmake/core/project/config.lua
+++ b/xmake/core/project/config.lua
@@ -96,8 +96,14 @@ function config.buildir(opt)
-- get the absolute path first
opt = opt or {}
local rootdir
- if os.isdir(path.join(os.workingdir(), ".xmake")) then
- -- we switch to independent working directory @see https://github.com/xmake-io/xmake/issues/820
+ -- we always switch to independent working directory if `-P/-F` is set
+ -- @see https://github.com/xmake-io/xmake/issues/3342
+ if not rootdir and (option.get("project") or option.get("projectfile")) then
+ rootdir = os.workingdir()
+ end
+ -- we switch to independent working directory if .xmake exists
+ -- @see https://github.com/xmake-io/xmake/issues/820
+ if not rootdir and os.isdir(path.join(os.workingdir(), "." .. xmake._NAME)) then
rootdir = os.workingdir()
end
if not rootdir then
@@ -129,10 +135,19 @@ end
function config.directory()
if config._DIRECTORY == nil then
local rootdir = os.getenv("XMAKE_CONFIGDIR")
- if not rootdir then
- -- @see https://github.com/xmake-io/xmake/issues/3342
+ -- we always switch to independent working directory if `-P/-F` is set
+ -- @see https://github.com/xmake-io/xmake/issues/3342
+ if not rootdir and (option.get("project") or option.get("projectfile")) then
rootdir = os.workingdir()
end
+ -- we switch to independent working directory if .xmake exists
+ -- @see https://github.com/xmake-io/xmake/issues/820
+ if not rootdir and os.isdir(path.join(os.workingdir(), "." .. xmake._NAME)) then
+ rootdir = os.workingdir()
+ end
+ if not rootdir then
+ rootdir = os.projectdir()
+ end
config._DIRECTORY = path.join(rootdir, "." .. xmake._NAME, os.host(), os.arch())
end
return config._DIRECTORY