-- -- vs2005_csproj.lua -- Generate a Visual Studio 2005/2008 C# project. -- Copyright (c) 2009-2011 Jason Perkins and the Premake project -- -- -- Set up namespaces -- premake.vstudio.cs2005 = { } local vstudio = premake.vstudio local cs2005 = premake.vstudio.cs2005 -- -- Figure out what elements a particular source code file need in its item -- block, based on its build action and any related files in the project. -- local function getelements(prj, action, fname) if action == "Compile" and fname:endswith(".cs") then if fname:endswith(".Designer.cs") then -- is there a matching *.cs file? local basename = fname:sub(1, -13) local testname = basename .. ".cs" if premake.findfile(prj, testname) then return "Dependency", testname end -- is there a matching *.resx file? testname = basename .. ".resx" if premake.findfile(prj, testname) then return "AutoGen", testname end elseif fname:endswith(".xaml.cs") then -- is there a matching *.cs file? local basename = fname:sub(1, -9) local testname = basename .. ".xaml" if premake.findfile(prj, testname) then return "SubTypeCode", testname end else -- is there a *.Designer.cs file? local basename = fname:sub(1, -4) local testname = basename .. ".Designer.cs" if premake.findfile(prj, testname) then return "SubTypeForm" end end end if action == "EmbeddedResource" and fname:endswith(".resx") then -- is there a matching *.cs file? local basename = fname:sub(1, -6) local testname = path.getname(basename .. ".cs") if premake.findfile(prj, testname) then if premake.findfile(prj, basename .. ".Designer.cs") then return "DesignerType", testname else return "Dependency", testname end else -- is there a matching *.Designer.cs? testname = path.getname(basename .. ".Designer.cs") if premake.findfile(prj, testname) then return "AutoGenerated" end end end if fname:endswith(".xaml") then return "XamlDesigner" end if action == "Content" then return "CopyNewest" end return "None" end -- -- Return the Visual Studio architecture identification string. The logic -- to select this is getting more complicated in VS2010, but I haven't -- tackled all the permutations yet. -- function cs2005.arch(prj) return "AnyCPU" end -- -- Write out the element. -- function cs2005.files(prj) local tr = premake.project.buildsourcetree(prj) premake.tree.traverse(tr, { onleaf = function(node) local action = premake.dotnet.getbuildaction(node.cfg) local fname = path.translate(premake.esc(node.cfg.name), "\\") local elements, dependency = getelements(prj, action, node.path) if elements == "None" then _p(' <%s Include="%s" />', action, fname) else _p(' <%s Include="%s">', action, fname) if elements == "AutoGen" then _p(' True') elseif elements == "AutoGenerated" then _p(' Designer') _p(' ResXFileCodeGenerator') _p(' %s.Designer.cs', premake.esc(path.getbasename(node.name))) elseif elements == "SubTypeDesigner" then _p(' Designer') elseif elements == "SubTypeForm" then _p(' Form') elseif elements == "SubTypeCode" then _p(' Code') elseif elements == "XamlDesigner" then _p(' Designer') _p(' MSBuild:Compile') elseif elements == "PreserveNewest" then _p(' PreserveNewest') end if dependency then _p(' %s', path.translate(premake.esc(dependency), "\\")) end _p(' ', action) end end }, false) end -- -- Write the opening element. -- function cs2005.projectelement(prj) local action = premake.action.current() local toolversion = '' if action.vstudio.toolsVersion then toolversion = string.format(' ToolsVersion="%s"', action.vstudio.toolsVersion) end if _ACTION > "vs2008" then _p('') end _p('', toolversion) end -- -- Write the opening PropertyGroup, which contains the project-level settings. -- function cs2005.projectsettings(prj) _p(' ') _p(' %s', premake.esc(prj.solution.configurations[1])) _p(' %s', cs2005.arch(prj)) local action = premake.action.current() if action.vstudio.productVersion then _p(' %s', action.vstudio.productVersion) end if _ACTION < "vs2012" then _p(' 2.0') end _p(' {%s}', prj.uuid) _p(' %s', premake.dotnet.getkind(prj)) _p(' Properties') _p(' %s', prj.buildtarget.basename) _p(' %s', prj.buildtarget.basename) local framework = prj.framework or action.vstudio.targetFramework if framework then _p(' v%s', framework) end if _ACTION == 'vs2010' then _p(' ') end if _ACTION >= "vs2010" then _p(' 512') end _p(' ') end -- -- Write the PropertyGroup element for a specific configuration block. -- function cs2005.propertygroup(cfg) _p(' ', premake.esc(cfg.name), cs2005.arch(cfg)) if _ACTION > "vs2008" then _p(' %s', cs2005.arch(cfg)) end end -- -- Write the build events groups. -- function cs2005.buildevents(cfg) if #cfg.prebuildcommands > 0 then _p(' ') _p(' %s', premake.esc(table.implode(cfg.prebuildcommands, "", "", "\r\n"))) _p(' ') end if #cfg.postbuildcommands > 0 then _p(' ') _p(' %s', premake.esc(table.implode(cfg.postbuildcommands, "", "", "\r\n"))) _p(' ') end end -- -- The main function: write the project file. -- function cs2005.generate(prj) io.eol = "\r\n" cs2005.projectelement(prj) if _ACTION > "vs2010" then _p(' ') end cs2005.projectsettings(prj) for cfg in premake.eachconfig(prj) do cs2005.propertygroup(cfg) if cfg.flags.Symbols then _p(' true') _p(' full') else _p(' pdbonly') end _p(' %s', iif(cfg.flags.Optimize or cfg.flags.OptimizeSize or cfg.flags.OptimizeSpeed, "true", "false")) _p(' %s', cfg.buildtarget.directory) _p(' %s', table.concat(premake.esc(cfg.defines), ";")) _p(' prompt') _p(' 4') if cfg.flags.Unsafe then _p(' true') end if cfg.flags.FatalWarnings then _p(' true') end _p(' ') end _p(' ') for _, ref in ipairs(premake.getlinks(prj, "siblings", "object")) do _p(' ', path.translate(path.getrelative(prj.location, vstudio.projectfile(ref)), "\\")) _p(' {%s}', ref.uuid) _p(' %s', premake.esc(ref.name)) _p(' ') end for _, linkname in ipairs(premake.getlinks(prj, "system", "name")) do _p(' ', premake.esc(linkname)) end _p(' ') _p(' ') cs2005.files(prj) _p(' ') local msbuild = iif(_ACTION < "vs2012", "Bin", "Tools") _p(' ', msbuild) _p(' ') _p('') end