1
0

cmXCodeScheme.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 <utility>
  8. #include <cmext/algorithm>
  9. #include "cmsys/String.h"
  10. #include "cmGeneratedFileStream.h"
  11. #include "cmGeneratorExpression.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmXMLSafe.h"
  14. cmXCodeScheme::cmXCodeScheme(cmLocalGenerator* lg, cmXCodeObject* xcObj,
  15. TestObjects tests,
  16. const std::vector<std::string>& configList,
  17. unsigned int xcVersion)
  18. : LocalGenerator(lg)
  19. , Target(xcObj)
  20. , Tests(std::move(tests))
  21. , TargetName(xcObj->GetTarget()->GetName())
  22. , ConfigList(configList)
  23. , XcodeVersion(xcVersion)
  24. {
  25. }
  26. void cmXCodeScheme::WriteXCodeSharedScheme(const std::string& xcProjDir,
  27. const std::string& container)
  28. {
  29. // Create shared scheme sub-directory tree
  30. //
  31. std::string xcodeSchemeDir = cmStrCat(xcProjDir, "/xcshareddata/xcschemes");
  32. cmSystemTools::MakeDirectory(xcodeSchemeDir);
  33. std::string xcodeSchemeFile =
  34. cmStrCat(xcodeSchemeDir, '/', this->TargetName, ".xcscheme");
  35. cmGeneratedFileStream fout(xcodeSchemeFile);
  36. fout.SetCopyIfDifferent(true);
  37. if (!fout) {
  38. return;
  39. }
  40. WriteXCodeXCScheme(fout, container);
  41. }
  42. void cmXCodeScheme::WriteXCodeXCScheme(std::ostream& fout,
  43. const std::string& container)
  44. {
  45. cmXMLWriter xout(fout);
  46. xout.SetIndentationElement(std::string(3, ' '));
  47. xout.StartDocument();
  48. xout.StartElement("Scheme");
  49. xout.BreakAttributes();
  50. xout.Attribute("LastUpgradeVersion", WriteVersionString());
  51. xout.Attribute("version", "1.3");
  52. WriteBuildAction(xout, container);
  53. WriteTestAction(xout, FindConfiguration("Debug"), container);
  54. WriteLaunchAction(xout, FindConfiguration("Debug"), container);
  55. WriteProfileAction(xout, FindConfiguration("Release"));
  56. WriteAnalyzeAction(xout, FindConfiguration("Debug"));
  57. WriteArchiveAction(xout, FindConfiguration("Release"));
  58. xout.EndElement();
  59. }
  60. void cmXCodeScheme::WriteBuildAction(cmXMLWriter& xout,
  61. const std::string& container)
  62. {
  63. xout.StartElement("BuildAction");
  64. xout.BreakAttributes();
  65. xout.Attribute("parallelizeBuildables", "YES");
  66. xout.Attribute("buildImplicitDependencies", "YES");
  67. xout.StartElement("BuildActionEntries");
  68. xout.StartElement("BuildActionEntry");
  69. xout.BreakAttributes();
  70. xout.Attribute("buildForTesting", "YES");
  71. xout.Attribute("buildForRunning", "YES");
  72. xout.Attribute("buildForProfiling", "YES");
  73. xout.Attribute("buildForArchiving", "YES");
  74. xout.Attribute("buildForAnalyzing", "YES");
  75. WriteBuildableReference(xout, this->Target, container);
  76. xout.EndElement(); // BuildActionEntry
  77. xout.EndElement(); // BuildActionEntries
  78. xout.EndElement(); // BuildAction
  79. }
  80. void cmXCodeScheme::WriteTestAction(cmXMLWriter& xout,
  81. const std::string& configuration,
  82. const std::string& container)
  83. {
  84. xout.StartElement("TestAction");
  85. xout.BreakAttributes();
  86. xout.Attribute("buildConfiguration", configuration);
  87. xout.Attribute("selectedDebuggerIdentifier",
  88. "Xcode.DebuggerFoundation.Debugger.LLDB");
  89. xout.Attribute("selectedLauncherIdentifier",
  90. "Xcode.DebuggerFoundation.Launcher.LLDB");
  91. xout.Attribute("shouldUseLaunchSchemeArgsEnv", "YES");
  92. xout.StartElement("Testables");
  93. for (auto test : this->Tests) {
  94. xout.StartElement("TestableReference");
  95. xout.BreakAttributes();
  96. xout.Attribute("skipped", "NO");
  97. WriteBuildableReference(xout, test, container);
  98. xout.EndElement(); // TestableReference
  99. }
  100. xout.EndElement();
  101. if (IsTestable()) {
  102. xout.StartElement("MacroExpansion");
  103. WriteBuildableReference(xout, this->Target, container);
  104. xout.EndElement(); // MacroExpansion
  105. }
  106. xout.StartElement("AdditionalOptions");
  107. xout.EndElement();
  108. xout.EndElement(); // TestAction
  109. }
  110. void cmXCodeScheme::WriteLaunchAction(cmXMLWriter& xout,
  111. const std::string& configuration,
  112. const std::string& container)
  113. {
  114. xout.StartElement("LaunchAction");
  115. xout.BreakAttributes();
  116. xout.Attribute("buildConfiguration", configuration);
  117. xout.Attribute("selectedDebuggerIdentifier",
  118. "Xcode.DebuggerFoundation.Debugger.LLDB");
  119. xout.Attribute("selectedLauncherIdentifier",
  120. "Xcode.DebuggerFoundation.Launcher.LLDB");
  121. xout.Attribute("launchStyle", "0");
  122. WriteCustomWorkingDirectory(xout, configuration);
  123. xout.Attribute("ignoresPersistentStateOnLaunch", "NO");
  124. WriteLaunchActionBooleanAttribute(xout, "debugDocumentVersioning",
  125. "XCODE_SCHEME_DEBUG_DOCUMENT_VERSIONING",
  126. true);
  127. xout.Attribute("debugServiceExtension", "internal");
  128. xout.Attribute("allowLocationSimulation", "YES");
  129. if (cmValue gpuFrameCaptureMode = this->Target->GetTarget()->GetProperty(
  130. "XCODE_SCHEME_ENABLE_GPU_FRAME_CAPTURE_MODE")) {
  131. std::string value = *gpuFrameCaptureMode;
  132. if (cmsysString_strcasecmp(value.c_str(), "Metal") == 0) {
  133. value = "1";
  134. } else if (cmsysString_strcasecmp(value.c_str(), "Disabled") == 0) {
  135. value = "3";
  136. }
  137. xout.Attribute("enableGPUFrameCaptureMode", value);
  138. }
  139. // Diagnostics tab begin
  140. bool useAddressSanitizer = WriteLaunchActionAttribute(
  141. xout, "enableAddressSanitizer",
  142. "XCODE_SCHEME_ADDRESS_SANITIZER"); // not allowed with
  143. // enableThreadSanitizer=YES
  144. WriteLaunchActionAttribute(
  145. xout, "enableASanStackUseAfterReturn",
  146. "XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN");
  147. bool useThreadSanitizer = false;
  148. if (!useAddressSanitizer) {
  149. useThreadSanitizer = WriteLaunchActionAttribute(
  150. xout, "enableThreadSanitizer",
  151. "XCODE_SCHEME_THREAD_SANITIZER"); // not allowed with
  152. // enableAddressSanitizer=YES
  153. }
  154. WriteLaunchActionAttribute(xout, "stopOnEveryThreadSanitizerIssue",
  155. "XCODE_SCHEME_THREAD_SANITIZER_STOP");
  156. WriteLaunchActionAttribute(xout, "enableUBSanitizer",
  157. "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER");
  158. WriteLaunchActionAttribute(
  159. xout, "stopOnEveryUBSanitizerIssue",
  160. "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER_STOP");
  161. WriteLaunchActionAttribute(
  162. xout, "disableMainThreadChecker",
  163. "XCODE_SCHEME_DISABLE_MAIN_THREAD_CHECKER"); // negative enabled!
  164. WriteLaunchActionAttribute(xout, "stopOnEveryMainThreadCheckerIssue",
  165. "XCODE_SCHEME_MAIN_THREAD_CHECKER_STOP");
  166. if (this->Target->GetTarget()->GetPropertyAsBool(
  167. "XCODE_SCHEME_DEBUG_AS_ROOT")) {
  168. xout.Attribute("debugAsWhichUser", "root");
  169. }
  170. // Diagnostics tab end
  171. if (IsExecutable(this->Target)) {
  172. xout.StartElement("BuildableProductRunnable");
  173. xout.BreakAttributes();
  174. xout.Attribute("runnableDebuggingMode", "0");
  175. } else {
  176. xout.StartElement("MacroExpansion");
  177. }
  178. WriteBuildableReference(xout, this->Target, container);
  179. xout.EndElement(); // MacroExpansion
  180. // Info tab begin
  181. if (cmValue exe =
  182. this->Target->GetTarget()->GetProperty("XCODE_SCHEME_EXECUTABLE")) {
  183. xout.StartElement("PathRunnable");
  184. xout.BreakAttributes();
  185. xout.Attribute("runnableDebuggingMode", "0");
  186. xout.Attribute("FilePath", *exe);
  187. xout.EndElement(); // PathRunnable
  188. }
  189. // Info tab end
  190. // Arguments tab begin
  191. if (cmValue argList =
  192. this->Target->GetTarget()->GetProperty("XCODE_SCHEME_ARGUMENTS")) {
  193. std::vector<std::string> arguments = cmExpandedList(*argList);
  194. if (!arguments.empty()) {
  195. xout.StartElement("CommandLineArguments");
  196. for (auto const& argument : arguments) {
  197. xout.StartElement("CommandLineArgument");
  198. xout.BreakAttributes();
  199. xout.Attribute("argument", argument);
  200. xout.Attribute("isEnabled", "YES");
  201. xout.EndElement(); // CommandLineArgument
  202. }
  203. xout.EndElement(); // CommandLineArguments
  204. }
  205. }
  206. if (cmValue envList =
  207. this->Target->GetTarget()->GetProperty("XCODE_SCHEME_ENVIRONMENT")) {
  208. std::vector<std::string> envs = cmExpandedList(*envList);
  209. if (!envs.empty()) {
  210. xout.StartElement("EnvironmentVariables");
  211. for (auto env : envs) {
  212. xout.StartElement("EnvironmentVariable");
  213. xout.BreakAttributes();
  214. std::string envValue;
  215. const auto p = env.find_first_of('=');
  216. if (p != std::string::npos) {
  217. envValue = env.substr(p + 1);
  218. env.resize(p);
  219. }
  220. xout.Attribute("key", env);
  221. xout.Attribute("value", envValue);
  222. xout.Attribute("isEnabled", "YES");
  223. xout.EndElement(); // EnvironmentVariable
  224. }
  225. xout.EndElement(); // EnvironmentVariables
  226. }
  227. }
  228. // Arguments tab end
  229. xout.StartElement("AdditionalOptions");
  230. if (!useThreadSanitizer) {
  231. WriteLaunchActionAdditionalOption(xout, "MallocScribble", "",
  232. "XCODE_SCHEME_MALLOC_SCRIBBLE");
  233. }
  234. if (!useThreadSanitizer && !useAddressSanitizer) {
  235. WriteLaunchActionAdditionalOption(xout, "MallocGuardEdges", "",
  236. "XCODE_SCHEME_MALLOC_GUARD_EDGES");
  237. }
  238. if (!useThreadSanitizer && !useAddressSanitizer) {
  239. WriteLaunchActionAdditionalOption(xout, "DYLD_INSERT_LIBRARIES",
  240. "/usr/lib/libgmalloc.dylib",
  241. "XCODE_SCHEME_GUARD_MALLOC");
  242. }
  243. WriteLaunchActionAdditionalOption(xout, "NSZombieEnabled", "YES",
  244. "XCODE_SCHEME_ZOMBIE_OBJECTS");
  245. if (!useThreadSanitizer && !useAddressSanitizer) {
  246. WriteLaunchActionAdditionalOption(xout, "MallocStackLogging", "",
  247. "XCODE_SCHEME_MALLOC_STACK");
  248. }
  249. WriteLaunchActionAdditionalOption(xout, "DYLD_PRINT_APIS", "",
  250. "XCODE_SCHEME_DYNAMIC_LINKER_API_USAGE");
  251. WriteLaunchActionAdditionalOption(xout, "DYLD_PRINT_LIBRARIES", "",
  252. "XCODE_SCHEME_DYNAMIC_LIBRARY_LOADS");
  253. xout.EndElement();
  254. xout.EndElement(); // LaunchAction
  255. }
  256. bool cmXCodeScheme::WriteLaunchActionAttribute(cmXMLWriter& xout,
  257. const std::string& attrName,
  258. const std::string& varName)
  259. {
  260. if (Target->GetTarget()->GetPropertyAsBool(varName)) {
  261. xout.Attribute(attrName.c_str(), "YES");
  262. return true;
  263. }
  264. return false;
  265. }
  266. bool cmXCodeScheme::WriteLaunchActionBooleanAttribute(
  267. cmXMLWriter& xout, const std::string& attrName, const std::string& varName,
  268. bool defaultValue)
  269. {
  270. cmValue property = Target->GetTarget()->GetProperty(varName);
  271. bool isOn = (!property && defaultValue) || cmIsOn(property);
  272. if (isOn) {
  273. xout.Attribute(attrName.c_str(), "YES");
  274. } else {
  275. xout.Attribute(attrName.c_str(), "NO");
  276. }
  277. return isOn;
  278. }
  279. bool cmXCodeScheme::WriteLaunchActionAdditionalOption(
  280. cmXMLWriter& xout, const std::string& key, const std::string& value,
  281. const std::string& varName)
  282. {
  283. if (Target->GetTarget()->GetPropertyAsBool(varName)) {
  284. xout.StartElement("AdditionalOption");
  285. xout.BreakAttributes();
  286. xout.Attribute("key", key);
  287. xout.Attribute("value", value);
  288. xout.Attribute("isEnabled", "YES");
  289. xout.EndElement(); // AdditionalOption
  290. return true;
  291. }
  292. return false;
  293. }
  294. void cmXCodeScheme::WriteProfileAction(cmXMLWriter& xout,
  295. const std::string& configuration)
  296. {
  297. xout.StartElement("ProfileAction");
  298. xout.BreakAttributes();
  299. xout.Attribute("buildConfiguration", configuration);
  300. xout.Attribute("shouldUseLaunchSchemeArgsEnv", "YES");
  301. xout.Attribute("savedToolIdentifier", "");
  302. WriteCustomWorkingDirectory(xout, configuration);
  303. WriteLaunchActionBooleanAttribute(xout, "debugDocumentVersioning",
  304. "XCODE_SCHEME_DEBUG_DOCUMENT_VERSIONING",
  305. true);
  306. xout.EndElement();
  307. }
  308. void cmXCodeScheme::WriteAnalyzeAction(cmXMLWriter& xout,
  309. const std::string& configuration)
  310. {
  311. xout.StartElement("AnalyzeAction");
  312. xout.BreakAttributes();
  313. xout.Attribute("buildConfiguration", configuration);
  314. xout.EndElement();
  315. }
  316. void cmXCodeScheme::WriteArchiveAction(cmXMLWriter& xout,
  317. const std::string& configuration)
  318. {
  319. xout.StartElement("ArchiveAction");
  320. xout.BreakAttributes();
  321. xout.Attribute("buildConfiguration", configuration);
  322. xout.Attribute("revealArchiveInOrganizer", "YES");
  323. xout.EndElement();
  324. }
  325. void cmXCodeScheme::WriteBuildableReference(cmXMLWriter& xout,
  326. const cmXCodeObject* xcObj,
  327. const std::string& container)
  328. {
  329. xout.StartElement("BuildableReference");
  330. xout.BreakAttributes();
  331. xout.Attribute("BuildableIdentifier", "primary");
  332. xout.Attribute("BlueprintIdentifier", xcObj->GetId());
  333. std::string const noConfig; // FIXME: What config to use here?
  334. xout.Attribute("BuildableName", xcObj->GetTarget()->GetFullName(noConfig));
  335. xout.Attribute("BlueprintName", xcObj->GetTarget()->GetName());
  336. xout.Attribute("ReferencedContainer", "container:" + container);
  337. xout.EndElement();
  338. }
  339. void cmXCodeScheme::WriteCustomWorkingDirectory(
  340. cmXMLWriter& xout, const std::string& configuration)
  341. {
  342. std::string const& propertyValue =
  343. this->Target->GetTarget()->GetSafeProperty(
  344. "XCODE_SCHEME_WORKING_DIRECTORY");
  345. if (propertyValue.empty()) {
  346. xout.Attribute("useCustomWorkingDirectory", "NO");
  347. } else {
  348. xout.Attribute("useCustomWorkingDirectory", "YES");
  349. auto customWorkingDirectory = cmGeneratorExpression::Evaluate(
  350. propertyValue, this->LocalGenerator, configuration);
  351. xout.Attribute("customWorkingDirectory", customWorkingDirectory);
  352. }
  353. }
  354. std::string cmXCodeScheme::WriteVersionString()
  355. {
  356. std::ostringstream v;
  357. v << std::setfill('0') << std::setw(4) << this->XcodeVersion * 10;
  358. return v.str();
  359. }
  360. std::string cmXCodeScheme::FindConfiguration(const std::string& name)
  361. {
  362. // Try to find the desired configuration by name,
  363. // and if it's not found return first from the list
  364. //
  365. if (!cm::contains(this->ConfigList, name) && !this->ConfigList.empty()) {
  366. return this->ConfigList[0];
  367. }
  368. return name;
  369. }
  370. bool cmXCodeScheme::IsTestable() const
  371. {
  372. return !this->Tests.empty() || IsExecutable(this->Target);
  373. }
  374. bool cmXCodeScheme::IsExecutable(const cmXCodeObject* target)
  375. {
  376. cmGeneratorTarget* gt = target->GetTarget();
  377. if (!gt) {
  378. cmSystemTools::Error("Error no target on xobject\n");
  379. return false;
  380. }
  381. return gt->GetType() == cmStateEnums::EXECUTABLE;
  382. }