cmXCodeScheme.cxx 16 KB

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