Skip to content

Commit

Permalink
Merge pull request #3344 from xmake-io/config
Browse files Browse the repository at this point in the history
Improve to configure options and build directory
  • Loading branch information
waruqi authored Feb 8, 2023
2 parents 90959a0 + 017058b commit 330d81f
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 59 deletions.
1 change: 1 addition & 0 deletions tests/actions/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
32 changes: 32 additions & 0 deletions tests/actions/config/test.lua
Original file line number Diff line number Diff line change
@@ -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

4 changes: 2 additions & 2 deletions xmake/actions/build/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
25 changes: 4 additions & 21 deletions xmake/actions/config/configheader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 25 additions & 27 deletions xmake/actions/config/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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})
Expand All @@ -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
Expand All @@ -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())
Expand Down
2 changes: 0 additions & 2 deletions xmake/actions/config/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

Expand Down
2 changes: 1 addition & 1 deletion xmake/actions/uninstall/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions xmake/core/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand All @@ -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
Expand Down
42 changes: 38 additions & 4 deletions xmake/core/project/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 330d81f

Please sign in to comment.