diff options
| author | ruki <[email protected]> | 2023-02-08 18:03:07 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-02-08 18:03:07 +0800 |
| commit | 330d81f404b45d5805622be09e2d9025c9d09b62 (patch) | |
| tree | 59066b8c06582022ed4a12843304dafaab2cef87 | |
| parent | 90959a037de19905aee76e24a0028b4e407969ff (diff) | |
| parent | 017058b97d16945f18ac305bde498e425008ca11 (diff) | |
Merge pull request #3344 from xmake-io/config
Improve to configure options and build directory
| -rw-r--r-- | tests/actions/config/.gitignore | 1 | ||||
| -rw-r--r-- | tests/actions/config/test.lua | 32 | ||||
| -rw-r--r-- | xmake/actions/build/main.lua | 4 | ||||
| -rw-r--r-- | xmake/actions/config/configheader.lua | 25 | ||||
| -rw-r--r-- | xmake/actions/config/main.lua | 52 | ||||
| -rw-r--r-- | xmake/actions/config/xmake.lua | 2 | ||||
| -rw-r--r-- | xmake/actions/uninstall/main.lua | 2 | ||||
| -rw-r--r-- | xmake/core/main.lua | 23 | ||||
| -rw-r--r-- | xmake/core/project/config.lua | 42 |
9 files changed, 124 insertions, 59 deletions
diff --git a/tests/actions/config/.gitignore b/tests/actions/config/.gitignore new file mode 100644 index 000000000..9daeafb98 --- /dev/null +++ b/tests/actions/config/.gitignore @@ -0,0 +1 @@ +test diff --git a/tests/actions/config/test.lua b/tests/actions/config/test.lua new file mode 100644 index 000000000..f8823c28c --- /dev/null +++ b/tests/actions/config/test.lua @@ -0,0 +1,32 @@ + +function test_workdir(t) + os.tryrm("test") + os.tryrm("build") + os.tryrm("build2") + os.tryrm(".xmake") + os.exec("xmake create test") + os.exec("xmake config -P test") + os.exec("xmake") + t:require(os.isdir("build")) + t:require(os.isdir(".xmake")) + t:require_not(os.isdir("test/build")) + t:require_not(os.isdir("test/.xmake")) + os.exec("xmake config -o build2") + os.exec("xmake") + t:require(os.isdir("build2")) + os.tryrm("build") + os.tryrm("build2") + os.tryrm(".xmake") + os.cd("test") + os.exec("xmake create -P subtest") + os.cd("subtest") + os.exec("xmake config -P .") + os.exec("xmake") + t:require(os.isdir("build")) + t:require(os.isdir(".xmake")) + t:require_not(os.isdir("../build")) + t:require_not(os.isdir("../.xmake")) + t:require_not(os.isdir("../../build")) + t:require_not(os.isdir("../../.xmake")) +end + diff --git a/xmake/actions/build/main.lua b/xmake/actions/build/main.lua index 8c5ede701..27b2419da 100644 --- a/xmake/actions/build/main.lua +++ b/xmake/actions/build/main.lua @@ -40,7 +40,7 @@ function _do_try_build(configfile, tool, trybuild, trybuild_detected, targetname if configfile and tool and (trybuild or utils.confirm({default = true, description = "${bright}" .. path.filename(configfile) .. "${clear} found, try building it or you can run `${bright}xmake f --trybuild=${clear}` to set buildsystem"})) then if not trybuild then - task.run("config", {target = targetname, trybuild = trybuild_detected}) + task.run("config", {trybuild = trybuild_detected}) end tool.build() return true @@ -136,7 +136,7 @@ function main() else targetname = option.get("target") end - task.run("config", {target = targetname}, {disable_dump = true}) + task.run("config", {}, {disable_dump = true}) -- enter project directory local oldir = os.cd(project.directory()) diff --git a/xmake/actions/config/configheader.lua b/xmake/actions/config/configheader.lua index f57e3efaf..995bf7a1b 100644 --- a/xmake/actions/config/configheader.lua +++ b/xmake/actions/config/configheader.lua @@ -76,36 +76,19 @@ function _make_for_target(target) _g.configfiles[configheader] = file end --- make the configure file for the given target and dependents -function _make_for_target_with_deps(targetname) - local target = project.target(targetname) - if target then - _make_for_target(target) - for _, dep in ipairs(target:get("deps")) do - _make_for_target_with_deps(dep) - end - end -end - -- the main entry function function main() - -- the target name - local targetname = option.get("target") - -- enter project directory local oldir = os.cd(project.directory()) -- make configure for the given target name _g.configfiles = {} _g.configpathes = {} - if targetname then - _make_for_target_with_deps(targetname) - else - -- make configure for all targets - for _, target in pairs(project.targets()) do - _make_for_target(target) - end + + -- make configure for all targets + for _, target in pairs(project.targets()) do + _make_for_target(target) end -- close and update files diff --git a/xmake/actions/config/main.lua b/xmake/actions/config/main.lua index fc14297e5..59bfe2b05 100644 --- a/xmake/actions/config/main.lua +++ b/xmake/actions/config/main.lua @@ -111,15 +111,9 @@ function _check_target(target) end -- check targets -function _check_targets(targetname) +function _check_targets() assert(not project.is_loaded(), "project and targets may have been loaded early!") - if not targetname then - for _, target in pairs(project.targets()) do - _check_target(target) - end - else - local target = project.target(targetname) - assert(target, "unknown target: %s", targetname) + for _, target in pairs(project.targets()) do _check_target(target) end end @@ -180,20 +174,11 @@ function _config_target(target) end -- config targets -function _config_targets(targetname) - if not targetname then - for _, target in ipairs(project.ordertargets()) do - if target:is_enabled() then - _config_target(target) - end - end - else - local target = project.target(targetname) - assert(target, "unknown target: %s", targetname) - for _, dep in ipairs(target:orderdeps()) do - _config_target(dep) +function _config_targets() + for _, target in ipairs(project.ordertargets()) do + if target:is_enabled() then + _config_target(target) end - _config_target(target) end end @@ -316,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()) @@ -332,9 +320,6 @@ force to build in current directory via run `xmake -P .`]], os.projectdir()) options_changed = menuconf_show() end - -- the target name - local targetname = option.get("target") - -- load the project configuration -- -- priority: option > option_cache > global > option_default > config_check > project_check > config_cache @@ -473,7 +458,7 @@ force to build in current directory via run `xmake -P .`]], os.projectdir()) -- check target and ensure to load all targets, @note we must load targets after installing required packages, -- otherwise has_package() will be invalid. - _check_targets(targetname) + _check_targets() -- update the config files generate_configfiles({force = recheck}) @@ -490,7 +475,7 @@ force to build in current directory via run `xmake -P .`]], os.projectdir()) _load_package_rules_for_targets() -- config targets - _config_targets(targetname) + _config_targets() end -- dump config @@ -503,6 +488,19 @@ force to build in current directory via run `xmake -P .`]], os.projectdir()) _export_configs() end + -- we need save it and enable external working mode + -- if we configure the given project directory + -- + -- @see https://github.com/xmake-io/xmake/issues/3342 + -- + local projectdir = option.get("project") + local projectfile = option.get("file") + if projectdir or projectfile then + localcache.set("project", "projectdir", projectdir) + localcache.set("project", "projectfile", projectfile) + localcache.save("project") + end + -- save options and config cache localcache.set("config", "recheck", false) localcache.set("config", "mtimes", project.mtimes()) diff --git a/xmake/actions/config/xmake.lua b/xmake/actions/config/xmake.lua index 50c22206e..861c0531d 100644 --- a/xmake/actions/config/xmake.lua +++ b/xmake/actions/config/xmake.lua @@ -227,8 +227,6 @@ task("config") " - xmake f --trybuild=autoconf --tryconfigs='--enable-shared=no'"}, {'o', "buildir", "kv", "build" , "Set build directory."}, {}, - {nil, "target", "v" , nil , "Configure for the given target." - , values = _target_values}, {category = "Project Configuration"}, _project_menu_options}} diff --git a/xmake/actions/uninstall/main.lua b/xmake/actions/uninstall/main.lua index 3132a3456..8dc0664e6 100644 --- a/xmake/actions/uninstall/main.lua +++ b/xmake/actions/uninstall/main.lua @@ -31,7 +31,7 @@ function main() -- config it first local targetname = option.get("target") - task.run("config", {target = targetname, require = "n", verbose = false}) + task.run("config", {require = "n", verbose = false}) -- attempt to uninstall directly try diff --git a/xmake/core/main.lua b/xmake/core/main.lua index 4e061f45d..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 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 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 bf68e93a8..57b016e13 100644 --- a/xmake/core/project/config.lua +++ b/xmake/core/project/config.lua @@ -29,6 +29,28 @@ local table = require("base/table") local utils = require("base/utils") local option = require("base/option") +-- always use workingdir? +-- +-- If the -P/-F parameter is specified, we use workingdir as the configuration root +-- +-- But we cannot call `option.get("project")`, because the menu script +-- will also fetch the configdir, but at this point, +-- the option has not yet finished parsing. +function config._use_workingdir() + local use_workingdir = config._USE_WORKINGDIR + if use_workingdir == nil then + for _, arg in ipairs(xmake._ARGV) do + if arg == "-P" or arg == "-F" or + arg:startswith("--project=") or arg:startswith("--file=") then + use_workingdir = true + end + end + use_workingdir = use_workingdir or false + config._USE_WORKINGDIR = use_workingdir + end + return use_workingdir +end + -- get the current given configuration function config.get(name) local value = nil @@ -96,8 +118,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 config._use_workingdir() 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,8 +157,14 @@ end function config.directory() if config._DIRECTORY == nil then local rootdir = os.getenv("XMAKE_CONFIGDIR") - if not rootdir and 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 config._use_workingdir() 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 |
