1 #include <msp/builder/builder.h>
2 #include <msp/builder/csourcefile.h>
3 #include <msp/builder/executable.h>
4 #include <msp/builder/sourcepackage.h>
5 #include <msp/core/application.h>
6 #include <msp/fs/utils.h>
7 #include <msp/io/print.h>
8 #include <msp/strings/utils.h>
9 #include "vcxprojectfile.h"
10 #include "vcxprojectgenerator.h"
15 VcxProjectGenerator::VcxProjectGenerator(Builder &b):
18 set_run_internal(_run);
21 Target *VcxProjectGenerator::create_target(const vector<Target *> &, const string &)
23 throw logic_error("Not implemented");
26 bool VcxProjectGenerator::_run(const VcxProjectFile &project)
28 const SourcePackage &spkg = *project.get_package();
29 Builder &builder = spkg.get_builder();
31 IO::BufferedFile out(project.get_path().str(), IO::M_WRITE);
32 IO::print(out, "<Project DefaultTargets=\"Build\" ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
34 IO::print(out, "\t<ItemGroup Label=\"ProjectConfigurations\">\n");
35 vector<string> build_types = builder.get_build_types();
36 const char *platforms[] = { "Win32", "x64" };
37 for(const char *p: platforms)
38 for(const string &b: build_types)
40 IO::print(out, "\t\t<ProjectConfiguration Include=\"%s|%s\">\n", b, p);
41 IO::print(out, "\t\t\t<Configuration>%s</Configuration>\n", b);
42 IO::print(out, "\t\t\t<Platform>%s</Platform>\n", p);
43 IO::print(out, "\t\t</ProjectConfiguration>\n");
45 IO::print(out, "\t</ItemGroup>\n");
47 IO::print(out, "\t<PropertyGroup Label=\"Globals\">\n");
48 IO::print(out, "\t\t<VCProjectVersion>15.0</VCProjectVersion>\n");
49 IO::print(out, "\t\t<Keyword>MakeFileProj</Keyword>\n");
50 IO::print(out, "\t\t<ProjectGuid>{%s}</ProjectGuid>\n", project.get_guid());
51 IO::print(out, "\t</PropertyGroup>\n");
53 IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n");
55 const Executable *exe = 0;
56 for(const Target *t: builder.get_build_graph().get_target("world")->get_dependencies())
57 if(t->get_package()==&spkg)
58 if((exe = dynamic_cast<const Executable *>(t)))
61 const char *argv0 = Application::get_argv0();
62 const string &toolchain = builder.get_current_arch().get_toolchain();
63 for(const char *p: platforms)
64 for(const string &b: build_types)
66 string base_cmd = format("%s --arch=%s-%s --build-type=%s --prefix=%s", argv0, p, toolchain, b, builder.get_prefix());
67 IO::print(out, "\t<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='%s|%s'\" Label=\"Configuration\">\n", b, p);
68 IO::print(out, "\t\t<ConfigurationType>MakeFile</ConfigurationType>\n");
69 IO::print(out, "\t\t<NMakeBuildCommandLine>%s</NMakeBuildCommandLine>\n", base_cmd);
70 IO::print(out, "\t\t<NMakeCleanCommandLine>%s -c</NMakeCleanCommandLine>\n", base_cmd);
71 IO::print(out, "\t\t<NMakeReBuildCommandLine>%s -B</NMakeReBuildCommandLine>\n", base_cmd);
73 IO::print(out, "\t\t<NMakeOutput>%s</NMakeOutput>\n", exe->get_path());
74 IO::print(out, "\t</PropertyGroup>\n");
77 IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n");
79 vector<const FileTarget *> sources;
80 vector<const FileTarget *> includes;
81 vector<const FileTarget *> others;
83 for(const auto &kvp: builder.get_build_graph().get_targets())
84 if(kvp.second->get_package()==&spkg)
86 if(kvp.second->is_buildable())
89 kvp.second->collect_build_info(tgt_binfo);
90 build_info.update_from(tgt_binfo, BuildInfo::CHAINED);
92 else if(const FileTarget *file = dynamic_cast<const FileTarget *>(kvp.second))
94 if(dynamic_cast<const CSourceFile *>(file))
96 string ext = tolower(FS::extpart(FS::basename(file->get_path())));
97 if(ext==".h" || ext==".hpp")
98 includes.push_back(file);
100 sources.push_back(file);
103 others.push_back(file);
107 if(!build_info.incpath.empty())
109 IO::print(out, "\t<PropertyGroup>\n");
111 for(const FS::Path &p: build_info.incpath)
112 append(path_str, ";", p.str());
113 IO::print(out, "\t\t<NMakeIncludeSearchPath>%s</NMakeIncludeSearchPath>\n", path_str);
114 IO::print(out, "\t</PropertyGroup>\n");
117 IO::print(out, "\t<ItemGroup>\n");
118 for(const FileTarget *s: sources)
119 IO::print(out, "\t\t<ClCompile Include=\"%s\" />\n", s->get_path());
120 IO::print(out, "\t</ItemGroup>\n");
122 IO::print(out, "\t<ItemGroup>\n");
123 for(const FileTarget *i: includes)
124 IO::print(out, "\t\t<ClInclude Include=\"%s\" />\n", i->get_path());
125 IO::print(out, "\t</ItemGroup>\n");
127 IO::print(out, "\t<ItemGroup>\n");
128 for(const FileTarget *t: others)
129 IO::print(out, "\t\t<None Include=\"%s\" />\n", t->get_path());
130 IO::print(out, "\t</ItemGroup>\n");
132 IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n");
133 IO::print(out, "</Project>\n");