diff options
| author | ruki <[email protected]> | 2016-08-29 12:02:02 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2016-08-29 12:02:02 +0800 |
| commit | c31f75567dc7946c78f25ecb043220934616b435 (patch) | |
| tree | 8f7ab4411f22b16c44db02c0bd9111e6f664a023 | |
| parent | bb3195b072a0568718f10be405423e3ad0153d4b (diff) | |
generate vs2013 project files
| -rwxr-xr-x | xmake/plugins/project/main.lua | 12 | ||||
| -rwxr-xr-x | xmake/plugins/project/vstudio/impl/vs200x_solution.lua | 11 | ||||
| -rwxr-xr-x | xmake/plugins/project/vstudio/impl/vs200x_vcproj.lua | 3 | ||||
| -rwxr-xr-x | xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua | 269 | ||||
| -rwxr-xr-x | xmake/plugins/project/vstudio/impl/vsfile.lua | 13 |
5 files changed, 304 insertions, 4 deletions
diff --git a/xmake/plugins/project/main.lua b/xmake/plugins/project/main.lua index 62ce0f253..e38579f4a 100755 --- a/xmake/plugins/project/main.lua +++ b/xmake/plugins/project/main.lua @@ -25,8 +25,14 @@ import("core.base.option") import("core.project.task") import("core.platform.environment") import("makefile.makefile") +import("vstudio.vs2002") +import("vstudio.vs2003") import("vstudio.vs2005") import("vstudio.vs2008") +import("vstudio.vs2010") +import("vstudio.vs2012") +import("vstudio.vs2013") +import("vstudio.vs2015") -- make project function _make(kind) @@ -35,8 +41,14 @@ function _make(kind) local maps = { makefile = makefile.make + , vs2002 = vs2002.make + , vs2003 = vs2003.make , vs2005 = vs2005.make , vs2008 = vs2008.make + , vs2010 = vs2010.make + , vs2012 = vs2012.make + , vs2013 = vs2013.make + , vs2015 = vs2015.make } assert(maps[kind], "the project kind(%s) is not supported!", kind) diff --git a/xmake/plugins/project/vstudio/impl/vs200x_solution.lua b/xmake/plugins/project/vstudio/impl/vs200x_solution.lua index ed9d0cdf2..b024cf9df 100755 --- a/xmake/plugins/project/vstudio/impl/vs200x_solution.lua +++ b/xmake/plugins/project/vstudio/impl/vs200x_solution.lua @@ -39,8 +39,14 @@ function _make_projects(slnfile, vsinfo) -- make all targets for targetname, target in pairs(project.targets()) do + -- the project suffix + local suffix = "vcproj" + if tonumber(vsinfo.vstudio_version) >= 2010 then + suffix = "vcxproj" + end + -- enter project - slnfile:enter("Project(\"{%s}\") = \"%s\", \"%s\\%s.vcproj\", \"{%s}\"", vctool, targetname, targetname, targetname, os.uuid(targetname)) + slnfile:enter("Project(\"{%s}\") = \"%s\", \"%s\\%s.%s\", \"{%s}\"", vctool, targetname, targetname, targetname, suffix, os.uuid(targetname)) -- add dependences for _, dep in ipairs(target:get("deps")) do @@ -91,6 +97,9 @@ function make(vsinfo) -- open solution file local slnfile = vsfile.open(path.join(vsinfo.solution_dir, vsinfo.solution_name .. ".sln"), "w") + -- init indent character + vsfile.indentchar('\t') + -- make header _make_header(slnfile, vsinfo) diff --git a/xmake/plugins/project/vstudio/impl/vs200x_vcproj.lua b/xmake/plugins/project/vstudio/impl/vs200x_vcproj.lua index fe81c962e..8162dc557 100755 --- a/xmake/plugins/project/vstudio/impl/vs200x_vcproj.lua +++ b/xmake/plugins/project/vstudio/impl/vs200x_vcproj.lua @@ -472,6 +472,9 @@ function make(vsinfo, target) -- open vcproj file local vcprojfile = vsfile.open(path.join(vcprojdir, targetname .. ".vcproj"), "w") + -- init indent character + vsfile.indentchar('\t') + -- make header _make_header(vcprojfile, vsinfo, target) diff --git a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua index c9ab61b0d..72d3ea6fb 100755 --- a/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua +++ b/xmake/plugins/project/vstudio/impl/vs201x_vcxproj.lua @@ -26,6 +26,257 @@ import("core.tool.compiler") import("core.project.config") import("vsfile") +-- make compiling flags +function _make_compflags(sourcefile, target, vcxprojdir) + + -- make the compiling flags + local _, compflags = compiler.compflags(sourcefile, target) + + -- replace -Idir or /Idir, -Fdsymbol.pdb or /Fdsymbol.pdb + local flags = {} + for _, flag in ipairs(compflags) do + + -- replace -Idir or /Idir + flag = flag:gsub("[%-|/]I(.*)", function (dir) + dir = dir:trim() + if not path.is_absolute(dir) then + dir = path.relative(path.absolute(dir), vcxprojdir) + end + return "/I" .. dir + end) + + -- replace -Fdsymbol.pdb or /Fdsymbol.pdb + flag = flag:gsub("[%-|/]Fd(.*)", function (dir) + dir = dir:trim() + if not path.is_absolute(dir) then + dir = path.relative(path.absolute(dir), vcxprojdir) + end + return "/Fd" .. dir + end) + + -- save flag + table.insert(flags, flag) + end + + -- concat flags + flags = table.concat(flags, " "):trim() + + -- ok? + return flags +end + +-- make linking flags +function _make_linkflags(target, vcxprojdir) + + -- make the linking flags + local _, linkflags = linker.linkflags(target) + + -- replace -libpath:dir or /libpath:dir, -pdb:symbol.pdb or /pdb:symbol.pdb + local flags = {} + for _, flag in ipairs(linkflags) do + + -- replace -libpath:dir or /libpath:dir + flag = flag:gsub("[%-|/]libpath:(.*)", function (dir) + dir = dir:trim() + if not path.is_absolute(dir) then + dir = path.relative(path.absolute(dir), vcxprojdir) + end + return "/libpath:" .. dir + end) + + -- replace -pdb:symbol.pdb or /pdb:symbol.pdb + flag = flag:gsub("[%-|/]pdb:(.*)", function (dir) + dir = dir:trim() + if not path.is_absolute(dir) then + dir = path.relative(path.absolute(dir), vcxprojdir) + end + return "/pdb:" .. dir + end) + + -- save flag + table.insert(flags, flag) + end + + -- concat flags + flags = table.concat(flags, " "):trim() + + -- ok? + return flags +end + +-- make header +function _make_header(vcxprojfile, vsinfo, target) + + -- the versions + local versions = + { + vs2010 = '10' + , vs2012 = '11' + , vs2013 = '12' + , vs2015 = '14' + } + + -- make header + vcxprojfile:print("<?xml version=\"1.0\" encoding=\"utf-8\"?>") + vcxprojfile:enter("<Project DefaultTargets=\"Build\" ToolsVersion=\"%s.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">", assert(versions["vs" .. vsinfo.vstudio_version])) +end + +-- make tailer +function _make_tailer(vcxprojfile, vsinfo, target) + vcxprojfile:print("<Import Project=\"%$(VCTargetsPath)\\Microsoft.Cpp.targets\" />") + vcxprojfile:enter("<ImportGroup Label=\"ExtensionTargets\">") + vcxprojfile:leave("</ImportGroup>") + vcxprojfile:leave("</Project>") +end + +-- make Configurations +function _make_configurations(vcxprojfile, vsinfo, target, vcxprojdir) + + -- the target name + local targetname = target:name() + + -- init configuration type + local configuration_types = + { + binary = "Application" + , shared = "DynamicLibrary" + , static = "StaticLibrary" + } + + -- the versions + local versions = + { + vs2010 = '10' + , vs2012 = '11' + , vs2013 = '12' + , vs2015 = '14' + } + + -- make ProjectConfigurations + vcxprojfile:enter("<ItemGroup Label=\"ProjectConfigurations\">") + vcxprojfile:enter("<ProjectConfiguration Include=\"$(mode)|Win32\">") + vcxprojfile:print("<Configuration>$(mode)</Configuration>") + vcxprojfile:print("<Platform>Win32</Platform>") + vcxprojfile:leave("</ProjectConfiguration>") + vcxprojfile:leave("</ItemGroup>") + + -- make Globals + vcxprojfile:enter("<PropertyGroup Label=\"Globals\">") + vcxprojfile:print("<ProjectGuid>{%s}</ProjectGuid>", os.uuid(targetname)) + vcxprojfile:print("<RootNamespace>%s</RootNamespace>", targetname) + vcxprojfile:leave("</PropertyGroup>") + + -- import Microsoft.Cpp.Default.props + vcxprojfile:print("<Import Project=\"%$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />") + + -- make Configuration + vcxprojfile:enter("<PropertyGroup Condition=\"\'%$(Configuration)|%$(Platform)\'==\'release|Win32\'\" Label=\"Configuration\">") + vcxprojfile:print("<ConfigurationType>%s</ConfigurationType>", assert(configuration_types[target:get("kind")])) + vcxprojfile:print("<PlatformToolset>v%s0</PlatformToolset>", assert(versions["vs" .. vsinfo.vstudio_version])) + vcxprojfile:print("<CharacterSet>MultiByte</CharacterSet>") + vcxprojfile:leave("</PropertyGroup>") + + -- import Microsoft.Cpp.props + vcxprojfile:print("<Import Project=\"%$(VCTargetsPath)\\Microsoft.Cpp.props\" />") + + -- make ExtensionSettings + vcxprojfile:enter("<ImportGroup Label=\"ExtensionSettings\">") + vcxprojfile:leave("</ImportGroup>") + + -- make PropertySheets + vcxprojfile:enter("<ImportGroup Condition=\"\'%$(Configuration)|%$(Platform)\'==\'$(mode)|Win32\'\" Label=\"PropertySheets\">") + vcxprojfile:print("<Import Project=\"%$(UserRootDir)\\Microsoft.Cpp.%$(Platform).user.props\" Condition=\"exists(\'%$(UserRootDir)\\Microsoft.Cpp.%$(Platform).user.props\')\" Label=\"LocalAppDataPlatform\" />") + vcxprojfile:leave("</ImportGroup>") + + -- make UserMacros + vcxprojfile:print("<PropertyGroup Label=\"UserMacros\" />") + + -- make OutputDirectory and IntermediateDirectory + vcxprojfile:enter("<PropertyGroup Condition=\"\'%$(Configuration)|%$(Platform)\'==\'$(mode)|Win32\'\">") + vcxprojfile:print("<OutDir>%s</OutDir>", path.relative(path.absolute(config.get("buildir")), vcxprojdir)) + vcxprojfile:print("<IntDir>%$(Configuration)</IntDir>") + if target:get("kind") == "binary" then + vcxprojfile:print("<LinkIncremental>true</LinkIncremental>") + end + vcxprojfile:leave("</PropertyGroup>") +end + +-- make ItemDefinitionGroup +function _make_item_define_group(vcxprojfile, vsinfo, target, vcxprojdir) + + -- make ItemDefinitionGroup for linker + if target:get("kind") == "binary" then + vcxprojfile:enter("<ItemDefinitionGroup Condition=\"\'%$(Configuration)|%$(Platform)\'==\'$(mode)|Win32\'\">") + vcxprojfile:enter("<Link>") + + -- make AdditionalOptions + vcxprojfile:print("<AdditionalOptions>%s %%(AdditionalOptions)</AdditionalOptions>", _make_linkflags(target, vcxprojdir)) + + -- generate debug infomation? + local debug = false + for _, symbol in ipairs(target:get("symbols")) do + if symbol == "debug" then + debug = true + break + end + end + vcxprojfile:print("<GenerateDebugInformation>%s</GenerateDebugInformation>", tostring(debug)) + + -- make SubSystem + vcxprojfile:print("<SubSystem>Console</SubSystem>") + + -- make TargetMachine + vcxprojfile:print("<TargetMachine>%s</TargetMachine>", ifelse(config.arch() == "x64", "MachineX64", "MachineX86")) + + vcxprojfile:leave("</Link>") + vcxprojfile:leave("</ItemDefinitionGroup>") + else + vcxprojfile:enter("<ItemDefinitionGroup>") + vcxprojfile:leave("</ItemDefinitionGroup>") + end +end + +-- make file +function _make_file(vcxprojfile, vsinfo, target, sourcefile, objectfile, vcxprojdir) + + -- get the target key + local key = tostring(target) + + -- make flags cache + _g.flags = _g.flags or {} + + -- make flags + local flags = _g.flags[key] or _make_compflags(sourcefile, target, vcxprojdir) + _g.flags[key] = flags + + -- add file + vcxprojfile:enter("<ClCompile Include=\"%s\">", path.relative(path.absolute(sourcefile), vcxprojdir)) + vcxprojfile:print("<AdditionalOptions Condition=\"\'%$(Configuration)|%$(Platform)\'==\'$(mode)|Win32\'\">%s %%(AdditionalOptions)</AdditionalOptions>", flags) + vcxprojfile:print("<ObjectFileName Condition=\"\'%$(Configuration)|%$(Platform)\'==\'$(mode)|Win32\'\">%s</ObjectFileName>", path.relative(path.absolute(objectfile), vcxprojdir)) + + -- complie as c++ if exists flag: /TP + if flags:find("[%-|/]TP") then + vcxprojfile:print("<CompileAs Condition=\"\'%$(Configuration)|%$(Platform)\'==\'$(mode)|Win32\'\">CompileAsCpp</CompileAs>") + end + + vcxprojfile:leave("</ClCompile>") +end + +-- make source code list +function _make_source_code_list(vcxprojfile, vsinfo, target, vcxprojdir) + + -- enter ItemGroup + vcxprojfile:enter("<ItemGroup>") + + -- add files + local objectfiles = target:objectfiles() + for idx, sourcefile in ipairs(target:sourcefiles()) do + _make_file(vcxprojfile, vsinfo, target, sourcefile, objectfiles[idx], vcxprojdir) + end + + vcxprojfile:leave("</ItemGroup>") +end + -- make vcxproj function make(vsinfo, target) @@ -38,6 +289,24 @@ function make(vsinfo, target) -- open vcxproj file local vcxprojfile = vsfile.open(path.join(vcxprojdir, targetname .. ".vcxproj"), "w") + -- init indent character + vsfile.indentchar(' ') + + -- make header + _make_header(vcxprojfile, vsinfo, target) + + -- make Configurations + _make_configurations(vcxprojfile, vsinfo, target, vcxprojdir) + + -- make ItemDefinitionGroup + _make_item_define_group(vcxprojfile, vsinfo, target, vcxprojdir) + + -- make source code list + _make_source_code_list(vcxprojfile, vsinfo, target, vcxprojdir) + + -- make tailer + _make_tailer(vcxprojfile, vsinfo, target) + -- exit solution file vcxprojfile:close() end diff --git a/xmake/plugins/project/vstudio/impl/vsfile.lua b/xmake/plugins/project/vstudio/impl/vsfile.lua index 516301b20..224c13d58 100755 --- a/xmake/plugins/project/vstudio/impl/vsfile.lua +++ b/xmake/plugins/project/vstudio/impl/vsfile.lua @@ -20,12 +20,15 @@ -- @file vsfile.lua -- +-- init the default indent character +_g.indentchar = '\t' + -- print file function _print(self, ...) -- print indent for i = 1, self._indent do - self:_write("\t") + self:_write(_g.indentchar) end -- print it @@ -37,7 +40,7 @@ function _printf(self, ...) -- print indent for i = 1, self._indent do - self:_write("\t") + self:_write(_g.indentchar) end -- printf it @@ -49,7 +52,7 @@ function _write(self, ...) -- print indent for i = 1, self._indent do - self:_write("\t") + self:_write(_g.indentchar) end -- write it @@ -105,3 +108,7 @@ function open(filepath, mode) return file end +-- set indent character +function indentchar(ch) + _g.indentchar = ch or '\t' +end |
