]> git.tdb.fi Git - builder.git/blobdiff - plugins/msvc/vcxprojectgenerator.cpp
Move the VS project and solution generators to the MSVC plugin
[builder.git] / plugins / msvc / vcxprojectgenerator.cpp
diff --git a/plugins/msvc/vcxprojectgenerator.cpp b/plugins/msvc/vcxprojectgenerator.cpp
new file mode 100644 (file)
index 0000000..7cb9faf
--- /dev/null
@@ -0,0 +1,136 @@
+#include <msp/builder/builder.h>
+#include <msp/builder/csourcefile.h>
+#include <msp/builder/executable.h>
+#include <msp/builder/sourcepackage.h>
+#include <msp/core/application.h>
+#include <msp/fs/utils.h>
+#include <msp/io/print.h>
+#include <msp/strings/utils.h>
+#include "vcxprojectfile.h"
+#include "vcxprojectgenerator.h"
+
+using namespace std;
+using namespace Msp;
+
+VcxProjectGenerator::VcxProjectGenerator(Builder &b):
+       Tool(b, "VCXG")
+{
+       set_run_internal(_run);
+}
+
+Target *VcxProjectGenerator::create_target(const vector<Target *> &, const string &)
+{
+       throw logic_error("Not implemented");
+}
+
+bool VcxProjectGenerator::_run(const VcxProjectFile &project)
+{
+       const SourcePackage &spkg = *project.get_package();
+       Builder &builder = spkg.get_builder();
+
+       IO::BufferedFile out(project.get_path().str(), IO::M_WRITE);
+       IO::print(out, "<Project DefaultTargets=\"Build\" ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
+
+       IO::print(out, "\t<ItemGroup Label=\"ProjectConfigurations\">\n");
+       vector<string> build_types = builder.get_build_types();
+       const char *platforms[] = { "Win32", "x64" };
+       for(const char *p: platforms)
+               for(const string &b: build_types)
+               {
+                       IO::print(out, "\t\t<ProjectConfiguration Include=\"%s|%s\">\n", b, p);
+                       IO::print(out, "\t\t\t<Configuration>%s</Configuration>\n", b);
+                       IO::print(out, "\t\t\t<Platform>%s</Platform>\n", p);
+                       IO::print(out, "\t\t</ProjectConfiguration>\n");
+               }
+       IO::print(out, "\t</ItemGroup>\n");
+
+       IO::print(out, "\t<PropertyGroup Label=\"Globals\">\n");
+       IO::print(out, "\t\t<VCProjectVersion>15.0</VCProjectVersion>\n");
+       IO::print(out, "\t\t<Keyword>MakeFileProj</Keyword>\n");
+       IO::print(out, "\t\t<ProjectGuid>{%s}</ProjectGuid>\n", project.get_guid());
+       IO::print(out, "\t</PropertyGroup>\n");
+
+       IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n");
+
+       const Executable *exe = 0;
+       for(const Target *t: builder.get_build_graph().get_target("world")->get_dependencies())
+               if(t->get_package()==&spkg)
+                       if((exe = dynamic_cast<const Executable *>(t)))
+                               break;
+
+       const char *argv0 = Application::get_argv0();
+       const string &toolchain = builder.get_current_arch().get_toolchain();
+       for(const char *p: platforms)
+               for(const string &b: build_types)
+               {
+                       string base_cmd = format("%s --arch=%s-%s --build-type=%s --prefix=%s", argv0, p, toolchain, b, builder.get_prefix());
+                       IO::print(out, "\t<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='%s|%s'\" Label=\"Configuration\">\n", b, p);
+                       IO::print(out, "\t\t<ConfigurationType>MakeFile</ConfigurationType>\n");
+                       IO::print(out, "\t\t<NMakeBuildCommandLine>%s</NMakeBuildCommandLine>\n", base_cmd);
+                       IO::print(out, "\t\t<NMakeCleanCommandLine>%s -c</NMakeCleanCommandLine>\n", base_cmd);
+                       IO::print(out, "\t\t<NMakeReBuildCommandLine>%s -B</NMakeReBuildCommandLine>\n", base_cmd);
+                       if(exe)
+                               IO::print(out, "\t\t<NMakeOutput>%s</NMakeOutput>\n", exe->get_path());
+                       IO::print(out, "\t</PropertyGroup>\n");
+               }
+
+       IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n");
+
+       vector<const FileTarget *> sources;
+       vector<const FileTarget *> includes;
+       vector<const FileTarget *> others;
+       BuildInfo build_info;
+       for(const auto &kvp: builder.get_build_graph().get_targets())
+               if(kvp.second->get_package()==&spkg)
+               {
+                       if(kvp.second->is_buildable())
+                       {
+                               BuildInfo tgt_binfo;
+                               kvp.second->collect_build_info(tgt_binfo);
+                               build_info.update_from(tgt_binfo, BuildInfo::CHAINED);
+                       }
+                       else if(const FileTarget *file = dynamic_cast<const FileTarget *>(kvp.second))
+                       {
+                               if(dynamic_cast<const CSourceFile *>(file))
+                               {
+                                       string ext = tolower(FS::extpart(FS::basename(file->get_path())));
+                                       if(ext==".h" || ext==".hpp")
+                                               includes.push_back(file);
+                                       else
+                                               sources.push_back(file);
+                               }
+                               else
+                                       others.push_back(file);
+                       }
+               }
+
+       if(!build_info.incpath.empty())
+       {
+               IO::print(out, "\t<PropertyGroup>\n");
+               string path_str;
+               for(const FS::Path &p: build_info.incpath)
+                       append(path_str, ";", p.str());
+               IO::print(out, "\t\t<NMakeIncludeSearchPath>%s</NMakeIncludeSearchPath>\n", path_str);
+               IO::print(out, "\t</PropertyGroup>\n");
+       }
+
+       IO::print(out, "\t<ItemGroup>\n");
+       for(const FileTarget *s: sources)
+               IO::print(out, "\t\t<ClCompile Include=\"%s\" />\n", s->get_path());
+       IO::print(out, "\t</ItemGroup>\n");
+
+       IO::print(out, "\t<ItemGroup>\n");
+       for(const FileTarget *i: includes)
+               IO::print(out, "\t\t<ClInclude Include=\"%s\" />\n", i->get_path());
+       IO::print(out, "\t</ItemGroup>\n");
+
+       IO::print(out, "\t<ItemGroup>\n");
+       for(const FileTarget *t: others)
+               IO::print(out, "\t\t<None Include=\"%s\" />\n", t->get_path());
+       IO::print(out, "\t</ItemGroup>\n");
+
+       IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n");
+       IO::print(out, "</Project>\n");
+
+       return true;
+}