Skip to content

Commit

Permalink
Merge pull request #4836 from xmake-io/fileconfig
Browse files Browse the repository at this point in the history
improve fileconfig
  • Loading branch information
waruqi authored Mar 15, 2024
2 parents 80af97d + d6939f2 commit 2c80879
Showing 1 changed file with 56 additions and 27 deletions.
83 changes: 56 additions & 27 deletions xmake/core/project/target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ function _instance:_invalidate(name)
-- we need to flush the source files cache if target/files are modified, e.g. `target:add("files", "xxx.c")`
if name == "files" then
self._SOURCEFILES = nil
self._FILESCONFIG = nil
self._OBJECTFILES = nil
self._SOURCEBATCHES = nil
self:_update_filerules()
Expand All @@ -242,6 +241,9 @@ function _instance:_invalidate(name)
self._ORDERDEPS = nil
self._INHERITDEPS = nil
end
if self._FILESCONFIG then
self._FILESCONFIG[name] = nil
end
end

-- build deps
Expand Down Expand Up @@ -1686,47 +1688,75 @@ function _instance:filerules(sourcefile)
end

-- get the config info of the given source file
function _instance:fileconfig(sourcefile)
local filesconfig = self._FILESCONFIG
function _instance:fileconfig(sourcefile, opt)
opt = opt or {}
local filetype = opt.filetype or "files"

-- get configs from user, e.g. target:fileconfig_set/add
-- it has contained all original configs
if self._FILESCONFIG_USER then
local filesconfig = self._FILESCONFIG_USER[filetype]
if filesconfig and filesconfig[sourcefile] then
return filesconfig[sourcefile]
end
end

-- get orignal configs from `add_xxxfiles()`
self._FILESCONFIG = self._FILESCONFIG or {}
local filesconfig = self._FILESCONFIG[filetype]
if not filesconfig then
filesconfig = {}
for filepath, fileconfig in pairs(table.wrap(self:extraconf("files"))) do

-- match source files
for filepath, fileconfig in pairs(table.wrap(self:extraconf(filetype))) do
local results = os.match(filepath)
if #results == 0 and not fileconfig.always_added then
local sourceinfo = self:sourceinfo("files", filepath) or {}
utils.warning("%s:%d${clear}: cannot match add_files(\"%s\") in %s(%s)", sourceinfo.file or "", sourceinfo.line or -1, filepath, self:type(), self:name())
end

-- process source files
for _, file in ipairs(results) do
if path.is_absolute(file) then
file = path.relative(file, os.projectdir())
if #results > 0 then
for _, file in ipairs(results) do
if path.is_absolute(file) then
file = path.relative(file, os.projectdir())
end
filesconfig[file] = fileconfig
end
else
-- we also need support always_added, @see https://github.com/xmake-io/xmake/issues/1634
if fileconfig.always_added then
filesconfig[filepath] = fileconfig
end
filesconfig[file] = fileconfig
end
-- we also need support always_added, @see https://github.com/xmake-io/xmake/issues/1634
if #results == 0 and fileconfig.always_added then
filesconfig[filepath] = fileconfig
end
end
self._FILESCONFIG = filesconfig
self._FILESCONFIG[filetype] = filesconfig
end
return filesconfig[sourcefile]
end

-- set the config info to the given source file
function _instance:fileconfig_set(sourcefile, info)
local filesconfig = self._FILESCONFIG or {}
function _instance:fileconfig_set(sourcefile, info, opt)
opt = opt or {}
self._FILESCONFIG_USER = self._FILESCONFIG_USER or {}
local filetype = opt.filetype or "files"
local filesconfig = self._FILESCONFIG_USER[filetype]
if not filesconfig then
filesconfig = {}
self._FILESCONFIG_USER[filetype] = filesconfig
end
filesconfig[sourcefile] = info
self._FILESCONFIG = filesconfig
end

-- add the config info to the given source file
function _instance:fileconfig_add(sourcefile, info)
local filesconfig = self._FILESCONFIG or {}
function _instance:fileconfig_add(sourcefile, info, opt)
opt = opt or {}
self._FILESCONFIG_USER = self._FILESCONFIG_USER or {}
local filetype = opt.filetype or "files"
local filesconfig = self._FILESCONFIG_USER[filetype]
if not filesconfig then
filesconfig = {}
self._FILESCONFIG_USER[filetype] = filesconfig
end

-- we fetch orignal configs first if no user configs
local fileconfig = filesconfig[sourcefile]
if not fileconfig then
fileconfig = table.clone(self:fileconfig(sourcefile, opt))
filesconfig[sourcefile] = fileconfig
end
if fileconfig then
for k, v in pairs(info) do
if k == "force" then
Expand All @@ -1752,7 +1782,6 @@ function _instance:fileconfig_add(sourcefile, info)
else
filesconfig[sourcefile] = info
end
self._FILESCONFIG = filesconfig
end

-- get the source files
Expand Down

0 comments on commit 2c80879

Please sign in to comment.