]> git.tdb.fi Git - builder.git/blob - source/vcxprojectgenerator.cpp
Inline simple constructors
[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 Target *VcxProjectGenerator::create_target(const vector<Target *> &, const string &)
16 {
17         throw logic_error("Not implemented");
18 }
19
20 Task *VcxProjectGenerator::run(const Target &target) const
21 {
22         const VcxProjectFile &project = dynamic_cast<const VcxProjectFile &>(target);
23         Worker *worker = new Worker(project);
24         return new InternalTask(worker);
25 }
26
27
28 void VcxProjectGenerator::Worker::main()
29 {
30         const SourcePackage &spkg = *target.get_package();
31         Builder &builder = spkg.get_builder();
32
33         IO::BufferedFile out(target.get_path().str(), IO::M_WRITE);
34         IO::print(out, "<Project DefaultTargets=\"Build\" ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
35
36         IO::print(out, "\t<ItemGroup Label=\"ProjectConfigurations\">\n");
37         vector<string> build_types = builder.get_build_types();
38         const char *platforms[] = { "Win32", "x64" };
39         for(const char *p: platforms)
40                 for(const string &b: build_types)
41                 {
42                         IO::print(out, "\t\t<ProjectConfiguration Include=\"%s|%s\">\n", b, p);
43                         IO::print(out, "\t\t\t<Configuration>%s</Configuration>\n", b);
44                         IO::print(out, "\t\t\t<Platform>%s</Platform>\n", p);
45                         IO::print(out, "\t\t</ProjectConfiguration>\n");
46                 }
47         IO::print(out, "\t</ItemGroup>\n");
48
49         IO::print(out, "\t<PropertyGroup Label=\"Globals\">\n");
50         IO::print(out, "\t\t<VCProjectVersion>15.0</VCProjectVersion>\n");
51         IO::print(out, "\t\t<Keyword>MakeFileProj</Keyword>\n");
52         IO::print(out, "\t\t<ProjectGuid>{%s}</ProjectGuid>\n", target.get_guid());
53         IO::print(out, "\t</PropertyGroup>\n");
54
55         IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n");
56
57         const Executable *exe = 0;
58         for(const Target *t: builder.get_build_graph().get_target("world")->get_dependencies())
59                 if(t->get_package()==&spkg)
60                         if((exe = dynamic_cast<const Executable *>(t)))
61                                 break;
62
63         const char *argv0 = Application::get_argv0();
64         const string &toolchain = builder.get_current_arch().get_toolchain();
65         for(const char *p: platforms)
66                 for(const string &b: build_types)
67                 {
68                         string base_cmd = format("%s --arch=%s-%s --build-type=%s --prefix=%s", argv0, p, toolchain, b, builder.get_prefix());
69                         IO::print(out, "\t<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='%s|%s'\" Label=\"Configuration\">\n", b, p);
70                         IO::print(out, "\t\t<ConfigurationType>MakeFile</ConfigurationType>\n");
71                         IO::print(out, "\t\t<NMakeBuildCommandLine>%s</NMakeBuildCommandLine>\n", base_cmd);
72                         IO::print(out, "\t\t<NMakeCleanCommandLine>%s -c</NMakeCleanCommandLine>\n", base_cmd);
73                         IO::print(out, "\t\t<NMakeReBuildCommandLine>%s -B</NMakeReBuildCommandLine>\n", base_cmd);
74                         if(exe)
75                                 IO::print(out, "\t\t<NMakeOutput>%s</NMakeOutput>\n", exe->get_path());
76                         IO::print(out, "\t</PropertyGroup>\n");
77                 }
78
79         IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n");
80
81         vector<const FileTarget *> sources;
82         vector<const FileTarget *> includes;
83         vector<const FileTarget *> others;
84         BuildInfo build_info;
85         for(const auto &kvp: builder.get_build_graph().get_targets())
86                 if(kvp.second->get_package()==&spkg)
87                 {
88                         if(kvp.second->is_buildable())
89                         {
90                                 BuildInfo tgt_binfo;
91                                 kvp.second->collect_build_info(tgt_binfo);
92                                 build_info.update_from(tgt_binfo, BuildInfo::CHAINED);
93                         }
94                         else if(const FileTarget *file = dynamic_cast<const FileTarget *>(kvp.second))
95                         {
96                                 if(dynamic_cast<const CSourceFile *>(file))
97                                 {
98                                         string ext = tolower(FS::extpart(FS::basename(file->get_path())));
99                                         if(ext==".h" || ext==".hpp")
100                                                 includes.push_back(file);
101                                         else
102                                                 sources.push_back(file);
103                                 }
104                                 else
105                                         others.push_back(file);
106                         }
107                 }
108
109         if(!build_info.incpath.empty())
110         {
111                 IO::print(out, "\t<PropertyGroup>\n");
112                 string path_str;
113                 for(const FS::Path &p: build_info.incpath)
114                         append(path_str, ";", p.str());
115                 IO::print(out, "\t\t<NMakeIncludeSearchPath>%s</NMakeIncludeSearchPath>\n", path_str);
116                 IO::print(out, "\t</PropertyGroup>\n");
117         }
118
119         IO::print(out, "\t<ItemGroup>\n");
120         for(const FileTarget *s: sources)
121                 IO::print(out, "\t\t<ClCompile Include=\"%s\" />\n", s->get_path());
122         IO::print(out, "\t</ItemGroup>\n");
123
124         IO::print(out, "\t<ItemGroup>\n");
125         for(const FileTarget *i: includes)
126                 IO::print(out, "\t\t<ClInclude Include=\"%s\" />\n", i->get_path());
127         IO::print(out, "\t</ItemGroup>\n");
128
129         IO::print(out, "\t<ItemGroup>\n");
130         for(const FileTarget *t: others)
131                 IO::print(out, "\t\t<None Include=\"%s\" />\n", t->get_path());
132         IO::print(out, "\t</ItemGroup>\n");
133
134         IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n");
135         IO::print(out, "</Project>\n");
136
137         status = Task::SUCCESS;
138 }