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