cmGlobalXCodeGenerator.cxx 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680
  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 "cmGlobalXCodeGenerator.h"
  4. #include "cmsys/RegularExpression.hxx"
  5. #include <assert.h>
  6. #include <iomanip>
  7. #include <memory> // IWYU pragma: keep
  8. #include <sstream>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "cmAlgorithms.h"
  12. #include "cmComputeLinkInformation.h"
  13. #include "cmCustomCommandGenerator.h"
  14. #include "cmDocumentationEntry.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGeneratorExpression.h"
  17. #include "cmGeneratorTarget.h"
  18. #include "cmGlobalGeneratorFactory.h"
  19. #include "cmLocalGenerator.h"
  20. #include "cmLocalXCodeGenerator.h"
  21. #include "cmMakefile.h"
  22. #include "cmOutputConverter.h"
  23. #include "cmSourceFile.h"
  24. #include "cmSourceGroup.h"
  25. #include "cmState.h"
  26. #include "cmStateTypes.h"
  27. #include "cmSystemTools.h"
  28. #include "cmTarget.h"
  29. #include "cmXCode21Object.h"
  30. #include "cmXCodeObject.h"
  31. #include "cmXCodeScheme.h"
  32. #include "cmake.h"
  33. struct cmLinkImplementation;
  34. #if defined(CMAKE_BUILD_WITH_CMAKE) && defined(__APPLE__)
  35. #define HAVE_APPLICATION_SERVICES
  36. #include <ApplicationServices/ApplicationServices.h>
  37. #endif
  38. #if defined(CMAKE_BUILD_WITH_CMAKE)
  39. #include "cmXMLParser.h"
  40. // parse the xml file storing the installed version of Xcode on
  41. // the machine
  42. class cmXcodeVersionParser : public cmXMLParser
  43. {
  44. public:
  45. cmXcodeVersionParser()
  46. : Version("1.5")
  47. {
  48. }
  49. void StartElement(const std::string&, const char**) override
  50. {
  51. this->Data = "";
  52. }
  53. void EndElement(const std::string& name) override
  54. {
  55. if (name == "key") {
  56. this->Key = this->Data;
  57. } else if (name == "string") {
  58. if (this->Key == "CFBundleShortVersionString") {
  59. this->Version = this->Data;
  60. }
  61. }
  62. }
  63. void CharacterDataHandler(const char* data, int length) override
  64. {
  65. this->Data.append(data, length);
  66. }
  67. std::string Version;
  68. std::string Key;
  69. std::string Data;
  70. };
  71. #endif
  72. // Builds either an object list or a space-separated string from the
  73. // given inputs.
  74. class cmGlobalXCodeGenerator::BuildObjectListOrString
  75. {
  76. cmGlobalXCodeGenerator* Generator;
  77. cmXCodeObject* Group;
  78. bool Empty;
  79. std::string String;
  80. public:
  81. BuildObjectListOrString(cmGlobalXCodeGenerator* gen, bool buildObjectList)
  82. : Generator(gen)
  83. , Group(nullptr)
  84. , Empty(true)
  85. {
  86. if (buildObjectList) {
  87. this->Group = this->Generator->CreateObject(cmXCodeObject::OBJECT_LIST);
  88. }
  89. }
  90. bool IsEmpty() const { return this->Empty; }
  91. void Add(const std::string& newString)
  92. {
  93. this->Empty = false;
  94. if (this->Group) {
  95. this->Group->AddObject(this->Generator->CreateString(newString));
  96. } else {
  97. this->String += newString;
  98. this->String += ' ';
  99. }
  100. }
  101. const std::string& GetString() const { return this->String; }
  102. cmXCodeObject* CreateList()
  103. {
  104. if (this->Group) {
  105. return this->Group;
  106. }
  107. return this->Generator->CreateString(this->String);
  108. }
  109. };
  110. class cmGlobalXCodeGenerator::Factory : public cmGlobalGeneratorFactory
  111. {
  112. public:
  113. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  114. cmake* cm) const override;
  115. void GetDocumentation(cmDocumentationEntry& entry) const override
  116. {
  117. cmGlobalXCodeGenerator::GetDocumentation(entry);
  118. }
  119. void GetGenerators(std::vector<std::string>& names) const override
  120. {
  121. names.push_back(cmGlobalXCodeGenerator::GetActualName());
  122. }
  123. bool SupportsToolset() const override { return true; }
  124. bool SupportsPlatform() const override { return false; }
  125. };
  126. cmGlobalXCodeGenerator::cmGlobalXCodeGenerator(
  127. cmake* cm, std::string const& version_string, unsigned int version_number)
  128. : cmGlobalGenerator(cm)
  129. {
  130. this->VersionString = version_string;
  131. this->XcodeVersion = version_number;
  132. this->RootObject = nullptr;
  133. this->MainGroupChildren = nullptr;
  134. this->CurrentMakefile = nullptr;
  135. this->CurrentLocalGenerator = nullptr;
  136. this->XcodeBuildCommandInitialized = false;
  137. this->ObjectDirArchDefault = "$(CURRENT_ARCH)";
  138. this->ObjectDirArch = this->ObjectDirArchDefault;
  139. cm->GetState()->SetIsGeneratorMultiConfig(true);
  140. }
  141. cmGlobalGeneratorFactory* cmGlobalXCodeGenerator::NewFactory()
  142. {
  143. return new Factory;
  144. }
  145. cmGlobalGenerator* cmGlobalXCodeGenerator::Factory::CreateGlobalGenerator(
  146. const std::string& name, cmake* cm) const
  147. {
  148. if (name != GetActualName()) {
  149. return nullptr;
  150. }
  151. #if defined(CMAKE_BUILD_WITH_CMAKE)
  152. cmXcodeVersionParser parser;
  153. std::string versionFile;
  154. {
  155. std::string out;
  156. std::string::size_type pos = 0;
  157. if (cmSystemTools::RunSingleCommand("xcode-select --print-path", &out,
  158. nullptr, nullptr, nullptr,
  159. cmSystemTools::OUTPUT_NONE) &&
  160. (pos = out.find(".app/"), pos != std::string::npos)) {
  161. versionFile = out.substr(0, pos + 5) + "Contents/version.plist";
  162. }
  163. }
  164. if (!versionFile.empty() && cmSystemTools::FileExists(versionFile.c_str())) {
  165. parser.ParseFile(versionFile.c_str());
  166. } else if (cmSystemTools::FileExists(
  167. "/Applications/Xcode.app/Contents/version.plist")) {
  168. parser.ParseFile("/Applications/Xcode.app/Contents/version.plist");
  169. } else {
  170. parser.ParseFile(
  171. "/Developer/Applications/Xcode.app/Contents/version.plist");
  172. }
  173. std::string const& version_string = parser.Version;
  174. // Compute an integer form of the version number.
  175. unsigned int v[2] = { 0, 0 };
  176. sscanf(version_string.c_str(), "%u.%u", &v[0], &v[1]);
  177. unsigned int version_number = 10 * v[0] + v[1];
  178. if (version_number < 30) {
  179. cm->IssueMessage(cmake::FATAL_ERROR,
  180. "Xcode " + version_string + " not supported.");
  181. return nullptr;
  182. }
  183. auto gg = cm::make_unique<cmGlobalXCodeGenerator>(cm, version_string,
  184. version_number);
  185. return gg.release();
  186. #else
  187. std::cerr << "CMake should be built with cmake to use Xcode, "
  188. "default to Xcode 1.5\n";
  189. return new cmGlobalXCodeGenerator(cm);
  190. #endif
  191. }
  192. bool cmGlobalXCodeGenerator::FindMakeProgram(cmMakefile* mf)
  193. {
  194. // The Xcode generator knows how to lookup its build tool
  195. // directly instead of needing a helper module to do it, so we
  196. // do not actually need to put CMAKE_MAKE_PROGRAM into the cache.
  197. if (cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM"))) {
  198. mf->AddDefinition("CMAKE_MAKE_PROGRAM",
  199. this->GetXcodeBuildCommand().c_str());
  200. }
  201. return true;
  202. }
  203. std::string const& cmGlobalXCodeGenerator::GetXcodeBuildCommand()
  204. {
  205. if (!this->XcodeBuildCommandInitialized) {
  206. this->XcodeBuildCommandInitialized = true;
  207. this->XcodeBuildCommand = this->FindXcodeBuildCommand();
  208. }
  209. return this->XcodeBuildCommand;
  210. }
  211. std::string cmGlobalXCodeGenerator::FindXcodeBuildCommand()
  212. {
  213. if (this->XcodeVersion >= 40) {
  214. std::string makeProgram = cmSystemTools::FindProgram("xcodebuild");
  215. if (makeProgram.empty()) {
  216. makeProgram = "xcodebuild";
  217. }
  218. return makeProgram;
  219. }
  220. // Use cmakexbuild wrapper to suppress environment dump from output.
  221. return cmSystemTools::GetCMakeCommand() + "xbuild";
  222. }
  223. bool cmGlobalXCodeGenerator::SetGeneratorToolset(std::string const& ts,
  224. cmMakefile* mf)
  225. {
  226. if (ts.find_first_of(",=") != std::string::npos) {
  227. std::ostringstream e;
  228. /* clang-format off */
  229. e <<
  230. "Generator\n"
  231. " " << this->GetName() << "\n"
  232. "does not recognize the toolset\n"
  233. " " << ts << "\n"
  234. "that was specified.";
  235. /* clang-format on */
  236. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  237. return false;
  238. }
  239. this->GeneratorToolset = ts;
  240. if (!this->GeneratorToolset.empty()) {
  241. mf->AddDefinition("CMAKE_XCODE_PLATFORM_TOOLSET",
  242. this->GeneratorToolset.c_str());
  243. }
  244. return true;
  245. }
  246. void cmGlobalXCodeGenerator::EnableLanguage(
  247. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  248. {
  249. mf->AddDefinition("XCODE", "1");
  250. mf->AddDefinition("XCODE_VERSION", this->VersionString.c_str());
  251. if (!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES")) {
  252. mf->AddCacheDefinition(
  253. "CMAKE_CONFIGURATION_TYPES", "Debug;Release;MinSizeRel;RelWithDebInfo",
  254. "Semicolon separated list of supported configuration types, "
  255. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  256. "anything else will be ignored.",
  257. cmStateEnums::STRING);
  258. }
  259. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  260. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  261. this->ComputeArchitectures(mf);
  262. }
  263. bool cmGlobalXCodeGenerator::Open(const std::string& bindir,
  264. const std::string& projectName, bool dryRun)
  265. {
  266. bool ret = false;
  267. #ifdef HAVE_APPLICATION_SERVICES
  268. std::string url = bindir + "/" + projectName + ".xcodeproj";
  269. if (dryRun) {
  270. return cmSystemTools::FileExists(url, false);
  271. }
  272. CFStringRef cfStr = CFStringCreateWithCString(
  273. kCFAllocatorDefault, url.c_str(), kCFStringEncodingUTF8);
  274. if (cfStr) {
  275. CFURLRef cfUrl = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfStr,
  276. kCFURLPOSIXPathStyle, true);
  277. if (cfUrl) {
  278. OSStatus err = LSOpenCFURLRef(cfUrl, nullptr);
  279. ret = err == noErr;
  280. CFRelease(cfUrl);
  281. }
  282. CFRelease(cfStr);
  283. }
  284. #endif
  285. return ret;
  286. }
  287. void cmGlobalXCodeGenerator::GenerateBuildCommand(
  288. std::vector<std::string>& makeCommand, const std::string& makeProgram,
  289. const std::string& projectName, const std::string& /*projectDir*/,
  290. const std::string& targetName, const std::string& config, bool /*fast*/,
  291. bool /*verbose*/, std::vector<std::string> const& makeOptions)
  292. {
  293. // now build the test
  294. makeCommand.push_back(
  295. this->SelectMakeProgram(makeProgram, this->GetXcodeBuildCommand()));
  296. makeCommand.push_back("-project");
  297. std::string projectArg = projectName;
  298. projectArg += ".xcode";
  299. projectArg += "proj";
  300. makeCommand.push_back(projectArg);
  301. bool clean = false;
  302. std::string realTarget = targetName;
  303. if (realTarget == "clean") {
  304. clean = true;
  305. realTarget = "ALL_BUILD";
  306. }
  307. if (clean) {
  308. makeCommand.push_back("clean");
  309. } else {
  310. makeCommand.push_back("build");
  311. }
  312. makeCommand.push_back("-target");
  313. if (!realTarget.empty()) {
  314. makeCommand.push_back(realTarget);
  315. } else {
  316. makeCommand.push_back("ALL_BUILD");
  317. }
  318. makeCommand.push_back("-configuration");
  319. makeCommand.push_back(!config.empty() ? config : "Debug");
  320. makeCommand.insert(makeCommand.end(), makeOptions.begin(),
  321. makeOptions.end());
  322. }
  323. ///! Create a local generator appropriate to this Global Generator
  324. cmLocalGenerator* cmGlobalXCodeGenerator::CreateLocalGenerator(cmMakefile* mf)
  325. {
  326. return new cmLocalXCodeGenerator(this, mf);
  327. }
  328. void cmGlobalXCodeGenerator::AddExtraIDETargets()
  329. {
  330. // make sure extra targets are added before calling
  331. // the parent generate which will call trace depends
  332. for (auto keyVal : this->ProjectMap) {
  333. cmLocalGenerator* root = keyVal.second[0];
  334. this->SetGenerationRoot(root);
  335. // add ALL_BUILD, INSTALL, etc
  336. this->AddExtraTargets(root, keyVal.second);
  337. }
  338. }
  339. void cmGlobalXCodeGenerator::Generate()
  340. {
  341. this->cmGlobalGenerator::Generate();
  342. if (cmSystemTools::GetErrorOccuredFlag()) {
  343. return;
  344. }
  345. for (auto keyVal : this->ProjectMap) {
  346. cmLocalGenerator* root = keyVal.second[0];
  347. bool generateTopLevelProjectOnly =
  348. root->GetMakefile()->IsOn("CMAKE_XCODE_GENERATE_TOP_LEVEL_PROJECT_ONLY");
  349. if (generateTopLevelProjectOnly) {
  350. cmStateSnapshot snp = root->GetStateSnapshot();
  351. if (snp.GetBuildsystemDirectoryParent().IsValid()) {
  352. continue;
  353. }
  354. }
  355. this->SetGenerationRoot(root);
  356. // now create the project
  357. this->OutputXCodeProject(root, keyVal.second);
  358. }
  359. }
  360. void cmGlobalXCodeGenerator::SetGenerationRoot(cmLocalGenerator* root)
  361. {
  362. this->CurrentProject = root->GetProjectName();
  363. this->SetCurrentLocalGenerator(root);
  364. cmSystemTools::SplitPath(
  365. this->CurrentLocalGenerator->GetCurrentSourceDirectory(),
  366. this->ProjectSourceDirectoryComponents);
  367. cmSystemTools::SplitPath(
  368. this->CurrentLocalGenerator->GetCurrentBinaryDirectory(),
  369. this->ProjectOutputDirectoryComponents);
  370. this->CurrentXCodeHackMakefile = root->GetCurrentBinaryDirectory();
  371. this->CurrentXCodeHackMakefile += "/CMakeScripts";
  372. cmSystemTools::MakeDirectory(this->CurrentXCodeHackMakefile.c_str());
  373. this->CurrentXCodeHackMakefile += "/XCODE_DEPEND_HELPER.make";
  374. }
  375. std::string cmGlobalXCodeGenerator::PostBuildMakeTarget(
  376. std::string const& tName, std::string const& configName)
  377. {
  378. std::string target = tName;
  379. std::replace(target.begin(), target.end(), ' ', '_');
  380. std::string out = "PostBuild." + target;
  381. out += "." + configName;
  382. return out;
  383. }
  384. #define CMAKE_CHECK_BUILD_SYSTEM_TARGET "ZERO_CHECK"
  385. void cmGlobalXCodeGenerator::AddExtraTargets(
  386. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& gens)
  387. {
  388. cmMakefile* mf = root->GetMakefile();
  389. // Add ALL_BUILD
  390. const char* no_working_directory = nullptr;
  391. std::vector<std::string> no_depends;
  392. cmTarget* allbuild = mf->AddUtilityCommand(
  393. "ALL_BUILD", cmMakefile::TargetOrigin::Generator, true, no_depends,
  394. no_working_directory, "echo", "Build all projects");
  395. cmGeneratorTarget* allBuildGt = new cmGeneratorTarget(allbuild, root);
  396. root->AddGeneratorTarget(allBuildGt);
  397. // Add XCODE depend helper
  398. std::string dir = root->GetCurrentBinaryDirectory();
  399. cmCustomCommandLine makeHelper;
  400. makeHelper.push_back("make");
  401. makeHelper.push_back("-C");
  402. makeHelper.push_back(dir);
  403. makeHelper.push_back("-f");
  404. makeHelper.push_back(this->CurrentXCodeHackMakefile);
  405. makeHelper.push_back(""); // placeholder, see below
  406. // Add ZERO_CHECK
  407. bool regenerate = !mf->IsOn("CMAKE_SUPPRESS_REGENERATION");
  408. bool generateTopLevelProjectOnly =
  409. mf->IsOn("CMAKE_XCODE_GENERATE_TOP_LEVEL_PROJECT_ONLY");
  410. bool isTopLevel =
  411. !root->GetStateSnapshot().GetBuildsystemDirectoryParent().IsValid();
  412. if (regenerate && (isTopLevel || !generateTopLevelProjectOnly)) {
  413. this->CreateReRunCMakeFile(root, gens);
  414. std::string file =
  415. this->ConvertToRelativeForMake(this->CurrentReRunCMakeMakefile.c_str());
  416. cmSystemTools::ReplaceString(file, "\\ ", " ");
  417. cmTarget* check = mf->AddUtilityCommand(
  418. CMAKE_CHECK_BUILD_SYSTEM_TARGET, cmMakefile::TargetOrigin::Generator,
  419. true, no_depends, no_working_directory, "make", "-f", file.c_str());
  420. cmGeneratorTarget* checkGt = new cmGeneratorTarget(check, root);
  421. root->AddGeneratorTarget(checkGt);
  422. }
  423. // now make the allbuild depend on all the non-utility targets
  424. // in the project
  425. for (auto& gen : gens) {
  426. if (this->IsExcluded(root, gen)) {
  427. continue;
  428. }
  429. for (auto target : gen->GetGeneratorTargets()) {
  430. if (target->GetType() == cmStateEnums::GLOBAL_TARGET) {
  431. continue;
  432. }
  433. std::string targetName = target->GetName();
  434. if (regenerate && (targetName != CMAKE_CHECK_BUILD_SYSTEM_TARGET)) {
  435. target->Target->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  436. }
  437. // make all exe, shared libs and modules
  438. // run the depend check makefile as a post build rule
  439. // this will make sure that when the next target is built
  440. // things are up-to-date
  441. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  442. (this->XcodeVersion < 50 &&
  443. (target->GetType() == cmStateEnums::EXECUTABLE ||
  444. target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  445. target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  446. target->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  447. makeHelper[makeHelper.size() - 1] = // fill placeholder
  448. this->PostBuildMakeTarget(target->GetName(), "$(CONFIGURATION)");
  449. cmCustomCommandLines commandLines;
  450. commandLines.push_back(makeHelper);
  451. std::vector<std::string> no_byproducts;
  452. gen->GetMakefile()->AddCustomCommandToTarget(
  453. target->GetName(), no_byproducts, no_depends, commandLines,
  454. cmTarget::POST_BUILD, "Depend check for xcode", dir.c_str(), true,
  455. false, "", false, cmMakefile::AcceptObjectLibraryCommands);
  456. }
  457. if (target->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  458. !target->GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  459. allbuild->AddUtility(target->GetName());
  460. }
  461. }
  462. }
  463. }
  464. void cmGlobalXCodeGenerator::CreateReRunCMakeFile(
  465. cmLocalGenerator* root, std::vector<cmLocalGenerator*> const& gens)
  466. {
  467. std::vector<std::string> lfiles;
  468. for (auto gen : gens) {
  469. std::vector<std::string> const& lf = gen->GetMakefile()->GetListFiles();
  470. lfiles.insert(lfiles.end(), lf.begin(), lf.end());
  471. }
  472. // sort the array
  473. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  474. std::vector<std::string>::iterator new_end =
  475. std::unique(lfiles.begin(), lfiles.end());
  476. lfiles.erase(new_end, lfiles.end());
  477. this->CurrentReRunCMakeMakefile = root->GetCurrentBinaryDirectory();
  478. this->CurrentReRunCMakeMakefile += "/CMakeScripts";
  479. cmSystemTools::MakeDirectory(this->CurrentReRunCMakeMakefile.c_str());
  480. this->CurrentReRunCMakeMakefile += "/ReRunCMake.make";
  481. cmGeneratedFileStream makefileStream(
  482. this->CurrentReRunCMakeMakefile.c_str());
  483. makefileStream.SetCopyIfDifferent(true);
  484. makefileStream << "# Generated by CMake, DO NOT EDIT\n\n";
  485. makefileStream << "TARGETS:= \n";
  486. makefileStream << "empty:= \n";
  487. makefileStream << "space:= $(empty) $(empty)\n";
  488. makefileStream << "spaceplus:= $(empty)\\ $(empty)\n\n";
  489. for (const auto& lfile : lfiles) {
  490. makefileStream << "TARGETS += $(subst $(space),$(spaceplus),$(wildcard "
  491. << this->ConvertToRelativeForMake(lfile.c_str()) << "))\n";
  492. }
  493. std::string checkCache = root->GetBinaryDirectory();
  494. checkCache += "/";
  495. checkCache += cmake::GetCMakeFilesDirectoryPostSlash();
  496. checkCache += "cmake.check_cache";
  497. makefileStream << "\n"
  498. << this->ConvertToRelativeForMake(checkCache.c_str())
  499. << ": $(TARGETS)\n";
  500. makefileStream << "\t"
  501. << this->ConvertToRelativeForMake(
  502. cmSystemTools::GetCMakeCommand().c_str())
  503. << " -H"
  504. << this->ConvertToRelativeForMake(root->GetSourceDirectory())
  505. << " -B"
  506. << this->ConvertToRelativeForMake(root->GetBinaryDirectory())
  507. << "\n";
  508. }
  509. static bool objectIdLessThan(cmXCodeObject* l, cmXCodeObject* r)
  510. {
  511. return l->GetId() < r->GetId();
  512. }
  513. void cmGlobalXCodeGenerator::SortXCodeObjects()
  514. {
  515. std::sort(this->XCodeObjects.begin(), this->XCodeObjects.end(),
  516. objectIdLessThan);
  517. }
  518. void cmGlobalXCodeGenerator::ClearXCodeObjects()
  519. {
  520. this->TargetDoneSet.clear();
  521. for (auto& obj : this->XCodeObjects) {
  522. delete obj;
  523. }
  524. this->XCodeObjects.clear();
  525. this->XCodeObjectIDs.clear();
  526. this->XCodeObjectMap.clear();
  527. this->GroupMap.clear();
  528. this->GroupNameMap.clear();
  529. this->TargetGroup.clear();
  530. this->FileRefs.clear();
  531. }
  532. void cmGlobalXCodeGenerator::addObject(cmXCodeObject* obj)
  533. {
  534. if (obj->GetType() == cmXCodeObject::OBJECT) {
  535. const std::string& id = obj->GetId();
  536. // If this is a duplicate id, it's an error:
  537. //
  538. if (this->XCodeObjectIDs.count(id)) {
  539. cmSystemTools::Error(
  540. "Xcode generator: duplicate object ids not allowed");
  541. }
  542. this->XCodeObjectIDs.insert(id);
  543. }
  544. this->XCodeObjects.push_back(obj);
  545. }
  546. cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(
  547. cmXCodeObject::PBXType ptype)
  548. {
  549. cmXCodeObject* obj = new cmXCode21Object(ptype, cmXCodeObject::OBJECT);
  550. this->addObject(obj);
  551. return obj;
  552. }
  553. cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::Type type)
  554. {
  555. cmXCodeObject* obj = new cmXCodeObject(cmXCodeObject::None, type);
  556. this->addObject(obj);
  557. return obj;
  558. }
  559. cmXCodeObject* cmGlobalXCodeGenerator::CreateString(const std::string& s)
  560. {
  561. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::STRING);
  562. obj->SetString(s);
  563. return obj;
  564. }
  565. cmXCodeObject* cmGlobalXCodeGenerator::CreateObjectReference(
  566. cmXCodeObject* ref)
  567. {
  568. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::OBJECT_REF);
  569. obj->SetObject(ref);
  570. return obj;
  571. }
  572. cmXCodeObject* cmGlobalXCodeGenerator::CreateFlatClone(cmXCodeObject* orig)
  573. {
  574. cmXCodeObject* obj = this->CreateObject(orig->GetType());
  575. obj->CopyAttributes(orig);
  576. return obj;
  577. }
  578. std::string GetGroupMapKeyFromPath(cmGeneratorTarget* target,
  579. const std::string& fullpath)
  580. {
  581. std::string key(target->GetName());
  582. key += "-";
  583. key += fullpath;
  584. return key;
  585. }
  586. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeSourceFileFromPath(
  587. const std::string& fullpath, cmGeneratorTarget* target,
  588. const std::string& lang, cmSourceFile* sf)
  589. {
  590. // Using a map and the full path guarantees that we will always get the same
  591. // fileRef object for any given full path.
  592. //
  593. cmXCodeObject* fileRef =
  594. this->CreateXCodeFileReferenceFromPath(fullpath, target, lang, sf);
  595. cmXCodeObject* buildFile = this->CreateObject(cmXCodeObject::PBXBuildFile);
  596. buildFile->SetComment(fileRef->GetComment());
  597. buildFile->AddAttribute("fileRef", this->CreateObjectReference(fileRef));
  598. return buildFile;
  599. }
  600. class XCodeGeneratorExpressionInterpreter
  601. : public cmGeneratorExpressionInterpreter
  602. {
  603. CM_DISABLE_COPY(XCodeGeneratorExpressionInterpreter)
  604. public:
  605. XCodeGeneratorExpressionInterpreter(cmSourceFile* sourceFile,
  606. cmLocalGenerator* localGenerator,
  607. cmGeneratorTarget* generatorTarget,
  608. const std::string& lang)
  609. : cmGeneratorExpressionInterpreter(localGenerator, generatorTarget,
  610. "NO-PER-CONFIG-SUPPORT-IN-XCODE",
  611. generatorTarget->GetName(), lang)
  612. , SourceFile(sourceFile)
  613. {
  614. }
  615. using cmGeneratorExpressionInterpreter::Evaluate;
  616. const char* Evaluate(const char* expression, const std::string& property)
  617. {
  618. const char* processed =
  619. this->cmGeneratorExpressionInterpreter::Evaluate(expression, property);
  620. if (this->GetCompiledGeneratorExpression()
  621. .GetHadContextSensitiveCondition()) {
  622. std::ostringstream e;
  623. /* clang-format off */
  624. e <<
  625. "Xcode does not support per-config per-source " << property << ":\n"
  626. " " << expression << "\n"
  627. "specified for source:\n"
  628. " " << this->SourceFile->GetFullPath() << "\n";
  629. /* clang-format on */
  630. this->GetLocalGenerator()->IssueMessage(cmake::FATAL_ERROR, e.str());
  631. }
  632. return processed;
  633. }
  634. private:
  635. cmSourceFile* SourceFile = nullptr;
  636. };
  637. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeSourceFile(
  638. cmLocalGenerator* lg, cmSourceFile* sf, cmGeneratorTarget* gtgt)
  639. {
  640. std::string lang = this->CurrentLocalGenerator->GetSourceFileLanguage(*sf);
  641. XCodeGeneratorExpressionInterpreter genexInterpreter(sf, lg, gtgt, lang);
  642. // Add flags from target and source file properties.
  643. std::string flags;
  644. const char* srcfmt = sf->GetProperty("Fortran_FORMAT");
  645. switch (cmOutputConverter::GetFortranFormat(srcfmt)) {
  646. case cmOutputConverter::FortranFormatFixed:
  647. flags = "-fixed " + flags;
  648. break;
  649. case cmOutputConverter::FortranFormatFree:
  650. flags = "-free " + flags;
  651. break;
  652. default:
  653. break;
  654. }
  655. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  656. if (const char* cflags = sf->GetProperty(COMPILE_FLAGS)) {
  657. lg->AppendFlags(flags, genexInterpreter.Evaluate(cflags, COMPILE_FLAGS));
  658. }
  659. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  660. if (const char* coptions = sf->GetProperty(COMPILE_OPTIONS)) {
  661. lg->AppendCompileOptions(
  662. flags, genexInterpreter.Evaluate(coptions, COMPILE_OPTIONS));
  663. }
  664. // Add per-source definitions.
  665. BuildObjectListOrString flagsBuild(this, false);
  666. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  667. if (const char* compile_defs = sf->GetProperty(COMPILE_DEFINITIONS)) {
  668. this->AppendDefines(
  669. flagsBuild, genexInterpreter.Evaluate(compile_defs, COMPILE_DEFINITIONS),
  670. true);
  671. }
  672. if (!flagsBuild.IsEmpty()) {
  673. if (!flags.empty()) {
  674. flags += ' ';
  675. }
  676. flags += flagsBuild.GetString();
  677. }
  678. // Add per-source include directories.
  679. std::vector<std::string> includes;
  680. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  681. if (const char* cincludes = sf->GetProperty(INCLUDE_DIRECTORIES)) {
  682. lg->AppendIncludeDirectories(
  683. includes, genexInterpreter.Evaluate(cincludes, INCLUDE_DIRECTORIES),
  684. *sf);
  685. }
  686. lg->AppendFlags(flags, lg->GetIncludeFlags(includes, gtgt, lang, true));
  687. cmXCodeObject* buildFile =
  688. this->CreateXCodeSourceFileFromPath(sf->GetFullPath(), gtgt, lang, sf);
  689. cmXCodeObject* settings = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  690. settings->AddAttributeIfNotEmpty("COMPILER_FLAGS",
  691. this->CreateString(flags));
  692. cmGeneratorTarget::SourceFileFlags tsFlags =
  693. gtgt->GetTargetSourceFileFlags(sf);
  694. cmXCodeObject* attrs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  695. // Is this a "private" or "public" framework header file?
  696. // Set the ATTRIBUTES attribute appropriately...
  697. //
  698. if (gtgt->IsFrameworkOnApple()) {
  699. if (tsFlags.Type == cmGeneratorTarget::SourceFileTypePrivateHeader) {
  700. attrs->AddObject(this->CreateString("Private"));
  701. } else if (tsFlags.Type == cmGeneratorTarget::SourceFileTypePublicHeader) {
  702. attrs->AddObject(this->CreateString("Public"));
  703. }
  704. }
  705. // Add user-specified file attributes.
  706. const char* extraFileAttributes = sf->GetProperty("XCODE_FILE_ATTRIBUTES");
  707. if (extraFileAttributes) {
  708. // Expand the list of attributes.
  709. std::vector<std::string> attributes;
  710. cmSystemTools::ExpandListArgument(extraFileAttributes, attributes);
  711. // Store the attributes.
  712. for (const auto& attribute : attributes) {
  713. attrs->AddObject(this->CreateString(attribute));
  714. }
  715. }
  716. settings->AddAttributeIfNotEmpty("ATTRIBUTES", attrs);
  717. buildFile->AddAttributeIfNotEmpty("settings", settings);
  718. return buildFile;
  719. }
  720. std::string GetSourcecodeValueFromFileExtension(const std::string& _ext,
  721. const std::string& lang,
  722. bool& keepLastKnownFileType)
  723. {
  724. std::string ext = cmSystemTools::LowerCase(_ext);
  725. std::string sourcecode = "sourcecode";
  726. if (ext == "o") {
  727. sourcecode = "compiled.mach-o.objfile";
  728. } else if (ext == "xctest") {
  729. sourcecode = "wrapper.cfbundle";
  730. } else if (ext == "xib") {
  731. keepLastKnownFileType = true;
  732. sourcecode = "file.xib";
  733. } else if (ext == "storyboard") {
  734. keepLastKnownFileType = true;
  735. sourcecode = "file.storyboard";
  736. } else if (ext == "mm") {
  737. sourcecode += ".cpp.objcpp";
  738. } else if (ext == "m") {
  739. sourcecode += ".c.objc";
  740. } else if (ext == "swift") {
  741. sourcecode += ".swift";
  742. } else if (ext == "plist") {
  743. sourcecode += ".text.plist";
  744. } else if (ext == "h") {
  745. sourcecode += ".c.h";
  746. } else if (ext == "hxx" || ext == "hpp" || ext == "txx" || ext == "pch" ||
  747. ext == "hh") {
  748. sourcecode += ".cpp.h";
  749. } else if (ext == "png" || ext == "gif" || ext == "jpg") {
  750. keepLastKnownFileType = true;
  751. sourcecode = "image";
  752. } else if (ext == "txt") {
  753. sourcecode += ".text";
  754. } else if (lang == "CXX") {
  755. sourcecode += ".cpp.cpp";
  756. } else if (lang == "C") {
  757. sourcecode += ".c.c";
  758. } else if (lang == "Fortran") {
  759. sourcecode += ".fortran.f90";
  760. } else if (lang == "ASM") {
  761. sourcecode += ".asm";
  762. } else if (ext == "metal") {
  763. sourcecode += ".metal";
  764. } else if (ext == "mig") {
  765. sourcecode += ".mig";
  766. }
  767. // else
  768. // {
  769. // // Already specialized above or we leave sourcecode == "sourcecode"
  770. // // which is probably the most correct choice. Extensionless headers,
  771. // // for example... Or file types unknown to Xcode that do not map to a
  772. // // valid explicitFileType value.
  773. // }
  774. return sourcecode;
  775. }
  776. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeFileReferenceFromPath(
  777. const std::string& fullpath, cmGeneratorTarget* target,
  778. const std::string& lang, cmSourceFile* sf)
  779. {
  780. std::string key = GetGroupMapKeyFromPath(target, fullpath);
  781. cmXCodeObject* fileRef = this->FileRefs[key];
  782. if (!fileRef) {
  783. fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  784. fileRef->SetComment(fullpath);
  785. this->FileRefs[key] = fileRef;
  786. }
  787. cmXCodeObject* group = this->GroupMap[key];
  788. cmXCodeObject* children = group->GetObject("children");
  789. if (!children->HasObject(fileRef)) {
  790. children->AddObject(fileRef);
  791. }
  792. fileRef->AddAttribute("fileEncoding", this->CreateString("4"));
  793. bool useLastKnownFileType = false;
  794. std::string fileType;
  795. if (sf) {
  796. if (const char* e = sf->GetProperty("XCODE_EXPLICIT_FILE_TYPE")) {
  797. fileType = e;
  798. } else if (const char* l = sf->GetProperty("XCODE_LAST_KNOWN_FILE_TYPE")) {
  799. useLastKnownFileType = true;
  800. fileType = l;
  801. }
  802. }
  803. if (fileType.empty()) {
  804. // Compute the extension without leading '.'.
  805. std::string ext = cmSystemTools::GetFilenameLastExtension(fullpath);
  806. if (!ext.empty()) {
  807. ext = ext.substr(1);
  808. }
  809. // If fullpath references a directory, then we need to specify
  810. // lastKnownFileType as folder in order for Xcode to be able to
  811. // open the contents of the folder.
  812. // (Xcode 4.6 does not like explicitFileType=folder).
  813. if (cmSystemTools::FileIsDirectory(fullpath)) {
  814. fileType = (ext == "xcassets" ? "folder.assetcatalog" : "folder");
  815. useLastKnownFileType = true;
  816. } else {
  817. fileType =
  818. GetSourcecodeValueFromFileExtension(ext, lang, useLastKnownFileType);
  819. }
  820. }
  821. fileRef->AddAttribute(useLastKnownFileType ? "lastKnownFileType"
  822. : "explicitFileType",
  823. this->CreateString(fileType));
  824. // Store the file path relative to the top of the source tree.
  825. std::string path = this->RelativeToSource(fullpath.c_str());
  826. std::string name = cmSystemTools::GetFilenameName(path);
  827. const char* sourceTree =
  828. (cmSystemTools::FileIsFullPath(path.c_str()) ? "<absolute>"
  829. : "SOURCE_ROOT");
  830. fileRef->AddAttribute("name", this->CreateString(name));
  831. fileRef->AddAttribute("path", this->CreateString(path));
  832. fileRef->AddAttribute("sourceTree", this->CreateString(sourceTree));
  833. return fileRef;
  834. }
  835. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeFileReference(
  836. cmSourceFile* sf, cmGeneratorTarget* target)
  837. {
  838. std::string lang = this->CurrentLocalGenerator->GetSourceFileLanguage(*sf);
  839. return this->CreateXCodeFileReferenceFromPath(sf->GetFullPath(), target,
  840. lang, sf);
  841. }
  842. bool cmGlobalXCodeGenerator::SpecialTargetEmitted(std::string const& tname)
  843. {
  844. if (tname == "ALL_BUILD" || tname == "XCODE_DEPEND_HELPER" ||
  845. tname == "install" || tname == "package" || tname == "RUN_TESTS" ||
  846. tname == CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  847. if (this->TargetDoneSet.find(tname) != this->TargetDoneSet.end()) {
  848. return true;
  849. }
  850. this->TargetDoneSet.insert(tname);
  851. return false;
  852. }
  853. return false;
  854. }
  855. void cmGlobalXCodeGenerator::SetCurrentLocalGenerator(cmLocalGenerator* gen)
  856. {
  857. this->CurrentLocalGenerator = gen;
  858. this->CurrentMakefile = gen->GetMakefile();
  859. // Select the current set of configuration types.
  860. this->CurrentConfigurationTypes.clear();
  861. this->CurrentMakefile->GetConfigurations(this->CurrentConfigurationTypes);
  862. if (this->CurrentConfigurationTypes.empty()) {
  863. this->CurrentConfigurationTypes.push_back("");
  864. }
  865. }
  866. struct cmSourceFilePathCompare
  867. {
  868. bool operator()(cmSourceFile* l, cmSourceFile* r)
  869. {
  870. return l->GetFullPath() < r->GetFullPath();
  871. }
  872. };
  873. struct cmCompareTargets
  874. {
  875. bool operator()(std::string const& a, std::string const& b) const
  876. {
  877. if (a == "ALL_BUILD") {
  878. return true;
  879. }
  880. if (b == "ALL_BUILD") {
  881. return false;
  882. }
  883. return strcmp(a.c_str(), b.c_str()) < 0;
  884. }
  885. };
  886. bool cmGlobalXCodeGenerator::CreateXCodeTargets(
  887. cmLocalGenerator* gen, std::vector<cmXCodeObject*>& targets)
  888. {
  889. this->SetCurrentLocalGenerator(gen);
  890. typedef std::map<std::string, cmGeneratorTarget*, cmCompareTargets>
  891. cmSortedTargets;
  892. cmSortedTargets sortedTargets;
  893. for (auto tgt : this->CurrentLocalGenerator->GetGeneratorTargets()) {
  894. sortedTargets[tgt->GetName()] = tgt;
  895. }
  896. for (auto& sortedTarget : sortedTargets) {
  897. cmGeneratorTarget* gtgt = sortedTarget.second;
  898. std::string targetName = gtgt->GetName();
  899. // make sure ALL_BUILD, INSTALL, etc are only done once
  900. if (this->SpecialTargetEmitted(targetName)) {
  901. continue;
  902. }
  903. if (gtgt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  904. continue;
  905. }
  906. if (gtgt->GetType() == cmStateEnums::UTILITY ||
  907. gtgt->GetType() == cmStateEnums::GLOBAL_TARGET) {
  908. cmXCodeObject* t = this->CreateUtilityTarget(gtgt);
  909. if (!t) {
  910. return false;
  911. }
  912. targets.push_back(t);
  913. continue;
  914. }
  915. // organize the sources
  916. std::vector<cmSourceFile*> classes;
  917. if (!gtgt->GetConfigCommonSourceFiles(classes)) {
  918. return false;
  919. }
  920. // Add CMakeLists.txt file for user convenience.
  921. std::string listfile =
  922. gtgt->GetLocalGenerator()->GetCurrentSourceDirectory();
  923. listfile += "/CMakeLists.txt";
  924. classes.push_back(gtgt->Makefile->GetOrCreateSource(listfile));
  925. std::sort(classes.begin(), classes.end(), cmSourceFilePathCompare());
  926. gtgt->ComputeObjectMapping();
  927. std::vector<cmXCodeObject*> externalObjFiles;
  928. std::vector<cmXCodeObject*> headerFiles;
  929. std::vector<cmXCodeObject*> resourceFiles;
  930. std::vector<cmXCodeObject*> sourceFiles;
  931. for (auto sourceFile : classes) {
  932. cmXCodeObject* xsf = this->CreateXCodeSourceFile(
  933. this->CurrentLocalGenerator, sourceFile, gtgt);
  934. cmXCodeObject* fr = xsf->GetObject("fileRef");
  935. cmXCodeObject* filetype = fr->GetObject()->GetObject("explicitFileType");
  936. cmGeneratorTarget::SourceFileFlags tsFlags =
  937. gtgt->GetTargetSourceFileFlags(sourceFile);
  938. if (filetype && filetype->GetString() == "compiled.mach-o.objfile") {
  939. if (sourceFile->GetObjectLibrary().empty()) {
  940. externalObjFiles.push_back(xsf);
  941. }
  942. } else if (this->IsHeaderFile(sourceFile) ||
  943. (tsFlags.Type ==
  944. cmGeneratorTarget::SourceFileTypePrivateHeader) ||
  945. (tsFlags.Type ==
  946. cmGeneratorTarget::SourceFileTypePublicHeader)) {
  947. headerFiles.push_back(xsf);
  948. } else if (tsFlags.Type == cmGeneratorTarget::SourceFileTypeResource) {
  949. resourceFiles.push_back(xsf);
  950. } else if (!sourceFile->GetPropertyAsBool("HEADER_FILE_ONLY")) {
  951. // Include this file in the build if it has a known language
  952. // and has not been listed as an ignored extension for this
  953. // generator.
  954. if (!this->CurrentLocalGenerator->GetSourceFileLanguage(*sourceFile)
  955. .empty() &&
  956. !this->IgnoreFile(sourceFile->GetExtension().c_str())) {
  957. sourceFiles.push_back(xsf);
  958. }
  959. }
  960. }
  961. if (this->XcodeVersion < 50) {
  962. // Add object library contents as external objects. (Equivalent to
  963. // the externalObjFiles above, except each one is not a cmSourceFile
  964. // within the target.)
  965. std::vector<cmSourceFile const*> objs;
  966. gtgt->GetExternalObjects(objs, "");
  967. for (auto sourceFile : objs) {
  968. if (sourceFile->GetObjectLibrary().empty()) {
  969. continue;
  970. }
  971. std::string const& obj = sourceFile->GetFullPath();
  972. cmXCodeObject* xsf =
  973. this->CreateXCodeSourceFileFromPath(obj, gtgt, "", nullptr);
  974. externalObjFiles.push_back(xsf);
  975. }
  976. }
  977. // some build phases only apply to bundles and/or frameworks
  978. bool isFrameworkTarget = gtgt->IsFrameworkOnApple();
  979. bool isBundleTarget = gtgt->GetPropertyAsBool("MACOSX_BUNDLE");
  980. bool isCFBundleTarget = gtgt->IsCFBundleOnApple();
  981. cmXCodeObject* buildFiles = nullptr;
  982. // create source build phase
  983. cmXCodeObject* sourceBuildPhase = nullptr;
  984. if (!sourceFiles.empty()) {
  985. sourceBuildPhase =
  986. this->CreateObject(cmXCodeObject::PBXSourcesBuildPhase);
  987. sourceBuildPhase->SetComment("Sources");
  988. sourceBuildPhase->AddAttribute("buildActionMask",
  989. this->CreateString("2147483647"));
  990. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  991. for (auto& sourceFile : sourceFiles) {
  992. buildFiles->AddObject(sourceFile);
  993. }
  994. sourceBuildPhase->AddAttribute("files", buildFiles);
  995. sourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  996. this->CreateString("0"));
  997. }
  998. // create header build phase - only for framework targets
  999. cmXCodeObject* headerBuildPhase = nullptr;
  1000. if (!headerFiles.empty() && isFrameworkTarget) {
  1001. headerBuildPhase =
  1002. this->CreateObject(cmXCodeObject::PBXHeadersBuildPhase);
  1003. headerBuildPhase->SetComment("Headers");
  1004. headerBuildPhase->AddAttribute("buildActionMask",
  1005. this->CreateString("2147483647"));
  1006. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1007. for (auto& headerFile : headerFiles) {
  1008. buildFiles->AddObject(headerFile);
  1009. }
  1010. headerBuildPhase->AddAttribute("files", buildFiles);
  1011. headerBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1012. this->CreateString("0"));
  1013. }
  1014. // create resource build phase - only for framework or bundle targets
  1015. cmXCodeObject* resourceBuildPhase = nullptr;
  1016. if (!resourceFiles.empty() &&
  1017. (isFrameworkTarget || isBundleTarget || isCFBundleTarget)) {
  1018. resourceBuildPhase =
  1019. this->CreateObject(cmXCodeObject::PBXResourcesBuildPhase);
  1020. resourceBuildPhase->SetComment("Resources");
  1021. resourceBuildPhase->AddAttribute("buildActionMask",
  1022. this->CreateString("2147483647"));
  1023. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1024. for (auto& resourceFile : resourceFiles) {
  1025. buildFiles->AddObject(resourceFile);
  1026. }
  1027. resourceBuildPhase->AddAttribute("files", buildFiles);
  1028. resourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1029. this->CreateString("0"));
  1030. }
  1031. // create vector of "non-resource content file" build phases - only for
  1032. // framework or bundle targets
  1033. std::vector<cmXCodeObject*> contentBuildPhases;
  1034. if (isFrameworkTarget || isBundleTarget || isCFBundleTarget) {
  1035. typedef std::map<std::string, std::vector<cmSourceFile*>>
  1036. mapOfVectorOfSourceFiles;
  1037. mapOfVectorOfSourceFiles bundleFiles;
  1038. for (auto sourceFile : classes) {
  1039. cmGeneratorTarget::SourceFileFlags tsFlags =
  1040. gtgt->GetTargetSourceFileFlags(sourceFile);
  1041. if (tsFlags.Type == cmGeneratorTarget::SourceFileTypeMacContent) {
  1042. bundleFiles[tsFlags.MacFolder].push_back(sourceFile);
  1043. }
  1044. }
  1045. for (auto keySources : bundleFiles) {
  1046. cmXCodeObject* copyFilesBuildPhase =
  1047. this->CreateObject(cmXCodeObject::PBXCopyFilesBuildPhase);
  1048. copyFilesBuildPhase->SetComment("Copy files");
  1049. copyFilesBuildPhase->AddAttribute("buildActionMask",
  1050. this->CreateString("2147483647"));
  1051. copyFilesBuildPhase->AddAttribute("dstSubfolderSpec",
  1052. this->CreateString("6"));
  1053. std::ostringstream ostr;
  1054. if (gtgt->IsFrameworkOnApple()) {
  1055. // dstPath in frameworks is relative to Versions/<version>
  1056. ostr << keySources.first;
  1057. } else if (keySources.first != "MacOS") {
  1058. if (gtgt->Target->GetMakefile()->PlatformIsAppleEmbedded()) {
  1059. ostr << keySources.first;
  1060. } else {
  1061. // dstPath in bundles is relative to Contents/MacOS
  1062. ostr << "../" << keySources.first;
  1063. }
  1064. }
  1065. copyFilesBuildPhase->AddAttribute("dstPath",
  1066. this->CreateString(ostr.str()));
  1067. copyFilesBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1068. this->CreateString("0"));
  1069. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1070. copyFilesBuildPhase->AddAttribute("files", buildFiles);
  1071. for (auto sourceFile : keySources.second) {
  1072. cmXCodeObject* xsf = this->CreateXCodeSourceFile(
  1073. this->CurrentLocalGenerator, sourceFile, gtgt);
  1074. buildFiles->AddObject(xsf);
  1075. }
  1076. contentBuildPhases.push_back(copyFilesBuildPhase);
  1077. }
  1078. }
  1079. // create vector of "resource content file" build phases - only for
  1080. // framework or bundle targets
  1081. if (isFrameworkTarget || isBundleTarget || isCFBundleTarget) {
  1082. typedef std::map<std::string, std::vector<cmSourceFile*>>
  1083. mapOfVectorOfSourceFiles;
  1084. mapOfVectorOfSourceFiles bundleFiles;
  1085. for (auto sourceFile : classes) {
  1086. cmGeneratorTarget::SourceFileFlags tsFlags =
  1087. gtgt->GetTargetSourceFileFlags(sourceFile);
  1088. if (tsFlags.Type == cmGeneratorTarget::SourceFileTypeDeepResource) {
  1089. bundleFiles[tsFlags.MacFolder].push_back(sourceFile);
  1090. }
  1091. }
  1092. for (auto keySources : bundleFiles) {
  1093. cmXCodeObject* copyFilesBuildPhase =
  1094. this->CreateObject(cmXCodeObject::PBXCopyFilesBuildPhase);
  1095. copyFilesBuildPhase->SetComment("Copy files");
  1096. copyFilesBuildPhase->AddAttribute("buildActionMask",
  1097. this->CreateString("2147483647"));
  1098. copyFilesBuildPhase->AddAttribute("dstSubfolderSpec",
  1099. this->CreateString("7"));
  1100. copyFilesBuildPhase->AddAttribute(
  1101. "dstPath", this->CreateString(keySources.first));
  1102. copyFilesBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1103. this->CreateString("0"));
  1104. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1105. copyFilesBuildPhase->AddAttribute("files", buildFiles);
  1106. for (auto sourceFile : keySources.second) {
  1107. cmXCodeObject* xsf = this->CreateXCodeSourceFile(
  1108. this->CurrentLocalGenerator, sourceFile, gtgt);
  1109. buildFiles->AddObject(xsf);
  1110. }
  1111. contentBuildPhases.push_back(copyFilesBuildPhase);
  1112. }
  1113. }
  1114. // create framework build phase
  1115. cmXCodeObject* frameworkBuildPhase = nullptr;
  1116. if (!externalObjFiles.empty()) {
  1117. frameworkBuildPhase =
  1118. this->CreateObject(cmXCodeObject::PBXFrameworksBuildPhase);
  1119. frameworkBuildPhase->SetComment("Frameworks");
  1120. frameworkBuildPhase->AddAttribute("buildActionMask",
  1121. this->CreateString("2147483647"));
  1122. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1123. frameworkBuildPhase->AddAttribute("files", buildFiles);
  1124. for (auto& externalObjFile : externalObjFiles) {
  1125. buildFiles->AddObject(externalObjFile);
  1126. }
  1127. frameworkBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1128. this->CreateString("0"));
  1129. }
  1130. // create list of build phases and create the Xcode target
  1131. cmXCodeObject* buildPhases =
  1132. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1133. this->CreateCustomCommands(buildPhases, sourceBuildPhase, headerBuildPhase,
  1134. resourceBuildPhase, contentBuildPhases,
  1135. frameworkBuildPhase, gtgt);
  1136. targets.push_back(this->CreateXCodeTarget(gtgt, buildPhases));
  1137. }
  1138. return true;
  1139. }
  1140. void cmGlobalXCodeGenerator::ForceLinkerLanguages()
  1141. {
  1142. for (auto localGenerator : this->LocalGenerators) {
  1143. // All targets depend on the build-system check target.
  1144. for (auto tgt : localGenerator->GetGeneratorTargets()) {
  1145. // This makes sure all targets link using the proper language.
  1146. this->ForceLinkerLanguage(tgt);
  1147. }
  1148. }
  1149. }
  1150. void cmGlobalXCodeGenerator::ForceLinkerLanguage(cmGeneratorTarget* gtgt)
  1151. {
  1152. // This matters only for targets that link.
  1153. if (gtgt->GetType() != cmStateEnums::EXECUTABLE &&
  1154. gtgt->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1155. gtgt->GetType() != cmStateEnums::MODULE_LIBRARY) {
  1156. return;
  1157. }
  1158. std::string llang = gtgt->GetLinkerLanguage("NOCONFIG");
  1159. if (llang.empty()) {
  1160. return;
  1161. }
  1162. // If the language is compiled as a source trust Xcode to link with it.
  1163. for (auto const& Language :
  1164. gtgt->GetLinkImplementation("NOCONFIG")->Languages) {
  1165. if (Language == llang) {
  1166. return;
  1167. }
  1168. }
  1169. // Add an empty source file to the target that compiles with the
  1170. // linker language. This should convince Xcode to choose the proper
  1171. // language.
  1172. cmMakefile* mf = gtgt->Target->GetMakefile();
  1173. std::string fname = gtgt->GetLocalGenerator()->GetCurrentBinaryDirectory();
  1174. fname += cmake::GetCMakeFilesDirectory();
  1175. fname += "/";
  1176. fname += gtgt->GetName();
  1177. fname += "-CMakeForceLinker";
  1178. fname += ".";
  1179. fname += cmSystemTools::LowerCase(llang);
  1180. {
  1181. cmGeneratedFileStream fout(fname.c_str());
  1182. fout << "\n";
  1183. }
  1184. if (cmSourceFile* sf = mf->GetOrCreateSource(fname)) {
  1185. sf->SetProperty("LANGUAGE", llang.c_str());
  1186. gtgt->AddSource(fname);
  1187. }
  1188. }
  1189. bool cmGlobalXCodeGenerator::IsHeaderFile(cmSourceFile* sf)
  1190. {
  1191. const std::vector<std::string>& hdrExts =
  1192. this->CMakeInstance->GetHeaderExtensions();
  1193. return (std::find(hdrExts.begin(), hdrExts.end(), sf->GetExtension()) !=
  1194. hdrExts.end());
  1195. }
  1196. cmXCodeObject* cmGlobalXCodeGenerator::CreateBuildPhase(
  1197. const char* name, const char* name2, cmGeneratorTarget* target,
  1198. const std::vector<cmCustomCommand>& commands)
  1199. {
  1200. if (commands.empty() && strcmp(name, "CMake ReRun") != 0) {
  1201. return nullptr;
  1202. }
  1203. cmXCodeObject* buildPhase =
  1204. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1205. buildPhase->AddAttribute("buildActionMask",
  1206. this->CreateString("2147483647"));
  1207. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1208. buildPhase->AddAttribute("files", buildFiles);
  1209. buildPhase->AddAttribute("name", this->CreateString(name));
  1210. buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1211. this->CreateString("0"));
  1212. buildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  1213. this->AddCommandsToBuildPhase(buildPhase, target, commands, name2);
  1214. return buildPhase;
  1215. }
  1216. void cmGlobalXCodeGenerator::CreateCustomCommands(
  1217. cmXCodeObject* buildPhases, cmXCodeObject* sourceBuildPhase,
  1218. cmXCodeObject* headerBuildPhase, cmXCodeObject* resourceBuildPhase,
  1219. std::vector<cmXCodeObject*> contentBuildPhases,
  1220. cmXCodeObject* frameworkBuildPhase, cmGeneratorTarget* gtgt)
  1221. {
  1222. std::vector<cmCustomCommand> const& prebuild = gtgt->GetPreBuildCommands();
  1223. std::vector<cmCustomCommand> const& prelink = gtgt->GetPreLinkCommands();
  1224. std::vector<cmCustomCommand> postbuild = gtgt->GetPostBuildCommands();
  1225. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY &&
  1226. !gtgt->IsFrameworkOnApple()) {
  1227. cmCustomCommandLines cmd;
  1228. cmd.resize(1);
  1229. cmd[0].push_back(cmSystemTools::GetCMakeCommand());
  1230. cmd[0].push_back("-E");
  1231. cmd[0].push_back("cmake_symlink_library");
  1232. std::string str_file = "$<TARGET_FILE:";
  1233. str_file += gtgt->GetName();
  1234. str_file += ">";
  1235. std::string str_so_file = "$<TARGET_SONAME_FILE:";
  1236. str_so_file += gtgt->GetName();
  1237. str_so_file += ">";
  1238. std::string str_link_file = "$<TARGET_LINKER_FILE:";
  1239. str_link_file += gtgt->GetName();
  1240. str_link_file += ">";
  1241. cmd[0].push_back(str_file);
  1242. cmd[0].push_back(str_so_file);
  1243. cmd[0].push_back(str_link_file);
  1244. cmCustomCommand command(this->CurrentMakefile, std::vector<std::string>(),
  1245. std::vector<std::string>(),
  1246. std::vector<std::string>(), cmd,
  1247. "Creating symlinks", "");
  1248. postbuild.push_back(command);
  1249. }
  1250. std::vector<cmSourceFile*> classes;
  1251. if (!gtgt->GetConfigCommonSourceFiles(classes)) {
  1252. return;
  1253. }
  1254. // add all the sources
  1255. std::vector<cmCustomCommand> commands;
  1256. for (auto sourceFile : classes) {
  1257. if (sourceFile->GetCustomCommand()) {
  1258. commands.push_back(*sourceFile->GetCustomCommand());
  1259. }
  1260. }
  1261. // create prebuild phase
  1262. cmXCodeObject* cmakeRulesBuildPhase = this->CreateBuildPhase(
  1263. "CMake Rules", "cmakeRulesBuildPhase", gtgt, commands);
  1264. // create prebuild phase
  1265. cmXCodeObject* preBuildPhase = this->CreateBuildPhase(
  1266. "CMake PreBuild Rules", "preBuildCommands", gtgt, prebuild);
  1267. // create prelink phase
  1268. cmXCodeObject* preLinkPhase = this->CreateBuildPhase(
  1269. "CMake PreLink Rules", "preLinkCommands", gtgt, prelink);
  1270. // create postbuild phase
  1271. cmXCodeObject* postBuildPhase = this->CreateBuildPhase(
  1272. "CMake PostBuild Rules", "postBuildPhase", gtgt, postbuild);
  1273. // The order here is the order they will be built in.
  1274. // The order "headers, resources, sources" mimics a native project generated
  1275. // from an xcode template...
  1276. //
  1277. if (preBuildPhase) {
  1278. buildPhases->AddObject(preBuildPhase);
  1279. }
  1280. if (cmakeRulesBuildPhase) {
  1281. buildPhases->AddObject(cmakeRulesBuildPhase);
  1282. }
  1283. if (headerBuildPhase) {
  1284. buildPhases->AddObject(headerBuildPhase);
  1285. }
  1286. if (resourceBuildPhase) {
  1287. buildPhases->AddObject(resourceBuildPhase);
  1288. }
  1289. for (auto obj : contentBuildPhases) {
  1290. buildPhases->AddObject(obj);
  1291. }
  1292. if (sourceBuildPhase) {
  1293. buildPhases->AddObject(sourceBuildPhase);
  1294. }
  1295. if (preLinkPhase) {
  1296. buildPhases->AddObject(preLinkPhase);
  1297. }
  1298. if (frameworkBuildPhase) {
  1299. buildPhases->AddObject(frameworkBuildPhase);
  1300. }
  1301. if (postBuildPhase) {
  1302. buildPhases->AddObject(postBuildPhase);
  1303. }
  1304. }
  1305. // This function removes each occurrence of the flag and returns the last one
  1306. // (i.e., the dominant flag in GCC)
  1307. std::string cmGlobalXCodeGenerator::ExtractFlag(const char* flag,
  1308. std::string& flags)
  1309. {
  1310. std::string retFlag;
  1311. std::string::size_type lastOccurancePos = flags.rfind(flag);
  1312. bool saved = false;
  1313. while (lastOccurancePos != std::string::npos) {
  1314. // increment pos, we use lastOccurancePos to reduce search space on next
  1315. // inc
  1316. std::string::size_type pos = lastOccurancePos;
  1317. if (pos == 0 || flags[pos - 1] == ' ') {
  1318. while (pos < flags.size() && flags[pos] != ' ') {
  1319. if (!saved) {
  1320. retFlag += flags[pos];
  1321. }
  1322. flags[pos] = ' ';
  1323. pos++;
  1324. }
  1325. saved = true;
  1326. }
  1327. // decrement lastOccurancePos while making sure we don't loop around
  1328. // and become a very large positive number since size_type is unsigned
  1329. lastOccurancePos = lastOccurancePos == 0 ? 0 : lastOccurancePos - 1;
  1330. lastOccurancePos = flags.rfind(flag, lastOccurancePos);
  1331. }
  1332. return retFlag;
  1333. }
  1334. // This function removes each matching occurrence of the expression and
  1335. // returns the last one (i.e., the dominant flag in GCC)
  1336. std::string cmGlobalXCodeGenerator::ExtractFlagRegex(const char* exp,
  1337. int matchIndex,
  1338. std::string& flags)
  1339. {
  1340. std::string retFlag;
  1341. cmsys::RegularExpression regex(exp);
  1342. assert(regex.is_valid());
  1343. if (!regex.is_valid()) {
  1344. return retFlag;
  1345. }
  1346. std::string::size_type offset = 0;
  1347. while (regex.find(flags.c_str() + offset)) {
  1348. const std::string::size_type startPos = offset + regex.start(matchIndex);
  1349. const std::string::size_type endPos = offset + regex.end(matchIndex);
  1350. const std::string::size_type size = endPos - startPos;
  1351. offset = startPos + 1;
  1352. retFlag.assign(flags, startPos, size);
  1353. flags.replace(startPos, size, size, ' ');
  1354. }
  1355. return retFlag;
  1356. }
  1357. //----------------------------------------------------------------------------
  1358. // This function strips off Xcode attributes that do not target the current
  1359. // configuration
  1360. void cmGlobalXCodeGenerator::FilterConfigurationAttribute(
  1361. std::string const& configName, std::string& attribute)
  1362. {
  1363. // Handle [variant=<config>] condition explicitly here.
  1364. std::string::size_type beginVariant = attribute.find("[variant=");
  1365. if (beginVariant == std::string::npos) {
  1366. // There is no variant in this attribute.
  1367. return;
  1368. }
  1369. std::string::size_type endVariant = attribute.find(']', beginVariant + 9);
  1370. if (endVariant == std::string::npos) {
  1371. // There is no terminating bracket.
  1372. return;
  1373. }
  1374. // Compare the variant to the configuration.
  1375. std::string variant =
  1376. attribute.substr(beginVariant + 9, endVariant - beginVariant - 9);
  1377. if (variant == configName) {
  1378. // The variant matches the configuration so use this
  1379. // attribute but drop the [variant=<config>] condition.
  1380. attribute.erase(beginVariant, endVariant - beginVariant + 1);
  1381. } else {
  1382. // The variant does not match the configuration so
  1383. // do not use this attribute.
  1384. attribute.clear();
  1385. }
  1386. }
  1387. void cmGlobalXCodeGenerator::AddCommandsToBuildPhase(
  1388. cmXCodeObject* buildphase, cmGeneratorTarget* target,
  1389. std::vector<cmCustomCommand> const& commands, const char* name)
  1390. {
  1391. std::string dir = this->CurrentLocalGenerator->GetCurrentBinaryDirectory();
  1392. dir += "/CMakeScripts";
  1393. cmSystemTools::MakeDirectory(dir.c_str());
  1394. std::string makefile = dir;
  1395. makefile += "/";
  1396. makefile += target->GetName();
  1397. makefile += "_";
  1398. makefile += name;
  1399. makefile += ".make";
  1400. for (const auto& currentConfig : this->CurrentConfigurationTypes) {
  1401. this->CreateCustomRulesMakefile(makefile.c_str(), target, commands,
  1402. currentConfig);
  1403. }
  1404. std::string cdir = this->CurrentLocalGenerator->GetCurrentBinaryDirectory();
  1405. cdir = this->ConvertToRelativeForMake(cdir.c_str());
  1406. std::string makecmd = "make -C ";
  1407. makecmd += cdir;
  1408. makecmd += " -f ";
  1409. makecmd +=
  1410. this->ConvertToRelativeForMake((makefile + "$CONFIGURATION").c_str());
  1411. makecmd += " all";
  1412. buildphase->AddAttribute("shellScript", this->CreateString(makecmd));
  1413. buildphase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  1414. }
  1415. void cmGlobalXCodeGenerator::CreateCustomRulesMakefile(
  1416. const char* makefileBasename, cmGeneratorTarget* target,
  1417. std::vector<cmCustomCommand> const& commands, const std::string& configName)
  1418. {
  1419. std::string makefileName = makefileBasename;
  1420. makefileName += configName;
  1421. cmGeneratedFileStream makefileStream(makefileName.c_str());
  1422. if (!makefileStream) {
  1423. return;
  1424. }
  1425. makefileStream.SetCopyIfDifferent(true);
  1426. makefileStream << "# Generated by CMake, DO NOT EDIT\n";
  1427. makefileStream << "# Custom rules for " << target->GetName() << "\n";
  1428. // disable the implicit rules
  1429. makefileStream << ".SUFFIXES: "
  1430. << "\n";
  1431. // have all depend on all outputs
  1432. makefileStream << "all: ";
  1433. std::map<const cmCustomCommand*, std::string> tname;
  1434. int count = 0;
  1435. for (auto const& command : commands) {
  1436. cmCustomCommandGenerator ccg(command, configName,
  1437. this->CurrentLocalGenerator);
  1438. if (ccg.GetNumberOfCommands() > 0) {
  1439. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1440. if (!outputs.empty()) {
  1441. for (auto const& output : outputs) {
  1442. makefileStream << "\\\n\t"
  1443. << this->ConvertToRelativeForMake(output.c_str());
  1444. }
  1445. } else {
  1446. std::ostringstream str;
  1447. str << "_buildpart_" << count++;
  1448. tname[&ccg.GetCC()] = std::string(target->GetName()) + str.str();
  1449. makefileStream << "\\\n\t" << tname[&ccg.GetCC()];
  1450. }
  1451. }
  1452. }
  1453. makefileStream << "\n\n";
  1454. for (auto const& command : commands) {
  1455. cmCustomCommandGenerator ccg(command, configName,
  1456. this->CurrentLocalGenerator);
  1457. if (ccg.GetNumberOfCommands() > 0) {
  1458. makefileStream << "\n";
  1459. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1460. if (!outputs.empty()) {
  1461. // There is at least one output, start the rule for it
  1462. const char* sep = "";
  1463. for (auto const& output : outputs) {
  1464. makefileStream << sep
  1465. << this->ConvertToRelativeForMake(output.c_str());
  1466. sep = " ";
  1467. }
  1468. makefileStream << ": ";
  1469. } else {
  1470. // There are no outputs. Use the generated force rule name.
  1471. makefileStream << tname[&ccg.GetCC()] << ": ";
  1472. }
  1473. for (auto const& d : ccg.GetDepends()) {
  1474. std::string dep;
  1475. if (this->CurrentLocalGenerator->GetRealDependency(d, configName,
  1476. dep)) {
  1477. makefileStream << "\\\n"
  1478. << this->ConvertToRelativeForMake(dep.c_str());
  1479. }
  1480. }
  1481. makefileStream << "\n";
  1482. if (const char* comment = ccg.GetComment()) {
  1483. std::string echo_cmd = "echo ";
  1484. echo_cmd += (this->CurrentLocalGenerator->EscapeForShell(
  1485. comment, ccg.GetCC().GetEscapeAllowMakeVars()));
  1486. makefileStream << "\t" << echo_cmd << "\n";
  1487. }
  1488. // Add each command line to the set of commands.
  1489. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  1490. // Build the command line in a single string.
  1491. std::string cmd2 = ccg.GetCommand(c);
  1492. cmSystemTools::ReplaceString(cmd2, "/./", "/");
  1493. cmd2 = this->ConvertToRelativeForMake(cmd2.c_str());
  1494. std::string cmd;
  1495. std::string wd = ccg.GetWorkingDirectory();
  1496. if (!wd.empty()) {
  1497. cmd += "cd ";
  1498. cmd += this->ConvertToRelativeForMake(wd.c_str());
  1499. cmd += " && ";
  1500. }
  1501. cmd += cmd2;
  1502. ccg.AppendArguments(c, cmd);
  1503. makefileStream << "\t" << cmd << "\n";
  1504. }
  1505. }
  1506. }
  1507. }
  1508. void cmGlobalXCodeGenerator::CreateBuildSettings(cmGeneratorTarget* gtgt,
  1509. cmXCodeObject* buildSettings,
  1510. const std::string& configName)
  1511. {
  1512. if (gtgt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  1513. return;
  1514. }
  1515. std::string defFlags;
  1516. bool shared = ((gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) ||
  1517. (gtgt->GetType() == cmStateEnums::MODULE_LIBRARY));
  1518. bool binary = ((gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) ||
  1519. (gtgt->GetType() == cmStateEnums::STATIC_LIBRARY) ||
  1520. (gtgt->GetType() == cmStateEnums::EXECUTABLE) || shared);
  1521. // Compute the compilation flags for each language.
  1522. std::set<std::string> languages;
  1523. gtgt->GetLanguages(languages, configName);
  1524. std::map<std::string, std::string> cflags;
  1525. for (auto const& lang : languages) {
  1526. std::string& flags = cflags[lang];
  1527. // Add language-specific flags.
  1528. this->CurrentLocalGenerator->AddLanguageFlags(flags, gtgt, lang,
  1529. configName);
  1530. // Add shared-library flags if needed.
  1531. this->CurrentLocalGenerator->AddCMP0018Flags(flags, gtgt, lang,
  1532. configName);
  1533. this->CurrentLocalGenerator->AddVisibilityPresetFlags(flags, gtgt, lang);
  1534. this->CurrentLocalGenerator->AddCompileOptions(flags, gtgt, lang,
  1535. configName);
  1536. }
  1537. std::string llang = gtgt->GetLinkerLanguage(configName);
  1538. if (binary && llang.empty()) {
  1539. cmSystemTools::Error(
  1540. "CMake can not determine linker language for target: ",
  1541. gtgt->GetName().c_str());
  1542. return;
  1543. }
  1544. std::string const& langForPreprocessor = llang;
  1545. if (gtgt->IsIPOEnabled(llang, configName)) {
  1546. const char* ltoValue =
  1547. this->CurrentMakefile->IsOn("_CMAKE_LTO_THIN") ? "YES_THIN" : "YES";
  1548. buildSettings->AddAttribute("LLVM_LTO", this->CreateString(ltoValue));
  1549. }
  1550. // Add define flags
  1551. this->CurrentLocalGenerator->AppendFlags(
  1552. defFlags, this->CurrentMakefile->GetDefineFlags());
  1553. // Add preprocessor definitions for this target and configuration.
  1554. BuildObjectListOrString ppDefs(this, true);
  1555. this->AppendDefines(
  1556. ppDefs, "CMAKE_INTDIR=\"$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)\"");
  1557. if (const char* exportMacro = gtgt->GetExportMacro()) {
  1558. // Add the export symbol definition for shared library objects.
  1559. this->AppendDefines(ppDefs, exportMacro);
  1560. }
  1561. std::vector<std::string> targetDefines;
  1562. if (!langForPreprocessor.empty()) {
  1563. gtgt->GetCompileDefinitions(targetDefines, configName,
  1564. langForPreprocessor);
  1565. }
  1566. this->AppendDefines(ppDefs, targetDefines);
  1567. buildSettings->AddAttribute("GCC_PREPROCESSOR_DEFINITIONS",
  1568. ppDefs.CreateList());
  1569. std::string extraLinkOptionsVar;
  1570. std::string extraLinkOptions;
  1571. if (gtgt->GetType() == cmStateEnums::EXECUTABLE) {
  1572. extraLinkOptionsVar = "CMAKE_EXE_LINKER_FLAGS";
  1573. } else if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  1574. extraLinkOptionsVar = "CMAKE_SHARED_LINKER_FLAGS";
  1575. } else if (gtgt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  1576. extraLinkOptionsVar = "CMAKE_MODULE_LINKER_FLAGS";
  1577. }
  1578. if (!extraLinkOptionsVar.empty()) {
  1579. this->CurrentLocalGenerator->AddConfigVariableFlags(
  1580. extraLinkOptions, extraLinkOptionsVar, configName);
  1581. }
  1582. if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  1583. gtgt->GetType() == cmStateEnums::STATIC_LIBRARY) {
  1584. this->CurrentLocalGenerator->GetStaticLibraryFlags(
  1585. extraLinkOptions, cmSystemTools::UpperCase(configName), gtgt);
  1586. } else {
  1587. const char* targetLinkFlags = gtgt->GetProperty("LINK_FLAGS");
  1588. if (targetLinkFlags) {
  1589. this->CurrentLocalGenerator->AppendFlags(extraLinkOptions,
  1590. targetLinkFlags);
  1591. }
  1592. if (!configName.empty()) {
  1593. std::string linkFlagsVar = "LINK_FLAGS_";
  1594. linkFlagsVar += cmSystemTools::UpperCase(configName);
  1595. if (const char* linkFlags = gtgt->GetProperty(linkFlagsVar)) {
  1596. this->CurrentLocalGenerator->AppendFlags(extraLinkOptions, linkFlags);
  1597. }
  1598. }
  1599. }
  1600. // Set target-specific architectures.
  1601. std::vector<std::string> archs;
  1602. gtgt->GetAppleArchs(configName, archs);
  1603. if (!archs.empty()) {
  1604. // Enable ARCHS attribute.
  1605. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH", this->CreateString("NO"));
  1606. // Store ARCHS value.
  1607. if (archs.size() == 1) {
  1608. buildSettings->AddAttribute("ARCHS", this->CreateString(archs[0]));
  1609. } else {
  1610. cmXCodeObject* archObjects =
  1611. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1612. for (auto& arch : archs) {
  1613. archObjects->AddObject(this->CreateString(arch));
  1614. }
  1615. buildSettings->AddAttribute("ARCHS", archObjects);
  1616. }
  1617. }
  1618. // Get the product name components.
  1619. std::string pnprefix;
  1620. std::string pnbase;
  1621. std::string pnsuffix;
  1622. gtgt->GetFullNameComponents(pnprefix, pnbase, pnsuffix, configName);
  1623. const char* version = gtgt->GetProperty("VERSION");
  1624. const char* soversion = gtgt->GetProperty("SOVERSION");
  1625. if (!gtgt->HasSOName(configName) || gtgt->IsFrameworkOnApple()) {
  1626. version = nullptr;
  1627. soversion = nullptr;
  1628. }
  1629. if (version && !soversion) {
  1630. soversion = version;
  1631. }
  1632. if (!version && soversion) {
  1633. version = soversion;
  1634. }
  1635. std::string realName = pnbase;
  1636. std::string soName = pnbase;
  1637. if (version && soversion) {
  1638. realName += ".";
  1639. realName += version;
  1640. soName += ".";
  1641. soName += soversion;
  1642. }
  1643. // Set attributes to specify the proper name for the target.
  1644. std::string pndir = this->CurrentLocalGenerator->GetCurrentBinaryDirectory();
  1645. if (gtgt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  1646. gtgt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1647. gtgt->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1648. gtgt->GetType() == cmStateEnums::EXECUTABLE) {
  1649. if (!gtgt->UsesDefaultOutputDir(configName,
  1650. cmStateEnums::RuntimeBinaryArtifact)) {
  1651. std::string pncdir = gtgt->GetDirectory(configName);
  1652. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  1653. this->CreateString(pncdir));
  1654. }
  1655. if (gtgt->IsFrameworkOnApple() || gtgt->IsCFBundleOnApple()) {
  1656. pnprefix = "";
  1657. }
  1658. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  1659. this->CreateString(pnprefix));
  1660. buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
  1661. this->CreateString(pnsuffix));
  1662. } else if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1663. pnprefix = "lib";
  1664. pnbase = gtgt->GetName();
  1665. pnsuffix = ".a";
  1666. std::string pncdir =
  1667. this->GetObjectsNormalDirectory(this->CurrentProject, configName, gtgt);
  1668. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  1669. this->CreateString(pncdir));
  1670. }
  1671. // Store the product name for all target types.
  1672. buildSettings->AddAttribute("PRODUCT_NAME", this->CreateString(realName));
  1673. buildSettings->AddAttribute("SYMROOT", this->CreateString(pndir));
  1674. // Handle settings for each target type.
  1675. switch (gtgt->GetType()) {
  1676. case cmStateEnums::STATIC_LIBRARY:
  1677. if (gtgt->GetPropertyAsBool("FRAMEWORK")) {
  1678. std::string fw_version = gtgt->GetFrameworkVersion();
  1679. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  1680. this->CreateString(fw_version));
  1681. const char* ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  1682. if (ext) {
  1683. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  1684. this->CreateString(ext));
  1685. }
  1686. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1687. // Xcode will create the final version of Info.plist at build time,
  1688. // so let it replace the framework name. This avoids creating
  1689. // a per-configuration Info.plist file.
  1690. this->CurrentLocalGenerator->GenerateFrameworkInfoPList(
  1691. gtgt, "$(EXECUTABLE_NAME)", plist.c_str());
  1692. buildSettings->AddAttribute("INFOPLIST_FILE",
  1693. this->CreateString(plist));
  1694. buildSettings->AddAttribute("MACH_O_TYPE",
  1695. this->CreateString("staticlib"));
  1696. } else {
  1697. buildSettings->AddAttribute("LIBRARY_STYLE",
  1698. this->CreateString("STATIC"));
  1699. }
  1700. break;
  1701. case cmStateEnums::OBJECT_LIBRARY: {
  1702. buildSettings->AddAttribute("LIBRARY_STYLE",
  1703. this->CreateString("STATIC"));
  1704. break;
  1705. }
  1706. case cmStateEnums::MODULE_LIBRARY: {
  1707. buildSettings->AddAttribute("LIBRARY_STYLE",
  1708. this->CreateString("BUNDLE"));
  1709. if (gtgt->IsCFBundleOnApple()) {
  1710. // It turns out that a BUNDLE is basically the same
  1711. // in many ways as an application bundle, as far as
  1712. // link flags go
  1713. std::string createFlags = this->LookupFlags(
  1714. "CMAKE_SHARED_MODULE_CREATE_", llang, "_FLAGS", "-bundle");
  1715. if (!createFlags.empty()) {
  1716. extraLinkOptions += " ";
  1717. extraLinkOptions += createFlags;
  1718. }
  1719. const char* ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  1720. if (ext) {
  1721. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  1722. this->CreateString(ext));
  1723. }
  1724. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1725. // Xcode will create the final version of Info.plist at build time,
  1726. // so let it replace the cfbundle name. This avoids creating
  1727. // a per-configuration Info.plist file. The cfbundle plist
  1728. // is very similar to the application bundle plist
  1729. this->CurrentLocalGenerator->GenerateAppleInfoPList(
  1730. gtgt, "$(EXECUTABLE_NAME)", plist.c_str());
  1731. buildSettings->AddAttribute("INFOPLIST_FILE",
  1732. this->CreateString(plist));
  1733. } else {
  1734. buildSettings->AddAttribute("MACH_O_TYPE",
  1735. this->CreateString("mh_bundle"));
  1736. buildSettings->AddAttribute("GCC_DYNAMIC_NO_PIC",
  1737. this->CreateString("NO"));
  1738. // Add the flags to create an executable.
  1739. std::string createFlags =
  1740. this->LookupFlags("CMAKE_", llang, "_LINK_FLAGS", "");
  1741. if (!createFlags.empty()) {
  1742. extraLinkOptions += " ";
  1743. extraLinkOptions += createFlags;
  1744. }
  1745. }
  1746. break;
  1747. }
  1748. case cmStateEnums::SHARED_LIBRARY: {
  1749. if (gtgt->GetPropertyAsBool("FRAMEWORK")) {
  1750. std::string fw_version = gtgt->GetFrameworkVersion();
  1751. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  1752. this->CreateString(fw_version));
  1753. const char* ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  1754. if (ext) {
  1755. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  1756. this->CreateString(ext));
  1757. }
  1758. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1759. // Xcode will create the final version of Info.plist at build time,
  1760. // so let it replace the framework name. This avoids creating
  1761. // a per-configuration Info.plist file.
  1762. this->CurrentLocalGenerator->GenerateFrameworkInfoPList(
  1763. gtgt, "$(EXECUTABLE_NAME)", plist.c_str());
  1764. buildSettings->AddAttribute("INFOPLIST_FILE",
  1765. this->CreateString(plist));
  1766. } else {
  1767. // Add the flags to create a shared library.
  1768. std::string createFlags = this->LookupFlags(
  1769. "CMAKE_SHARED_LIBRARY_CREATE_", llang, "_FLAGS", "-dynamiclib");
  1770. if (!createFlags.empty()) {
  1771. extraLinkOptions += " ";
  1772. extraLinkOptions += createFlags;
  1773. }
  1774. }
  1775. buildSettings->AddAttribute("LIBRARY_STYLE",
  1776. this->CreateString("DYNAMIC"));
  1777. break;
  1778. }
  1779. case cmStateEnums::EXECUTABLE: {
  1780. // Add the flags to create an executable.
  1781. std::string createFlags =
  1782. this->LookupFlags("CMAKE_", llang, "_LINK_FLAGS", "");
  1783. if (!createFlags.empty()) {
  1784. extraLinkOptions += " ";
  1785. extraLinkOptions += createFlags;
  1786. }
  1787. // Handle bundles and normal executables separately.
  1788. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  1789. const char* ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  1790. if (ext) {
  1791. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  1792. this->CreateString(ext));
  1793. }
  1794. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1795. // Xcode will create the final version of Info.plist at build time,
  1796. // so let it replace the executable name. This avoids creating
  1797. // a per-configuration Info.plist file.
  1798. this->CurrentLocalGenerator->GenerateAppleInfoPList(
  1799. gtgt, "$(EXECUTABLE_NAME)", plist.c_str());
  1800. buildSettings->AddAttribute("INFOPLIST_FILE",
  1801. this->CreateString(plist));
  1802. }
  1803. } break;
  1804. default:
  1805. break;
  1806. }
  1807. if (this->XcodeVersion < 40) {
  1808. buildSettings->AddAttribute("PREBINDING", this->CreateString("NO"));
  1809. }
  1810. BuildObjectListOrString dirs(this, true);
  1811. BuildObjectListOrString fdirs(this, true);
  1812. BuildObjectListOrString sysdirs(this, true);
  1813. BuildObjectListOrString sysfdirs(this, true);
  1814. const bool emitSystemIncludes = this->XcodeVersion >= 83;
  1815. std::vector<std::string> includes;
  1816. if (!langForPreprocessor.empty()) {
  1817. this->CurrentLocalGenerator->GetIncludeDirectories(
  1818. includes, gtgt, langForPreprocessor, configName);
  1819. }
  1820. std::set<std::string> emitted;
  1821. emitted.insert("/System/Library/Frameworks");
  1822. for (auto& include : includes) {
  1823. if (this->NameResolvesToFramework(include)) {
  1824. std::string frameworkDir = include;
  1825. frameworkDir += "/../";
  1826. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1827. if (emitted.insert(frameworkDir).second) {
  1828. std::string incpath = this->XCodeEscapePath(frameworkDir);
  1829. if (emitSystemIncludes &&
  1830. gtgt->IsSystemIncludeDirectory(frameworkDir, configName,
  1831. langForPreprocessor)) {
  1832. sysfdirs.Add(incpath);
  1833. } else {
  1834. fdirs.Add(incpath);
  1835. }
  1836. }
  1837. } else {
  1838. std::string incpath = this->XCodeEscapePath(include);
  1839. if (emitSystemIncludes &&
  1840. gtgt->IsSystemIncludeDirectory(include, configName,
  1841. langForPreprocessor)) {
  1842. sysdirs.Add(incpath);
  1843. } else {
  1844. dirs.Add(incpath);
  1845. }
  1846. }
  1847. }
  1848. // Add framework search paths needed for linking.
  1849. if (cmComputeLinkInformation* cli = gtgt->GetLinkInformation(configName)) {
  1850. for (auto const& fwDir : cli->GetFrameworkPaths()) {
  1851. if (emitted.insert(fwDir).second) {
  1852. std::string incpath = this->XCodeEscapePath(fwDir);
  1853. if (emitSystemIncludes &&
  1854. gtgt->IsSystemIncludeDirectory(fwDir, configName,
  1855. langForPreprocessor)) {
  1856. sysfdirs.Add(incpath);
  1857. } else {
  1858. fdirs.Add(incpath);
  1859. }
  1860. }
  1861. }
  1862. }
  1863. if (!fdirs.IsEmpty()) {
  1864. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS", fdirs.CreateList());
  1865. }
  1866. if (!dirs.IsEmpty()) {
  1867. buildSettings->AddAttribute("HEADER_SEARCH_PATHS", dirs.CreateList());
  1868. }
  1869. if (!sysfdirs.IsEmpty()) {
  1870. buildSettings->AddAttribute("SYSTEM_FRAMEWORK_SEARCH_PATHS",
  1871. sysfdirs.CreateList());
  1872. }
  1873. if (!sysdirs.IsEmpty()) {
  1874. buildSettings->AddAttribute("SYSTEM_HEADER_SEARCH_PATHS",
  1875. sysdirs.CreateList());
  1876. }
  1877. if (this->XcodeVersion >= 60 && !emitSystemIncludes) {
  1878. // Add those per-language flags in addition to HEADER_SEARCH_PATHS to gain
  1879. // system include directory awareness. We need to also keep on setting
  1880. // HEADER_SEARCH_PATHS to work around a missing compile options flag for
  1881. // GNU assembly files (#16449)
  1882. for (auto const& language : languages) {
  1883. std::string includeFlags = this->CurrentLocalGenerator->GetIncludeFlags(
  1884. includes, gtgt, language, true, false, configName);
  1885. if (!includeFlags.empty()) {
  1886. cflags[language] += " " + includeFlags;
  1887. }
  1888. }
  1889. }
  1890. bool same_gflags = true;
  1891. std::map<std::string, std::string> gflags;
  1892. std::string const* last_gflag = nullptr;
  1893. std::string optLevel = "0";
  1894. // Minimal map of flags to build settings.
  1895. for (auto const& language : languages) {
  1896. std::string& flags = cflags[language];
  1897. std::string& gflag = gflags[language];
  1898. std::string oflag =
  1899. this->ExtractFlagRegex("(^| )(-Ofast|-Os|-O[0-9]*)( |$)", 2, flags);
  1900. if (oflag.size() == 2) {
  1901. optLevel = "1";
  1902. } else if (oflag.size() > 2) {
  1903. optLevel = oflag.substr(2);
  1904. }
  1905. gflag = this->ExtractFlag("-g", flags);
  1906. // put back gdwarf-2 if used since there is no way
  1907. // to represent it in the gui, but we still want debug yes
  1908. if (gflag == "-gdwarf-2") {
  1909. flags += " ";
  1910. flags += gflag;
  1911. }
  1912. if (last_gflag && *last_gflag != gflag) {
  1913. same_gflags = false;
  1914. }
  1915. last_gflag = &gflag;
  1916. }
  1917. const char* debugStr = "YES";
  1918. if (!same_gflags) {
  1919. // We can't set the Xcode flag differently depending on the language,
  1920. // so put them back in this case.
  1921. for (auto const& language : languages) {
  1922. cflags[language] += " ";
  1923. cflags[language] += gflags[language];
  1924. }
  1925. debugStr = "NO";
  1926. } else if (last_gflag && (last_gflag->empty() || *last_gflag == "-g0")) {
  1927. debugStr = "NO";
  1928. }
  1929. buildSettings->AddAttribute("COMBINE_HIDPI_IMAGES",
  1930. this->CreateString("YES"));
  1931. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  1932. this->CreateString(debugStr));
  1933. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  1934. this->CreateString(optLevel));
  1935. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  1936. this->CreateString("NO"));
  1937. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  1938. this->CreateString("NO"));
  1939. for (auto const& language : languages) {
  1940. std::string flags = cflags[language] + " " + defFlags;
  1941. if (language == "CXX") {
  1942. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  1943. this->CreateString(flags));
  1944. } else if (language == "Fortran") {
  1945. buildSettings->AddAttribute("IFORT_OTHER_FLAGS",
  1946. this->CreateString(flags));
  1947. } else if (language == "C") {
  1948. buildSettings->AddAttribute("OTHER_CFLAGS", this->CreateString(flags));
  1949. } else if (language == "Swift") {
  1950. buildSettings->AddAttribute("OTHER_SWIFT_FLAGS",
  1951. this->CreateString(flags));
  1952. }
  1953. }
  1954. // Add Fortran source format attribute if property is set.
  1955. const char* format = nullptr;
  1956. const char* tgtfmt = gtgt->GetProperty("Fortran_FORMAT");
  1957. switch (cmOutputConverter::GetFortranFormat(tgtfmt)) {
  1958. case cmOutputConverter::FortranFormatFixed:
  1959. format = "fixed";
  1960. break;
  1961. case cmOutputConverter::FortranFormatFree:
  1962. format = "free";
  1963. break;
  1964. default:
  1965. break;
  1966. }
  1967. if (format) {
  1968. buildSettings->AddAttribute("IFORT_LANG_SRCFMT",
  1969. this->CreateString(format));
  1970. }
  1971. // Create the INSTALL_PATH attribute.
  1972. std::string install_name_dir;
  1973. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  1974. // Get the install_name directory for the build tree.
  1975. install_name_dir = gtgt->GetInstallNameDirForBuildTree(configName);
  1976. // Xcode doesn't create the correct install_name in some cases.
  1977. // That is, if the INSTALL_PATH is empty, or if we have versioning
  1978. // of dylib libraries, we want to specify the install_name.
  1979. // This is done by adding a link flag to create an install_name
  1980. // with just the library soname.
  1981. std::string install_name;
  1982. if (!install_name_dir.empty()) {
  1983. // Convert to a path for the native build tool.
  1984. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  1985. install_name += install_name_dir;
  1986. install_name += "/";
  1987. }
  1988. install_name += gtgt->GetSOName(configName);
  1989. if ((realName != soName) || install_name_dir.empty()) {
  1990. install_name_dir = "";
  1991. extraLinkOptions += " -install_name ";
  1992. extraLinkOptions += XCodeEscapePath(install_name);
  1993. }
  1994. }
  1995. buildSettings->AddAttribute("INSTALL_PATH",
  1996. this->CreateString(install_name_dir));
  1997. // Create the LD_RUNPATH_SEARCH_PATHS
  1998. cmComputeLinkInformation* pcli = gtgt->GetLinkInformation(configName);
  1999. if (pcli) {
  2000. std::string search_paths;
  2001. std::vector<std::string> runtimeDirs;
  2002. pcli->GetRPath(runtimeDirs, false);
  2003. // runpath dirs needs to be unique to prevent corruption
  2004. std::set<std::string> unique_dirs;
  2005. for (auto runpath : runtimeDirs) {
  2006. runpath = this->ExpandCFGIntDir(runpath, configName);
  2007. if (unique_dirs.find(runpath) == unique_dirs.end()) {
  2008. unique_dirs.insert(runpath);
  2009. if (!search_paths.empty()) {
  2010. search_paths += " ";
  2011. }
  2012. search_paths += this->XCodeEscapePath(runpath);
  2013. }
  2014. }
  2015. if (!search_paths.empty()) {
  2016. buildSettings->AddAttribute("LD_RUNPATH_SEARCH_PATHS",
  2017. this->CreateString(search_paths));
  2018. }
  2019. }
  2020. buildSettings->AddAttribute(this->GetTargetLinkFlagsVar(gtgt),
  2021. this->CreateString(extraLinkOptions));
  2022. buildSettings->AddAttribute("OTHER_REZFLAGS", this->CreateString(""));
  2023. buildSettings->AddAttribute("SECTORDER_FLAGS", this->CreateString(""));
  2024. buildSettings->AddAttribute("USE_HEADERMAP", this->CreateString("NO"));
  2025. cmXCodeObject* group = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2026. group->AddObject(this->CreateString("-Wmost"));
  2027. group->AddObject(this->CreateString("-Wno-four-char-constants"));
  2028. group->AddObject(this->CreateString("-Wno-unknown-pragmas"));
  2029. group->AddObject(this->CreateString("$(inherited)"));
  2030. buildSettings->AddAttribute("WARNING_CFLAGS", group);
  2031. // Runtime version information.
  2032. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  2033. int major;
  2034. int minor;
  2035. int patch;
  2036. // VERSION -> current_version
  2037. gtgt->GetTargetVersion(false, major, minor, patch);
  2038. std::ostringstream v;
  2039. // Xcode always wants at least 1.0.0 or nothing
  2040. if (!(major == 0 && minor == 0 && patch == 0)) {
  2041. v << major << "." << minor << "." << patch;
  2042. }
  2043. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  2044. this->CreateString(v.str()));
  2045. // SOVERSION -> compatibility_version
  2046. gtgt->GetTargetVersion(true, major, minor, patch);
  2047. std::ostringstream vso;
  2048. // Xcode always wants at least 1.0.0 or nothing
  2049. if (!(major == 0 && minor == 0 && patch == 0)) {
  2050. vso << major << "." << minor << "." << patch;
  2051. }
  2052. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  2053. this->CreateString(vso.str()));
  2054. }
  2055. // put this last so it can override existing settings
  2056. // Convert "XCODE_ATTRIBUTE_*" properties directly.
  2057. {
  2058. for (auto const& prop : gtgt->GetPropertyKeys()) {
  2059. if (prop.find("XCODE_ATTRIBUTE_") == 0) {
  2060. std::string attribute = prop.substr(16);
  2061. this->FilterConfigurationAttribute(configName, attribute);
  2062. if (!attribute.empty()) {
  2063. cmGeneratorExpression ge;
  2064. std::string processed =
  2065. ge.Parse(gtgt->GetProperty(prop))
  2066. ->Evaluate(this->CurrentLocalGenerator, configName);
  2067. buildSettings->AddAttribute(attribute,
  2068. this->CreateString(processed));
  2069. }
  2070. }
  2071. }
  2072. }
  2073. }
  2074. cmXCodeObject* cmGlobalXCodeGenerator::CreateUtilityTarget(
  2075. cmGeneratorTarget* gtgt)
  2076. {
  2077. cmXCodeObject* shellBuildPhase =
  2078. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  2079. shellBuildPhase->AddAttribute("buildActionMask",
  2080. this->CreateString("2147483647"));
  2081. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2082. shellBuildPhase->AddAttribute("files", buildFiles);
  2083. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2084. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  2085. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2086. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  2087. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  2088. this->CreateString("0"));
  2089. shellBuildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  2090. shellBuildPhase->AddAttribute(
  2091. "shellScript", this->CreateString("# shell script goes here\nexit 0"));
  2092. shellBuildPhase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  2093. cmXCodeObject* target =
  2094. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  2095. target->SetComment(gtgt->GetName());
  2096. cmXCodeObject* buildPhases = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2097. std::vector<cmXCodeObject*> emptyContentVector;
  2098. this->CreateCustomCommands(buildPhases, nullptr, nullptr, nullptr,
  2099. emptyContentVector, nullptr, gtgt);
  2100. target->AddAttribute("buildPhases", buildPhases);
  2101. this->AddConfigurations(target, gtgt);
  2102. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2103. target->AddAttribute("dependencies", dependencies);
  2104. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2105. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2106. target->SetTarget(gtgt);
  2107. this->XCodeObjectMap[gtgt] = target;
  2108. // Add source files without build rules for editing convenience.
  2109. if (gtgt->GetType() == cmStateEnums::UTILITY &&
  2110. gtgt->GetName() != CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  2111. std::vector<cmSourceFile*> sources;
  2112. if (!gtgt->GetConfigCommonSourceFiles(sources)) {
  2113. return nullptr;
  2114. }
  2115. // Add CMakeLists.txt file for user convenience.
  2116. std::string listfile =
  2117. gtgt->GetLocalGenerator()->GetCurrentSourceDirectory();
  2118. listfile += "/CMakeLists.txt";
  2119. sources.push_back(gtgt->Makefile->GetOrCreateSource(listfile));
  2120. for (auto sourceFile : sources) {
  2121. if (!sourceFile->GetPropertyAsBool("GENERATED")) {
  2122. this->CreateXCodeFileReference(sourceFile, gtgt);
  2123. }
  2124. }
  2125. }
  2126. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2127. return target;
  2128. }
  2129. std::string cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  2130. cmGeneratorTarget* gtgt)
  2131. {
  2132. std::string configTypes =
  2133. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES");
  2134. std::vector<std::string> configVectorIn;
  2135. std::vector<std::string> configVector;
  2136. configVectorIn.push_back(configTypes);
  2137. cmSystemTools::ExpandList(configVectorIn, configVector);
  2138. cmXCodeObject* configlist =
  2139. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2140. cmXCodeObject* buildConfigurations =
  2141. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2142. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2143. std::string comment = "Build configuration list for ";
  2144. comment += cmXCodeObject::PBXTypeNames[target->GetIsA()];
  2145. comment += " \"";
  2146. comment += gtgt->GetName();
  2147. comment += "\"";
  2148. configlist->SetComment(comment);
  2149. target->AddAttribute("buildConfigurationList",
  2150. this->CreateObjectReference(configlist));
  2151. for (auto const& i : configVector) {
  2152. cmXCodeObject* config =
  2153. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2154. buildConfigurations->AddObject(config);
  2155. cmXCodeObject* buildSettings =
  2156. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2157. this->CreateBuildSettings(gtgt, buildSettings, i);
  2158. config->AddAttribute("name", this->CreateString(i));
  2159. config->SetComment(i);
  2160. config->AddAttribute("buildSettings", buildSettings);
  2161. }
  2162. if (!configVector.empty()) {
  2163. configlist->AddAttribute("defaultConfigurationName",
  2164. this->CreateString(configVector[0]));
  2165. configlist->AddAttribute("defaultConfigurationIsVisible",
  2166. this->CreateString("0"));
  2167. return configVector[0];
  2168. }
  2169. return "";
  2170. }
  2171. const char* cmGlobalXCodeGenerator::GetTargetLinkFlagsVar(
  2172. cmGeneratorTarget const* target) const
  2173. {
  2174. if (this->XcodeVersion >= 60 &&
  2175. (target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2176. target->GetType() == cmStateEnums::OBJECT_LIBRARY)) {
  2177. return "OTHER_LIBTOOLFLAGS";
  2178. }
  2179. return "OTHER_LDFLAGS";
  2180. }
  2181. const char* cmGlobalXCodeGenerator::GetTargetFileType(
  2182. cmGeneratorTarget* target)
  2183. {
  2184. if (const char* e = target->GetProperty("XCODE_EXPLICIT_FILE_TYPE")) {
  2185. return e;
  2186. }
  2187. switch (target->GetType()) {
  2188. case cmStateEnums::OBJECT_LIBRARY:
  2189. return "archive.ar";
  2190. case cmStateEnums::STATIC_LIBRARY:
  2191. return (target->GetPropertyAsBool("FRAMEWORK") ? "wrapper.framework"
  2192. : "archive.ar");
  2193. case cmStateEnums::MODULE_LIBRARY:
  2194. if (target->IsXCTestOnApple()) {
  2195. return "wrapper.cfbundle";
  2196. }
  2197. if (target->IsCFBundleOnApple()) {
  2198. return "wrapper.plug-in";
  2199. }
  2200. return "compiled.mach-o.executable";
  2201. case cmStateEnums::SHARED_LIBRARY:
  2202. return (target->GetPropertyAsBool("FRAMEWORK")
  2203. ? "wrapper.framework"
  2204. : "compiled.mach-o.dylib");
  2205. case cmStateEnums::EXECUTABLE:
  2206. return "compiled.mach-o.executable";
  2207. default:
  2208. break;
  2209. }
  2210. return nullptr;
  2211. }
  2212. const char* cmGlobalXCodeGenerator::GetTargetProductType(
  2213. cmGeneratorTarget* target)
  2214. {
  2215. if (const char* e = target->GetProperty("XCODE_PRODUCT_TYPE")) {
  2216. return e;
  2217. }
  2218. switch (target->GetType()) {
  2219. case cmStateEnums::OBJECT_LIBRARY:
  2220. return "com.apple.product-type.library.static";
  2221. case cmStateEnums::STATIC_LIBRARY:
  2222. return (target->GetPropertyAsBool("FRAMEWORK")
  2223. ? "com.apple.product-type.framework"
  2224. : "com.apple.product-type.library.static");
  2225. case cmStateEnums::MODULE_LIBRARY:
  2226. if (target->IsXCTestOnApple()) {
  2227. return "com.apple.product-type.bundle.unit-test";
  2228. } else if (target->IsCFBundleOnApple()) {
  2229. return "com.apple.product-type.bundle";
  2230. } else {
  2231. return "com.apple.product-type.tool";
  2232. }
  2233. case cmStateEnums::SHARED_LIBRARY:
  2234. return (target->GetPropertyAsBool("FRAMEWORK")
  2235. ? "com.apple.product-type.framework"
  2236. : "com.apple.product-type.library.dynamic");
  2237. case cmStateEnums::EXECUTABLE:
  2238. return (target->GetPropertyAsBool("MACOSX_BUNDLE")
  2239. ? "com.apple.product-type.application"
  2240. : "com.apple.product-type.tool");
  2241. default:
  2242. break;
  2243. }
  2244. return nullptr;
  2245. }
  2246. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeTarget(
  2247. cmGeneratorTarget* gtgt, cmXCodeObject* buildPhases)
  2248. {
  2249. if (gtgt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2250. return nullptr;
  2251. }
  2252. cmXCodeObject* target = this->CreateObject(cmXCodeObject::PBXNativeTarget);
  2253. target->AddAttribute("buildPhases", buildPhases);
  2254. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2255. target->AddAttribute("buildRules", buildRules);
  2256. std::string defConfig;
  2257. defConfig = this->AddConfigurations(target, gtgt);
  2258. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2259. target->AddAttribute("dependencies", dependencies);
  2260. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2261. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2262. cmXCodeObject* fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  2263. if (const char* fileType = this->GetTargetFileType(gtgt)) {
  2264. fileRef->AddAttribute("explicitFileType", this->CreateString(fileType));
  2265. }
  2266. std::string fullName;
  2267. if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  2268. fullName = "lib";
  2269. fullName += gtgt->GetName();
  2270. fullName += ".a";
  2271. } else {
  2272. fullName = gtgt->GetFullName(defConfig);
  2273. }
  2274. fileRef->AddAttribute("path", this->CreateString(fullName));
  2275. fileRef->AddAttribute("sourceTree",
  2276. this->CreateString("BUILT_PRODUCTS_DIR"));
  2277. fileRef->SetComment(gtgt->GetName());
  2278. target->AddAttribute("productReference",
  2279. this->CreateObjectReference(fileRef));
  2280. if (const char* productType = this->GetTargetProductType(gtgt)) {
  2281. target->AddAttribute("productType", this->CreateString(productType));
  2282. }
  2283. target->SetTarget(gtgt);
  2284. this->XCodeObjectMap[gtgt] = target;
  2285. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2286. return target;
  2287. }
  2288. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(
  2289. cmGeneratorTarget const* t)
  2290. {
  2291. if (!t) {
  2292. return nullptr;
  2293. }
  2294. std::map<cmGeneratorTarget const*, cmXCodeObject*>::const_iterator const i =
  2295. this->XCodeObjectMap.find(t);
  2296. if (i == this->XCodeObjectMap.end()) {
  2297. return nullptr;
  2298. }
  2299. return i->second;
  2300. }
  2301. std::string cmGlobalXCodeGenerator::GetOrCreateId(const std::string& name,
  2302. const std::string& id)
  2303. {
  2304. std::string guidStoreName = name;
  2305. guidStoreName += "_GUID_CMAKE";
  2306. const char* storedGUID =
  2307. this->CMakeInstance->GetCacheDefinition(guidStoreName);
  2308. if (storedGUID) {
  2309. return storedGUID;
  2310. }
  2311. this->CMakeInstance->AddCacheEntry(guidStoreName, id.c_str(),
  2312. "Stored Xcode object GUID",
  2313. cmStateEnums::INTERNAL);
  2314. return id;
  2315. }
  2316. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  2317. cmXCodeObject* dependTarget)
  2318. {
  2319. // This is called once for every edge in the target dependency graph.
  2320. cmXCodeObject* container =
  2321. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  2322. container->SetComment("PBXContainerItemProxy");
  2323. container->AddAttribute("containerPortal",
  2324. this->CreateObjectReference(this->RootObject));
  2325. container->AddAttribute("proxyType", this->CreateString("1"));
  2326. container->AddAttribute("remoteGlobalIDString",
  2327. this->CreateObjectReference(dependTarget));
  2328. container->AddAttribute(
  2329. "remoteInfo", this->CreateString(dependTarget->GetTarget()->GetName()));
  2330. cmXCodeObject* targetdep =
  2331. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  2332. targetdep->SetComment("PBXTargetDependency");
  2333. targetdep->AddAttribute("target", this->CreateObjectReference(dependTarget));
  2334. targetdep->AddAttribute("targetProxy",
  2335. this->CreateObjectReference(container));
  2336. cmXCodeObject* depends = target->GetObject("dependencies");
  2337. if (!depends) {
  2338. cmSystemTools::Error(
  2339. "target does not have dependencies attribute error..");
  2340. } else {
  2341. depends->AddUniqueObject(targetdep);
  2342. }
  2343. }
  2344. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  2345. const char* attribute,
  2346. const char* value)
  2347. {
  2348. if (settings) {
  2349. cmXCodeObject* attr = settings->GetObject(attribute);
  2350. if (!attr) {
  2351. settings->AddAttribute(attribute, this->CreateString(value));
  2352. } else {
  2353. std::string oldValue = attr->GetString();
  2354. oldValue += " ";
  2355. oldValue += value;
  2356. attr->SetString(oldValue);
  2357. }
  2358. }
  2359. }
  2360. void cmGlobalXCodeGenerator::AppendBuildSettingAttribute(
  2361. cmXCodeObject* target, const char* attribute, const char* value,
  2362. const std::string& configName)
  2363. {
  2364. // There are multiple configurations. Add the setting to the
  2365. // buildSettings of the configuration name given.
  2366. cmXCodeObject* configurationList =
  2367. target->GetObject("buildConfigurationList")->GetObject();
  2368. cmXCodeObject* buildConfigs =
  2369. configurationList->GetObject("buildConfigurations");
  2370. for (auto obj : buildConfigs->GetObjectList()) {
  2371. if (configName.empty() ||
  2372. obj->GetObject("name")->GetString() == configName) {
  2373. cmXCodeObject* settings = obj->GetObject("buildSettings");
  2374. this->AppendOrAddBuildSetting(settings, attribute, value);
  2375. }
  2376. }
  2377. }
  2378. void cmGlobalXCodeGenerator::AddDependAndLinkInformation(cmXCodeObject* target)
  2379. {
  2380. cmGeneratorTarget* gt = target->GetTarget();
  2381. if (!gt) {
  2382. cmSystemTools::Error("Error no target on xobject\n");
  2383. return;
  2384. }
  2385. if (gt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2386. return;
  2387. }
  2388. // Add dependencies on other CMake targets.
  2389. for (const auto& dep : this->GetTargetDirectDepends(gt)) {
  2390. if (cmXCodeObject* dptarget = this->FindXCodeTarget(dep)) {
  2391. this->AddDependTarget(target, dptarget);
  2392. }
  2393. }
  2394. // Loop over configuration types and set per-configuration info.
  2395. for (auto const& configName : this->CurrentConfigurationTypes) {
  2396. // Get the current configuration name.
  2397. if (this->XcodeVersion >= 50) {
  2398. // Add object library contents as link flags.
  2399. std::string linkObjs;
  2400. const char* sep = "";
  2401. std::vector<cmSourceFile const*> objs;
  2402. gt->GetExternalObjects(objs, configName);
  2403. for (auto sourceFile : objs) {
  2404. if (sourceFile->GetObjectLibrary().empty()) {
  2405. continue;
  2406. }
  2407. linkObjs += sep;
  2408. sep = " ";
  2409. linkObjs += this->XCodeEscapePath(sourceFile->GetFullPath());
  2410. }
  2411. this->AppendBuildSettingAttribute(
  2412. target, this->GetTargetLinkFlagsVar(gt), linkObjs.c_str(), configName);
  2413. }
  2414. // Skip link information for object libraries.
  2415. if (gt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  2416. gt->GetType() == cmStateEnums::STATIC_LIBRARY) {
  2417. continue;
  2418. }
  2419. // Compute the link library and directory information.
  2420. cmComputeLinkInformation* pcli = gt->GetLinkInformation(configName);
  2421. if (!pcli) {
  2422. continue;
  2423. }
  2424. cmComputeLinkInformation& cli = *pcli;
  2425. // Add dependencies directly on library files.
  2426. for (auto const& libDep : cli.GetDepends()) {
  2427. target->AddDependLibrary(configName, libDep);
  2428. }
  2429. // add the library search paths
  2430. {
  2431. std::string linkDirs;
  2432. for (auto const& libDir : cli.GetDirectories()) {
  2433. if (!libDir.empty() && libDir != "/usr/lib") {
  2434. // Now add the same one but append
  2435. // $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) to it:
  2436. linkDirs += " ";
  2437. linkDirs += this->XCodeEscapePath(
  2438. libDir + "/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)");
  2439. linkDirs += " ";
  2440. linkDirs += this->XCodeEscapePath(libDir);
  2441. }
  2442. }
  2443. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  2444. linkDirs.c_str(), configName);
  2445. }
  2446. // now add the link libraries
  2447. {
  2448. std::string linkLibs;
  2449. const char* sep = "";
  2450. for (auto const& libName : cli.GetItems()) {
  2451. linkLibs += sep;
  2452. sep = " ";
  2453. if (libName.IsPath) {
  2454. linkLibs += this->XCodeEscapePath(libName.Value);
  2455. } else if (!libName.Target ||
  2456. libName.Target->GetType() !=
  2457. cmStateEnums::INTERFACE_LIBRARY) {
  2458. linkLibs += libName.Value;
  2459. }
  2460. if (libName.Target && !libName.Target->IsImported()) {
  2461. target->AddDependTarget(configName, libName.Target->GetName());
  2462. }
  2463. }
  2464. this->AppendBuildSettingAttribute(
  2465. target, this->GetTargetLinkFlagsVar(gt), linkLibs.c_str(), configName);
  2466. }
  2467. }
  2468. }
  2469. bool cmGlobalXCodeGenerator::CreateGroups(
  2470. std::vector<cmLocalGenerator*>& generators)
  2471. {
  2472. for (auto& generator : generators) {
  2473. cmMakefile* mf = generator->GetMakefile();
  2474. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  2475. for (auto gtgt : generator->GetGeneratorTargets()) {
  2476. // Same skipping logic here as in CreateXCodeTargets so that we do not
  2477. // end up with (empty anyhow) ZERO_CHECK, install, or test source
  2478. // groups:
  2479. //
  2480. if (gtgt->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2481. continue;
  2482. }
  2483. if (gtgt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2484. continue;
  2485. }
  2486. if (gtgt->GetName() == CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  2487. continue;
  2488. }
  2489. // add the soon to be generated Info.plist file as a source for a
  2490. // MACOSX_BUNDLE file
  2491. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  2492. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2493. mf->GetOrCreateSource(plist, true);
  2494. gtgt->AddSource(plist);
  2495. }
  2496. // Put cmSourceFile instances in proper groups:
  2497. for (auto const& si : gtgt->GetAllConfigSources()) {
  2498. cmSourceFile const* sf = si.Source;
  2499. if (this->XcodeVersion >= 50 && !sf->GetObjectLibrary().empty()) {
  2500. // Object library files go on the link line instead.
  2501. continue;
  2502. }
  2503. // Add the file to the list of sources.
  2504. std::string const& source = sf->GetFullPath();
  2505. cmSourceGroup* sourceGroup =
  2506. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2507. cmXCodeObject* pbxgroup = this->CreateOrGetPBXGroup(gtgt, sourceGroup);
  2508. std::string key = GetGroupMapKeyFromPath(gtgt, source);
  2509. this->GroupMap[key] = pbxgroup;
  2510. }
  2511. // Add CMakeLists.txt file for user convenience.
  2512. {
  2513. std::string listfile =
  2514. gtgt->GetLocalGenerator()->GetCurrentSourceDirectory();
  2515. listfile += "/CMakeLists.txt";
  2516. cmSourceFile* sf = gtgt->Makefile->GetOrCreateSource(listfile);
  2517. std::string const& source = sf->GetFullPath();
  2518. cmSourceGroup* sourceGroup =
  2519. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2520. cmXCodeObject* pbxgroup = this->CreateOrGetPBXGroup(gtgt, sourceGroup);
  2521. std::string key = GetGroupMapKeyFromPath(gtgt, source);
  2522. this->GroupMap[key] = pbxgroup;
  2523. }
  2524. }
  2525. }
  2526. return true;
  2527. }
  2528. cmXCodeObject* cmGlobalXCodeGenerator::CreatePBXGroup(cmXCodeObject* parent,
  2529. const std::string& name)
  2530. {
  2531. cmXCodeObject* parentChildren = nullptr;
  2532. if (parent) {
  2533. parentChildren = parent->GetObject("children");
  2534. }
  2535. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  2536. cmXCodeObject* groupChildren =
  2537. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2538. group->AddAttribute("name", this->CreateString(name));
  2539. group->AddAttribute("children", groupChildren);
  2540. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  2541. if (parentChildren) {
  2542. parentChildren->AddObject(group);
  2543. }
  2544. return group;
  2545. }
  2546. cmXCodeObject* cmGlobalXCodeGenerator::CreateOrGetPBXGroup(
  2547. cmGeneratorTarget* gtgt, cmSourceGroup* sg)
  2548. {
  2549. std::string s;
  2550. std::string target;
  2551. const std::string targetFolder = gtgt->GetEffectiveFolderName();
  2552. if (!targetFolder.empty()) {
  2553. target = targetFolder;
  2554. target += "/";
  2555. }
  2556. target += gtgt->GetName();
  2557. s = target + "/";
  2558. s += sg->GetFullName();
  2559. std::map<std::string, cmXCodeObject*>::iterator it =
  2560. this->GroupNameMap.find(s);
  2561. if (it != this->GroupNameMap.end()) {
  2562. return it->second;
  2563. }
  2564. it = this->TargetGroup.find(target);
  2565. cmXCodeObject* tgroup = nullptr;
  2566. if (it != this->TargetGroup.end()) {
  2567. tgroup = it->second;
  2568. } else {
  2569. std::vector<std::string> tgt_folders =
  2570. cmSystemTools::tokenize(target, "/");
  2571. std::string curr_tgt_folder;
  2572. for (std::vector<std::string>::size_type i = 0; i < tgt_folders.size();
  2573. i++) {
  2574. if (i != 0) {
  2575. curr_tgt_folder += "/";
  2576. }
  2577. curr_tgt_folder += tgt_folders[i];
  2578. it = this->TargetGroup.find(curr_tgt_folder);
  2579. if (it != this->TargetGroup.end()) {
  2580. tgroup = it->second;
  2581. continue;
  2582. }
  2583. tgroup = this->CreatePBXGroup(tgroup, tgt_folders[i]);
  2584. this->TargetGroup[curr_tgt_folder] = tgroup;
  2585. if (i == 0) {
  2586. this->MainGroupChildren->AddObject(tgroup);
  2587. }
  2588. }
  2589. }
  2590. this->TargetGroup[target] = tgroup;
  2591. // If it's the default source group (empty name) then put the source file
  2592. // directly in the tgroup...
  2593. //
  2594. if (sg->GetFullName().empty()) {
  2595. this->GroupNameMap[s] = tgroup;
  2596. return tgroup;
  2597. }
  2598. // It's a recursive folder structure, let's find the real parent group
  2599. if (sg->GetFullName() != sg->GetName()) {
  2600. std::string curr_folder = target;
  2601. curr_folder += "/";
  2602. for (auto const& folder :
  2603. cmSystemTools::tokenize(sg->GetFullName(), "\\")) {
  2604. curr_folder += folder;
  2605. std::map<std::string, cmXCodeObject*>::iterator i_folder =
  2606. this->GroupNameMap.find(curr_folder);
  2607. // Create new folder
  2608. if (i_folder == this->GroupNameMap.end()) {
  2609. cmXCodeObject* group = this->CreatePBXGroup(tgroup, folder);
  2610. this->GroupNameMap[curr_folder] = group;
  2611. tgroup = group;
  2612. } else {
  2613. tgroup = i_folder->second;
  2614. }
  2615. curr_folder = curr_folder + "\\";
  2616. }
  2617. return tgroup;
  2618. }
  2619. cmXCodeObject* group = this->CreatePBXGroup(tgroup, sg->GetName());
  2620. this->GroupNameMap[s] = group;
  2621. return group;
  2622. }
  2623. bool cmGlobalXCodeGenerator::CreateXCodeObjects(
  2624. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  2625. {
  2626. this->ClearXCodeObjects();
  2627. this->RootObject = nullptr;
  2628. this->MainGroupChildren = nullptr;
  2629. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2630. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2631. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2632. for (auto& CurrentConfigurationType : this->CurrentConfigurationTypes) {
  2633. cmXCodeObject* buildStyle =
  2634. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2635. const char* name = CurrentConfigurationType.c_str();
  2636. buildStyle->AddAttribute("name", this->CreateString(name));
  2637. buildStyle->SetComment(name);
  2638. cmXCodeObject* sgroup = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2639. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2640. buildStyle->AddAttribute("buildSettings", sgroup);
  2641. listObjs->AddObject(buildStyle);
  2642. }
  2643. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2644. this->MainGroupChildren = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2645. mainGroup->AddAttribute("children", this->MainGroupChildren);
  2646. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2647. // now create the cmake groups
  2648. if (!this->CreateGroups(generators)) {
  2649. return false;
  2650. }
  2651. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2652. productGroup->AddAttribute("name", this->CreateString("Products"));
  2653. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2654. cmXCodeObject* productGroupChildren =
  2655. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2656. productGroup->AddAttribute("children", productGroupChildren);
  2657. this->MainGroupChildren->AddObject(productGroup);
  2658. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  2659. this->RootObject->SetComment("Project object");
  2660. std::string project_id = "PROJECT_";
  2661. project_id += root->GetProjectName();
  2662. this->RootObject->SetId(
  2663. this->GetOrCreateId(project_id, this->RootObject->GetId()));
  2664. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2665. this->RootObject->AddAttribute("mainGroup",
  2666. this->CreateObjectReference(mainGroup));
  2667. this->RootObject->AddAttribute("buildSettings", group);
  2668. this->RootObject->AddAttribute("buildStyles", listObjs);
  2669. this->RootObject->AddAttribute("hasScannedForEncodings",
  2670. this->CreateString("0"));
  2671. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2672. group->AddAttribute("BuildIndependentTargetsInParallel",
  2673. this->CreateString("YES"));
  2674. std::ostringstream v;
  2675. v << std::setfill('0') << std::setw(4) << XcodeVersion * 10;
  2676. group->AddAttribute("LastUpgradeCheck", this->CreateString(v.str()));
  2677. this->RootObject->AddAttribute("attributes", group);
  2678. if (this->XcodeVersion >= 32) {
  2679. this->RootObject->AddAttribute("compatibilityVersion",
  2680. this->CreateString("Xcode 3.2"));
  2681. } else if (this->XcodeVersion >= 31) {
  2682. this->RootObject->AddAttribute("compatibilityVersion",
  2683. this->CreateString("Xcode 3.1"));
  2684. } else {
  2685. this->RootObject->AddAttribute("compatibilityVersion",
  2686. this->CreateString("Xcode 3.0"));
  2687. }
  2688. // Point Xcode at the top of the source tree.
  2689. {
  2690. std::string pdir =
  2691. this->RelativeToBinary(root->GetCurrentSourceDirectory());
  2692. this->RootObject->AddAttribute("projectDirPath", this->CreateString(pdir));
  2693. this->RootObject->AddAttribute("projectRoot", this->CreateString(""));
  2694. }
  2695. cmXCodeObject* configlist =
  2696. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2697. cmXCodeObject* buildConfigurations =
  2698. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2699. typedef std::vector<std::pair<std::string, cmXCodeObject*>> Configs;
  2700. Configs configs;
  2701. std::string defaultConfigName;
  2702. for (const auto& name : this->CurrentConfigurationTypes) {
  2703. if (defaultConfigName.empty()) {
  2704. defaultConfigName = name;
  2705. }
  2706. cmXCodeObject* config =
  2707. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2708. config->AddAttribute("name", this->CreateString(name));
  2709. configs.push_back(std::make_pair(name, config));
  2710. }
  2711. if (defaultConfigName.empty()) {
  2712. defaultConfigName = "Debug";
  2713. }
  2714. for (auto& config : configs) {
  2715. buildConfigurations->AddObject(config.second);
  2716. }
  2717. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2718. std::string comment = "Build configuration list for PBXProject";
  2719. comment += " \"";
  2720. comment += this->CurrentProject;
  2721. comment += "\"";
  2722. configlist->SetComment(comment);
  2723. configlist->AddAttribute("defaultConfigurationIsVisible",
  2724. this->CreateString("0"));
  2725. configlist->AddAttribute("defaultConfigurationName",
  2726. this->CreateString(defaultConfigName));
  2727. cmXCodeObject* buildSettings =
  2728. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2729. const char* sysroot =
  2730. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  2731. const char* deploymentTarget =
  2732. this->CurrentMakefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  2733. if (sysroot) {
  2734. buildSettings->AddAttribute("SDKROOT", this->CreateString(sysroot));
  2735. }
  2736. // recompute this as it may have been changed since enable language
  2737. this->ComputeArchitectures(this->CurrentMakefile);
  2738. std::string const archs = cmJoin(this->Architectures, " ");
  2739. if (archs.empty()) {
  2740. // Tell Xcode to use NATIVE_ARCH instead of ARCHS.
  2741. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH", this->CreateString("YES"));
  2742. } else {
  2743. // Tell Xcode to use ARCHS (ONLY_ACTIVE_ARCH defaults to NO).
  2744. buildSettings->AddAttribute("ARCHS", this->CreateString(archs));
  2745. }
  2746. if (deploymentTarget && *deploymentTarget) {
  2747. buildSettings->AddAttribute(GetDeploymentPlatform(root->GetMakefile()),
  2748. this->CreateString(deploymentTarget));
  2749. }
  2750. if (!this->GeneratorToolset.empty()) {
  2751. buildSettings->AddAttribute("GCC_VERSION",
  2752. this->CreateString(this->GeneratorToolset));
  2753. }
  2754. if (this->GetLanguageEnabled("Swift")) {
  2755. std::string swiftVersion;
  2756. if (const char* vers = this->CurrentMakefile->GetDefinition(
  2757. "CMAKE_Swift_LANGUAGE_VERSION")) {
  2758. swiftVersion = vers;
  2759. } else if (this->XcodeVersion >= 83) {
  2760. swiftVersion = "3.0";
  2761. } else {
  2762. swiftVersion = "2.3";
  2763. }
  2764. buildSettings->AddAttribute("SWIFT_VERSION",
  2765. this->CreateString(swiftVersion));
  2766. }
  2767. std::string symroot = root->GetCurrentBinaryDirectory();
  2768. symroot += "/build";
  2769. buildSettings->AddAttribute("SYMROOT", this->CreateString(symroot));
  2770. for (auto& config : configs) {
  2771. cmXCodeObject* buildSettingsForCfg = this->CreateFlatClone(buildSettings);
  2772. // Put this last so it can override existing settings
  2773. // Convert "CMAKE_XCODE_ATTRIBUTE_*" variables directly.
  2774. for (const auto& var : this->CurrentMakefile->GetDefinitions()) {
  2775. if (var.find("CMAKE_XCODE_ATTRIBUTE_") == 0) {
  2776. std::string attribute = var.substr(22);
  2777. this->FilterConfigurationAttribute(config.first, attribute);
  2778. if (!attribute.empty()) {
  2779. cmGeneratorExpression ge;
  2780. std::string processed =
  2781. ge.Parse(this->CurrentMakefile->GetDefinition(var))
  2782. ->Evaluate(this->CurrentLocalGenerator, config.first);
  2783. buildSettingsForCfg->AddAttribute(attribute,
  2784. this->CreateString(processed));
  2785. }
  2786. }
  2787. }
  2788. // store per-config buildSettings into configuration object
  2789. config.second->AddAttribute("buildSettings", buildSettingsForCfg);
  2790. }
  2791. this->RootObject->AddAttribute("buildConfigurationList",
  2792. this->CreateObjectReference(configlist));
  2793. std::vector<cmXCodeObject*> targets;
  2794. for (auto& generator : generators) {
  2795. if (!this->CreateXCodeTargets(generator, targets)) {
  2796. return false;
  2797. }
  2798. }
  2799. // loop over all targets and add link and depend info
  2800. for (auto t : targets) {
  2801. this->AddDependAndLinkInformation(t);
  2802. }
  2803. this->CreateXCodeDependHackTarget(targets);
  2804. // now add all targets to the root object
  2805. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2806. for (auto t : targets) {
  2807. allTargets->AddObject(t);
  2808. cmXCodeObject* productRef = t->GetObject("productReference");
  2809. if (productRef) {
  2810. productGroupChildren->AddObject(productRef->GetObject());
  2811. }
  2812. }
  2813. this->RootObject->AddAttribute("targets", allTargets);
  2814. return true;
  2815. }
  2816. std::string cmGlobalXCodeGenerator::GetObjectsNormalDirectory(
  2817. const std::string& projName, const std::string& configName,
  2818. const cmGeneratorTarget* t) const
  2819. {
  2820. std::string dir = t->GetLocalGenerator()->GetCurrentBinaryDirectory();
  2821. dir += "/";
  2822. dir += projName;
  2823. dir += ".build/";
  2824. dir += configName;
  2825. dir += "/";
  2826. dir += t->GetName();
  2827. dir += ".build/Objects-normal/";
  2828. return dir;
  2829. }
  2830. void cmGlobalXCodeGenerator::ComputeArchitectures(cmMakefile* mf)
  2831. {
  2832. this->Architectures.clear();
  2833. const char* osxArch = mf->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  2834. const char* sysroot = mf->GetDefinition("CMAKE_OSX_SYSROOT");
  2835. if (osxArch && sysroot) {
  2836. cmSystemTools::ExpandListArgument(std::string(osxArch),
  2837. this->Architectures);
  2838. }
  2839. if (this->Architectures.empty()) {
  2840. // With no ARCHS we use ONLY_ACTIVE_ARCH.
  2841. // Look up the arch that Xcode chooses in this case.
  2842. if (const char* arch = mf->GetDefinition("CMAKE_XCODE_CURRENT_ARCH")) {
  2843. this->ObjectDirArchDefault = arch;
  2844. }
  2845. }
  2846. this->ComputeObjectDirArch(mf);
  2847. }
  2848. void cmGlobalXCodeGenerator::ComputeObjectDirArch(cmMakefile* mf)
  2849. {
  2850. if (this->Architectures.size() > 1 || this->UseEffectivePlatformName(mf)) {
  2851. this->ObjectDirArch = "$(CURRENT_ARCH)";
  2852. } else if (!this->Architectures.empty()) {
  2853. this->ObjectDirArch = this->Architectures[0];
  2854. } else {
  2855. this->ObjectDirArch = this->ObjectDirArchDefault;
  2856. }
  2857. }
  2858. void cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  2859. std::vector<cmXCodeObject*>& targets)
  2860. {
  2861. cmGeneratedFileStream makefileStream(this->CurrentXCodeHackMakefile.c_str());
  2862. if (!makefileStream) {
  2863. cmSystemTools::Error("Could not create",
  2864. this->CurrentXCodeHackMakefile.c_str());
  2865. return;
  2866. }
  2867. makefileStream.SetCopyIfDifferent(true);
  2868. // one more pass for external depend information not handled
  2869. // correctly by xcode
  2870. /* clang-format off */
  2871. makefileStream << "# DO NOT EDIT\n";
  2872. makefileStream << "# This makefile makes sure all linkable targets are\n";
  2873. makefileStream << "# up-to-date with anything they link to\n"
  2874. "default:\n"
  2875. "\techo \"Do not invoke directly\"\n"
  2876. "\n";
  2877. /* clang-format on */
  2878. std::set<std::string> dummyRules;
  2879. // Write rules to help Xcode relink things at the right time.
  2880. /* clang-format off */
  2881. makefileStream <<
  2882. "# Rules to remove targets that are older than anything to which they\n"
  2883. "# link. This forces Xcode to relink the targets from scratch. It\n"
  2884. "# does not seem to check these dependencies itself.\n";
  2885. /* clang-format on */
  2886. for (const auto& configName : this->CurrentConfigurationTypes) {
  2887. for (auto target : targets) {
  2888. cmGeneratorTarget* gt = target->GetTarget();
  2889. if (gt->GetType() == cmStateEnums::EXECUTABLE ||
  2890. gt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  2891. gt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2892. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2893. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2894. // Declare an entry point for the target post-build phase.
  2895. makefileStream << this->PostBuildMakeTarget(gt->GetName(), configName)
  2896. << ":\n";
  2897. }
  2898. if (gt->GetType() == cmStateEnums::EXECUTABLE ||
  2899. gt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2900. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2901. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2902. std::string tfull = gt->GetFullPath(configName);
  2903. std::string trel = this->ConvertToRelativeForMake(tfull.c_str());
  2904. // Add this target to the post-build phases of its dependencies.
  2905. std::map<std::string, cmXCodeObject::StringVec>::const_iterator y =
  2906. target->GetDependTargets().find(configName);
  2907. if (y != target->GetDependTargets().end()) {
  2908. for (auto const& deptgt : y->second) {
  2909. makefileStream << this->PostBuildMakeTarget(deptgt, configName)
  2910. << ": " << trel << "\n";
  2911. }
  2912. }
  2913. std::vector<cmGeneratorTarget*> objlibs;
  2914. gt->GetObjectLibrariesCMP0026(objlibs);
  2915. for (auto objLib : objlibs) {
  2916. makefileStream << this->PostBuildMakeTarget(objLib->GetName(),
  2917. configName)
  2918. << ": " << trel << "\n";
  2919. }
  2920. // Create a rule for this target.
  2921. makefileStream << trel << ":";
  2922. // List dependencies if any exist.
  2923. std::map<std::string, cmXCodeObject::StringVec>::const_iterator x =
  2924. target->GetDependLibraries().find(configName);
  2925. if (x != target->GetDependLibraries().end()) {
  2926. for (auto const& deplib : x->second) {
  2927. std::string file = this->ConvertToRelativeForMake(deplib.c_str());
  2928. makefileStream << "\\\n\t" << file;
  2929. dummyRules.insert(file);
  2930. }
  2931. }
  2932. for (auto objLib : objlibs) {
  2933. const std::string objLibName = objLib->GetName();
  2934. std::string d = this->GetObjectsNormalDirectory(this->CurrentProject,
  2935. configName, objLib);
  2936. d += "lib";
  2937. d += objLibName;
  2938. d += ".a";
  2939. std::string dependency = this->ConvertToRelativeForMake(d.c_str());
  2940. makefileStream << "\\\n\t" << dependency;
  2941. dummyRules.insert(dependency);
  2942. }
  2943. // Write the action to remove the target if it is out of date.
  2944. makefileStream << "\n";
  2945. makefileStream << "\t/bin/rm -f "
  2946. << this->ConvertToRelativeForMake(tfull.c_str())
  2947. << "\n";
  2948. // if building for more than one architecture
  2949. // then remove those executables as well
  2950. if (this->Architectures.size() > 1) {
  2951. std::string universal = this->GetObjectsNormalDirectory(
  2952. this->CurrentProject, configName, gt);
  2953. for (const auto& architecture : this->Architectures) {
  2954. std::string universalFile = universal;
  2955. universalFile += architecture;
  2956. universalFile += "/";
  2957. universalFile += gt->GetFullName(configName);
  2958. makefileStream << "\t/bin/rm -f "
  2959. << this->ConvertToRelativeForMake(
  2960. universalFile.c_str())
  2961. << "\n";
  2962. }
  2963. }
  2964. makefileStream << "\n\n";
  2965. }
  2966. }
  2967. }
  2968. makefileStream << "\n\n"
  2969. << "# For each target create a dummy rule"
  2970. << "so the target does not have to exist\n";
  2971. for (auto const& dummyRule : dummyRules) {
  2972. makefileStream << dummyRule << ":\n";
  2973. }
  2974. }
  2975. void cmGlobalXCodeGenerator::OutputXCodeProject(
  2976. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  2977. {
  2978. if (generators.empty()) {
  2979. return;
  2980. }
  2981. // Skip local generators that are excluded from this project.
  2982. for (auto generator : generators) {
  2983. if (this->IsExcluded(root, generator)) {
  2984. continue;
  2985. }
  2986. }
  2987. if (!this->CreateXCodeObjects(root, generators)) {
  2988. return;
  2989. }
  2990. std::string xcodeDir = root->GetCurrentBinaryDirectory();
  2991. xcodeDir += "/";
  2992. xcodeDir += root->GetProjectName();
  2993. xcodeDir += ".xcodeproj";
  2994. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  2995. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  2996. cmGeneratedFileStream fout(xcodeProjFile.c_str());
  2997. fout.SetCopyIfDifferent(true);
  2998. if (!fout) {
  2999. return;
  3000. }
  3001. this->WriteXCodePBXProj(fout, root, generators);
  3002. // Since the lowest available Xcode version for testing was 6.4,
  3003. // I'm setting this as a limit then
  3004. if (this->XcodeVersion >= 64) {
  3005. if (root->GetMakefile()->GetCMakeInstance()->GetIsInTryCompile() ||
  3006. root->GetMakefile()->IsOn("CMAKE_XCODE_GENERATE_SCHEME")) {
  3007. this->OutputXCodeSharedSchemes(xcodeDir);
  3008. this->OutputXCodeWorkspaceSettings(xcodeDir);
  3009. }
  3010. }
  3011. this->ClearXCodeObjects();
  3012. // Since this call may have created new cache entries, save the cache:
  3013. //
  3014. root->GetMakefile()->GetCMakeInstance()->SaveCache(
  3015. root->GetBinaryDirectory());
  3016. }
  3017. void cmGlobalXCodeGenerator::OutputXCodeSharedSchemes(
  3018. const std::string& xcProjDir)
  3019. {
  3020. // collect all tests for the targets
  3021. std::map<std::string, cmXCodeScheme::TestObjects> testables;
  3022. for (auto obj : this->XCodeObjects) {
  3023. if (obj->GetType() != cmXCodeObject::OBJECT ||
  3024. obj->GetIsA() != cmXCodeObject::PBXNativeTarget) {
  3025. continue;
  3026. }
  3027. if (!obj->GetTarget()->IsXCTestOnApple()) {
  3028. continue;
  3029. }
  3030. const char* testee = obj->GetTarget()->GetProperty("XCTEST_TESTEE");
  3031. if (!testee) {
  3032. continue;
  3033. }
  3034. testables[testee].push_back(obj);
  3035. }
  3036. // generate scheme
  3037. for (auto obj : this->XCodeObjects) {
  3038. if (obj->GetType() == cmXCodeObject::OBJECT &&
  3039. (obj->GetIsA() == cmXCodeObject::PBXNativeTarget ||
  3040. obj->GetIsA() == cmXCodeObject::PBXAggregateTarget)) {
  3041. const std::string& targetName = obj->GetTarget()->GetName();
  3042. cmXCodeScheme schm(obj, testables[targetName],
  3043. this->CurrentConfigurationTypes, this->XcodeVersion);
  3044. schm.WriteXCodeSharedScheme(xcProjDir,
  3045. this->RelativeToSource(xcProjDir.c_str()));
  3046. }
  3047. }
  3048. }
  3049. void cmGlobalXCodeGenerator::OutputXCodeWorkspaceSettings(
  3050. const std::string& xcProjDir)
  3051. {
  3052. std::string xcodeSharedDataDir = xcProjDir;
  3053. xcodeSharedDataDir += "/project.xcworkspace/xcshareddata";
  3054. cmSystemTools::MakeDirectory(xcodeSharedDataDir);
  3055. std::string workspaceSettingsFile = xcodeSharedDataDir;
  3056. workspaceSettingsFile += "/WorkspaceSettings.xcsettings";
  3057. cmGeneratedFileStream fout(workspaceSettingsFile.c_str());
  3058. fout.SetCopyIfDifferent(true);
  3059. if (!fout) {
  3060. return;
  3061. }
  3062. cmXMLWriter xout(fout);
  3063. xout.StartDocument();
  3064. xout.Doctype("plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\""
  3065. "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
  3066. xout.StartElement("plist");
  3067. xout.Attribute("version", "1.0");
  3068. xout.StartElement("dict");
  3069. xout.Element("key", "IDEWorkspaceSharedSettings_AutocreateContextsIfNeeded");
  3070. xout.Element("false");
  3071. xout.EndElement(); // dict
  3072. xout.EndElement(); // plist
  3073. xout.EndDocument();
  3074. }
  3075. void cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  3076. cmLocalGenerator*,
  3077. std::vector<cmLocalGenerator*>&)
  3078. {
  3079. SortXCodeObjects();
  3080. fout << "// !$*UTF8*$!\n";
  3081. fout << "{\n";
  3082. cmXCodeObject::Indent(1, fout);
  3083. fout << "archiveVersion = 1;\n";
  3084. cmXCodeObject::Indent(1, fout);
  3085. fout << "classes = {\n";
  3086. cmXCodeObject::Indent(1, fout);
  3087. fout << "};\n";
  3088. cmXCodeObject::Indent(1, fout);
  3089. if (this->XcodeVersion >= 32) {
  3090. fout << "objectVersion = 46;\n";
  3091. } else if (this->XcodeVersion >= 31) {
  3092. fout << "objectVersion = 45;\n";
  3093. } else {
  3094. fout << "objectVersion = 44;\n";
  3095. }
  3096. cmXCode21Object::PrintList(this->XCodeObjects, fout);
  3097. cmXCodeObject::Indent(1, fout);
  3098. fout << "rootObject = " << this->RootObject->GetId()
  3099. << " /* Project object */;\n";
  3100. fout << "}\n";
  3101. }
  3102. const char* cmGlobalXCodeGenerator::GetCMakeCFGIntDir() const
  3103. {
  3104. return "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)";
  3105. }
  3106. std::string cmGlobalXCodeGenerator::ExpandCFGIntDir(
  3107. const std::string& str, const std::string& config) const
  3108. {
  3109. std::string replace1 = "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)";
  3110. std::string replace2 = "$(CONFIGURATION)";
  3111. std::string tmp = str;
  3112. for (std::string::size_type i = tmp.find(replace1); i != std::string::npos;
  3113. i = tmp.find(replace1, i)) {
  3114. tmp.replace(i, replace1.size(), config);
  3115. i += config.size();
  3116. }
  3117. for (std::string::size_type i = tmp.find(replace2); i != std::string::npos;
  3118. i = tmp.find(replace2, i)) {
  3119. tmp.replace(i, replace2.size(), config);
  3120. i += config.size();
  3121. }
  3122. return tmp;
  3123. }
  3124. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  3125. {
  3126. entry.Name = cmGlobalXCodeGenerator::GetActualName();
  3127. entry.Brief = "Generate Xcode project files.";
  3128. }
  3129. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(
  3130. std::string const& p)
  3131. {
  3132. return cmSystemTools::ConvertToOutputPath(p);
  3133. }
  3134. std::string cmGlobalXCodeGenerator::RelativeToSource(const char* p)
  3135. {
  3136. // We force conversion because Xcode breakpoints do not work unless
  3137. // they are in a file named relative to the source tree.
  3138. return cmOutputConverter::ForceToRelativePath(
  3139. cmSystemTools::JoinPath(this->ProjectSourceDirectoryComponents), p);
  3140. }
  3141. std::string cmGlobalXCodeGenerator::RelativeToBinary(const char* p)
  3142. {
  3143. return this->CurrentLocalGenerator->ConvertToRelativePath(
  3144. cmSystemTools::JoinPath(this->ProjectOutputDirectoryComponents), p);
  3145. }
  3146. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const std::string& p)
  3147. {
  3148. if (p.find(' ') != std::string::npos) {
  3149. std::string t = "\"";
  3150. t += p;
  3151. t += "\"";
  3152. return t;
  3153. }
  3154. return p;
  3155. }
  3156. void cmGlobalXCodeGenerator::AppendDirectoryForConfig(
  3157. const std::string& prefix, const std::string& config,
  3158. const std::string& suffix, std::string& dir)
  3159. {
  3160. if (!config.empty()) {
  3161. dir += prefix;
  3162. dir += config;
  3163. dir += suffix;
  3164. }
  3165. }
  3166. std::string cmGlobalXCodeGenerator::LookupFlags(
  3167. const std::string& varNamePrefix, const std::string& varNameLang,
  3168. const std::string& varNameSuffix, const std::string& default_flags)
  3169. {
  3170. if (!varNameLang.empty()) {
  3171. std::string varName = varNamePrefix;
  3172. varName += varNameLang;
  3173. varName += varNameSuffix;
  3174. if (const char* varValue = this->CurrentMakefile->GetDefinition(varName)) {
  3175. if (*varValue) {
  3176. return varValue;
  3177. }
  3178. }
  3179. }
  3180. return default_flags;
  3181. }
  3182. void cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  3183. const char* defines_list,
  3184. bool dflag)
  3185. {
  3186. // Skip this if there are no definitions.
  3187. if (!defines_list) {
  3188. return;
  3189. }
  3190. // Expand the list of definitions.
  3191. std::vector<std::string> defines;
  3192. cmSystemTools::ExpandListArgument(defines_list, defines);
  3193. // Store the definitions in the string.
  3194. this->AppendDefines(defs, defines, dflag);
  3195. }
  3196. void cmGlobalXCodeGenerator::AppendDefines(
  3197. BuildObjectListOrString& defs, std::vector<std::string> const& defines,
  3198. bool dflag)
  3199. {
  3200. // GCC_PREPROCESSOR_DEFINITIONS is a space-separated list of definitions.
  3201. std::string def;
  3202. for (auto const& define : defines) {
  3203. // Start with -D if requested.
  3204. def = dflag ? "-D" : "";
  3205. def += define;
  3206. // Append the flag with needed escapes.
  3207. std::string tmp;
  3208. this->AppendFlag(tmp, def);
  3209. defs.Add(tmp);
  3210. }
  3211. }
  3212. void cmGlobalXCodeGenerator::AppendFlag(std::string& flags,
  3213. std::string const& flag) const
  3214. {
  3215. // Short-circuit for an empty flag.
  3216. if (flag.empty()) {
  3217. return;
  3218. }
  3219. // Separate from previous flags.
  3220. if (!flags.empty()) {
  3221. flags += " ";
  3222. }
  3223. // Check if the flag needs quoting.
  3224. bool quoteFlag =
  3225. flag.find_first_of("`~!@#$%^&*()+={}[]|:;\"'<>,.? ") != std::string::npos;
  3226. // We escape a flag as follows:
  3227. // - Place each flag in single quotes ''
  3228. // - Escape a single quote as \'
  3229. // - Escape a backslash as \\ since it itself is an escape
  3230. // Note that in the code below we need one more level of escapes for
  3231. // C string syntax in this source file.
  3232. //
  3233. // The final level of escaping is done when the string is stored
  3234. // into the project file by cmXCodeObject::PrintString.
  3235. if (quoteFlag) {
  3236. // Open single quote.
  3237. flags += "'";
  3238. }
  3239. // Flag value with escaped quotes and backslashes.
  3240. for (auto c : flag) {
  3241. if (c == '\'') {
  3242. if (this->XcodeVersion >= 40) {
  3243. flags += "'\\''";
  3244. } else {
  3245. flags += "\\'";
  3246. }
  3247. } else if (c == '\\') {
  3248. flags += "\\\\";
  3249. } else {
  3250. flags += c;
  3251. }
  3252. }
  3253. if (quoteFlag) {
  3254. // Close single quote.
  3255. flags += "'";
  3256. }
  3257. }
  3258. std::string cmGlobalXCodeGenerator::ComputeInfoPListLocation(
  3259. cmGeneratorTarget* target)
  3260. {
  3261. std::string plist = target->GetLocalGenerator()->GetCurrentBinaryDirectory();
  3262. plist += cmake::GetCMakeFilesDirectory();
  3263. plist += "/";
  3264. plist += target->GetName();
  3265. plist += ".dir/Info.plist";
  3266. return plist;
  3267. }
  3268. // Return true if the generated build tree may contain multiple builds.
  3269. // i.e. "Can I build Debug and Release in the same tree?"
  3270. bool cmGlobalXCodeGenerator::IsMultiConfig() const
  3271. {
  3272. // Newer Xcode versions are multi config:
  3273. return true;
  3274. }
  3275. bool cmGlobalXCodeGenerator::HasKnownObjectFileLocation(
  3276. std::string* reason) const
  3277. {
  3278. if (this->ObjectDirArch.find('$') != std::string::npos) {
  3279. if (reason != nullptr) {
  3280. *reason = " under Xcode with multiple architectures";
  3281. }
  3282. return false;
  3283. }
  3284. return true;
  3285. }
  3286. bool cmGlobalXCodeGenerator::UseEffectivePlatformName(cmMakefile* mf) const
  3287. {
  3288. const char* epnValue =
  3289. this->GetCMakeInstance()->GetState()->GetGlobalProperty(
  3290. "XCODE_EMIT_EFFECTIVE_PLATFORM_NAME");
  3291. if (!epnValue) {
  3292. return mf->PlatformIsAppleEmbedded();
  3293. }
  3294. return cmSystemTools::IsOn(epnValue);
  3295. }
  3296. bool cmGlobalXCodeGenerator::ShouldStripResourcePath(cmMakefile*) const
  3297. {
  3298. // Xcode determines Resource location itself
  3299. return true;
  3300. }
  3301. void cmGlobalXCodeGenerator::ComputeTargetObjectDirectory(
  3302. cmGeneratorTarget* gt) const
  3303. {
  3304. std::string configName = this->GetCMakeCFGIntDir();
  3305. std::string dir =
  3306. this->GetObjectsNormalDirectory("$(PROJECT_NAME)", configName, gt);
  3307. dir += this->ObjectDirArch;
  3308. dir += "/";
  3309. gt->ObjectDirectory = dir;
  3310. }
  3311. std::string cmGlobalXCodeGenerator::GetDeploymentPlatform(const cmMakefile* mf)
  3312. {
  3313. switch (mf->GetAppleSDKType()) {
  3314. case cmMakefile::AppleSDK::AppleTVOS:
  3315. case cmMakefile::AppleSDK::AppleTVSimulator:
  3316. return "TVOS_DEPLOYMENT_TARGET";
  3317. case cmMakefile::AppleSDK::IPhoneOS:
  3318. case cmMakefile::AppleSDK::IPhoneSimulator:
  3319. return "IPHONEOS_DEPLOYMENT_TARGET";
  3320. case cmMakefile::AppleSDK::WatchOS:
  3321. case cmMakefile::AppleSDK::WatchSimulator:
  3322. return "WATCHOS_DEPLOYMENT_TARGET";
  3323. case cmMakefile::AppleSDK::MacOS:
  3324. default:
  3325. return "MACOSX_DEPLOYMENT_TARGET";
  3326. }
  3327. }