cmXCodeScheme.cxx 14 KB

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