Skip to content

Commit

Permalink
Merge pull request #1174 from xmake-io/group
Browse files Browse the repository at this point in the history
Add group for vs/vsxmake
  • Loading branch information
waruqi authored Dec 29, 2020
2 parents 1e5aeb9 + 9c28990 commit fb4c70e
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 35 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [#1079](https://github.com/xmake-io/xmake/issues/1079): Add autoupdate plugin rule for vsxmake, `add_rules("plugin.vsxmake.autoupdate")`
* Add `xmake f --vs_runtime=MT` and `set_runtimes("MT")` to set vs runtime for targets and packages
* [#1032](https://github.com/xmake-io/xmake/issues/1032): Support to enum registry keys and values
* [#1026](https://github.com/xmake-io/xmake/issues/1026): Support group for vs/vsxmake project

### Change

Expand Down Expand Up @@ -898,6 +899,7 @@
* [#1079](https://github.com/xmake-io/xmake/issues/1079): 为 vsxmake 插件添加工程自动更新插件,`add_rules("plugin.vsxmake.autoupdate")`
* 添加 `xmake f --vs_runtime=MT``set_runtimes("MT")` 去更方便的对 target 和 package 进行设置
* [#1032](https://github.com/xmake-io/xmake/issues/1032): 支持枚举注册表 keys 和 values
* [#1026](https://github.com/xmake-io/xmake/issues/1026): 支持对 vs/vsmake 工程增加分组设置

### 改进

Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/project/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function test_vsxmake(t)
{
function ()
print("--- sln file ---")
io.cat(projname .. "_" .. vstype .. ".sln")
io.cat(projname .. ".sln")
print("--- vcx file ---")
io.cat(projname .. "/" .. projname .. ".vcxproj")
print("--- filter file ---")
Expand Down
8 changes: 8 additions & 0 deletions tests/projects/other/group/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/

# MacOS Cache
.DS_Store


9 changes: 9 additions & 0 deletions tests/projects/other/group/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
cout << "hello world!" << endl;
return 0;
}
29 changes: 29 additions & 0 deletions tests/projects/other/group/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
add_rules("mode.debug", "mode.release")

target("test1")
set_kind("binary")
add_files("src/*.cpp")
set_group("group1")

target("test2")
set_kind("binary")
add_files("src/*.cpp")
set_group("group1")

target("test3")
set_kind("binary")
add_files("src/*.cpp")
set_group("group1/group2")

target("test4")
set_kind("binary")
add_files("src/*.cpp")
set_group("group3/group4")

target("test5")
set_kind("binary")
add_files("src/*.cpp")

target("test6")
set_kind("binary")
add_files("src/*.cpp")
1 change: 1 addition & 0 deletions xmake/core/project/target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,7 @@ function target.apis()
, "target.set_arch"
, "target.set_strip"
, "target.set_rules"
, "target.set_group"
, "target.set_version"
, "target.set_license"
, "target.set_enabled"
Expand Down
49 changes: 40 additions & 9 deletions xmake/plugins/project/vstudio/impl/vs201x_solution.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,33 @@ end
-- make projects
function _make_projects(slnfile, vsinfo)

-- the vstudio tool uuid for vc project
local vctool = "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942"

-- make all targets
local groups = {}
local vctool = "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942"
for targetname, target in pairs(project.targets()) do
if not target:isphony() then

-- enter project
slnfile:enter("Project(\"{%s}\") = \"%s\", \"%s\\%s.vcxproj\", \"{%s}\"", vctool, targetname, targetname, targetname, hash.uuid4(targetname))

-- add dependences
for _, dep in ipairs(target:get("deps")) do
slnfile:enter("ProjectSection(ProjectDependencies) = postProject")
slnfile:print("{%s} = {%s}", hash.uuid4(dep), hash.uuid4(dep))
slnfile:leave("EndProjectSection")
end

-- leave project
slnfile:leave("EndProject")
local group_path = target:get("group")
if group_path then
for _, group_name in ipairs(path.split(group_path)) do
groups[group_name] = hash.uuid4(group_name)
end
end
end
end

-- make all groups
local project_group_uuid = "2150E333-8FDC-42A3-9474-1A3956D46DE8"
for group_name, group_uuid in pairs(groups) do
slnfile:enter("Project(\"{%s}\") = \"%s\", \"%s\", \"{%s}\"", project_group_uuid, group_name, group_name, group_uuid)
slnfile:leave("EndProject")
end
end

-- make global
Expand Down Expand Up @@ -98,6 +104,31 @@ function _make_global(slnfile, vsinfo)
slnfile:print("HideSolutionNode = FALSE")
slnfile:leave("EndGlobalSection")

-- add project groups
slnfile:enter("GlobalSection(NestedProjects) = preSolution")
local subgroups = {}
for targetname, target in pairs(project.targets()) do
if not target:isphony() then
local group_path = target:get("group")
if group_path then
-- target -> group
local group_name = path.filename(group_path)
slnfile:print("{%s} = {%s}", hash.uuid4(targetname), hash.uuid4(group_name))
-- group -> group -> ...
local group_names = path.split(group_path)
for idx, group_name in ipairs(group_names) do
local key = group_name .. (group_name_sub or "")
local group_name_sub = group_names[idx + 1]
if group_name_sub and not subgroups[key] then
slnfile:print("{%s} = {%s}", hash.uuid4(group_name_sub), hash.uuid4(group_name))
subgroups[key] = true
end
end
end
end
end
slnfile:leave("EndGlobalSection")

-- leave global
slnfile:leave("EndGlobal")
end
Expand Down
60 changes: 46 additions & 14 deletions xmake/plugins/project/vsxmake/getinfo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,31 @@ function _make_vsinfo_archs()
return vsinfo_archs
end

function _make_vsinfo_groups()
local groups = {}
local group_deps = {}
for targetname, target in pairs(project.targets()) do
if not target:isphony() then
local group_path = target:get("group")
if group_path then
local group_name = path.filename(group_path)
local group_names = path.split(group_path)
for idx, name in ipairs(group_names) do
local group = groups["group." .. name] or {}
group.group = name
group.group_id = hash.uuid4(name)
if idx > 1 then
group_deps["group_dep." .. name] = {current_id = group.group_id, parent_id = hash.uuid4(group_names[idx - 1])}
end
groups["group." .. name] = group
end
group_deps["group_dep.target." .. targetname] = {current_id = hash.uuid4(targetname), parent_id = groups["group." .. group_name].group_id}
end
end
end
return groups, group_deps
end

-- make vstudio project
function main(outputdir, vsinfo)

Expand All @@ -290,16 +315,24 @@ function main(outputdir, vsinfo)

-- init modes
vsinfo.modes = _make_vsinfo_modes()

-- init archs
vsinfo.archs = _make_vsinfo_archs()

-- init groups
local groups, group_deps = _make_vsinfo_groups()
vsinfo.groups = table.keys(groups)
vsinfo.group_deps = table.keys(group_deps)
vsinfo._groups = groups
vsinfo._group_deps = group_deps

-- load targets
local targets = {}
vsinfo._sub2 = {}
vsinfo._arch_modes = {}
for _, mode in ipairs(vsinfo.modes) do
vsinfo._sub2[mode] = {}
vsinfo._arch_modes[mode] = {}
for _, arch in ipairs(vsinfo.archs) do
vsinfo._sub2[mode][arch] = { mode = mode, arch = arch }
vsinfo._arch_modes[mode][arch] = { mode = mode, arch = arch }

-- trace
print("checking for %s.%s ...", mode, arch)
Expand Down Expand Up @@ -356,33 +389,32 @@ function main(outputdir, vsinfo)
_target.projectdir = path.relative(project.directory(), _target.vcxprojdir)
local targetdir = target:get("targetdir")
if targetdir then _target.targetdir = path.relative(targetdir, _target.vcxprojdir) end
_target._sub = _target._sub or {}
_target._sub[mode] = _target._sub[mode] or {}
_target._targets = _target._targets or {}
_target._targets[mode] = _target._targets[mode] or {}
local targetinfo = _make_targetinfo(mode, arch, target)
_target._sub[mode][arch] = targetinfo
_target._targets[mode][arch] = targetinfo
_target.sdkver = targetinfo.sdkver

-- save all sourcefiles and headerfiles
_target.sourcefiles = table.unique(table.join(_target.sourcefiles or {}, (target:sourcefiles())))
_target.headerfiles = table.unique(table.join(_target.headerfiles or {}, (target:headerfiles())))

-- save deps
_target.deps = table.unique(table.join(_target.deps or {}, table.keys(target:deps()), nil))
end
end
end
end

-- leave project directory
os.cd(oldir)
for _, target in pairs(targets) do
target._sub2 = {}
target._paths = {}
local dirs = {}
local root = project.directory()
target.sourcefiles = table.imap(target.sourcefiles, function(_, v) return path.relative(v, root) end)
target.headerfiles = table.imap(target.headerfiles, function(_, v) return path.relative(v, root) end)
for _, f in ipairs(table.join(target.sourcefiles, target.headerfiles)) do
local dir = path.directory(f)
target._sub2[f] =
target._paths[f] =
{
path = _escape(f),
dir = _escape(dir)
Expand All @@ -398,14 +430,14 @@ function main(outputdir, vsinfo)
dir = path.directory(dir)
end
end
target._sub3 = dirs
target._dirs = dirs
target.dirs = table.keys(dirs)
target._sub4 = {}
target._deps = {}
for _, v in ipairs(target.deps) do
target._sub4[v] = targets[v]
target._deps[v] = targets[v]
end
end
vsinfo.targets = table.keys(targets)
vsinfo._sub = targets
vsinfo._targets = targets
return vsinfo
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "#group#", "#group#", "{#group_id#}"
EndProject
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{#current_id#} = {#parent_id#}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Microsoft Visual Studio Solution File, Format Version #solution_version#.0
VisualStudioVersion = #vs_version#
MinimumVisualStudioVersion = 10.0
#Import(Project)#
#Import(ProjectGroup)#
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Properties", "Properties", "{007754BD-BB1F-452F-A0D8-13EF216C5ED9}"
ProjectSection(SolutionItems) = preProject
Xmake.Custom.props = Xmake.Custom.props
Expand All @@ -26,6 +27,8 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
#Import(ProjectGroupConfig)# EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {#solution_id#}
EndGlobalSection
Expand Down
49 changes: 38 additions & 11 deletions xmake/plugins/project/vsxmake/vsxmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,28 @@ function _buildparams(info, target, default)
opt = table.join(target, opt)
end
for _, k in ipairs(opt) do
local v = (i._sub or {})[k] or (i._sub2 or {})[k] or (i._sub3 or {})[k] or (i._sub4 or {})[k]or i[k]
local v = (i._targets or {})[k]
if v == nil and i._arch_modes then
v = i._arch_modes[k]
end
if v == nil and i._paths then
v = i._paths[k]
end
if v == nil and i._dirs then
v = i._dirs[k]
end
if v == nil and i._deps then
v = i._deps[k]
end
if v == nil and i._groups then
v = i._groups[k]
end
if v == nil and i._group_deps then
v = i._group_deps[k]
end
if v == nil then
v = i[k]
end
if v == nil then
raise("key '" .. k .. "' not found")
end
Expand All @@ -82,32 +103,38 @@ function _buildparams(info, target, default)
if args.arch then
table.insert(r, info.archs)
end
if args.group then
table.insert(r, info.groups)
end
if args.group_dep then
table.insert(r, info.group_deps)
end
if args.dir then
table.insert(r, info._sub[target].dirs)
table.insert(r, info._targets[target].dirs)
end
if args.dep then
table.insert(r, info._sub[target].deps)
table.insert(r, info._targets[target].deps)
end
if args.filec then
local files = info._sub[target].sourcefiles
local files = info._targets[target].sourcefiles
table.insert(r, _filter_files(files, {".c"}))
elseif args.filecxx then
local files = info._sub[target].sourcefiles
local files = info._targets[target].sourcefiles
table.insert(r, _filter_files(files, {".cpp", ".cc", ".cxx"}))
elseif args.filecu then
local files = info._sub[target].sourcefiles
local files = info._targets[target].sourcefiles
table.insert(r, _filter_files(files, {".cu"}))
elseif args.fileobj then
local files = info._sub[target].sourcefiles
local files = info._targets[target].sourcefiles
table.insert(r, _filter_files(files, {".obj", ".o"}))
elseif args.filerc then
local files = info._sub[target].sourcefiles
local files = info._targets[target].sourcefiles
table.insert(r, _filter_files(files, {".rc"}))
elseif args.incc then
local files = info._sub[target].headerfiles
local files = info._targets[target].headerfiles
table.insert(r, _filter_files(files, nil, {".natvis"}))
elseif args.incnatvis then
local files = info._sub[target].headerfiles
local files = info._targets[target].headerfiles
table.insert(r, _filter_files(files, {".natvis"}))
end
return r
Expand Down Expand Up @@ -173,7 +200,7 @@ function make(version)

for _, target in ipairs(info.targets) do
local paramsprovidertarget = _buildparams(info, target, "<!-- nil -->")
local proj_dir = info._sub[target].vcxprojdir
local proj_dir = info._targets[target].vcxprojdir

-- write project file
local proj = path.join(proj_dir, target .. ".vcxproj")
Expand Down

0 comments on commit fb4c70e

Please sign in to comment.