cmXCodeScheme.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "cmXCodeScheme.h"
  4. #include <iomanip>
  5. #include <iostream>
  6. #include <sstream>
  7. #include "cmGeneratedFileStream.h"
  8. #include "cmGeneratorTarget.h"
  9. #include "cmXMLSafe.h"
  10. cmXCodeScheme::cmXCodeScheme(cmXCodeObject* xcObj, const TestObjects& tests,
  11. const std::vector<std::string>& configList,
  12. unsigned int xcVersion)
  13. : Target(xcObj)
  14. , Tests(tests)
  15. , TargetName(xcObj->GetTarget()->GetName())
  16. , ConfigList(configList)
  17. , XcodeVersion(xcVersion)
  18. {
  19. }
  20. void cmXCodeScheme::WriteXCodeSharedScheme(const std::string& xcProjDir,
  21. const std::string& container)
  22. {
  23. // Create shared scheme sub-directory tree
  24. //
  25. std::string xcodeSchemeDir = xcProjDir;
  26. xcodeSchemeDir += "/xcshareddata/xcschemes";
  27. cmSystemTools::MakeDirectory(xcodeSchemeDir.c_str());
  28. std::string xcodeSchemeFile = xcodeSchemeDir;
  29. xcodeSchemeFile += "/";
  30. xcodeSchemeFile += this->TargetName;
  31. xcodeSchemeFile += ".xcscheme";
  32. cmGeneratedFileStream fout(xcodeSchemeFile.c_str());
  33. fout.SetCopyIfDifferent(true);
  34. if (!fout) {
  35. return;
  36. }
  37. WriteXCodeXCScheme(fout, container);
  38. }
  39. void cmXCodeScheme::WriteXCodeXCScheme(std::ostream& fout,
  40. const std::string& container)
  41. {
  42. cmXMLWriter xout(fout);
  43. xout.SetIndentationElement(std::string(3, ' '));
  44. xout.StartDocument();
  45. xout.StartElement("Scheme");
  46. xout.BreakAttributes();
  47. xout.Attribute("LastUpgradeVersion", WriteVersionString());
  48. xout.Attribute("version", "1.3");
  49. WriteBuildAction(xout, container);
  50. WriteTestAction(xout, FindConfiguration("Debug"), container);
  51. WriteLaunchAction(xout, FindConfiguration("Debug"), container);
  52. WriteProfileAction(xout, FindConfiguration("Release"));
  53. WriteAnalyzeAction(xout, FindConfiguration("Debug"));
  54. WriteArchiveAction(xout, FindConfiguration("Release"));
  55. xout.EndElement();
  56. }
  57. void cmXCodeScheme::WriteBuildAction(cmXMLWriter& xout,
  58. const std::string& container)
  59. {
  60. xout.StartElement("BuildAction");
  61. xout.BreakAttributes();
  62. xout.Attribute("parallelizeBuildables", "YES");
  63. xout.Attribute("buildImplicitDependencies", "YES");
  64. xout.StartElement("BuildActionEntries");
  65. xout.StartElement("BuildActionEntry");
  66. xout.BreakAttributes();
  67. xout.Attribute("buildForTesting", "YES");
  68. xout.Attribute("buildForRunning", "YES");
  69. xout.Attribute("buildForProfiling", "YES");
  70. xout.Attribute("buildForArchiving", "YES");
  71. xout.Attribute("buildForAnalyzing", "YES");
  72. WriteBuildableReference(xout, this->Target, container);
  73. xout.EndElement(); // BuildActionEntry
  74. xout.EndElement(); // BuildActionEntries
  75. xout.EndElement(); // BuildAction
  76. }
  77. void cmXCodeScheme::WriteTestAction(cmXMLWriter& xout,
  78. const std::string& configuration,
  79. const std::string& container)
  80. {
  81. xout.StartElement("TestAction");
  82. xout.BreakAttributes();
  83. xout.Attribute("buildConfiguration", configuration);
  84. xout.Attribute("selectedDebuggerIdentifier",
  85. "Xcode.DebuggerFoundation.Debugger.LLDB");
  86. xout.Attribute("selectedLauncherIdentifier",
  87. "Xcode.DebuggerFoundation.Launcher.LLDB");
  88. xout.Attribute("shouldUseLaunchSchemeArgsEnv", "YES");
  89. xout.StartElement("Testables");
  90. for (auto test : this->Tests) {
  91. xout.StartElement("TestableReference");
  92. xout.BreakAttributes();
  93. xout.Attribute("skipped", "NO");
  94. WriteBuildableReference(xout, test, container);
  95. xout.EndElement(); // TestableReference
  96. }
  97. xout.EndElement();
  98. if (IsTestable()) {
  99. xout.StartElement("MacroExpansion");
  100. WriteBuildableReference(xout, this->Target, container);
  101. xout.EndElement(); // MacroExpansion
  102. }
  103. xout.StartElement("AdditionalOptions");
  104. xout.EndElement();
  105. xout.EndElement(); // TestAction
  106. }
  107. void cmXCodeScheme::WriteLaunchAction(cmXMLWriter& xout,
  108. const std::string& configuration,
  109. const std::string& container)
  110. {
  111. xout.StartElement("LaunchAction");
  112. xout.BreakAttributes();
  113. xout.Attribute("buildConfiguration", configuration);
  114. xout.Attribute("selectedDebuggerIdentifier",
  115. "Xcode.DebuggerFoundation.Debugger.LLDB");
  116. xout.Attribute("selectedLauncherIdentifier",
  117. "Xcode.DebuggerFoundation.Launcher.LLDB");
  118. xout.Attribute("launchStyle", "0");
  119. xout.Attribute("useCustomWorkingDirectory", "NO");
  120. xout.Attribute("ignoresPersistentStateOnLaunch", "NO");
  121. xout.Attribute("debugDocumentVersioning", "YES");
  122. xout.Attribute("debugServiceExtension", "internal");
  123. xout.Attribute("allowLocationSimulation", "YES");
  124. if (IsExecutable(this->Target)) {
  125. xout.StartElement("BuildableProductRunnable");
  126. xout.BreakAttributes();
  127. xout.Attribute("runnableDebuggingMode", "0");
  128. } else {
  129. xout.StartElement("MacroExpansion");
  130. }
  131. WriteBuildableReference(xout, this->Target, container);
  132. xout.EndElement(); // MacroExpansion
  133. xout.StartElement("AdditionalOptions");
  134. xout.EndElement();
  135. xout.EndElement(); // LaunchAction
  136. }
  137. void cmXCodeScheme::WriteProfileAction(cmXMLWriter& xout,
  138. const std::string& configuration)
  139. {
  140. xout.StartElement("ProfileAction");
  141. xout.BreakAttributes();
  142. xout.Attribute("buildConfiguration", configuration);
  143. xout.Attribute("shouldUseLaunchSchemeArgsEnv", "YES");
  144. xout.Attribute("savedToolIdentifier", "");
  145. xout.Attribute("useCustomWorkingDirectory", "NO");
  146. xout.Attribute("debugDocumentVersioning", "YES");
  147. xout.EndElement();
  148. }
  149. void cmXCodeScheme::WriteAnalyzeAction(cmXMLWriter& xout,
  150. const std::string& configuration)
  151. {
  152. xout.StartElement("AnalyzeAction");
  153. xout.BreakAttributes();
  154. xout.Attribute("buildConfiguration", configuration);
  155. xout.EndElement();
  156. }
  157. void cmXCodeScheme::WriteArchiveAction(cmXMLWriter& xout,
  158. const std::string& configuration)
  159. {
  160. xout.StartElement("ArchiveAction");
  161. xout.BreakAttributes();
  162. xout.Attribute("buildConfiguration", configuration);
  163. xout.Attribute("revealArchiveInOrganizer", "YES");
  164. xout.EndElement();
  165. }
  166. void cmXCodeScheme::WriteBuildableReference(cmXMLWriter& xout,
  167. const cmXCodeObject* xcObj,
  168. const std::string& container)
  169. {
  170. xout.StartElement("BuildableReference");
  171. xout.BreakAttributes();
  172. xout.Attribute("BuildableIdentifier", "primary");
  173. xout.Attribute("BlueprintIdentifier", xcObj->GetId());
  174. xout.Attribute("BuildableName", xcObj->GetTarget()->GetFullName());
  175. xout.Attribute("BlueprintName", xcObj->GetTarget()->GetName());
  176. xout.Attribute("ReferencedContainer", "container:" + container);
  177. xout.EndElement();
  178. }
  179. std::string cmXCodeScheme::WriteVersionString()
  180. {
  181. std::ostringstream v;
  182. v << std::setfill('0') << std::setw(4) << this->XcodeVersion * 10;
  183. return v.str();
  184. }
  185. std::string cmXCodeScheme::FindConfiguration(const std::string& name)
  186. {
  187. // Try to find the desired configuration by name,
  188. // and if it's not found return first from the list
  189. //
  190. if (std::find(this->ConfigList.begin(), this->ConfigList.end(), name) ==
  191. this->ConfigList.end() &&
  192. !this->ConfigList.empty()) {
  193. return this->ConfigList[0];
  194. }
  195. return name;
  196. }
  197. bool cmXCodeScheme::IsTestable() const
  198. {
  199. return !this->Tests.empty() || IsExecutable(this->Target);
  200. }
  201. bool cmXCodeScheme::IsExecutable(const cmXCodeObject* target)
  202. {
  203. cmGeneratorTarget* gt = target->GetTarget();
  204. if (!gt) {
  205. cmSystemTools::Error("Error no target on xobject\n");
  206. return false;
  207. }
  208. return gt->GetType() == cmStateEnums::EXECUTABLE;
  209. }