summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-03-14 23:40:53 +0800
committerruki <[email protected]>2024-03-14 23:40:53 +0800
commitfb0a5cb9f8beea482f7240c537ceb2a3e6d63edd (patch)
tree72226e382e1ca871b799674ef43d484822b39c1f
parente971dacae2e572575b5ef11d37cbfde2785b4daf (diff)
improve headerfiles
-rw-r--r--xmake/core/base/private/match_copyfiles.lua119
-rw-r--r--xmake/core/project/target.lua99
2 files changed, 85 insertions, 133 deletions
diff --git a/xmake/core/base/private/match_copyfiles.lua b/xmake/core/base/private/match_copyfiles.lua
index 06a773fd2..4dc751b00 100644
--- a/xmake/core/base/private/match_copyfiles.lua
+++ b/xmake/core/base/private/match_copyfiles.lua
@@ -31,11 +31,14 @@ local os = require("base/os")
-- add_configfiles
-- add_installfiles
-- add_extrafiles
-function match_copyfiles(instance, filetype, outputdir, pathfilter)
+function match_copyfiles(instance, filetype, outputdir, opt)
+ opt = opt or {}
- -- no copied files?
- local copyfiles = instance:get(filetype)
- if not copyfiles then return end
+ -- get copyfiles?
+ local copyfiles = opt.copyfiles or instance:get(filetype)
+ if not copyfiles then
+ return
+ end
-- get the extra information
local extrainfo = table.wrap(instance:extraconf(filetype))
@@ -44,8 +47,18 @@ function match_copyfiles(instance, filetype, outputdir, pathfilter)
local srcfiles = {}
local dstfiles = {}
local fileinfos = {}
+ local srcfiles_removed = {}
+ local removed_count = 0
for _, copyfile in ipairs(table.wrap(copyfiles)) do
+ -- mark as removed files?
+ local removed = false
+ local prefix = "__remove_"
+ if copyfile:startswith(prefix) then
+ copyfile = copyfile:sub(#prefix + 1)
+ removed = true
+ end
+
-- get the root directory
local rootdir, count = copyfile:gsub("|.*$", ""):gsub("%(.*%)$", "")
if count == 0 then
@@ -62,58 +75,82 @@ function match_copyfiles(instance, filetype, outputdir, pathfilter)
-- get the source paths
srcpaths = os.match(srcpaths)
if srcpaths and #srcpaths > 0 then
+ if removed then
+ removed_count = removed_count + #srcpaths
+ table.join2(srcfiles_removed, srcpaths)
+ else
- -- add the source copied files
- table.join2(srcfiles, srcpaths)
+ -- add the source copied files
+ table.join2(srcfiles, srcpaths)
- -- the copied directory exists?
- if outputdir then
+ -- the copied directory exists?
+ if outputdir then
- -- get the file info
- local fileinfo = extrainfo[copyfile] or {}
+ -- get the file info
+ local fileinfo = extrainfo[copyfile] or {}
- -- get the prefix directory
- local prefixdir = fileinfo.prefixdir
- if fileinfo.rootdir then
- rootdir = fileinfo.rootdir
- end
+ -- get the prefix directory
+ local prefixdir = fileinfo.prefixdir
+ if fileinfo.rootdir then
+ rootdir = fileinfo.rootdir
+ end
- -- add the destinate copied files
- for _, srcpath in ipairs(srcpaths) do
+ -- add the destinate copied files
+ for _, srcpath in ipairs(srcpaths) do
- -- get the destinate directory
- local dstdir = outputdir
- if prefixdir then
- dstdir = path.join(dstdir, prefixdir)
- end
+ -- get the destinate directory
+ local dstdir = outputdir
+ if prefixdir then
+ dstdir = path.join(dstdir, prefixdir)
+ end
- -- the destinate file
- local dstfile = nil
- if rootdir then
- dstfile = path.absolute(path.relative(srcpath, rootdir), dstdir)
- else
- dstfile = path.join(dstdir, path.filename(srcpath))
- end
- assert(dstfile)
+ -- the destinate file
+ local dstfile = nil
+ if rootdir then
+ dstfile = path.absolute(path.relative(srcpath, rootdir), dstdir)
+ else
+ dstfile = path.join(dstdir, path.filename(srcpath))
+ end
+ assert(dstfile)
- -- modify filename
- if fileinfo.filename then
- dstfile = path.join(path.directory(dstfile), fileinfo.filename)
- end
+ -- modify filename
+ if fileinfo.filename then
+ dstfile = path.join(path.directory(dstfile), fileinfo.filename)
+ end
- -- filter the destinate file path
- if pathfilter then
- dstfile = pathfilter(dstfile, fileinfo)
- end
+ -- filter the destinate file path
+ if opt.pathfilter then
+ dstfile = opt.pathfilter(dstfile, fileinfo)
+ end
- -- add it
- table.insert(dstfiles, dstfile)
- table.insert(fileinfos, fileinfo)
+ -- add it
+ table.insert(dstfiles, dstfile)
+ table.insert(fileinfos, fileinfo)
+ end
end
end
end
end
end
+
+ -- remove all srcfiles which need be removed
+ if removed_count > 0 then
+ table.remove_if(srcfiles, function (i, srcfile)
+ for _, removed_file in ipairs(srcfiles_removed) do
+ local pattern = path.translate((removed_file:gsub("|.*$", "")))
+ if pattern:sub(1, 2):find('%.[/\\]') then
+ pattern = pattern:sub(3)
+ end
+ pattern = path.pattern(pattern)
+ if srcfile:match(pattern) then
+ if i <= #dstfiles then
+ table.remove(dstfiles, i)
+ end
+ return true
+ end
+ end
+ end)
+ end
return srcfiles, dstfiles, fileinfos
end
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index 228c046f9..12bf3ed27 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -1937,125 +1937,40 @@ end
-- get the header files
function _instance:headerfiles(outputdir, opt)
-
- -- get header files?
opt = opt or {}
- local headers = self:get("headerfiles")
+ local headerfiles = self:get("headerfiles")
-- add_headerfiles("src/*.h", {install = false})
-- @see https://github.com/xmake-io/xmake/issues/2577
if opt.installonly then
local installfiles = {}
- for _, headerfile in ipairs(table.wrap(headers)) do
+ for _, headerfile in ipairs(table.wrap(headerfiles)) do
if self:extraconf("headerfiles", headerfile, "install") ~= false then
table.insert(installfiles, headerfile)
end
end
- headers = installfiles
+ headerfiles = installfiles
end
- if not headers then
+ if not headerfiles then
return
end
- -- get the installed header directory
local headerdir = outputdir
if not headerdir then
if self:installdir() then
headerdir = path.join(self:installdir(), "include")
end
end
-
- -- get the extra information
- local extrainfo = table.wrap(self:extraconf("headerfiles"))
-
- -- get the source paths and destinate paths
- local srcheaders = {}
- local dstheaders = {}
- local srcheaders_removed = {}
- local removed_count = 0
- for _, header in ipairs(table.wrap(headers)) do
-
- -- mark as removed files?
- local removed = false
- local prefix = "__remove_"
- if header:startswith(prefix) then
- header = header:sub(#prefix + 1)
- removed = true
- end
-
- -- get the root directory
- local rootdir, count = header:gsub("|.*$", ""):gsub("%(.*%)$", "")
- if count == 0 then
- rootdir = nil
- end
- if rootdir and rootdir:trim() == "" then
- rootdir = "."
- end
-
- -- remove '(' and ')' first
- local srcpaths = header:gsub("[%(%)]", "")
- if srcpaths then
-
- -- get the source paths
- srcpaths = os.match(srcpaths)
- if srcpaths then
- if removed then
- removed_count = removed_count + #srcpaths
- table.join2(srcheaders_removed, srcpaths)
- else
- -- add the source headers
- table.join2(srcheaders, srcpaths)
-
- -- get the destinate directories if the install directory exists
- if headerdir then
- local prefixdir = (extrainfo[header] or {}).prefixdir
- for _, srcpath in ipairs(srcpaths) do
- local dstdir = headerdir
- if prefixdir then
- dstdir = path.join(dstdir, prefixdir)
- end
- local dstheader = nil
- if rootdir then
- dstheader = path.absolute(path.relative(srcpath, rootdir), dstdir)
- else
- dstheader = path.join(dstdir, path.filename(srcpath))
- end
- table.insert(dstheaders, dstheader)
- end
- end
- end
- end
- end
- end
-
- -- remove all header files which need be removed
- if removed_count > 0 then
- table.remove_if(srcheaders, function (i, srcheader)
- for _, removed_file in ipairs(srcheaders_removed) do
- local pattern = path.translate((removed_file:gsub("|.*$", "")))
- if pattern:sub(1, 2):find('%.[/\\]') then
- pattern = pattern:sub(3)
- end
- pattern = path.pattern(pattern)
- if srcheader:match(pattern) then
- if i <= #dstheaders then
- table.remove(dstheaders, i)
- end
- return true
- end
- end
- end)
- end
- return srcheaders, dstheaders
+ return match_copyfiles(self, "headerfiles", headerdir, {copyfiles = headerfiles})
end
-- get the configuration files
function _instance:configfiles(outputdir)
- return match_copyfiles(self, "configfiles", outputdir or self:configdir(), function (dstpath, fileinfo)
+ return match_copyfiles(self, "configfiles", outputdir or self:configdir(), {pathfilter = function (dstpath, fileinfo)
if dstpath:endswith(".in") then
dstpath = dstpath:sub(1, -4)
end
return dstpath
- end)
+ end})
end
-- get the install files