cmXCodeScheme.cxx 7.8 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. 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 (TestObjects::const_iterator it = this->Tests.begin();
  91. it != this->Tests.end(); ++it) {
  92. xout.StartElement("TestableReference");
  93. xout.BreakAttributes();
  94. xout.Attribute("skipped", "NO");
  95. WriteBuildableReference(xout, *it, container);
  96. xout.EndElement(); // TestableReference
  97. }
  98. xout.EndElement();
  99. if (IsTestable()) {
  100. xout.StartElement("MacroExpansion");
  101. WriteBuildableReference(xout, this->Target, container);
  102. xout.EndElement(); // MacroExpansion
  103. }
  104. xout.StartElement("AdditionalOptions");
  105. xout.EndElement();
  106. xout.EndElement(); // TestAction
  107. }
  108. void cmXCodeScheme::WriteLaunchAction(cmXMLWriter& xout,
  109. std::string configuration,
  110. const std::string& container)
  111. {
  112. xout.StartElement("LaunchAction");
  113. xout.BreakAttributes();
  114. xout.Attribute("buildConfiguration", configuration);
  115. xout.Attribute("selectedDebuggerIdentifier",
  116. "Xcode.DebuggerFoundation.Debugger.LLDB");
  117. xout.Attribute("selectedLauncherIdentifier",
  118. "Xcode.DebuggerFoundation.Launcher.LLDB");
  119. xout.Attribute("launchStyle", "0");
  120. xout.Attribute("useCustomWorkingDirectory", "NO");
  121. xout.Attribute("ignoresPersistentStateOnLaunch", "NO");
  122. xout.Attribute("debugDocumentVersioning", "YES");
  123. xout.Attribute("debugServiceExtension", "internal");
  124. xout.Attribute("allowLocationSimulation", "YES");
  125. if (IsExecutable(this->Target)) {
  126. xout.StartElement("BuildableProductRunnable");
  127. xout.BreakAttributes();
  128. xout.Attribute("runnableDebuggingMode", "0");
  129. } else {
  130. xout.StartElement("MacroExpansion");
  131. }
  132. WriteBuildableReference(xout, this->Target, container);
  133. xout.EndElement(); // MacroExpansion
  134. xout.StartElement("AdditionalOptions");
  135. xout.EndElement();
  136. xout.EndElement(); // LaunchAction
  137. }
  138. void cmXCodeScheme::WriteProfileAction(cmXMLWriter& xout,
  139. std::string configuration)
  140. {
  141. xout.StartElement("ProfileAction");
  142. xout.BreakAttributes();
  143. xout.Attribute("buildConfiguration", configuration);
  144. xout.Attribute("shouldUseLaunchSchemeArgsEnv", "YES");
  145. xout.Attribute("savedToolIdentifier", "");
  146. xout.Attribute("useCustomWorkingDirectory", "NO");
  147. xout.Attribute("debugDocumentVersioning", "YES");
  148. xout.EndElement();
  149. }
  150. void cmXCodeScheme::WriteAnalyzeAction(cmXMLWriter& xout,
  151. std::string configuration)
  152. {
  153. xout.StartElement("AnalyzeAction");
  154. xout.BreakAttributes();
  155. xout.Attribute("buildConfiguration", configuration);
  156. xout.EndElement();
  157. }
  158. void cmXCodeScheme::WriteArchiveAction(cmXMLWriter& xout,
  159. std::string configuration)
  160. {
  161. xout.StartElement("ArchiveAction");
  162. xout.BreakAttributes();
  163. xout.Attribute("buildConfiguration", configuration);
  164. xout.Attribute("revealArchiveInOrganizer", "YES");
  165. xout.EndElement();
  166. }
  167. void cmXCodeScheme::WriteBuildableReference(cmXMLWriter& xout,
  168. const cmXCodeObject* xcObj,
  169. const std::string& container)
  170. {
  171. xout.StartElement("BuildableReference");
  172. xout.BreakAttributes();
  173. xout.Attribute("BuildableIdentifier", "primary");
  174. xout.Attribute("BlueprintIdentifier", xcObj->GetId());
  175. xout.Attribute("BuildableName", xcObj->GetTarget()->GetFullName());
  176. xout.Attribute("BlueprintName", xcObj->GetTarget()->GetName());
  177. xout.Attribute("ReferencedContainer", "container:" + container);
  178. xout.EndElement();
  179. }
  180. std::string cmXCodeScheme::WriteVersionString()
  181. {
  182. std::ostringstream v;
  183. v << std::setfill('0') << std::setw(4) << this->XcodeVersion * 10;
  184. return v.str();
  185. }
  186. std::string cmXCodeScheme::FindConfiguration(const std::string& name)
  187. {
  188. // Try to find the desired configuration by name,
  189. // and if it's not found return first from the list
  190. //
  191. if (std::find(this->ConfigList.begin(), this->ConfigList.end(), name) ==
  192. this->ConfigList.end() &&
  193. !this->ConfigList.empty())
  194. return this->ConfigList[0];
  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. }