1 #include <msp/core/application.h>
2 #include <msp/fs/utils.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/utils.h>
6 #include "csourcefile.h"
7 #include "executable.h"
8 #include "sourcepackage.h"
9 #include "vcxprojectfile.h"
10 #include "vcxprojectgenerator.h"
15 VcxProjectGenerator::VcxProjectGenerator(Builder &b):
19 Target *VcxProjectGenerator::create_target(const vector<Target *> &, const string &)
21 throw logic_error("Not implemented");
24 Task *VcxProjectGenerator::run(const Target &target) const
26 const VcxProjectFile &project = dynamic_cast<const VcxProjectFile &>(target);
27 Worker *worker = new Worker(project);
28 return new InternalTask(worker);
32 VcxProjectGenerator::Worker::Worker(const VcxProjectFile &t):
36 void VcxProjectGenerator::Worker::main()
38 const SourcePackage &spkg = *target.get_package();
39 Builder &builder = spkg.get_builder();
41 IO::BufferedFile out(target.get_path().str(), IO::M_WRITE);
42 IO::print(out, "<Project DefaultTargets=\"Build\" ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
44 IO::print(out, "\t<ItemGroup Label=\"ProjectConfigurations\">\n");
45 vector<string> build_types = builder.get_build_types();
46 const char *platforms[] = { "Win32", "x64" };
47 for(const char *p: platforms)
48 for(const string &b: build_types)
50 IO::print(out, "\t\t<ProjectConfiguration Include=\"%s|%s\">\n", b, p);
51 IO::print(out, "\t\t\t<Configuration>%s</Configuration>\n", b);
52 IO::print(out, "\t\t\t<Platform>%s</Platform>\n", p);
53 IO::print(out, "\t\t</ProjectConfiguration>\n");
55 IO::print(out, "\t</ItemGroup>\n");
57 IO::print(out, "\t<PropertyGroup Label=\"Globals\">\n");
58 IO::print(out, "\t\t<VCProjectVersion>15.0</VCProjectVersion>\n");
59 IO::print(out, "\t\t<Keyword>MakeFileProj</Keyword>\n");
60 IO::print(out, "\t\t<ProjectGuid>{%s}</ProjectGuid>\n", target.get_guid());
61 IO::print(out, "\t</PropertyGroup>\n");
63 IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n");
65 const Executable *exe = 0;
66 for(const Target *t: builder.get_build_graph().get_target("world")->get_dependencies())
67 if(t->get_package()==&spkg)
68 if((exe = dynamic_cast<const Executable *>(t)))
71 const char *argv0 = Application::get_argv0();
72 const string &toolchain = builder.get_current_arch().get_toolchain();
73 for(const char *p: platforms)
74 for(const string &b: build_types)
76 string base_cmd = format("%s --arch=%s-%s --build-type=%s --prefix=%s", argv0, p, toolchain, b, builder.get_prefix());
77 IO::print(out, "\t<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='%s|%s'\" Label=\"Configuration\">\n", b, p);
78 IO::print(out, "\t\t<ConfigurationType>MakeFile</ConfigurationType>\n");
79 IO::print(out, "\t\t<NMakeBuildCommandLine>%s</NMakeBuildCommandLine>\n", base_cmd);
80 IO::print(out, "\t\t<NMakeCleanCommandLine>%s -c</NMakeCleanCommandLine>\n", base_cmd);
81 IO::print(out, "\t\t<NMakeReBuildCommandLine>%s -B</NMakeReBuildCommandLine>\n", base_cmd);
83 IO::print(out, "\t\t<NMakeOutput>%s</NMakeOutput>\n", exe->get_path());
84 IO::print(out, "\t</PropertyGroup>\n");
87 IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n");
89 vector<const FileTarget *> sources;
90 vector<const FileTarget *> includes;
91 vector<const FileTarget *> others;
93 for(const auto &kvp: builder.get_build_graph().get_targets())
94 if(kvp.second->get_package()==&spkg)
96 if(kvp.second->is_buildable())
99 kvp.second->collect_build_info(tgt_binfo);
100 build_info.update_from(tgt_binfo, BuildInfo::CHAINED);
102 else if(const FileTarget *file = dynamic_cast<const FileTarget *>(kvp.second))
104 if(dynamic_cast<const CSourceFile *>(file))
106 string ext = tolower(FS::extpart(FS::basename(file->get_path())));
107 if(ext==".h" || ext==".hpp")
108 includes.push_back(file);
110 sources.push_back(file);
113 others.push_back(file);
117 if(!build_info.incpath.empty())
119 IO::print(out, "\t<PropertyGroup>\n");
121 for(const FS::Path &p: build_info.incpath)
122 append(path_str, ";", p.str());
123 IO::print(out, "\t\t<NMakeIncludeSearchPath>%s</NMakeIncludeSearchPath>\n", path_str);
124 IO::print(out, "\t</PropertyGroup>\n");
127 IO::print(out, "\t<ItemGroup>\n");
128 for(const FileTarget *s: sources)
129 IO::print(out, "\t\t<ClCompile Include=\"%s\" />\n", s->get_path());
130 IO::print(out, "\t</ItemGroup>\n");
132 IO::print(out, "\t<ItemGroup>\n");
133 for(const FileTarget *i: includes)
134 IO::print(out, "\t\t<ClInclude Include=\"%s\" />\n", i->get_path());
135 IO::print(out, "\t</ItemGroup>\n");
137 IO::print(out, "\t<ItemGroup>\n");
138 for(const FileTarget *t: others)
139 IO::print(out, "\t\t<None Include=\"%s\" />\n", t->get_path());
140 IO::print(out, "\t</ItemGroup>\n");
142 IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n");
143 IO::print(out, "</Project>\n");
145 status = Task::SUCCESS;