summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-19 23:41:38 +0800
committerGitHub <[email protected]>2022-04-19 23:41:38 +0800
commit8cf2fc8a4af32248837c7a48e7a0028b65f02979 (patch)
treed2b76adc721208b8b66fff6ca7cbb6f593a9672f
parent77e9c353d2d6c4236465ba343191e162445e884a (diff)
parent93ea7fcca2680a7a8cf94b7147d6e3b04c77eef1 (diff)
Merge pull request #2283 from xmake-io/filegroup
Add add_filegroups to support source file group for vs/vsxmake/cmakelists generator
-rw-r--r--CHANGELOG.md2
-rw-r--r--xmake/core/project/target.lua1
-rw-r--r--xmake/plugins/project/cmake/cmakelists.lua24
-rw-r--r--xmake/plugins/project/vstudio/impl/vs201x.lua4
-rw-r--r--xmake/plugins/project/vstudio/impl/vs201x_vcxproj_filters.lua64
-rw-r--r--xmake/plugins/project/vsxmake/getinfo.lua59
6 files changed, 142 insertions, 12 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 34955a6e9..8f8518bc7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@
* [#2227](https://github.com/xmake-io/xmake/issues/2227): Improve cargo package with Cargo.toml file
* Improve `add_requires` to support git commit as version
* [#622](https://github.com/xmake-io/xmake/issues/622): Support remote compilation
+* [#2282](https://github.com/xmake-io/xmake/issues/2282): Add `add_filegroups` to support file group for vs/vsxmake/cmake generator
### Changes
@@ -1250,6 +1251,7 @@
* [#2227](https://github.com/xmake-io/xmake/issues/2227): 改进 cargo 包,支持指定 Cargo.toml 文件
* 改进 `add_requires` 支持 git command 作为版本
* [#622](https://github.com/xmake-io/xmake/issues/622): 支持远程编译
+* [#2282](https://github.com/xmake-io/xmake/issues/2282): 添加 `add_filegroups` 接口为 vs/vsxmake/cmake generator 增加文件组支持
### 改进
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index 122082129..88ba324f4 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -2039,6 +2039,7 @@ function target.apis()
, "target.set_strip"
, "target.set_rules"
, "target.set_group"
+ , "target.add_filegroups"
, "target.set_version"
, "target.set_license"
, "target.set_enabled"
diff --git a/xmake/plugins/project/cmake/cmakelists.lua b/xmake/plugins/project/cmake/cmakelists.lua
index dba4e0c0e..ee056c5b4 100644
--- a/xmake/plugins/project/cmake/cmakelists.lua
+++ b/xmake/plugins/project/cmake/cmakelists.lua
@@ -215,6 +215,27 @@ function _add_target_sources(cmakelists, target)
end
end
+-- add target source groups
+function _add_target_source_groups(cmakelists, target)
+ --[[TODO
+ local filegroups = target:get("filegroups")
+ for _, filegroup in ipairs(filegroups) do
+ local files = target:extraconf("filegroups", filegroup, "files") or "**"
+ local mode = target:extraconf("filegroups", filegroup, "mode")
+ if mode and mode == "plain" then
+ mode = ""
+ else
+ mode = "TREE "
+ end
+ local rootdir = target:extraconf("filegroups", filegroup, "rootdir")
+ assert(rootdir, "please set root directory, e.g. add_filegroups(%s, {rootdir = 'xxx'})", filegroup)
+ for _, filepattern in ipairs(files) do
+ cmakelists:print("source_group(%s%s %s REGULAR_EXPRESSION %s)",
+ mode, _get_unix_path(filegroup), _get_unix_path(rootdir), _get_unix_path(filepattern))
+ end
+ end]]
+end
+
-- add target precompilied header
function _add_target_precompiled_header(cmakelists, target)
local precompiled_header = target:get("pcheader") or target:get("pcxxheader")
@@ -782,6 +803,9 @@ function _add_target(cmakelists, target)
-- add target sources
_add_target_sources(cmakelists, target)
+ -- add target source groups
+ _add_target_source_groups(cmakelists, target)
+
-- end
cmakelists:print("")
end
diff --git a/xmake/plugins/project/vstudio/impl/vs201x.lua b/xmake/plugins/project/vstudio/impl/vs201x.lua
index 2560faa77..29d4b3f53 100644
--- a/xmake/plugins/project/vstudio/impl/vs201x.lua
+++ b/xmake/plugins/project/vstudio/impl/vs201x.lua
@@ -582,6 +582,10 @@ function make(outputdir, vsinfo)
_target.sourcefiles = table.unique(table.join(_target.sourcefiles or {}, (target:sourcefiles())))
_target.headerfiles = table.unique(table.join(_target.headerfiles or {}, (target:headerfiles())))
+ -- save file groups
+ _target.filegroups = target:get("filegroups")
+ _target.filegroups_extraconf = target:extraconf("filegroups")
+
-- make target headers
_make_targetheaders(mode, arch, target, mode_idx == #vsinfo.modes and arch_idx == 2)
end
diff --git a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj_filters.lua b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj_filters.lua
index fb47926ed..b8dc48534 100644
--- a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj_filters.lua
+++ b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj_filters.lua
@@ -51,18 +51,64 @@ function _make_tailer(filtersfile, vsinfo)
filtersfile:leave("</Project>")
end
+-- strip dot directories, e.g. ..\..\.. => ..
+-- @see https://github.com/xmake-io/xmake/issues/2039
+function _strip_dotdirs(dir)
+ local count
+ dir, count = dir:gsub("%.%.[\\/]%.%.", "..")
+ if count > 0 then
+ dir = _strip_dotdirs(dir)
+ end
+ return dir
+end
+
-- make filter
function _make_filter(filepath, target, vcxprojdir)
-
- -- make filter
- local filter = path.relative(path.absolute(path.directory(filepath)), target.scriptdir or vcxprojdir)
-
- -- is '.'? no filter
- if filter and filter == '.' then
- filter = nil
+ local filter
+ local filegroups = target.filegroups
+ if filegroups then
+ -- @see https://github.com/xmake-io/xmake/issues/2282
+ filepath = path.absolute(filepath)
+ local scriptdir = target.scriptdir
+ local filegroups_extraconf = target.filegroups_extraconf or {}
+ for _, filegroup in ipairs(filegroups) do
+ local extraconf = filegroups_extraconf[filegroup] or {}
+ local rootdir = extraconf.rootdir
+ assert(rootdir, "please set root directory, e.g. add_filegroups(%s, {rootdir = 'xxx'})", filegroup)
+ if not path.is_absolute(rootdir) then
+ rootdir = path.absolute(rootdir, scriptdir)
+ end
+ local fileitem = path.relative(filepath, rootdir)
+ local files = extraconf.files or "**"
+ local mode = extraconf.mode
+ for _, filepattern in ipairs(files) do
+ filepattern = path.pattern(path.translate(filepattern))
+ if fileitem:match(filepattern) then
+ if mode == "plain" then
+ filter = path.translate(filegroup)
+ else
+ -- file tree mode (default)
+ filter = path.join(filegroup, path.directory(fileitem))
+ end
+ if filter and filter == '.' then
+ filter = nil
+ end
+ break
+ end
+ end
+ end
+ end
+ if not filter then
+ -- use the default filter rule
+ filter = path.relative(path.absolute(path.directory(filepath)), target.scriptdir or vcxprojdir)
+ -- @see https://github.com/xmake-io/xmake/issues/2039
+ if filter then
+ filter = _strip_dotdirs(filter)
+ end
+ if filter and filter == '.' then
+ filter = nil
+ end
end
-
- -- ok?
return filter
end
diff --git a/xmake/plugins/project/vsxmake/getinfo.lua b/xmake/plugins/project/vsxmake/getinfo.lua
index 57a7d9289..a6f5e1aaa 100644
--- a/xmake/plugins/project/vsxmake/getinfo.lua
+++ b/xmake/plugins/project/vsxmake/getinfo.lua
@@ -340,6 +340,57 @@ function _config_targets()
end
end
+-- make filter
+function _make_filter(filepath, target, vcxprojdir)
+ local filter
+ local filegroups = target.filegroups
+ if filegroups then
+ -- @see https://github.com/xmake-io/xmake/issues/2282
+ filepath = path.absolute(filepath)
+ local scriptdir = target.absscriptdir
+ local filegroups_extraconf = target.filegroups_extraconf or {}
+ for _, filegroup in ipairs(filegroups) do
+ local extraconf = filegroups_extraconf[filegroup] or {}
+ local rootdir = extraconf.rootdir
+ assert(rootdir, "please set root directory, e.g. add_filegroups(%s, {rootdir = 'xxx'})", filegroup)
+ if not path.is_absolute(rootdir) then
+ rootdir = path.absolute(rootdir, scriptdir)
+ end
+ local fileitem = path.relative(filepath, rootdir)
+ local files = extraconf.files or "**"
+ local mode = extraconf.mode
+ for _, filepattern in ipairs(files) do
+ filepattern = path.pattern(path.translate(filepattern))
+ if fileitem:match(filepattern) then
+ if mode == "plain" then
+ filter = path.translate(filegroup)
+ else
+ -- file tree mode (default)
+ filter = path.join(filegroup, path.directory(fileitem))
+ end
+ if filter and filter == '.' then
+ filter = nil
+ end
+ break
+ end
+ end
+ end
+ end
+ if not filter then
+ -- use the default filter rule
+ filter = path.relative(path.absolute(path.directory(filepath)), vcxprojdir)
+ -- @see https://github.com/xmake-io/xmake/issues/2039
+ if filter then
+ filter = _strip_dotdirs(filter)
+ end
+ if filter and filter == '.' then
+ filter = nil
+ end
+ end
+ return filter
+end
+
+
-- make vstudio project
function main(outputdir, vsinfo)
@@ -458,6 +509,10 @@ function main(outputdir, vsinfo)
_target.sourcefiles = table.unique(table.join(_target.sourcefiles or {}, (target:sourcefiles())))
_target.headerfiles = table.unique(table.join(_target.headerfiles or {}, (target:headerfiles())))
+ -- save file groups
+ _target.filegroups = target:get("filegroups")
+ _target.filegroups_extraconf = target:extraconf("filegroups")
+
-- save deps
_target.deps = table.unique(table.join(_target.deps or {}, table.keys(target:deps()), nil))
end
@@ -472,10 +527,8 @@ function main(outputdir, vsinfo)
target.sourcefiles = table.imap(target.sourcefiles, function(_, v) return path.relative(v, projectdir) end)
target.headerfiles = table.imap(target.headerfiles, function(_, v) return path.relative(v, projectdir) end)
for _, f in ipairs(table.join(target.sourcefiles, target.headerfiles)) do
- local dir = path.directory(path.relative(f, root))
+ local dir = _make_filter(f, target, root)
local escaped_f = _escape(f)
- -- @see https://github.com/xmake-io/xmake/issues/2039
- dir = _strip_dotdirs(dir)
target._paths[f] =
{
-- @see https://github.com/xmake-io/xmake/issues/2077