cmXCodeScheme.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 "cmsys/String.h"
  9. #include "cmGeneratedFileStream.h"
  10. #include "cmGeneratorExpression.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmStateTypes.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmSystemTools.h"
  15. #include "cmValue.h"
  16. #include "cmXCodeObject.h"
  17. #include "cmXMLWriter.h"
  18. class cmLocalGenerator;
  19. cmXCodeScheme::cmXCodeScheme(cmLocalGenerator* lg, cmXCodeObject* xcObj,
  20. TestObjects tests,
  21. const std::vector<std::string>& configList,
  22. unsigned int xcVersion)
  23. : LocalGenerator(lg)
  24. , Target(xcObj)
  25. , Tests(std::move(tests))
  26. , TargetName(xcObj->GetTarget()->GetName())
  27. , ConfigList(configList)
  28. , XcodeVersion(xcVersion)
  29. {
  30. }
  31. void cmXCodeScheme::WriteXCodeSharedScheme(const std::string& xcProjDir,
  32. const std::string& container)
  33. {
  34. // Create shared scheme sub-directory tree
  35. //
  36. std::string xcodeSchemeDir = cmStrCat(xcProjDir, "/xcshareddata/xcschemes");
  37. cmSystemTools::MakeDirectory(xcodeSchemeDir);
  38. std::string xcodeSchemeFile =
  39. cmStrCat(xcodeSchemeDir, '/', this->TargetName, ".xcscheme");
  40. cmGeneratedFileStream fout(xcodeSchemeFile);
  41. fout.SetCopyIfDifferent(true);
  42. if (!fout) {
  43. return;
  44. }
  45. WriteXCodeXCScheme(fout, container);
  46. }
  47. void cmXCodeScheme::WriteXCodeXCScheme(std::ostream& fout,
  48. const std::string& container)
  49. {
  50. cmXMLWriter xout(fout);
  51. xout.SetIndentationElement(std::string(3, ' '));
  52. xout.StartDocument();
  53. xout.StartElement("Scheme");
  54. xout.BreakAttributes();
  55. xout.Attribute("LastUpgradeVersion", WriteVersionString());
  56. xout.Attribute("version", "1.3");
  57. cmValue propDftCfg =
  58. Target->GetTarget()->GetProperty("XCODE_SCHEME_LAUNCH_CONFIGURATION");
  59. std::string launchConfiguration =
  60. !propDftCfg.IsEmpty() ? *propDftCfg : "Debug";
  61. WriteBuildAction(xout, container);
  62. WriteTestAction(xout, FindConfiguration("Debug"), container);
  63. WriteLaunchAction(xout, FindConfiguration(launchConfiguration), container);
  64. WriteProfileAction(xout, FindConfiguration("Release"));
  65. WriteAnalyzeAction(xout, FindConfiguration("Debug"));
  66. WriteArchiveAction(xout, FindConfiguration("Release"));
  67. xout.EndElement();
  68. }
  69. void cmXCodeScheme::WriteBuildAction(cmXMLWriter& xout,
  70. const std::string& container)
  71. {
  72. xout.StartElement("BuildAction");
  73. xout.BreakAttributes();
  74. xout.Attribute("parallelizeBuildables", "YES");
  75. xout.Attribute("buildImplicitDependencies", "YES");
  76. xout.StartElement("BuildActionEntries");
  77. xout.StartElement("BuildActionEntry");
  78. xout.BreakAttributes();
  79. xout.Attribute("buildForTesting", "YES");
  80. xout.Attribute("buildForRunning", "YES");
  81. xout.Attribute("buildForProfiling", "YES");
  82. xout.Attribute("buildForArchiving", "YES");
  83. xout.Attribute("buildForAnalyzing", "YES");
  84. WriteBuildableReference(xout, this->Target, container);
  85. xout.EndElement(); // BuildActionEntry
  86. xout.EndElement(); // BuildActionEntries
  87. xout.EndElement(); // BuildAction
  88. }
  89. void cmXCodeScheme::WriteTestAction(cmXMLWriter& xout,
  90. const std::string& configuration,
  91. const std::string& container)
  92. {
  93. xout.StartElement("TestAction");
  94. xout.BreakAttributes();
  95. xout.Attribute("buildConfiguration", configuration);
  96. xout.Attribute("selectedDebuggerIdentifier",
  97. "Xcode.DebuggerFoundation.Debugger.LLDB");
  98. xout.Attribute("selectedLauncherIdentifier",
  99. "Xcode.DebuggerFoundation.Launcher.LLDB");
  100. xout.Attribute("shouldUseLaunchSchemeArgsEnv", "YES");
  101. xout.StartElement("Testables");
  102. for (auto test : this->Tests) {
  103. xout.StartElement("TestableReference");
  104. xout.BreakAttributes();
  105. xout.Attribute("skipped", "NO");
  106. WriteBuildableReference(xout, test, container);
  107. xout.EndElement(); // TestableReference
  108. }
  109. xout.EndElement();
  110. if (IsTestable()) {
  111. xout.StartElement("MacroExpansion");
  112. WriteBuildableReference(xout, this->Target, container);
  113. xout.EndElement(); // MacroExpansion
  114. }
  115. xout.StartElement("AdditionalOptions");
  116. xout.EndElement();
  117. xout.EndElement(); // TestAction
  118. }
  119. void cmXCodeScheme::WriteLaunchAction(cmXMLWriter& xout,
  120. const std::string& configuration,
  121. const std::string& container)
  122. {
  123. xout.StartElement("LaunchAction");
  124. xout.BreakAttributes();
  125. xout.Attribute("buildConfiguration", configuration);
  126. xout.Attribute("selectedDebuggerIdentifier",
  127. "Xcode.DebuggerFoundation.Debugger.LLDB");
  128. xout.Attribute("selectedLauncherIdentifier",
  129. "Xcode.DebuggerFoundation.Launcher.LLDB");
  130. {
  131. cmValue launchMode =
  132. this->Target->GetTarget()->GetProperty("XCODE_SCHEME_LAUNCH_MODE");
  133. std::string value = "0"; // == 'AUTO'
  134. if (launchMode && *launchMode == "WAIT") {
  135. value = "1";
  136. }
  137. xout.Attribute("launchStyle", value);
  138. }
  139. WriteCustomWorkingDirectory(xout, configuration);
  140. xout.Attribute("ignoresPersistentStateOnLaunch", "NO");
  141. WriteLaunchActionBooleanAttribute(xout, "debugDocumentVersioning",
  142. "XCODE_SCHEME_DEBUG_DOCUMENT_VERSIONING",
  143. true);
  144. xout.Attribute("debugServiceExtension", "internal");
  145. xout.Attribute("allowLocationSimulation", "YES");
  146. if (cmValue gpuFrameCaptureMode = this->Target->GetTarget()->GetProperty(
  147. "XCODE_SCHEME_ENABLE_GPU_FRAME_CAPTURE_MODE")) {
  148. std::string value = *gpuFrameCaptureMode;
  149. if (cmsysString_strcasecmp(value.c_str(), "Metal") == 0) {
  150. value = "1";
  151. } else if (cmsysString_strcasecmp(value.c_str(), "Disabled") == 0) {
  152. value = "3";
  153. }
  154. xout.Attribute("enableGPUFrameCaptureMode", value);
  155. }
  156. // Diagnostics tab begin
  157. bool useAddressSanitizer = WriteLaunchActionAttribute(
  158. xout, "enableAddressSanitizer",
  159. "XCODE_SCHEME_ADDRESS_SANITIZER"); // not allowed with
  160. // enableThreadSanitizer=YES
  161. WriteLaunchActionAttribute(
  162. xout, "enableASanStackUseAfterReturn",
  163. "XCODE_SCHEME_ADDRESS_SANITIZER_USE_AFTER_RETURN");
  164. bool useThreadSanitizer = false;
  165. if (!useAddressSanitizer) {
  166. useThreadSanitizer = WriteLaunchActionAttribute(
  167. xout, "enableThreadSanitizer",
  168. "XCODE_SCHEME_THREAD_SANITIZER"); // not allowed with
  169. // enableAddressSanitizer=YES
  170. }
  171. WriteLaunchActionAttribute(xout, "stopOnEveryThreadSanitizerIssue",
  172. "XCODE_SCHEME_THREAD_SANITIZER_STOP");
  173. WriteLaunchActionAttribute(xout, "enableUBSanitizer",
  174. "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER");
  175. if (cmValue value = this->Target->GetTarget()->GetProperty(
  176. "XCODE_SCHEME_ENABLE_GPU_API_VALIDATION")) {
  177. if (value.IsOff()) {
  178. xout.Attribute("enableGPUValidationMode",
  179. "1"); // unset means YES, "1" means NO
  180. }
  181. }
  182. if (cmValue value = this->Target->GetTarget()->GetProperty(
  183. "XCODE_SCHEME_ENABLE_GPU_SHADER_VALIDATION")) {
  184. if (value.IsOn()) {
  185. xout.Attribute("enableGPUShaderValidationMode",
  186. "2"); // unset means NO, "2" means YES
  187. }
  188. }
  189. WriteLaunchActionAttribute(
  190. xout, "stopOnEveryUBSanitizerIssue",
  191. "XCODE_SCHEME_UNDEFINED_BEHAVIOUR_SANITIZER_STOP");
  192. WriteLaunchActionAttribute(
  193. xout, "disableMainThreadChecker",
  194. "XCODE_SCHEME_DISABLE_MAIN_THREAD_CHECKER"); // negative enabled!
  195. WriteLaunchActionAttribute(xout, "stopOnEveryMainThreadCheckerIssue",
  196. "XCODE_SCHEME_MAIN_THREAD_CHECKER_STOP");
  197. if (this->Target->GetTarget()->GetPropertyAsBool(
  198. "XCODE_SCHEME_DEBUG_AS_ROOT")) {
  199. xout.Attribute("debugAsWhichUser", "root");
  200. }
  201. // Diagnostics tab end
  202. if (IsExecutable(this->Target)) {
  203. xout.StartElement("BuildableProductRunnable");
  204. xout.BreakAttributes();
  205. xout.Attribute("runnableDebuggingMode", "0");
  206. } else {
  207. xout.StartElement("MacroExpansion");
  208. }
  209. WriteBuildableReference(xout, this->Target, container);
  210. xout.EndElement(); // MacroExpansion
  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. std::vector<std::string> arguments = cmExpandedList(*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. std::vector<std::string> envs = cmExpandedList(*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. {
  328. xout.StartElement("ProfileAction");
  329. xout.BreakAttributes();
  330. xout.Attribute("buildConfiguration", configuration);
  331. xout.Attribute("shouldUseLaunchSchemeArgsEnv", "YES");
  332. xout.Attribute("savedToolIdentifier", "");
  333. WriteCustomWorkingDirectory(xout, configuration);
  334. WriteLaunchActionBooleanAttribute(xout, "debugDocumentVersioning",
  335. "XCODE_SCHEME_DEBUG_DOCUMENT_VERSIONING",
  336. true);
  337. xout.EndElement();
  338. }
  339. void cmXCodeScheme::WriteAnalyzeAction(cmXMLWriter& xout,
  340. const std::string& configuration)
  341. {
  342. xout.StartElement("AnalyzeAction");
  343. xout.BreakAttributes();
  344. xout.Attribute("buildConfiguration", configuration);
  345. xout.EndElement();
  346. }
  347. void cmXCodeScheme::WriteArchiveAction(cmXMLWriter& xout,
  348. const std::string& configuration)
  349. {
  350. xout.StartElement("ArchiveAction");
  351. xout.BreakAttributes();
  352. xout.Attribute("buildConfiguration", configuration);
  353. xout.Attribute("revealArchiveInOrganizer", "YES");
  354. xout.EndElement();
  355. }
  356. void cmXCodeScheme::WriteBuildableReference(cmXMLWriter& xout,
  357. const cmXCodeObject* xcObj,
  358. const std::string& container)
  359. {
  360. xout.StartElement("BuildableReference");
  361. xout.BreakAttributes();
  362. xout.Attribute("BuildableIdentifier", "primary");
  363. xout.Attribute("BlueprintIdentifier", xcObj->GetId());
  364. std::string const noConfig; // FIXME: What config to use here?
  365. xout.Attribute("BuildableName", xcObj->GetTarget()->GetFullName(noConfig));
  366. xout.Attribute("BlueprintName", xcObj->GetTarget()->GetName());
  367. xout.Attribute("ReferencedContainer", "container:" + container);
  368. xout.EndElement();
  369. }
  370. void cmXCodeScheme::WriteCustomWorkingDirectory(
  371. cmXMLWriter& xout, const std::string& configuration)
  372. {
  373. std::string const& propertyValue =
  374. this->Target->GetTarget()->GetSafeProperty(
  375. "XCODE_SCHEME_WORKING_DIRECTORY");
  376. if (propertyValue.empty()) {
  377. xout.Attribute("useCustomWorkingDirectory", "NO");
  378. } else {
  379. xout.Attribute("useCustomWorkingDirectory", "YES");
  380. auto customWorkingDirectory = cmGeneratorExpression::Evaluate(
  381. propertyValue, this->LocalGenerator, configuration);
  382. xout.Attribute("customWorkingDirectory", customWorkingDirectory);
  383. }
  384. }
  385. std::string cmXCodeScheme::WriteVersionString()
  386. {
  387. std::ostringstream v;
  388. v << std::setfill('0') << std::setw(4) << this->XcodeVersion * 10;
  389. return v.str();
  390. }
  391. std::string cmXCodeScheme::FindConfiguration(const std::string& name)
  392. {
  393. // Try to find the desired configuration by name,
  394. // and if it's not found return first from the list
  395. //
  396. if (!cm::contains(this->ConfigList, name) && !this->ConfigList.empty()) {
  397. return this->ConfigList[0];
  398. }
  399. return name;
  400. }
  401. bool cmXCodeScheme::IsTestable() const
  402. {
  403. return !this->Tests.empty() || IsExecutable(this->Target);
  404. }
  405. bool cmXCodeScheme::IsExecutable(const cmXCodeObject* target)
  406. {
  407. cmGeneratorTarget* gt = target->GetTarget();
  408. if (!gt) {
  409. cmSystemTools::Error("Error no target on xobject\n");
  410. return false;
  411. }
  412. return gt->GetType() == cmStateEnums::EXECUTABLE;
  413. }