From fa824cccb3765035444015e0dc979cbd4fda0348 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 15 Mar 2024 00:49:39 +0800 Subject: [PATCH 1/3] improve fileconfig --- xmake/core/project/target.lua | 62 ++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 4d36a1abd5a..3e97d85d35c 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -233,9 +233,9 @@ 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:_memcache():set("filesconfig", nil) self:_update_filerules() elseif name == "deps" then self._DEPS = nil @@ -1686,46 +1686,55 @@ 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" + local filesconfig = self:_memcache():get2("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:_memcache():set2("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 {} + local filetype = opt.filetype or "files" + local filesconfig = self:_memcache():get2("filesconfig", filetype) + if not filesconfig then + filesconfig = {} + self:_memcache():set2("filesconfig", 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 {} + local filetype = opt.filetype or "files" + local filesconfig = self:_memcache():get2("filesconfig", filetype) + if not filesconfig then + filesconfig = {} + self:_memcache():set2("filesconfig", filetype, filesconfig) + end + local fileconfig = filesconfig[sourcefile] if fileconfig then for k, v in pairs(info) do @@ -1752,7 +1761,6 @@ function _instance:fileconfig_add(sourcefile, info) else filesconfig[sourcefile] = info end - self._FILESCONFIG = filesconfig end -- get the source files From 033d921aa7c7e9c54b05f62943359dee3a0eee04 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 15 Mar 2024 22:36:48 +0800 Subject: [PATCH 2/3] fix fileconfigs --- xmake/core/project/target.lua | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 3e97d85d35c..476db548e58 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -235,13 +235,15 @@ function _instance:_invalidate(name) self._SOURCEFILES = nil self._OBJECTFILES = nil self._SOURCEBATCHES = nil - self:_memcache():set("filesconfig", nil) self:_update_filerules() elseif name == "deps" then self._DEPS = nil self._ORDERDEPS = nil self._INHERITDEPS = nil end + if self._FILESCONFIG then + self._FILESCONFIG[name] = nil + end end -- build deps @@ -1689,7 +1691,19 @@ end function _instance:fileconfig(sourcefile, opt) opt = opt or {} local filetype = opt.filetype or "files" - local filesconfig = self:_memcache():get2("filesconfig", filetype) + + -- 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(filetype))) do @@ -1708,7 +1722,7 @@ function _instance:fileconfig(sourcefile, opt) end end end - self:_memcache():set2("filesconfig", filetype, filesconfig) + self._FILESCONFIG[filetype] = filesconfig end return filesconfig[sourcefile] end @@ -1716,11 +1730,12 @@ end -- set the config info to the given source file 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:_memcache():get2("filesconfig", filetype) + local filesconfig = self._FILESCONFIG_USER[filetype] if not filesconfig then filesconfig = {} - self:_memcache():set2("filesconfig", filetype, filesconfig) + self._FILESCONFIG_USER[filetype] = filesconfig end filesconfig[sourcefile] = info end @@ -1728,14 +1743,20 @@ end -- add the config info to the given source file 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:_memcache():get2("filesconfig", filetype) + local filesconfig = self._FILESCONFIG_USER[filetype] if not filesconfig then filesconfig = {} - self:_memcache():set2("filesconfig", filetype, filesconfig) + self._FILESCONFIG_USER[filetype] = filesconfig end + -- we fetch orignal configs first if no user configs local fileconfig = filesconfig[sourcefile] + if not filesconfig 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 From d6939f248cfe6297247857ced8f7a5fdbcbae8f0 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 15 Mar 2024 22:43:35 +0800 Subject: [PATCH 3/3] fix fileconfig_add --- xmake/core/project/target.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 476db548e58..2c2fafdc348 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -1753,7 +1753,7 @@ function _instance:fileconfig_add(sourcefile, info, opt) -- we fetch orignal configs first if no user configs local fileconfig = filesconfig[sourcefile] - if not filesconfig then + if not fileconfig then fileconfig = table.clone(self:fileconfig(sourcefile, opt)) filesconfig[sourcefile] = fileconfig end