File: cmVSSolution.cxx

package info (click to toggle)
cmake 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 152,348 kB
  • sloc: ansic: 403,894; cpp: 303,807; sh: 4,097; python: 3,582; yacc: 3,106; lex: 1,279; f90: 538; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 108; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (397 lines) | stat: -rw-r--r-- 13,646 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */
#include "cmVSSolution.h"

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>

#include <cm/string_view>
#include <cmext/string_view>

#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmXMLWriter.h"

namespace cm {
namespace VS {

cm::string_view const Solution::Project::TypeIdCSharp =
  "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC"_s;
cm::string_view const Solution::Project::TypeIdDatabase =
  "C8D11400-126E-41CD-887F-60BD40844F9E"_s;
cm::string_view const Solution::Project::TypeIdDefault =
  "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942"_s;
cm::string_view const Solution::Project::TypeIdFSharp =
  "F2A71F9B-5D33-465A-A702-920D77279786"_s;
cm::string_view const Solution::Project::TypeIdFortran =
  "6989167D-11E4-40FE-8C1A-2192A86A7E90"_s;
cm::string_view const Solution::Project::TypeIdPython =
  "888888A0-9F3D-457C-B088-3A5042F75D52"_s;
cm::string_view const Solution::Project::TypeIdVDProj =
  "54435603-DBB4-11D2-8724-00A0C9A8B90C"_s;
cm::string_view const Solution::Project::TypeIdVisualBasic =
  "F184B08F-C81C-45F6-A57F-5ABD9991F28F"_s;
cm::string_view const Solution::Project::TypeIdWinAppPkg =
  "C7167F0D-BC9F-4E6E-AFE1-012C56B48DB5"_s;
cm::string_view const Solution::Project::TypeIdWiX =
  "930C7802-8A8C-48F9-8165-68863BCCD9DD"_s;

cm::string_view const Solution::Folder::TypeId =
  "2150E333-8FDC-42A3-9474-1A3956D46DE8"_s;

std::vector<Solution::Project const*> Solution::GetAllProjects() const
{
  std::vector<Project const*> projects;
  projects.reserve(this->ProjectMap.size());
  for (Project const* project : this->Projects) {
    projects.emplace_back(project);
  }
  for (Folder const* folder : this->Folders) {
    for (Project const* project : folder->Projects) {
      projects.emplace_back(project);
    }
  }
  return projects;
}

namespace {
template <typename T>
T* GetEntry(std::map<cm::string_view, std::unique_ptr<T>>& entryMap,
            cm::string_view name)
{
  auto i = entryMap.find(name);
  if (i == entryMap.end()) {
    auto p = cm::make_unique<T>();
    p->Name = name;
    i = entryMap.emplace(p->Name, std::move(p)).first;
  }
  return i->second.get();
}
}

Solution::Folder* Solution::GetFolder(cm::string_view name)
{
  return GetEntry(this->FolderMap, name);
}

Solution::Project* Solution::GetProject(cm::string_view name)
{
  return GetEntry(this->ProjectMap, name);
}

Solution::PropertyGroup* Solution::GetPropertyGroup(cm::string_view name)
{
  return GetEntry(this->PropertyGroupMap, name);
}

namespace {
struct OrderByName
{
  template <typename T>
  bool operator()(T const* l, T const* r) const
  {
    return l->Name < r->Name;
  }
};
}

void Solution::CanonicalizeOrder()
{
  std::sort(this->Folders.begin(), this->Folders.end(), OrderByName());
  for (auto& fi : this->FolderMap) {
    Folder* folder = fi.second.get();
    std::sort(folder->Folders.begin(), folder->Folders.end(), OrderByName());
    std::sort(folder->Projects.begin(), folder->Projects.end(), OrderByName());
  }
  std::sort(this->Projects.begin(), this->Projects.end(), OrderByName());
  for (auto& pi : this->ProjectMap) {
    Project* project = pi.second.get();
    std::sort(project->BuildDependencies.begin(),
              project->BuildDependencies.end(), OrderByName());
  }
}

namespace {

void WriteSlnHeader(std::ostream& sln, Version version, VersionExpress express)
{
  char utf8bom[] = { char(0xEF), char(0xBB), char(0xBF) };
  sln.write(utf8bom, 3);
  sln << '\n';

  switch (version) {
    case Version::VS14:
      // Visual Studio 14 writes .sln format 12.00
      sln << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
      if (express == VersionExpress::Yes) {
        sln << "# Visual Studio Express 14 for Windows Desktop\n";
      } else {
        sln << "# Visual Studio 14\n";
      }
      break;
    case Version::VS15:
      // Visual Studio 15 writes .sln format 12.00
      sln << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
      sln << "# Visual Studio 15\n";
      break;
    case Version::VS16:
      // Visual Studio 16 writes .sln format 12.00
      sln << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
      sln << "# Visual Studio Version 16\n";
      break;
    case Version::VS17:
      // Visual Studio 17 writes .sln format 12.00
      sln << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
      sln << "# Visual Studio Version 17\n";
      break;
    case Version::VS18:
      // Visual Studio 18 writes .sln format 12.00
      sln << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
      sln << "# Visual Studio Version 18\n";
      break;
  }
}

void WriteSlnProject(std::ostream& sln, Solution::Project const& project)
{
  std::string projectPath = project.Path;
  std::replace(projectPath.begin(), projectPath.end(), '/', '\\');
  sln << "Project(\"{" << project.TypeId << "}\") = \"" << project.Name
      << "\", \"" << projectPath << "\", \"{" << project.Id << "}\"\n";
  sln << "\tProjectSection(ProjectDependencies) = postProject\n";
  for (Solution::Project const* d : project.BuildDependencies) {
    sln << "\t\t{" << d->Id << "} = {" << d->Id << "}\n";
  }
  sln << "\tEndProjectSection\n";
  sln << "EndProject\n";
}

void WriteSlnFolder(std::ostream& sln, Solution::Folder const& folder)
{
  std::string folderName = folder.Name;
  std::replace(folderName.begin(), folderName.end(), '/', '\\');
  std::string const fileName = cmSystemTools::GetFilenameName(folder.Name);
  sln << "Project(\"{" << Solution::Folder::TypeId << "}\") = \"" << fileName
      << "\", \"" << folderName << "\", \"{" << folder.Id << "}\"\n";
  if (!folder.Files.empty()) {
    sln << "\tProjectSection(SolutionItems) = preProject\n";
    for (std::string const& item : folder.Files) {
      sln << "\t\t" << item << " = " << item << "\n";
    }
    sln << "\tEndProjectSection\n";
  }
  sln << "EndProject\n";
}

void WriteSlnSolutionConfigurationPlatforms(std::ostream& sln,
                                            Solution const& solution)
{
  sln << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  for (std::string const& config : solution.Configs) {
    sln << "\t\t" << config << '|' << solution.Platform << " = " << config
        << '|' << solution.Platform << '\n';
  }
  sln << "\tEndGlobalSection\n";
}

void WriteSlnProjectConfigurationPlatforms(std::ostream& sln,
                                           Solution const& solution,
                                           Solution::Project const& project)
{
  auto const writeStep = [&sln, &solution, &project](std::size_t i,
                                                     cm::string_view step) {
    sln << "\t\t{" << project.Id << "}." << solution.Configs[i] << '|'
        << solution.Platform << "." << step << " = "
        << project.Configs[i].Config << '|' << project.Platform << '\n';
  };
  assert(project.Configs.size() == solution.Configs.size());
  for (std::size_t i = 0; i < solution.Configs.size(); ++i) {
    writeStep(i, "ActiveCfg"_s);
    if (project.Configs[i].Build) {
      writeStep(i, "Build.0"_s);
    }
    if (project.Configs[i].Deploy) {
      writeStep(i, "Deploy.0"_s);
    }
  }
}

void WriteSlnProjectConfigurationPlatforms(
  std::ostream& sln, Solution const& solution,
  std::vector<Solution::Project const*> const& projects)
{
  sln << "\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n";
  for (Solution::Project const* project : projects) {
    WriteSlnProjectConfigurationPlatforms(sln, solution, *project);
  }
  sln << "\tEndGlobalSection\n";
}

void WriteSlnNestedProjects(
  std::ostream& sln, std::vector<Solution::Folder const*> const& folders)
{
  sln << "\tGlobalSection(NestedProjects) = preSolution\n";
  for (Solution::Folder const* folder : folders) {
    for (Solution::Folder const* nestedFolder : folder->Folders) {
      sln << "\t\t{" << nestedFolder->Id << "} = {" << folder->Id << "}\n";
    }
    for (Solution::Project const* project : folder->Projects) {
      sln << "\t\t{" << project->Id << "} = {" << folder->Id << "}\n";
    }
  }
  sln << "\tEndGlobalSection\n";
}

void WriteSlnPropertyGroup(std::ostream& sln,
                           Solution::PropertyGroup const& pg)
{
  cm::string_view const order = pg.Scope == Solution::PropertyGroup::Load::Pre
    ? "preSolution"_s
    : "postSolution"_s;
  sln << "\tGlobalSection(" << pg.Name << ") = " << order << '\n';
  for (auto const& i : pg.Map) {
    sln << "\t\t" << i.first << " = " << i.second << '\n';
  }
  sln << "\tEndGlobalSection\n";
}

}

void WriteSln(std::ostream& sln, Solution const& solution)
{
  assert(solution.VSVersion);
  assert(solution.VSExpress);

  std::vector<Solution::Project const*> projects = solution.GetAllProjects();
  std::sort(projects.begin(), projects.end(),
            [&solution](Solution::Project const* l,
                        Solution::Project const* r) -> bool {
              if (r->Name == solution.StartupProject) {
                return false;
              }
              if (l->Name == solution.StartupProject) {
                return true;
              }
              return l->Name < r->Name;
            });

  WriteSlnHeader(sln, *solution.VSVersion, *solution.VSExpress);
  for (Solution::Folder const* folder : solution.Folders) {
    WriteSlnFolder(sln, *folder);
  }
  for (Solution::Project const* project : projects) {
    WriteSlnProject(sln, *project);
  }
  sln << "Global\n";
  WriteSlnSolutionConfigurationPlatforms(sln, solution);
  WriteSlnProjectConfigurationPlatforms(sln, solution, projects);
  if (!solution.Folders.empty()) {
    WriteSlnNestedProjects(sln, solution.Folders);
  }
  for (Solution::PropertyGroup const* pg : solution.PropertyGroups) {
    WriteSlnPropertyGroup(sln, *pg);
  }
  sln << "EndGlobal\n";
}

namespace {
void WriteSlnxSolutionConfigurationPlatforms(cmXMLElement& xmlParent,
                                             Solution const& solution)
{
  cmXMLElement xmlConfigurations(xmlParent, "Configurations");
  for (std::string const& c : solution.Configs) {
    cmXMLElement(xmlConfigurations, "BuildType").Attribute("Name", c);
  }
  cmXMLElement(xmlConfigurations, "Platform")
    .Attribute("Name", solution.Platform);
};

void WriteSlnxProject(cmXMLElement& xmlParent, Solution const& solution,
                      Solution::Project const& project)
{
  cmXMLElement xmlProject(xmlParent, "Project");
  xmlProject.Attribute("Path", project.Path);
  xmlProject.Attribute("Type", cmSystemTools::LowerCase(project.TypeId));
  xmlProject.Attribute("Id", cmSystemTools::LowerCase(project.Id));
  if (project.Name == solution.StartupProject) {
    xmlProject.Attribute("DefaultStartup", "true");
  }
  for (Solution::Project const* d : project.BuildDependencies) {
    cmXMLElement(xmlProject, "BuildDependency").Attribute("Project", d->Path);
  }
  assert(project.Configs.size() == solution.Configs.size());
  for (std::size_t i = 0; i < solution.Configs.size(); ++i) {
    if (project.Configs[i].Config != solution.Configs[i]) {
      cmXMLElement(xmlProject, "BuildType")
        .Attribute("Solution", cmStrCat(solution.Configs[i], "|*"))
        .Attribute("Project", project.Configs[i].Config);
    }
    if (!project.Configs[i].Build) {
      cmXMLElement(xmlProject, "Build")
        .Attribute("Solution", cmStrCat(solution.Configs[i], "|*"))
        .Attribute("Project", "false");
    }
    if (project.Configs[i].Deploy) {
      cmXMLElement(xmlProject, "Deploy")
        .Attribute("Solution", cmStrCat(solution.Configs[i], "|*"));
    }
  }
  if (project.Platform != solution.Platform ||
      // C# projects do not build interactively in the VS IDE unless they
      // have an explicit platform, even if it matches the SLN platform.
      project.TypeId == Solution::Project::TypeIdCSharp) {
    cmXMLElement(xmlProject, "Platform")
      .Attribute("Project", project.Platform);
  }
};

void WriteSlnxFolder(cmXMLElement& xmlParent, Solution const& solution,
                     Solution::Folder const& folder)
{
  cmXMLElement xmlFolder(xmlParent, "Folder");
  xmlFolder.Attribute("Name", cmStrCat('/', folder.Name, '/'));
  for (std::string const& filePath : folder.Files) {
    cmXMLElement(xmlFolder, "File").Attribute("Path", filePath);
  }
  for (Solution::Project const* project : folder.Projects) {
    WriteSlnxProject(xmlFolder, solution, *project);
  }
};

void WriteSlnxPropertyGroup(cmXMLElement& xmlParent,
                            Solution::PropertyGroup const& pg)
{
  cmXMLElement xmlProperties(xmlParent, "Properties");
  xmlProperties.Attribute("Name", pg.Name);
  if (pg.Scope == Solution::PropertyGroup::Load::Post) {
    xmlProperties.Attribute("Scope", "PostLoad");
  }
  for (auto const& i : pg.Map) {
    cmXMLElement(xmlProperties, "Properties")
      .Attribute("Name", i.first)
      .Attribute("Value", i.second);
  }
}
}

void WriteSlnx(std::ostream& slnx, Solution const& solution)
{
  cmXMLWriter xw(slnx);
  cmXMLDocument xml(xw);
  cmXMLElement xmlSolution(xml, "Solution");
  WriteSlnxSolutionConfigurationPlatforms(xmlSolution, solution);
  for (Solution::Project const* project : solution.Projects) {
    WriteSlnxProject(xmlSolution, solution, *project);
  }
  for (Solution::Folder const* folder : solution.Folders) {
    WriteSlnxFolder(xmlSolution, solution, *folder);
  }
  for (Solution::PropertyGroup const* pg : solution.PropertyGroups) {
    WriteSlnxPropertyGroup(xmlSolution, *pg);
  }
}

}
}