cmXCodeScheme.cxx 13 KB

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