cmXCodeScheme.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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);
  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. // Diagnostics tab begin
  125. bool useAddressSanitizer = WriteLaunchActionAttribute(
  126. xout, "enableAddressSanitizer",
  127. "XCODE_SCHEME_ADDRESS_SANITIZER"); // not allowed with
  128. // enableThreadSanitizer=YES
  129. WriteLaunchActionAttribute(
  130. xout, "enableASanStackUseAfterReturn",
  131. "XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN");
  132. bool useThreadSanitizer = false;
  133. if (!useAddressSanitizer) {
  134. useThreadSanitizer = WriteLaunchActionAttribute(
  135. xout, "enableThreadSanitizer",
  136. "XCODE_SCHEME_THREAD_SANITIZER"); // not allowed with
  137. // enableAddressSanitizer=YES
  138. }
  139. WriteLaunchActionAttribute(xout, "stopOnEveryThreadSanitizerIssue",
  140. "XCODE_SCHEME_THREAD_SANITIZER_STOP");
  141. WriteLaunchActionAttribute(xout, "enableUBSanitizer",
  142. "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER");
  143. WriteLaunchActionAttribute(
  144. xout, "stopOnEveryUBSanitizerIssue",
  145. "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER_STOP");
  146. WriteLaunchActionAttribute(
  147. xout, "disableMainThreadChecker",
  148. "XCODE_SCHEME_DISABLE_MAIN_THREAD_CHECKER"); // negative enabled!
  149. WriteLaunchActionAttribute(xout, "stopOnEveryMainThreadCheckerIssue",
  150. "XCODE_SCHEME_MAIN_THREAD_CHECKER_STOP");
  151. // Diagnostics tab end
  152. if (IsExecutable(this->Target)) {
  153. xout.StartElement("BuildableProductRunnable");
  154. xout.BreakAttributes();
  155. xout.Attribute("runnableDebuggingMode", "0");
  156. } else {
  157. xout.StartElement("MacroExpansion");
  158. }
  159. WriteBuildableReference(xout, this->Target, container);
  160. xout.EndElement(); // MacroExpansion
  161. // Info tab begin
  162. if (const char* exe =
  163. this->Target->GetTarget()->GetProperty("XCODE_SCHEME_EXECUTABLE")) {
  164. xout.StartElement("PathRunnable");
  165. xout.BreakAttributes();
  166. xout.Attribute("runnableDebuggingMode", "0");
  167. xout.Attribute("FilePath", exe);
  168. xout.EndElement(); // PathRunnable
  169. }
  170. // Info tab end
  171. // Arguments tab begin
  172. if (const char* argList =
  173. this->Target->GetTarget()->GetProperty("XCODE_SCHEME_ARGUMENTS")) {
  174. std::vector<std::string> arguments;
  175. cmSystemTools::ExpandListArgument(argList, arguments);
  176. if (!arguments.empty()) {
  177. xout.StartElement("CommandLineArguments");
  178. for (auto const& argument : arguments) {
  179. xout.StartElement("CommandLineArgument");
  180. xout.BreakAttributes();
  181. xout.Attribute("argument", argument);
  182. xout.Attribute("isEnabled", "YES");
  183. xout.EndElement(); // CommandLineArgument
  184. }
  185. xout.EndElement(); // CommandLineArguments
  186. }
  187. }
  188. if (const char* envList =
  189. this->Target->GetTarget()->GetProperty("XCODE_SCHEME_ENVIRONMENT")) {
  190. std::vector<std::string> envs;
  191. cmSystemTools::ExpandListArgument(envList, envs);
  192. if (!envs.empty()) {
  193. xout.StartElement("EnvironmentVariables");
  194. for (auto env : envs) {
  195. xout.StartElement("EnvironmentVariable");
  196. xout.BreakAttributes();
  197. std::string envValue;
  198. const auto p = env.find_first_of('=');
  199. if (p != std::string::npos) {
  200. envValue = env.substr(p + 1);
  201. env.resize(p);
  202. }
  203. xout.Attribute("key", env);
  204. xout.Attribute("value", envValue);
  205. xout.Attribute("isEnabled", "YES");
  206. xout.EndElement(); // EnvironmentVariable
  207. }
  208. xout.EndElement(); // EnvironmentVariables
  209. }
  210. }
  211. // Arguments tab end
  212. xout.StartElement("AdditionalOptions");
  213. if (!useThreadSanitizer) {
  214. WriteLaunchActionAdditionalOption(xout, "MallocScribble", "",
  215. "XCODE_SCHEME_MALLOC_SCRIBBLE");
  216. }
  217. if (!useThreadSanitizer && !useAddressSanitizer) {
  218. WriteLaunchActionAdditionalOption(xout, "MallocGuardEdges", "",
  219. "XCODE_SCHEME_MALLOC_GUARD_EDGES");
  220. }
  221. if (!useThreadSanitizer && !useAddressSanitizer) {
  222. WriteLaunchActionAdditionalOption(xout, "DYLD_INSERT_LIBRARIES",
  223. "/usr/lib/libgmalloc.dylib",
  224. "XCODE_SCHEME_GUARD_MALLOC");
  225. }
  226. WriteLaunchActionAdditionalOption(xout, "NSZombieEnabled", "YES",
  227. "XCODE_SCHEME_ZOMBIE_OBJECTS");
  228. if (!useThreadSanitizer && !useAddressSanitizer) {
  229. WriteLaunchActionAdditionalOption(xout, "MallocStackLogging", "",
  230. "XCODE_SCHEME_MALLOC_STACK");
  231. }
  232. WriteLaunchActionAdditionalOption(xout, "DYLD_PRINT_APIS", "",
  233. "XCODE_SCHEME_DYNAMIC_LINKER_API_USAGE");
  234. WriteLaunchActionAdditionalOption(xout, "DYLD_PRINT_LIBRARIES", "",
  235. "XCODE_SCHEME_DYNAMIC_LIBRARY_LOADS");
  236. xout.EndElement();
  237. xout.EndElement(); // LaunchAction
  238. }
  239. bool cmXCodeScheme::WriteLaunchActionAttribute(cmXMLWriter& xout,
  240. const std::string& attrName,
  241. const std::string& varName)
  242. {
  243. if (Target->GetTarget()->GetPropertyAsBool(varName)) {
  244. xout.Attribute(attrName.c_str(), "YES");
  245. return true;
  246. }
  247. return false;
  248. }
  249. bool cmXCodeScheme::WriteLaunchActionAdditionalOption(
  250. cmXMLWriter& xout, const std::string& key, const std::string& value,
  251. const std::string& varName)
  252. {
  253. if (Target->GetTarget()->GetPropertyAsBool(varName)) {
  254. xout.StartElement("AdditionalOption");
  255. xout.BreakAttributes();
  256. xout.Attribute("key", key);
  257. xout.Attribute("value", value);
  258. xout.Attribute("isEnabled", "YES");
  259. xout.EndElement(); // AdditionalOption
  260. return true;
  261. }
  262. return false;
  263. }
  264. void cmXCodeScheme::WriteProfileAction(cmXMLWriter& xout,
  265. const std::string& configuration)
  266. {
  267. xout.StartElement("ProfileAction");
  268. xout.BreakAttributes();
  269. xout.Attribute("buildConfiguration", configuration);
  270. xout.Attribute("shouldUseLaunchSchemeArgsEnv", "YES");
  271. xout.Attribute("savedToolIdentifier", "");
  272. xout.Attribute("useCustomWorkingDirectory", "NO");
  273. xout.Attribute("debugDocumentVersioning", "YES");
  274. xout.EndElement();
  275. }
  276. void cmXCodeScheme::WriteAnalyzeAction(cmXMLWriter& xout,
  277. const std::string& configuration)
  278. {
  279. xout.StartElement("AnalyzeAction");
  280. xout.BreakAttributes();
  281. xout.Attribute("buildConfiguration", configuration);
  282. xout.EndElement();
  283. }
  284. void cmXCodeScheme::WriteArchiveAction(cmXMLWriter& xout,
  285. const std::string& configuration)
  286. {
  287. xout.StartElement("ArchiveAction");
  288. xout.BreakAttributes();
  289. xout.Attribute("buildConfiguration", configuration);
  290. xout.Attribute("revealArchiveInOrganizer", "YES");
  291. xout.EndElement();
  292. }
  293. void cmXCodeScheme::WriteBuildableReference(cmXMLWriter& xout,
  294. const cmXCodeObject* xcObj,
  295. const std::string& container)
  296. {
  297. xout.StartElement("BuildableReference");
  298. xout.BreakAttributes();
  299. xout.Attribute("BuildableIdentifier", "primary");
  300. xout.Attribute("BlueprintIdentifier", xcObj->GetId());
  301. xout.Attribute("BuildableName", xcObj->GetTarget()->GetFullName());
  302. xout.Attribute("BlueprintName", xcObj->GetTarget()->GetName());
  303. xout.Attribute("ReferencedContainer", "container:" + container);
  304. xout.EndElement();
  305. }
  306. std::string cmXCodeScheme::WriteVersionString()
  307. {
  308. std::ostringstream v;
  309. v << std::setfill('0') << std::setw(4) << this->XcodeVersion * 10;
  310. return v.str();
  311. }
  312. std::string cmXCodeScheme::FindConfiguration(const std::string& name)
  313. {
  314. // Try to find the desired configuration by name,
  315. // and if it's not found return first from the list
  316. //
  317. if (std::find(this->ConfigList.begin(), this->ConfigList.end(), name) ==
  318. this->ConfigList.end() &&
  319. !this->ConfigList.empty()) {
  320. return this->ConfigList[0];
  321. }
  322. return name;
  323. }
  324. bool cmXCodeScheme::IsTestable() const
  325. {
  326. return !this->Tests.empty() || IsExecutable(this->Target);
  327. }
  328. bool cmXCodeScheme::IsExecutable(const cmXCodeObject* target)
  329. {
  330. cmGeneratorTarget* gt = target->GetTarget();
  331. if (!gt) {
  332. cmSystemTools::Error("Error no target on xobject\n");
  333. return false;
  334. }
  335. return gt->GetType() == cmStateEnums::EXECUTABLE;
  336. }