cmGlobalVisualStudio71Generator.cxx 8.4 KB

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