]> git.tdb.fi Git - builder.git/blob - source/vcxprojectgenerator.cpp
Replace basic for loops with range-based loops or algorithms
[builder.git] / source / vcxprojectgenerator.cpp
1 #include <msp/core/application.h>
2 #include <msp/fs/utils.h>
3 #include <msp/io/print.h>
4 #include <msp/strings/utils.h>
5 #include "builder.h"
6 #include "csourcefile.h"
7 #include "executable.h"
8 #include "sourcepackage.h"
9 #include "vcxprojectfile.h"
10 #include "vcxprojectgenerator.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 VcxProjectGenerator::VcxProjectGenerator(Builder &b):
16         Tool(b, "VCXG")
17 { }
18
19 Target *VcxProjectGenerator::create_target(const list<Target *> &, const string &)
20 {
21         throw logic_error("Not implemented");
22 }
23
24 Task *VcxProjectGenerator::run(const Target &target) const
25 {
26         const VcxProjectFile &project = dynamic_cast<const VcxProjectFile &>(target);
27         Worker *worker = new Worker(project);
28         return new InternalTask(worker);
29 }
30
31
32 VcxProjectGenerator::Worker::Worker(const VcxProjectFile &t):
33         target(t)
34 { }
35
36 void VcxProjectGenerator::Worker::main()
37 {
38         const SourcePackage &spkg = *target.get_package();
39         Builder &builder = spkg.get_builder();
40
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");
43
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)
49                 {
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");
54                 }
55         IO::print(out, "\t</ItemGroup>\n");
56
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");
62
63         IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n");
64
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)))
69                                 break;
70
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)
75                 {
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);
82                         if(exe)
83                                 IO::print(out, "\t\t<NMakeOutput>%s</NMakeOutput>\n", exe->get_path());
84                         IO::print(out, "\t</PropertyGroup>\n");
85                 }
86
87         IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n");
88
89         vector<const FileTarget *> sources;
90         vector<const FileTarget *> includes;
91         vector<const FileTarget *> others;
92         BuildInfo build_info;
93         for(const auto &kvp: builder.get_build_graph().get_targets())
94                 if(kvp.second->get_package()==&spkg)
95                 {
96                         if(kvp.second->is_buildable())
97                         {
98                                 BuildInfo tgt_binfo;
99                                 kvp.second->collect_build_info(tgt_binfo);
100                                 build_info.update_from(tgt_binfo, BuildInfo::CHAINED);
101                         }
102                         else if(const FileTarget *file = dynamic_cast<const FileTarget *>(kvp.second))
103                         {
104                                 if(dynamic_cast<const CSourceFile *>(file))
105                                 {
106                                         string ext = tolower(FS::extpart(FS::basename(file->get_path())));
107                                         if(ext==".h" || ext==".hpp")
108                                                 includes.push_back(file);
109                                         else
110                                                 sources.push_back(file);
111                                 }
112                                 else
113                                         others.push_back(file);
114                         }
115                 }
116
117         if(!build_info.incpath.empty())
118         {
119                 IO::print(out, "\t<PropertyGroup>\n");
120                 string path_str;
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");
125         }
126
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");
131
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");
136
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");
141
142         IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n");
143         IO::print(out, "</Project>\n");
144
145         status = Task::SUCCESS;
146 }