cmGlobalVisualStudio71Generator.cxx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGlobalVisualStudio71Generator.h"
  4. #include <map>
  5. #include <sstream>
  6. #include "cmGeneratorTarget.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmGlobalVisualStudioGenerator.h"
  9. #include "cmListFileCache.h"
  10. #include "cmLocalGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. class cmake;
  15. cmGlobalVisualStudio71Generator::cmGlobalVisualStudio71Generator(
  16. cmake* cm, const std::string& platformName)
  17. : cmGlobalVisualStudio7Generator(cm, platformName)
  18. {
  19. this->ProjectConfigurationSectionName = "ProjectConfiguration";
  20. }
  21. void cmGlobalVisualStudio71Generator::WriteSLNFile(
  22. std::ostream& fout, cmLocalGenerator* root,
  23. std::vector<cmLocalGenerator*>& generators)
  24. {
  25. std::vector<std::string> configs =
  26. root->GetMakefile()->GetGeneratorConfigs(cmMakefile::ExcludeEmptyConfig);
  27. // Write out the header for a SLN file
  28. this->WriteSLNHeader(fout);
  29. // Collect all targets under this root generator and the transitive
  30. // closure of their dependencies.
  31. TargetDependSet projectTargets;
  32. TargetDependSet originalTargets;
  33. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  34. OrderedTargetDependSet orderedProjectTargets(
  35. projectTargets, this->GetStartupProjectName(root));
  36. // Generate the targets specification to a string. We will put this in
  37. // the actual .sln file later. As a side effect, this method also
  38. // populates the set of folders.
  39. std::ostringstream targetsSlnString;
  40. this->WriteTargetsToSolution(targetsSlnString, root, orderedProjectTargets);
  41. // Generate folder specification.
  42. bool useFolderProperty = this->UseFolderProperty();
  43. if (useFolderProperty) {
  44. this->WriteFolders(fout);
  45. }
  46. // Now write the actual target specification content.
  47. fout << targetsSlnString.str();
  48. // Write out the configurations information for the solution
  49. fout << "Global\n";
  50. // Write out the configurations for the solution
  51. this->WriteSolutionConfigurations(fout, configs);
  52. fout << "\tGlobalSection(" << this->ProjectConfigurationSectionName
  53. << ") = postSolution\n";
  54. // Write out the configurations for all the targets in the project
  55. this->WriteTargetConfigurations(fout, configs, orderedProjectTargets);
  56. fout << "\tEndGlobalSection\n";
  57. if (useFolderProperty) {
  58. // Write out project folders
  59. fout << "\tGlobalSection(NestedProjects) = preSolution\n";
  60. this->WriteFoldersContent(fout);
  61. fout << "\tEndGlobalSection\n";
  62. }
  63. // Write out global sections
  64. this->WriteSLNGlobalSections(fout, root);
  65. // Write the footer for the SLN file
  66. this->WriteSLNFooter(fout);
  67. }
  68. void cmGlobalVisualStudio71Generator::WriteSolutionConfigurations(
  69. std::ostream& fout, std::vector<std::string> const& configs)
  70. {
  71. fout << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  72. for (std::string const& i : configs) {
  73. fout << "\t\t" << i << " = " << i << "\n";
  74. }
  75. fout << "\tEndGlobalSection\n";
  76. }
  77. // Write a dsp file into the SLN file,
  78. // Note, that dependencies from executables to
  79. // the libraries it uses are also done here
  80. void cmGlobalVisualStudio71Generator::WriteProject(std::ostream& fout,
  81. const std::string& dspname,
  82. const std::string& dir,
  83. cmGeneratorTarget const* t)
  84. {
  85. // check to see if this is a fortran build
  86. std::string ext = ".vcproj";
  87. const char* project =
  88. R"(Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = ")";
  89. if (this->TargetIsFortranOnly(t)) {
  90. ext = ".vfproj";
  91. project = R"(Project("{6989167D-11E4-40FE-8C1A-2192A86A7E90}") = ")";
  92. }
  93. if (t->IsCSharpOnly()) {
  94. ext = ".csproj";
  95. project = R"(Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = ")";
  96. }
  97. cmValue targetExt = t->GetProperty("GENERATOR_FILE_NAME_EXT");
  98. if (targetExt) {
  99. ext = *targetExt;
  100. }
  101. std::string guid = this->GetGUID(dspname);
  102. fout << project << dspname << "\", \"" << this->ConvertToSolutionPath(dir)
  103. << (!dir.empty() ? "\\" : "") << dspname << ext << "\", \"{" << guid
  104. << "}\"\n";
  105. fout << "\tProjectSection(ProjectDependencies) = postProject\n";
  106. this->WriteProjectDepends(fout, dspname, dir, t);
  107. fout << "\tEndProjectSection\n";
  108. fout << "EndProject\n";
  109. auto ui = this->UtilityDepends.find(t);
  110. if (ui != this->UtilityDepends.end()) {
  111. const char* uname = ui->second.c_str();
  112. /* clang-format off */
  113. fout << R"(Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = ")"
  114. << uname << "\", \""
  115. << this->ConvertToSolutionPath(dir) << (dir[0]? "\\":"")
  116. << uname << ".vcproj" << "\", \"{"
  117. << this->GetGUID(uname) << "}\"\n"
  118. << "\tProjectSection(ProjectDependencies) = postProject\n"
  119. << "\t\t{" << guid << "} = {" << guid << "}\n"
  120. << "\tEndProjectSection\n"
  121. << "EndProject\n";
  122. /* clang-format on */
  123. }
  124. }
  125. // Write a dsp file into the SLN file,
  126. // Note, that dependencies from executables to
  127. // the libraries it uses are also done here
  128. void cmGlobalVisualStudio71Generator::WriteProjectDepends(
  129. std::ostream& fout, const std::string&, const std::string&,
  130. cmGeneratorTarget const* target)
  131. {
  132. VSDependSet const& depends = this->VSTargetDepends[target];
  133. for (std::string const& name : depends) {
  134. std::string guid = this->GetGUID(name);
  135. if (guid.empty()) {
  136. std::string m = cmStrCat("Target: ", target->GetName(),
  137. " depends on unknown target: ", name);
  138. cmSystemTools::Error(m);
  139. }
  140. fout << "\t\t{" << guid << "} = {" << guid << "}\n";
  141. }
  142. }
  143. // Write a dsp file into the SLN file, Note, that dependencies from
  144. // executables to the libraries it uses are also done here
  145. void cmGlobalVisualStudio71Generator::WriteExternalProject(
  146. std::ostream& fout, const std::string& name, const std::string& location,
  147. cmValue typeGuid, const std::set<BT<std::pair<std::string, bool>>>& depends)
  148. {
  149. fout << "Project(\"{"
  150. << (typeGuid ? typeGuid
  151. : std::string(
  152. cmGlobalVisualStudio71Generator::ExternalProjectType(
  153. location)))
  154. << "}\") = \"" << name << "\", \""
  155. << this->ConvertToSolutionPath(location) << "\", \"{"
  156. << this->GetGUID(name) << "}\"\n";
  157. // write out the dependencies here VS 7.1 includes dependencies with the
  158. // project instead of in the global section
  159. if (!depends.empty()) {
  160. fout << "\tProjectSection(ProjectDependencies) = postProject\n";
  161. for (BT<std::pair<std::string, bool>> const& it : depends) {
  162. std::string const& dep = it.Value.first;
  163. if (this->IsDepInSolution(dep)) {
  164. fout << "\t\t{" << this->GetGUID(dep) << "} = {" << this->GetGUID(dep)
  165. << "}\n";
  166. }
  167. }
  168. fout << "\tEndProjectSection\n";
  169. }
  170. fout << "EndProject\n";
  171. }
  172. // Write a dsp file into the SLN file, Note, that dependencies from
  173. // executables to the libraries it uses are also done here
  174. void cmGlobalVisualStudio71Generator::WriteProjectConfigurations(
  175. std::ostream& fout, const std::string& name, cmGeneratorTarget const& target,
  176. std::vector<std::string> const& configs,
  177. const std::set<std::string>& configsPartOfDefaultBuild,
  178. std::string const& platformMapping)
  179. {
  180. const std::string& platformName =
  181. !platformMapping.empty() ? platformMapping : this->GetPlatformName();
  182. std::string guid = this->GetGUID(name);
  183. for (std::string const& i : configs) {
  184. std::vector<std::string> mapConfig;
  185. const char* dstConfig = i.c_str();
  186. if (target.GetProperty("EXTERNAL_MSPROJECT")) {
  187. if (cmValue m = target.GetProperty("MAP_IMPORTED_CONFIG_" +
  188. cmSystemTools::UpperCase(i))) {
  189. cmExpandList(*m, mapConfig);
  190. if (!mapConfig.empty()) {
  191. dstConfig = mapConfig[0].c_str();
  192. }
  193. }
  194. }
  195. fout << "\t\t{" << guid << "}." << i << ".ActiveCfg = " << dstConfig << "|"
  196. << platformName << std::endl;
  197. auto ci = configsPartOfDefaultBuild.find(i);
  198. if (!(ci == configsPartOfDefaultBuild.end())) {
  199. fout << "\t\t{" << guid << "}." << i << ".Build.0 = " << dstConfig << "|"
  200. << platformName << std::endl;
  201. }
  202. }
  203. }