]> git.tdb.fi Git - builder.git/blob - plugins/base/vcxprojectgenerator.cpp
Rename builtintools to basetools
[builder.git] / plugins / base / vcxprojectgenerator.cpp
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"
11
12 using namespace std;
13 using namespace Msp;
14
15 VcxProjectGenerator::VcxProjectGenerator(Builder &b):
16         Tool(b, "VCXG")
17 {
18         set_run_internal(_run);
19 }
20
21 Target *VcxProjectGenerator::create_target(const vector<Target *> &, const string &)
22 {
23         throw logic_error("Not implemented");
24 }
25
26 bool VcxProjectGenerator::_run(const VcxProjectFile &project)
27 {
28         const SourcePackage &spkg = *project.get_package();
29         Builder &builder = spkg.get_builder();
30
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");
33
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)
39                 {
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");
44                 }
45         IO::print(out, "\t</ItemGroup>\n");
46
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");
52
53         IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n");
54
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)))
59                                 break;
60
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)
65                 {
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);
72                         if(exe)
73                                 IO::print(out, "\t\t<NMakeOutput>%s</NMakeOutput>\n", exe->get_path());
74                         IO::print(out, "\t</PropertyGroup>\n");
75                 }
76
77         IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n");
78
79         vector<const FileTarget *> sources;
80         vector<const FileTarget *> includes;
81         vector<const FileTarget *> others;
82         BuildInfo build_info;
83         for(const auto &kvp: builder.get_build_graph().get_targets())
84                 if(kvp.second->get_package()==&spkg)
85                 {
86                         if(kvp.second->is_buildable())
87                         {
88                                 BuildInfo tgt_binfo;
89                                 kvp.second->collect_build_info(tgt_binfo);
90                                 build_info.update_from(tgt_binfo, BuildInfo::CHAINED);
91                         }
92                         else if(const FileTarget *file = dynamic_cast<const FileTarget *>(kvp.second))
93                         {
94                                 if(dynamic_cast<const CSourceFile *>(file))
95                                 {
96                                         string ext = tolower(FS::extpart(FS::basename(file->get_path())));
97                                         if(ext==".h" || ext==".hpp")
98                                                 includes.push_back(file);
99                                         else
100                                                 sources.push_back(file);
101                                 }
102                                 else
103                                         others.push_back(file);
104                         }
105                 }
106
107         if(!build_info.incpath.empty())
108         {
109                 IO::print(out, "\t<PropertyGroup>\n");
110                 string path_str;
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");
115         }
116
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");
121
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");
126
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");
131
132         IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n");
133         IO::print(out, "</Project>\n");
134
135         return true;
136 }