cmGlobalXCodeGenerator.cxx 164 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619
  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 <algorithm>
  5. #include <cassert>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <iomanip>
  9. #include <sstream>
  10. #include <unordered_set>
  11. #include <utility>
  12. #include <cm/memory>
  13. #include <cmext/algorithm>
  14. #include <cmext/string_view>
  15. #include "cmsys/RegularExpression.hxx"
  16. #include "cmComputeLinkInformation.h"
  17. #include "cmCustomCommand.h"
  18. #include "cmCustomCommandGenerator.h"
  19. #include "cmCustomCommandLines.h"
  20. #include "cmDocumentationEntry.h"
  21. #include "cmGeneratedFileStream.h"
  22. #include "cmGeneratorExpression.h"
  23. #include "cmGeneratorTarget.h"
  24. #include "cmGlobalGeneratorFactory.h"
  25. #include "cmLocalGenerator.h"
  26. #include "cmLocalXCodeGenerator.h"
  27. #include "cmMakefile.h"
  28. #include "cmMessageType.h"
  29. #include "cmOutputConverter.h"
  30. #include "cmSourceFile.h"
  31. #include "cmSourceGroup.h"
  32. #include "cmState.h"
  33. #include "cmStateTypes.h"
  34. #include "cmStringAlgorithms.h"
  35. #include "cmSystemTools.h"
  36. #include "cmTarget.h"
  37. #include "cmXCode21Object.h"
  38. #include "cmXCodeObject.h"
  39. #include "cmXCodeScheme.h"
  40. #include "cmake.h"
  41. struct cmLinkImplementation;
  42. #if !defined(CMAKE_BOOTSTRAP) && defined(__APPLE__)
  43. # define HAVE_APPLICATION_SERVICES
  44. # include <ApplicationServices/ApplicationServices.h>
  45. #endif
  46. #if !defined(CMAKE_BOOTSTRAP)
  47. # include "cmXMLParser.h"
  48. // parse the xml file storing the installed version of Xcode on
  49. // the machine
  50. class cmXcodeVersionParser : public cmXMLParser
  51. {
  52. public:
  53. cmXcodeVersionParser()
  54. : Version("1.5")
  55. {
  56. }
  57. void StartElement(const std::string&, const char**) override
  58. {
  59. this->Data = "";
  60. }
  61. void EndElement(const std::string& name) override
  62. {
  63. if (name == "key") {
  64. this->Key = this->Data;
  65. } else if (name == "string") {
  66. if (this->Key == "CFBundleShortVersionString") {
  67. this->Version = this->Data;
  68. }
  69. }
  70. }
  71. void CharacterDataHandler(const char* data, int length) override
  72. {
  73. this->Data.append(data, length);
  74. }
  75. std::string Version;
  76. std::string Key;
  77. std::string Data;
  78. };
  79. #endif
  80. // Builds either an object list or a space-separated string from the
  81. // given inputs.
  82. class cmGlobalXCodeGenerator::BuildObjectListOrString
  83. {
  84. cmGlobalXCodeGenerator* Generator;
  85. cmXCodeObject* Group;
  86. bool Empty;
  87. std::string String;
  88. public:
  89. BuildObjectListOrString(cmGlobalXCodeGenerator* gen, bool buildObjectList)
  90. : Generator(gen)
  91. , Group(nullptr)
  92. , Empty(true)
  93. {
  94. if (buildObjectList) {
  95. this->Group = this->Generator->CreateObject(cmXCodeObject::OBJECT_LIST);
  96. }
  97. }
  98. bool IsEmpty() const { return this->Empty; }
  99. void Add(const std::string& newString)
  100. {
  101. this->Empty = false;
  102. if (this->Group) {
  103. this->Group->AddObject(this->Generator->CreateString(newString));
  104. } else {
  105. this->String += newString;
  106. this->String += ' ';
  107. }
  108. }
  109. const std::string& GetString() const { return this->String; }
  110. cmXCodeObject* CreateList()
  111. {
  112. if (this->Group) {
  113. return this->Group;
  114. }
  115. return this->Generator->CreateString(this->String);
  116. }
  117. };
  118. class cmGlobalXCodeGenerator::Factory : public cmGlobalGeneratorFactory
  119. {
  120. public:
  121. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  122. const std::string& name, bool allowArch, cmake* cm) const override;
  123. void GetDocumentation(cmDocumentationEntry& entry) const override
  124. {
  125. cmGlobalXCodeGenerator::GetDocumentation(entry);
  126. }
  127. std::vector<std::string> GetGeneratorNames() const override
  128. {
  129. std::vector<std::string> names;
  130. names.push_back(cmGlobalXCodeGenerator::GetActualName());
  131. return names;
  132. }
  133. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  134. {
  135. return std::vector<std::string>();
  136. }
  137. bool SupportsToolset() const override { return true; }
  138. bool SupportsPlatform() const override { return false; }
  139. std::vector<std::string> GetKnownPlatforms() const override
  140. {
  141. return std::vector<std::string>();
  142. }
  143. std::string GetDefaultPlatformName() const override { return std::string(); }
  144. };
  145. cmGlobalXCodeGenerator::cmGlobalXCodeGenerator(
  146. cmake* cm, std::string const& version_string, unsigned int version_number)
  147. : cmGlobalGenerator(cm)
  148. {
  149. this->VersionString = version_string;
  150. this->XcodeVersion = version_number;
  151. if (this->XcodeVersion >= 120) {
  152. this->XcodeBuildSystem = BuildSystem::Twelve;
  153. } else {
  154. this->XcodeBuildSystem = BuildSystem::One;
  155. }
  156. this->RootObject = nullptr;
  157. this->MainGroupChildren = nullptr;
  158. this->FrameworkGroup = nullptr;
  159. this->CurrentMakefile = nullptr;
  160. this->CurrentLocalGenerator = nullptr;
  161. this->XcodeBuildCommandInitialized = false;
  162. this->ObjectDirArchDefault = "$(CURRENT_ARCH)";
  163. this->ObjectDirArch = this->ObjectDirArchDefault;
  164. cm->GetState()->SetIsGeneratorMultiConfig(true);
  165. }
  166. std::unique_ptr<cmGlobalGeneratorFactory> cmGlobalXCodeGenerator::NewFactory()
  167. {
  168. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
  169. }
  170. std::unique_ptr<cmGlobalGenerator>
  171. cmGlobalXCodeGenerator::Factory::CreateGlobalGenerator(const std::string& name,
  172. bool /*allowArch*/,
  173. cmake* cm) const
  174. {
  175. if (name != GetActualName()) {
  176. return std::unique_ptr<cmGlobalGenerator>();
  177. }
  178. #if !defined(CMAKE_BOOTSTRAP)
  179. cmXcodeVersionParser parser;
  180. std::string versionFile;
  181. {
  182. std::string out;
  183. bool commandResult = cmSystemTools::RunSingleCommand(
  184. "xcode-select --print-path", &out, nullptr, nullptr, nullptr,
  185. cmSystemTools::OUTPUT_NONE);
  186. if (commandResult) {
  187. std::string::size_type pos = out.find(".app/");
  188. if (pos != std::string::npos) {
  189. versionFile = out.substr(0, pos + 5) + "Contents/version.plist";
  190. }
  191. }
  192. }
  193. if (!versionFile.empty() && cmSystemTools::FileExists(versionFile)) {
  194. parser.ParseFile(versionFile.c_str());
  195. } else if (cmSystemTools::FileExists(
  196. "/Applications/Xcode.app/Contents/version.plist")) {
  197. parser.ParseFile("/Applications/Xcode.app/Contents/version.plist");
  198. } else {
  199. parser.ParseFile(
  200. "/Developer/Applications/Xcode.app/Contents/version.plist");
  201. }
  202. std::string const& version_string = parser.Version;
  203. // Compute an integer form of the version number.
  204. unsigned int v[2] = { 0, 0 };
  205. sscanf(version_string.c_str(), "%u.%u", &v[0], &v[1]);
  206. unsigned int version_number = 10 * v[0] + v[1];
  207. if (version_number < 50) {
  208. cm->IssueMessage(MessageType::FATAL_ERROR,
  209. "Xcode " + version_string + " not supported.");
  210. return std::unique_ptr<cmGlobalGenerator>();
  211. }
  212. return std::unique_ptr<cmGlobalGenerator>(
  213. cm::make_unique<cmGlobalXCodeGenerator>(cm, version_string,
  214. version_number));
  215. #else
  216. std::cerr << "CMake should be built with cmake to use Xcode, "
  217. "default to Xcode 1.5\n";
  218. return std::unique_ptr<cmGlobalGenerator>(
  219. cm::make_unique<cmGlobalXCodeGenerator>(cm));
  220. #endif
  221. }
  222. bool cmGlobalXCodeGenerator::FindMakeProgram(cmMakefile* mf)
  223. {
  224. // The Xcode generator knows how to lookup its build tool
  225. // directly instead of needing a helper module to do it, so we
  226. // do not actually need to put CMAKE_MAKE_PROGRAM into the cache.
  227. if (cmIsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM"))) {
  228. mf->AddDefinition("CMAKE_MAKE_PROGRAM", this->GetXcodeBuildCommand());
  229. }
  230. return true;
  231. }
  232. std::string const& cmGlobalXCodeGenerator::GetXcodeBuildCommand()
  233. {
  234. if (!this->XcodeBuildCommandInitialized) {
  235. this->XcodeBuildCommandInitialized = true;
  236. this->XcodeBuildCommand = this->FindXcodeBuildCommand();
  237. }
  238. return this->XcodeBuildCommand;
  239. }
  240. std::string cmGlobalXCodeGenerator::FindXcodeBuildCommand()
  241. {
  242. std::string makeProgram = cmSystemTools::FindProgram("xcodebuild");
  243. if (makeProgram.empty()) {
  244. makeProgram = "xcodebuild";
  245. }
  246. return makeProgram;
  247. }
  248. bool cmGlobalXCodeGenerator::SetSystemName(std::string const& s,
  249. cmMakefile* mf)
  250. {
  251. this->SystemName = s;
  252. return this->cmGlobalGenerator::SetSystemName(s, mf);
  253. }
  254. namespace {
  255. cm::string_view cmXcodeBuildSystemString(cmGlobalXCodeGenerator::BuildSystem b)
  256. {
  257. switch (b) {
  258. case cmGlobalXCodeGenerator::BuildSystem::One:
  259. return "1"_s;
  260. case cmGlobalXCodeGenerator::BuildSystem::Twelve:
  261. return "12"_s;
  262. }
  263. return {};
  264. }
  265. }
  266. bool cmGlobalXCodeGenerator::SetGeneratorToolset(std::string const& ts,
  267. bool build, cmMakefile* mf)
  268. {
  269. if (!this->ParseGeneratorToolset(ts, mf)) {
  270. return false;
  271. }
  272. if (build) {
  273. return true;
  274. }
  275. if (!this->GeneratorToolset.empty()) {
  276. mf->AddDefinition("CMAKE_XCODE_PLATFORM_TOOLSET", this->GeneratorToolset);
  277. }
  278. mf->AddDefinition("CMAKE_XCODE_BUILD_SYSTEM",
  279. cmXcodeBuildSystemString(this->XcodeBuildSystem));
  280. return true;
  281. }
  282. bool cmGlobalXCodeGenerator::ParseGeneratorToolset(std::string const& ts,
  283. cmMakefile* mf)
  284. {
  285. std::vector<std::string> const fields = cmTokenize(ts, ",");
  286. auto fi = fields.cbegin();
  287. if (fi == fields.cend()) {
  288. return true;
  289. }
  290. // The first field may be the Xcode GCC_VERSION.
  291. if (fi->find('=') == fi->npos) {
  292. this->GeneratorToolset = *fi;
  293. ++fi;
  294. }
  295. std::unordered_set<std::string> handled;
  296. // The rest of the fields must be key=value pairs.
  297. for (; fi != fields.cend(); ++fi) {
  298. std::string::size_type pos = fi->find('=');
  299. if (pos == fi->npos) {
  300. /* clang-format off */
  301. std::string const& e = cmStrCat(
  302. "Generator\n"
  303. " ", this->GetName(), "\n"
  304. "given toolset specification\n"
  305. " ", ts, "\n"
  306. "that contains a field after the first ',' with no '='."
  307. );
  308. /* clang-format on */
  309. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  310. return false;
  311. }
  312. std::string const key = fi->substr(0, pos);
  313. std::string const value = fi->substr(pos + 1);
  314. if (!handled.insert(key).second) {
  315. /* clang-format off */
  316. std::string const& e = cmStrCat(
  317. "Generator\n"
  318. " ", this->GetName(), "\n"
  319. "given toolset specification\n"
  320. " ", ts, "\n"
  321. "that contains duplicate field key '", key, "'."
  322. );
  323. /* clang-format on */
  324. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  325. return false;
  326. }
  327. if (!this->ProcessGeneratorToolsetField(key, value, mf)) {
  328. return false;
  329. }
  330. }
  331. return true;
  332. }
  333. bool cmGlobalXCodeGenerator::ProcessGeneratorToolsetField(
  334. std::string const& key, std::string const& value, cmMakefile* mf)
  335. {
  336. if (key == "buildsystem") {
  337. if (value == "1"_s) {
  338. this->XcodeBuildSystem = BuildSystem::One;
  339. } else if (value == "12"_s) {
  340. this->XcodeBuildSystem = BuildSystem::Twelve;
  341. } else {
  342. /* clang-format off */
  343. std::string const& e = cmStrCat(
  344. "Generator\n"
  345. " ", this->GetName(), "\n"
  346. "toolset specification field\n"
  347. " buildsystem=", value, "\n"
  348. "value is unkonwn. It must be '1' or '12'."
  349. );
  350. /* clang-format on */
  351. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  352. return false;
  353. }
  354. if (this->XcodeBuildSystem == BuildSystem::Twelve &&
  355. this->XcodeVersion < 120) {
  356. /* clang-format off */
  357. std::string const& e = cmStrCat(
  358. "Generator\n"
  359. " ", this->GetName(), "\n"
  360. "toolset specification field\n"
  361. " buildsystem=", value, "\n"
  362. "is not allowed with Xcode ", this->VersionString, '.'
  363. );
  364. /* clang-format on */
  365. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  366. return false;
  367. }
  368. return true;
  369. }
  370. /* clang-format off */
  371. std::string const& e = cmStrCat(
  372. "Generator\n"
  373. " ", this->GetName(), "\n"
  374. "given toolset specification that contains invalid field '", key, "'."
  375. );
  376. /* clang-format on */
  377. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  378. return false;
  379. }
  380. void cmGlobalXCodeGenerator::EnableLanguage(
  381. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  382. {
  383. mf->AddDefinition("XCODE", "1");
  384. mf->AddDefinition("XCODE_VERSION", this->VersionString);
  385. if (!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES")) {
  386. mf->AddCacheDefinition(
  387. "CMAKE_CONFIGURATION_TYPES", "Debug;Release;MinSizeRel;RelWithDebInfo",
  388. "Semicolon separated list of supported configuration types, "
  389. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  390. "anything else will be ignored.",
  391. cmStateEnums::STRING);
  392. }
  393. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  394. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  395. this->ComputeArchitectures(mf);
  396. }
  397. bool cmGlobalXCodeGenerator::Open(const std::string& bindir,
  398. const std::string& projectName, bool dryRun)
  399. {
  400. bool ret = false;
  401. #ifdef HAVE_APPLICATION_SERVICES
  402. std::string url = bindir + "/" + projectName + ".xcodeproj";
  403. if (dryRun) {
  404. return cmSystemTools::FileExists(url, false);
  405. }
  406. CFStringRef cfStr = CFStringCreateWithCString(
  407. kCFAllocatorDefault, url.c_str(), kCFStringEncodingUTF8);
  408. if (cfStr) {
  409. CFURLRef cfUrl = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfStr,
  410. kCFURLPOSIXPathStyle, true);
  411. if (cfUrl) {
  412. OSStatus err = LSOpenCFURLRef(cfUrl, nullptr);
  413. ret = err == noErr;
  414. CFRelease(cfUrl);
  415. }
  416. CFRelease(cfStr);
  417. }
  418. #endif
  419. return ret;
  420. }
  421. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  422. cmGlobalXCodeGenerator::GenerateBuildCommand(
  423. const std::string& makeProgram, const std::string& projectName,
  424. const std::string& /*projectDir*/,
  425. std::vector<std::string> const& targetNames, const std::string& config,
  426. bool /*fast*/, int jobs, bool /*verbose*/,
  427. std::vector<std::string> const& makeOptions)
  428. {
  429. GeneratedMakeCommand makeCommand;
  430. // now build the test
  431. makeCommand.Add(
  432. this->SelectMakeProgram(makeProgram, this->GetXcodeBuildCommand()));
  433. if (!projectName.empty()) {
  434. makeCommand.Add("-project");
  435. std::string projectArg = cmStrCat(projectName, ".xcodeproj");
  436. makeCommand.Add(projectArg);
  437. }
  438. if (cm::contains(targetNames, "clean")) {
  439. makeCommand.Add("clean");
  440. makeCommand.Add("-target", "ALL_BUILD");
  441. } else {
  442. makeCommand.Add("build");
  443. if (targetNames.empty() ||
  444. ((targetNames.size() == 1) && targetNames.front().empty())) {
  445. makeCommand.Add("-target", "ALL_BUILD");
  446. } else {
  447. for (const auto& tname : targetNames) {
  448. if (!tname.empty()) {
  449. makeCommand.Add("-target", tname);
  450. }
  451. }
  452. }
  453. }
  454. if (this->XcodeBuildSystem >= BuildSystem::Twelve) {
  455. makeCommand.Add("-parallelizeTargets");
  456. }
  457. makeCommand.Add("-configuration", (config.empty() ? "Debug" : config));
  458. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  459. makeCommand.Add("-jobs");
  460. if (jobs != cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  461. makeCommand.Add(std::to_string(jobs));
  462. }
  463. }
  464. if (this->XcodeVersion >= 70) {
  465. makeCommand.Add("-hideShellScriptEnvironment");
  466. }
  467. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  468. return { std::move(makeCommand) };
  469. }
  470. //! Create a local generator appropriate to this Global Generator
  471. std::unique_ptr<cmLocalGenerator> cmGlobalXCodeGenerator::CreateLocalGenerator(
  472. cmMakefile* mf)
  473. {
  474. std::unique_ptr<cmLocalGenerator> lg(
  475. cm::make_unique<cmLocalXCodeGenerator>(this, mf));
  476. if (this->XcodeBuildSystem >= BuildSystem::Twelve) {
  477. // For this build system variant we generate custom commands as
  478. // shell scripts directly rather than inside Makefiles.
  479. // FIXME: Rename or refactor this option for clarity.
  480. lg->SetLinkScriptShell(true);
  481. }
  482. return lg;
  483. }
  484. void cmGlobalXCodeGenerator::AddExtraIDETargets()
  485. {
  486. // make sure extra targets are added before calling
  487. // the parent generate which will call trace depends
  488. for (auto keyVal : this->ProjectMap) {
  489. cmLocalGenerator* root = keyVal.second[0];
  490. this->SetGenerationRoot(root);
  491. // add ALL_BUILD, INSTALL, etc
  492. this->AddExtraTargets(root, keyVal.second);
  493. }
  494. }
  495. void cmGlobalXCodeGenerator::Generate()
  496. {
  497. this->cmGlobalGenerator::Generate();
  498. if (cmSystemTools::GetErrorOccuredFlag()) {
  499. return;
  500. }
  501. for (auto keyVal : this->ProjectMap) {
  502. cmLocalGenerator* root = keyVal.second[0];
  503. bool generateTopLevelProjectOnly =
  504. root->GetMakefile()->IsOn("CMAKE_XCODE_GENERATE_TOP_LEVEL_PROJECT_ONLY");
  505. if (generateTopLevelProjectOnly) {
  506. cmStateSnapshot snp = root->GetStateSnapshot();
  507. if (snp.GetBuildsystemDirectoryParent().IsValid()) {
  508. continue;
  509. }
  510. }
  511. // cache the enabled languages for source file type queries
  512. this->GetEnabledLanguages(this->EnabledLangs);
  513. this->SetGenerationRoot(root);
  514. // now create the project
  515. this->OutputXCodeProject(root, keyVal.second);
  516. }
  517. }
  518. void cmGlobalXCodeGenerator::SetGenerationRoot(cmLocalGenerator* root)
  519. {
  520. this->CurrentProject = root->GetProjectName();
  521. this->SetCurrentLocalGenerator(root);
  522. cmSystemTools::SplitPath(
  523. this->CurrentLocalGenerator->GetCurrentSourceDirectory(),
  524. this->ProjectSourceDirectoryComponents);
  525. cmSystemTools::SplitPath(
  526. this->CurrentLocalGenerator->GetCurrentBinaryDirectory(),
  527. this->ProjectOutputDirectoryComponents);
  528. this->CurrentXCodeHackMakefile =
  529. cmStrCat(root->GetCurrentBinaryDirectory(), "/CMakeScripts");
  530. cmSystemTools::MakeDirectory(this->CurrentXCodeHackMakefile);
  531. this->CurrentXCodeHackMakefile += "/XCODE_DEPEND_HELPER.make";
  532. }
  533. std::string cmGlobalXCodeGenerator::PostBuildMakeTarget(
  534. std::string const& tName, std::string const& configName)
  535. {
  536. std::string target = tName;
  537. std::replace(target.begin(), target.end(), ' ', '_');
  538. std::string out = cmStrCat("PostBuild.", target, '.', configName);
  539. return out;
  540. }
  541. #define CMAKE_CHECK_BUILD_SYSTEM_TARGET "ZERO_CHECK"
  542. #define OBJECT_LIBRARY_ARTIFACT_DIR std::string()
  543. void cmGlobalXCodeGenerator::AddExtraTargets(
  544. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& gens)
  545. {
  546. const char* no_working_directory = nullptr;
  547. std::vector<std::string> no_byproducts;
  548. std::vector<std::string> no_depends;
  549. // Add ALL_BUILD
  550. cmTarget* allbuild = root->AddUtilityCommand(
  551. "ALL_BUILD", true, no_working_directory, no_byproducts, no_depends,
  552. cmMakeSingleCommandLine({ "echo", "Build all projects" }));
  553. root->AddGeneratorTarget(cm::make_unique<cmGeneratorTarget>(allbuild, root));
  554. // Add XCODE depend helper
  555. std::string legacyDependHelperDir = root->GetCurrentBinaryDirectory();
  556. cmCustomCommandLines legacyDependHelperCommandLines;
  557. if (this->XcodeBuildSystem == BuildSystem::One) {
  558. legacyDependHelperCommandLines = cmMakeSingleCommandLine(
  559. { "make", "-C", legacyDependHelperDir, "-f",
  560. this->CurrentXCodeHackMakefile, "OBJDIR=$(OBJDIR)",
  561. /* placeholder, see below */ "" });
  562. }
  563. // Add ZERO_CHECK
  564. bool regenerate = !this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION");
  565. bool generateTopLevelProjectOnly =
  566. root->GetMakefile()->IsOn("CMAKE_XCODE_GENERATE_TOP_LEVEL_PROJECT_ONLY");
  567. bool isTopLevel =
  568. !root->GetStateSnapshot().GetBuildsystemDirectoryParent().IsValid();
  569. bool isGenerateProject = isTopLevel || !generateTopLevelProjectOnly;
  570. if (regenerate && isGenerateProject) {
  571. this->CreateReRunCMakeFile(root, gens);
  572. std::string file =
  573. this->ConvertToRelativeForMake(this->CurrentReRunCMakeMakefile);
  574. cmSystemTools::ReplaceString(file, "\\ ", " ");
  575. cmTarget* check =
  576. root->AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, true,
  577. no_working_directory, no_byproducts, no_depends,
  578. cmMakeSingleCommandLine({ "make", "-f", file }));
  579. root->AddGeneratorTarget(cm::make_unique<cmGeneratorTarget>(check, root));
  580. }
  581. // now make the allbuild depend on all the non-utility targets
  582. // in the project
  583. for (auto& gen : gens) {
  584. for (const auto& target : gen->GetGeneratorTargets()) {
  585. if (target->GetType() == cmStateEnums::GLOBAL_TARGET) {
  586. continue;
  587. }
  588. if (regenerate &&
  589. (target->GetName() != CMAKE_CHECK_BUILD_SYSTEM_TARGET)) {
  590. target->Target->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false);
  591. }
  592. // make all exe, shared libs and modules
  593. // run the depend check makefile as a post build rule
  594. // this will make sure that when the next target is built
  595. // things are up-to-date
  596. if (this->XcodeBuildSystem == BuildSystem::One && isGenerateProject &&
  597. target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  598. legacyDependHelperCommandLines.front().back() = // fill placeholder
  599. this->PostBuildMakeTarget(target->GetName(), "$(CONFIGURATION)");
  600. gen->AddCustomCommandToTarget(
  601. target->GetName(), no_byproducts, no_depends,
  602. legacyDependHelperCommandLines, cmCustomCommandType::POST_BUILD,
  603. "Depend check for xcode", legacyDependHelperDir.c_str(), true, false,
  604. "", "", false, cmObjectLibraryCommands::Accept);
  605. }
  606. if (!this->IsExcluded(gens[0], target.get())) {
  607. allbuild->AddUtility(target->GetName(), false);
  608. }
  609. }
  610. }
  611. }
  612. void cmGlobalXCodeGenerator::CreateReRunCMakeFile(
  613. cmLocalGenerator* root, std::vector<cmLocalGenerator*> const& gens)
  614. {
  615. std::vector<std::string> lfiles;
  616. for (auto gen : gens) {
  617. cm::append(lfiles, gen->GetMakefile()->GetListFiles());
  618. }
  619. // sort the array
  620. std::sort(lfiles.begin(), lfiles.end());
  621. lfiles.erase(std::unique(lfiles.begin(), lfiles.end()), lfiles.end());
  622. cmake* cm = this->GetCMakeInstance();
  623. if (cm->DoWriteGlobVerifyTarget()) {
  624. lfiles.emplace_back(cm->GetGlobVerifyStamp());
  625. }
  626. this->CurrentReRunCMakeMakefile =
  627. cmStrCat(root->GetCurrentBinaryDirectory(), "/CMakeScripts");
  628. cmSystemTools::MakeDirectory(this->CurrentReRunCMakeMakefile);
  629. this->CurrentReRunCMakeMakefile += "/ReRunCMake.make";
  630. cmGeneratedFileStream makefileStream(this->CurrentReRunCMakeMakefile);
  631. makefileStream.SetCopyIfDifferent(true);
  632. makefileStream << "# Generated by CMake, DO NOT EDIT\n\n";
  633. makefileStream << "TARGETS:= \n";
  634. makefileStream << "empty:= \n";
  635. makefileStream << "space:= $(empty) $(empty)\n";
  636. makefileStream << "spaceplus:= $(empty)\\ $(empty)\n\n";
  637. for (const auto& lfile : lfiles) {
  638. makefileStream << "TARGETS += $(subst $(space),$(spaceplus),$(wildcard "
  639. << this->ConvertToRelativeForMake(lfile) << "))\n";
  640. }
  641. makefileStream << "\n";
  642. std::string checkCache =
  643. cmStrCat(root->GetBinaryDirectory(), "/CMakeFiles/cmake.check_cache");
  644. if (cm->DoWriteGlobVerifyTarget()) {
  645. makefileStream << ".NOTPARALLEL:\n\n";
  646. makefileStream << ".PHONY: all VERIFY_GLOBS\n\n";
  647. makefileStream << "all: VERIFY_GLOBS "
  648. << this->ConvertToRelativeForMake(checkCache) << "\n\n";
  649. makefileStream << "VERIFY_GLOBS:\n";
  650. makefileStream << "\t"
  651. << this->ConvertToRelativeForMake(
  652. cmSystemTools::GetCMakeCommand())
  653. << " -P "
  654. << this->ConvertToRelativeForMake(cm->GetGlobVerifyScript())
  655. << "\n\n";
  656. }
  657. makefileStream << this->ConvertToRelativeForMake(checkCache)
  658. << ": $(TARGETS)\n";
  659. makefileStream << "\t"
  660. << this->ConvertToRelativeForMake(
  661. cmSystemTools::GetCMakeCommand())
  662. << " -H"
  663. << this->ConvertToRelativeForMake(root->GetSourceDirectory())
  664. << " -B"
  665. << this->ConvertToRelativeForMake(root->GetBinaryDirectory())
  666. << "\n";
  667. }
  668. static bool objectIdLessThan(const std::unique_ptr<cmXCodeObject>& l,
  669. const std::unique_ptr<cmXCodeObject>& r)
  670. {
  671. return l->GetId() < r->GetId();
  672. }
  673. void cmGlobalXCodeGenerator::SortXCodeObjects()
  674. {
  675. std::sort(this->XCodeObjects.begin(), this->XCodeObjects.end(),
  676. objectIdLessThan);
  677. }
  678. void cmGlobalXCodeGenerator::ClearXCodeObjects()
  679. {
  680. this->TargetDoneSet.clear();
  681. this->XCodeObjects.clear();
  682. this->XCodeObjectIDs.clear();
  683. this->XCodeObjectMap.clear();
  684. this->GroupMap.clear();
  685. this->GroupNameMap.clear();
  686. this->TargetGroup.clear();
  687. this->FileRefs.clear();
  688. this->ExternalLibRefs.clear();
  689. this->FileRefToBuildFileMap.clear();
  690. this->CommandsVisited.clear();
  691. }
  692. void cmGlobalXCodeGenerator::addObject(std::unique_ptr<cmXCodeObject> obj)
  693. {
  694. if (obj->GetType() == cmXCodeObject::OBJECT) {
  695. const std::string& id = obj->GetId();
  696. // If this is a duplicate id, it's an error:
  697. //
  698. if (this->XCodeObjectIDs.count(id)) {
  699. cmSystemTools::Error(
  700. "Xcode generator: duplicate object ids not allowed");
  701. }
  702. this->XCodeObjectIDs.insert(id);
  703. }
  704. this->XCodeObjects.push_back(std::move(obj));
  705. }
  706. cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(
  707. cmXCodeObject::PBXType ptype)
  708. {
  709. auto obj = cm::make_unique<cmXCode21Object>(ptype, cmXCodeObject::OBJECT,
  710. this->GetObjectId());
  711. auto ptr = obj.get();
  712. this->addObject(std::move(obj));
  713. return ptr;
  714. }
  715. cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::Type type)
  716. {
  717. auto obj = cm::make_unique<cmXCodeObject>(
  718. cmXCodeObject::None, type,
  719. "Temporary cmake object, should not be referred to in Xcode file");
  720. auto ptr = obj.get();
  721. this->addObject(std::move(obj));
  722. return ptr;
  723. }
  724. cmXCodeObject* cmGlobalXCodeGenerator::CreateString(const std::string& s)
  725. {
  726. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::STRING);
  727. obj->SetString(s);
  728. return obj;
  729. }
  730. cmXCodeObject* cmGlobalXCodeGenerator::CreateObjectReference(
  731. cmXCodeObject* ref)
  732. {
  733. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::OBJECT_REF);
  734. obj->SetObject(ref);
  735. return obj;
  736. }
  737. cmXCodeObject* cmGlobalXCodeGenerator::CreateFlatClone(cmXCodeObject* orig)
  738. {
  739. cmXCodeObject* obj = this->CreateObject(orig->GetType());
  740. obj->CopyAttributes(orig);
  741. return obj;
  742. }
  743. std::string GetGroupMapKeyFromPath(cmGeneratorTarget* target,
  744. const std::string& fullpath)
  745. {
  746. std::string key(target->GetName());
  747. key += "-";
  748. key += fullpath;
  749. return key;
  750. }
  751. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeBuildFileFromPath(
  752. const std::string& fullpath, cmGeneratorTarget* target,
  753. const std::string& lang, cmSourceFile* sf)
  754. {
  755. // Using a map and the full path guarantees that we will always get the same
  756. // fileRef object for any given full path. Same goes for the buildFile
  757. // object.
  758. cmXCodeObject* fileRef =
  759. this->CreateXCodeFileReferenceFromPath(fullpath, target, lang, sf);
  760. if (fileRef) {
  761. auto it = this->FileRefToBuildFileMap.find(fileRef);
  762. if (it == this->FileRefToBuildFileMap.end()) {
  763. cmXCodeObject* buildFile =
  764. this->CreateObject(cmXCodeObject::PBXBuildFile);
  765. buildFile->SetComment(fileRef->GetComment());
  766. buildFile->AddAttribute("fileRef", this->CreateObjectReference(fileRef));
  767. this->FileRefToBuildFileMap[fileRef] = buildFile;
  768. return buildFile;
  769. }
  770. return it->second;
  771. }
  772. return nullptr;
  773. }
  774. class XCodeGeneratorExpressionInterpreter
  775. : public cmGeneratorExpressionInterpreter
  776. {
  777. public:
  778. XCodeGeneratorExpressionInterpreter(cmSourceFile* sourceFile,
  779. cmLocalGenerator* localGenerator,
  780. cmGeneratorTarget* headTarget,
  781. const std::string& lang)
  782. : cmGeneratorExpressionInterpreter(
  783. localGenerator, "NO-PER-CONFIG-SUPPORT-IN-XCODE", headTarget, lang)
  784. , SourceFile(sourceFile)
  785. {
  786. }
  787. XCodeGeneratorExpressionInterpreter(
  788. XCodeGeneratorExpressionInterpreter const&) = delete;
  789. XCodeGeneratorExpressionInterpreter& operator=(
  790. XCodeGeneratorExpressionInterpreter const&) = delete;
  791. const std::string& Evaluate(const char* expression,
  792. const std::string& property)
  793. {
  794. return this->Evaluate(std::string(expression ? expression : ""), property);
  795. }
  796. const std::string& Evaluate(const std::string& expression,
  797. const std::string& property)
  798. {
  799. const std::string& processed =
  800. this->cmGeneratorExpressionInterpreter::Evaluate(expression, property);
  801. if (this->CompiledGeneratorExpression->GetHadContextSensitiveCondition()) {
  802. std::ostringstream e;
  803. /* clang-format off */
  804. e <<
  805. "Xcode does not support per-config per-source " << property << ":\n"
  806. " " << expression << "\n"
  807. "specified for source:\n"
  808. " " << this->SourceFile->ResolveFullPath() << "\n";
  809. /* clang-format on */
  810. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  811. }
  812. return processed;
  813. }
  814. private:
  815. cmSourceFile* SourceFile = nullptr;
  816. };
  817. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeSourceFile(
  818. cmLocalGenerator* lg, cmSourceFile* sf, cmGeneratorTarget* gtgt)
  819. {
  820. std::string lang = this->CurrentLocalGenerator->GetSourceFileLanguage(*sf);
  821. XCodeGeneratorExpressionInterpreter genexInterpreter(sf, lg, gtgt, lang);
  822. // Add flags from target and source file properties.
  823. std::string flags;
  824. std::string const& srcfmt = sf->GetSafeProperty("Fortran_FORMAT");
  825. switch (cmOutputConverter::GetFortranFormat(srcfmt)) {
  826. case cmOutputConverter::FortranFormatFixed:
  827. flags = "-fixed " + flags;
  828. break;
  829. case cmOutputConverter::FortranFormatFree:
  830. flags = "-free " + flags;
  831. break;
  832. default:
  833. break;
  834. }
  835. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  836. if (cmProp cflags = sf->GetProperty(COMPILE_FLAGS)) {
  837. lg->AppendFlags(flags, genexInterpreter.Evaluate(*cflags, COMPILE_FLAGS));
  838. }
  839. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  840. if (cmProp coptions = sf->GetProperty(COMPILE_OPTIONS)) {
  841. lg->AppendCompileOptions(
  842. flags, genexInterpreter.Evaluate(*coptions, COMPILE_OPTIONS));
  843. }
  844. // Add per-source definitions.
  845. BuildObjectListOrString flagsBuild(this, false);
  846. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  847. if (cmProp compile_defs = sf->GetProperty(COMPILE_DEFINITIONS)) {
  848. this->AppendDefines(
  849. flagsBuild,
  850. genexInterpreter.Evaluate(*compile_defs, COMPILE_DEFINITIONS).c_str(),
  851. true);
  852. }
  853. if (sf->GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS")) {
  854. this->AppendDefines(flagsBuild, "CMAKE_SKIP_PRECOMPILE_HEADERS", true);
  855. }
  856. if (!flagsBuild.IsEmpty()) {
  857. if (!flags.empty()) {
  858. flags += ' ';
  859. }
  860. flags += flagsBuild.GetString();
  861. }
  862. // Add per-source include directories.
  863. std::vector<std::string> includes;
  864. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  865. if (cmProp cincludes = sf->GetProperty(INCLUDE_DIRECTORIES)) {
  866. lg->AppendIncludeDirectories(
  867. includes, genexInterpreter.Evaluate(*cincludes, INCLUDE_DIRECTORIES),
  868. *sf);
  869. }
  870. lg->AppendFlags(flags, lg->GetIncludeFlags(includes, gtgt, lang, true));
  871. cmXCodeObject* buildFile =
  872. this->CreateXCodeBuildFileFromPath(sf->ResolveFullPath(), gtgt, lang, sf);
  873. cmXCodeObject* settings = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  874. settings->AddAttributeIfNotEmpty("COMPILER_FLAGS",
  875. this->CreateString(flags));
  876. cmGeneratorTarget::SourceFileFlags tsFlags =
  877. gtgt->GetTargetSourceFileFlags(sf);
  878. cmXCodeObject* attrs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  879. // Is this a "private" or "public" framework header file?
  880. // Set the ATTRIBUTES attribute appropriately...
  881. //
  882. if (gtgt->IsFrameworkOnApple()) {
  883. if (tsFlags.Type == cmGeneratorTarget::SourceFileTypePrivateHeader) {
  884. attrs->AddObject(this->CreateString("Private"));
  885. } else if (tsFlags.Type == cmGeneratorTarget::SourceFileTypePublicHeader) {
  886. attrs->AddObject(this->CreateString("Public"));
  887. }
  888. }
  889. // Add user-specified file attributes.
  890. cmProp extraFileAttributes = sf->GetProperty("XCODE_FILE_ATTRIBUTES");
  891. if (extraFileAttributes) {
  892. // Expand the list of attributes.
  893. std::vector<std::string> attributes = cmExpandedList(*extraFileAttributes);
  894. // Store the attributes.
  895. for (const auto& attribute : attributes) {
  896. attrs->AddObject(this->CreateString(attribute));
  897. }
  898. }
  899. settings->AddAttributeIfNotEmpty("ATTRIBUTES", attrs);
  900. if (buildFile) {
  901. buildFile->AddAttributeIfNotEmpty("settings", settings);
  902. }
  903. return buildFile;
  904. }
  905. void cmGlobalXCodeGenerator::AddXCodeProjBuildRule(
  906. cmGeneratorTarget* target, std::vector<cmSourceFile*>& sources) const
  907. {
  908. std::string listfile =
  909. cmStrCat(target->GetLocalGenerator()->GetCurrentSourceDirectory(),
  910. "/CMakeLists.txt");
  911. cmSourceFile* srcCMakeLists = target->Makefile->GetOrCreateSource(
  912. listfile, false, cmSourceFileLocationKind::Known);
  913. if (!cm::contains(sources, srcCMakeLists)) {
  914. sources.push_back(srcCMakeLists);
  915. }
  916. }
  917. namespace {
  918. bool IsLinkPhaseLibraryExtension(const std::string& fileExt)
  919. {
  920. // Empty file extension is a special case for paths to framework's
  921. // internal binary which could be MyFw.framework/Versions/*/MyFw
  922. return (fileExt == ".framework" || fileExt == ".a" || fileExt == ".o" ||
  923. fileExt == ".dylib" || fileExt == ".tbd" || fileExt.empty());
  924. }
  925. bool IsLibraryType(const std::string& fileType)
  926. {
  927. return (fileType == "wrapper.framework" || fileType == "archive.ar" ||
  928. fileType == "compiled.mach-o.objfile" ||
  929. fileType == "compiled.mach-o.dylib" ||
  930. fileType == "compiled.mach-o.executable" ||
  931. fileType == "sourcecode.text-based-dylib-definition");
  932. }
  933. std::string GetDirectoryValueFromFileExtension(const std::string& dirExt)
  934. {
  935. std::string ext = cmSystemTools::LowerCase(dirExt);
  936. if (ext == "framework") {
  937. return "wrapper.framework";
  938. }
  939. if (ext == "xcassets") {
  940. return "folder.assetcatalog";
  941. }
  942. return "folder";
  943. }
  944. std::string GetSourcecodeValueFromFileExtension(
  945. const std::string& _ext, const std::string& lang,
  946. bool& keepLastKnownFileType, const std::vector<std::string>& enabled_langs)
  947. {
  948. std::string ext = cmSystemTools::LowerCase(_ext);
  949. std::string sourcecode = "sourcecode";
  950. if (ext == "o") {
  951. keepLastKnownFileType = true;
  952. sourcecode = "compiled.mach-o.objfile";
  953. } else if (ext == "xctest") {
  954. sourcecode = "wrapper.cfbundle";
  955. } else if (ext == "xib") {
  956. keepLastKnownFileType = true;
  957. sourcecode = "file.xib";
  958. } else if (ext == "storyboard") {
  959. keepLastKnownFileType = true;
  960. sourcecode = "file.storyboard";
  961. } else if (ext == "mm" && !cm::contains(enabled_langs, "OBJCXX")) {
  962. sourcecode += ".cpp.objcpp";
  963. } else if (ext == "m" && !cm::contains(enabled_langs, "OBJC")) {
  964. sourcecode += ".c.objc";
  965. } else if (ext == "swift") {
  966. sourcecode += ".swift";
  967. } else if (ext == "plist") {
  968. sourcecode += ".text.plist";
  969. } else if (ext == "h") {
  970. sourcecode += ".c.h";
  971. } else if (ext == "hxx" || ext == "hpp" || ext == "txx" || ext == "pch" ||
  972. ext == "hh") {
  973. sourcecode += ".cpp.h";
  974. } else if (ext == "png" || ext == "gif" || ext == "jpg") {
  975. keepLastKnownFileType = true;
  976. sourcecode = "image";
  977. } else if (ext == "txt") {
  978. sourcecode += ".text";
  979. } else if (lang == "CXX") {
  980. sourcecode += ".cpp.cpp";
  981. } else if (lang == "C") {
  982. sourcecode += ".c.c";
  983. } else if (lang == "OBJCXX") {
  984. sourcecode += ".cpp.objcpp";
  985. } else if (lang == "OBJC") {
  986. sourcecode += ".c.objc";
  987. } else if (lang == "Fortran") {
  988. sourcecode += ".fortran.f90";
  989. } else if (lang == "ASM") {
  990. sourcecode += ".asm";
  991. } else if (ext == "metal") {
  992. sourcecode += ".metal";
  993. } else if (ext == "mig") {
  994. sourcecode += ".mig";
  995. } else if (ext == "tbd") {
  996. sourcecode += ".text-based-dylib-definition";
  997. } else if (ext == "a") {
  998. keepLastKnownFileType = true;
  999. sourcecode = "archive.ar";
  1000. } else if (ext == "dylib") {
  1001. keepLastKnownFileType = true;
  1002. sourcecode = "compiled.mach-o.dylib";
  1003. } else if (ext == "framework") {
  1004. keepLastKnownFileType = true;
  1005. sourcecode = "wrapper.framework";
  1006. } else if (ext == "xcassets") {
  1007. keepLastKnownFileType = true;
  1008. sourcecode = "folder.assetcatalog";
  1009. }
  1010. // else
  1011. // {
  1012. // // Already specialized above or we leave sourcecode == "sourcecode"
  1013. // // which is probably the most correct choice. Extensionless headers,
  1014. // // for example... Or file types unknown to Xcode that do not map to a
  1015. // // valid explicitFileType value.
  1016. // }
  1017. return sourcecode;
  1018. }
  1019. // If the file has no extension it's either a raw executable or might
  1020. // be a direct reference to a binary within a framework (bad practice!).
  1021. // This is where we change the path to point to the framework directory.
  1022. // .tbd files also can be located in SDK frameworks (they are
  1023. // placeholders for actual libraries shipped with the OS)
  1024. std::string GetLibraryOrFrameworkPath(const std::string& path)
  1025. {
  1026. auto ext = cmSystemTools::GetFilenameLastExtension(path);
  1027. if (ext.empty() || ext == ".tbd") {
  1028. auto name = cmSystemTools::GetFilenameWithoutExtension(path);
  1029. // Check for iOS framework structure:
  1030. // FwName.framework/FwName (and also on macOS where FwName lib is a
  1031. // symlink)
  1032. auto parentDir = cmSystemTools::GetParentDirectory(path);
  1033. auto parentName = cmSystemTools::GetFilenameWithoutExtension(parentDir);
  1034. ext = cmSystemTools::GetFilenameLastExtension(parentDir);
  1035. if (ext == ".framework" && name == parentName) {
  1036. return parentDir;
  1037. }
  1038. // Check for macOS framework structure:
  1039. // FwName.framework/Versions/*/FwName
  1040. std::vector<std::string> components;
  1041. cmSystemTools::SplitPath(path, components);
  1042. if (components.size() > 3 &&
  1043. components[components.size() - 3] == "Versions") {
  1044. ext = cmSystemTools::GetFilenameLastExtension(
  1045. components[components.size() - 4]);
  1046. parentName = cmSystemTools::GetFilenameWithoutExtension(
  1047. components[components.size() - 4]);
  1048. if (ext == ".framework" && name == parentName) {
  1049. components.erase(components.begin() + components.size() - 3,
  1050. components.end());
  1051. return cmSystemTools::JoinPath(components);
  1052. }
  1053. }
  1054. }
  1055. return path;
  1056. }
  1057. } // anonymous
  1058. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeFileReferenceFromPath(
  1059. const std::string& fullpath, cmGeneratorTarget* target,
  1060. const std::string& lang, cmSourceFile* sf)
  1061. {
  1062. bool useLastKnownFileType = false;
  1063. std::string fileType;
  1064. if (sf) {
  1065. if (cmProp e = sf->GetProperty("XCODE_EXPLICIT_FILE_TYPE")) {
  1066. fileType = *e;
  1067. } else if (cmProp l = sf->GetProperty("XCODE_LAST_KNOWN_FILE_TYPE")) {
  1068. useLastKnownFileType = true;
  1069. fileType = *l;
  1070. }
  1071. }
  1072. // Make a copy so that we can override it later
  1073. std::string path = fullpath;
  1074. // Compute the extension without leading '.'.
  1075. std::string ext = cmSystemTools::GetFilenameLastExtension(path);
  1076. if (!ext.empty()) {
  1077. ext = ext.substr(1);
  1078. }
  1079. if (fileType.empty()) {
  1080. path = GetLibraryOrFrameworkPath(path);
  1081. ext = cmSystemTools::GetFilenameLastExtension(path);
  1082. if (!ext.empty()) {
  1083. ext = ext.substr(1);
  1084. }
  1085. // If fullpath references a directory, then we need to specify
  1086. // lastKnownFileType as folder in order for Xcode to be able to
  1087. // open the contents of the folder.
  1088. // (Xcode 4.6 does not like explicitFileType=folder).
  1089. if (cmSystemTools::FileIsDirectory(path)) {
  1090. fileType = GetDirectoryValueFromFileExtension(ext);
  1091. useLastKnownFileType = true;
  1092. } else {
  1093. if (ext.empty() && !sf) {
  1094. // Special case for executable or library without extension
  1095. // that is not a source file. We can't tell which without reading
  1096. // its Mach-O header, but the file might not exist yet, so we
  1097. // have to pick one here.
  1098. useLastKnownFileType = true;
  1099. fileType = "compiled.mach-o.executable";
  1100. } else {
  1101. fileType = GetSourcecodeValueFromFileExtension(
  1102. ext, lang, useLastKnownFileType, this->EnabledLangs);
  1103. }
  1104. }
  1105. }
  1106. std::string key = GetGroupMapKeyFromPath(target, path);
  1107. cmXCodeObject* fileRef = this->FileRefs[key];
  1108. if (!fileRef) {
  1109. fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  1110. fileRef->SetComment(path);
  1111. this->FileRefs[key] = fileRef;
  1112. }
  1113. fileRef->AddAttribute("fileEncoding", this->CreateString("4"));
  1114. fileRef->AddAttribute(useLastKnownFileType ? "lastKnownFileType"
  1115. : "explicitFileType",
  1116. this->CreateString(fileType));
  1117. // Store the file path relative to the top of the source tree.
  1118. if (!IsLibraryType(fileType)) {
  1119. path = this->RelativeToSource(path);
  1120. }
  1121. std::string name = cmSystemTools::GetFilenameName(path);
  1122. const char* sourceTree =
  1123. cmSystemTools::FileIsFullPath(path) ? "<absolute>" : "SOURCE_ROOT";
  1124. fileRef->AddAttribute("name", this->CreateString(name));
  1125. fileRef->AddAttribute("path", this->CreateString(path));
  1126. fileRef->AddAttribute("sourceTree", this->CreateString(sourceTree));
  1127. cmXCodeObject* group = this->GroupMap[key];
  1128. if (!group && IsLibraryType(fileType)) {
  1129. group = this->FrameworkGroup;
  1130. this->GroupMap[key] = group;
  1131. }
  1132. if (!group) {
  1133. cmSystemTools::Error("Could not find a PBX group for " + key);
  1134. return nullptr;
  1135. }
  1136. cmXCodeObject* children = group->GetAttribute("children");
  1137. if (!children->HasObject(fileRef)) {
  1138. children->AddObject(fileRef);
  1139. }
  1140. return fileRef;
  1141. }
  1142. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeFileReference(
  1143. cmSourceFile* sf, cmGeneratorTarget* target)
  1144. {
  1145. std::string lang = this->CurrentLocalGenerator->GetSourceFileLanguage(*sf);
  1146. return this->CreateXCodeFileReferenceFromPath(sf->ResolveFullPath(), target,
  1147. lang, sf);
  1148. }
  1149. bool cmGlobalXCodeGenerator::SpecialTargetEmitted(std::string const& tname)
  1150. {
  1151. if (tname == "ALL_BUILD" || tname == "install" || tname == "package" ||
  1152. tname == "RUN_TESTS" || tname == CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  1153. if (this->TargetDoneSet.find(tname) != this->TargetDoneSet.end()) {
  1154. return true;
  1155. }
  1156. this->TargetDoneSet.insert(tname);
  1157. return false;
  1158. }
  1159. return false;
  1160. }
  1161. void cmGlobalXCodeGenerator::SetCurrentLocalGenerator(cmLocalGenerator* gen)
  1162. {
  1163. this->CurrentLocalGenerator = gen;
  1164. this->CurrentMakefile = gen->GetMakefile();
  1165. // Select the current set of configuration types.
  1166. this->CurrentConfigurationTypes =
  1167. this->CurrentMakefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  1168. }
  1169. struct cmSourceFilePathCompare
  1170. {
  1171. bool operator()(cmSourceFile* l, cmSourceFile* r)
  1172. {
  1173. return l->ResolveFullPath() < r->ResolveFullPath();
  1174. }
  1175. };
  1176. struct cmCompareTargets
  1177. {
  1178. bool operator()(cmXCodeObject* l, cmXCodeObject* r) const
  1179. {
  1180. std::string const& a = l->GetTarget()->GetName();
  1181. std::string const& b = r->GetTarget()->GetName();
  1182. if (a == "ALL_BUILD") {
  1183. return true;
  1184. }
  1185. if (b == "ALL_BUILD") {
  1186. return false;
  1187. }
  1188. return a < b;
  1189. }
  1190. };
  1191. bool cmGlobalXCodeGenerator::CreateXCodeTargets(
  1192. cmLocalGenerator* gen, std::vector<cmXCodeObject*>& targets)
  1193. {
  1194. this->SetCurrentLocalGenerator(gen);
  1195. std::vector<cmGeneratorTarget*> gts =
  1196. this->GetLocalGeneratorTargetsInOrder(gen);
  1197. for (auto gtgt : gts) {
  1198. if (!this->CreateXCodeTarget(gtgt, targets)) {
  1199. return false;
  1200. }
  1201. }
  1202. std::sort(targets.begin(), targets.end(), cmCompareTargets());
  1203. return true;
  1204. }
  1205. bool cmGlobalXCodeGenerator::CreateXCodeTarget(
  1206. cmGeneratorTarget* gtgt, std::vector<cmXCodeObject*>& targets)
  1207. {
  1208. std::string targetName = gtgt->GetName();
  1209. // make sure ALL_BUILD, INSTALL, etc are only done once
  1210. if (this->SpecialTargetEmitted(targetName)) {
  1211. return true;
  1212. }
  1213. if (!gtgt->IsInBuildSystem()) {
  1214. return true;
  1215. }
  1216. auto& gtgt_visited = this->CommandsVisited[gtgt];
  1217. auto& deps = this->GetTargetDirectDepends(gtgt);
  1218. for (auto& d : deps) {
  1219. // Take the union of visited source files of custom commands so far.
  1220. // ComputeTargetOrder ensures our dependencies already visited their
  1221. // custom commands and updated CommandsVisited.
  1222. auto& dep_visited = this->CommandsVisited[d];
  1223. gtgt_visited.insert(dep_visited.begin(), dep_visited.end());
  1224. }
  1225. if (gtgt->GetType() == cmStateEnums::UTILITY ||
  1226. gtgt->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  1227. gtgt->GetType() == cmStateEnums::GLOBAL_TARGET) {
  1228. cmXCodeObject* t = this->CreateUtilityTarget(gtgt);
  1229. if (!t) {
  1230. return false;
  1231. }
  1232. targets.push_back(t);
  1233. return true;
  1234. }
  1235. // organize the sources
  1236. std::vector<cmSourceFile*> commonSourceFiles;
  1237. if (!gtgt->GetConfigCommonSourceFiles(commonSourceFiles)) {
  1238. return false;
  1239. }
  1240. // Add CMakeLists.txt file for user convenience.
  1241. this->AddXCodeProjBuildRule(gtgt, commonSourceFiles);
  1242. // Add the Info.plist we are about to generate for an App Bundle.
  1243. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  1244. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1245. cmSourceFile* sf = gtgt->Makefile->GetOrCreateSource(
  1246. plist, true, cmSourceFileLocationKind::Known);
  1247. commonSourceFiles.push_back(sf);
  1248. }
  1249. std::sort(commonSourceFiles.begin(), commonSourceFiles.end(),
  1250. cmSourceFilePathCompare());
  1251. gtgt->ComputeObjectMapping();
  1252. std::vector<cmXCodeObject*> externalObjFiles;
  1253. std::vector<cmXCodeObject*> headerFiles;
  1254. std::vector<cmXCodeObject*> resourceFiles;
  1255. std::vector<cmXCodeObject*> sourceFiles;
  1256. for (auto sourceFile : commonSourceFiles) {
  1257. cmXCodeObject* xsf = this->CreateXCodeSourceFile(
  1258. this->CurrentLocalGenerator, sourceFile, gtgt);
  1259. cmXCodeObject* fr = xsf->GetAttribute("fileRef");
  1260. cmXCodeObject* filetype =
  1261. fr->GetObject()->GetAttribute("explicitFileType");
  1262. if (!filetype) {
  1263. filetype = fr->GetObject()->GetAttribute("lastKnownFileType");
  1264. }
  1265. cmGeneratorTarget::SourceFileFlags tsFlags =
  1266. gtgt->GetTargetSourceFileFlags(sourceFile);
  1267. if (filetype && filetype->GetString() == "compiled.mach-o.objfile") {
  1268. if (sourceFile->GetObjectLibrary().empty()) {
  1269. externalObjFiles.push_back(xsf);
  1270. }
  1271. } else if (this->IsHeaderFile(sourceFile) ||
  1272. (tsFlags.Type ==
  1273. cmGeneratorTarget::SourceFileTypePrivateHeader) ||
  1274. (tsFlags.Type ==
  1275. cmGeneratorTarget::SourceFileTypePublicHeader)) {
  1276. headerFiles.push_back(xsf);
  1277. } else if (tsFlags.Type == cmGeneratorTarget::SourceFileTypeResource) {
  1278. resourceFiles.push_back(xsf);
  1279. } else if (!sourceFile->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  1280. !gtgt->IsSourceFilePartOfUnityBatch(
  1281. sourceFile->ResolveFullPath())) {
  1282. // Include this file in the build if it has a known language
  1283. // and has not been listed as an ignored extension for this
  1284. // generator.
  1285. if (!this->CurrentLocalGenerator->GetSourceFileLanguage(*sourceFile)
  1286. .empty() &&
  1287. !this->IgnoreFile(sourceFile->GetExtension().c_str())) {
  1288. sourceFiles.push_back(xsf);
  1289. }
  1290. }
  1291. }
  1292. // some build phases only apply to bundles and/or frameworks
  1293. bool isFrameworkTarget = gtgt->IsFrameworkOnApple();
  1294. bool isBundleTarget = gtgt->GetPropertyAsBool("MACOSX_BUNDLE");
  1295. bool isCFBundleTarget = gtgt->IsCFBundleOnApple();
  1296. cmXCodeObject* buildFiles = nullptr;
  1297. // create source build phase
  1298. cmXCodeObject* sourceBuildPhase = nullptr;
  1299. if (!sourceFiles.empty()) {
  1300. sourceBuildPhase = this->CreateObject(cmXCodeObject::PBXSourcesBuildPhase);
  1301. sourceBuildPhase->SetComment("Sources");
  1302. sourceBuildPhase->AddAttribute("buildActionMask",
  1303. this->CreateString("2147483647"));
  1304. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1305. for (auto& sourceFile : sourceFiles) {
  1306. buildFiles->AddObject(sourceFile);
  1307. }
  1308. sourceBuildPhase->AddAttribute("files", buildFiles);
  1309. sourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1310. this->CreateString("0"));
  1311. }
  1312. // create header build phase - only for framework targets
  1313. cmXCodeObject* headerBuildPhase = nullptr;
  1314. if (!headerFiles.empty() && isFrameworkTarget) {
  1315. headerBuildPhase = this->CreateObject(cmXCodeObject::PBXHeadersBuildPhase);
  1316. headerBuildPhase->SetComment("Headers");
  1317. headerBuildPhase->AddAttribute("buildActionMask",
  1318. this->CreateString("2147483647"));
  1319. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1320. for (auto& headerFile : headerFiles) {
  1321. buildFiles->AddObject(headerFile);
  1322. }
  1323. headerBuildPhase->AddAttribute("files", buildFiles);
  1324. headerBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1325. this->CreateString("0"));
  1326. }
  1327. // create resource build phase - only for framework or bundle targets
  1328. cmXCodeObject* resourceBuildPhase = nullptr;
  1329. if (!resourceFiles.empty() &&
  1330. (isFrameworkTarget || isBundleTarget || isCFBundleTarget)) {
  1331. resourceBuildPhase =
  1332. this->CreateObject(cmXCodeObject::PBXResourcesBuildPhase);
  1333. resourceBuildPhase->SetComment("Resources");
  1334. resourceBuildPhase->AddAttribute("buildActionMask",
  1335. this->CreateString("2147483647"));
  1336. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1337. for (auto& resourceFile : resourceFiles) {
  1338. buildFiles->AddObject(resourceFile);
  1339. }
  1340. resourceBuildPhase->AddAttribute("files", buildFiles);
  1341. resourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1342. this->CreateString("0"));
  1343. }
  1344. // create vector of "non-resource content file" build phases - only for
  1345. // framework or bundle targets
  1346. std::vector<cmXCodeObject*> contentBuildPhases;
  1347. if (isFrameworkTarget || isBundleTarget || isCFBundleTarget) {
  1348. using mapOfVectorOfSourceFiles =
  1349. std::map<std::string, std::vector<cmSourceFile*>>;
  1350. mapOfVectorOfSourceFiles bundleFiles;
  1351. for (auto sourceFile : commonSourceFiles) {
  1352. cmGeneratorTarget::SourceFileFlags tsFlags =
  1353. gtgt->GetTargetSourceFileFlags(sourceFile);
  1354. if (tsFlags.Type == cmGeneratorTarget::SourceFileTypeMacContent) {
  1355. bundleFiles[tsFlags.MacFolder].push_back(sourceFile);
  1356. }
  1357. }
  1358. for (auto const& keySources : bundleFiles) {
  1359. cmXCodeObject* copyFilesBuildPhase =
  1360. this->CreateObject(cmXCodeObject::PBXCopyFilesBuildPhase);
  1361. copyFilesBuildPhase->SetComment("Copy files");
  1362. copyFilesBuildPhase->AddAttribute("buildActionMask",
  1363. this->CreateString("2147483647"));
  1364. copyFilesBuildPhase->AddAttribute("dstSubfolderSpec",
  1365. this->CreateString("6"));
  1366. std::ostringstream ostr;
  1367. if (gtgt->IsFrameworkOnApple()) {
  1368. // dstPath in frameworks is relative to Versions/<version>
  1369. ostr << keySources.first;
  1370. } else if (keySources.first != "MacOS") {
  1371. if (gtgt->Target->GetMakefile()->PlatformIsAppleEmbedded()) {
  1372. ostr << keySources.first;
  1373. } else {
  1374. // dstPath in bundles is relative to Contents/MacOS
  1375. ostr << "../" << keySources.first;
  1376. }
  1377. }
  1378. copyFilesBuildPhase->AddAttribute("dstPath",
  1379. this->CreateString(ostr.str()));
  1380. copyFilesBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1381. this->CreateString("0"));
  1382. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1383. copyFilesBuildPhase->AddAttribute("files", buildFiles);
  1384. for (auto sourceFile : keySources.second) {
  1385. cmXCodeObject* xsf = this->CreateXCodeSourceFile(
  1386. this->CurrentLocalGenerator, sourceFile, gtgt);
  1387. buildFiles->AddObject(xsf);
  1388. }
  1389. contentBuildPhases.push_back(copyFilesBuildPhase);
  1390. }
  1391. }
  1392. // create vector of "resource content file" build phases - only for
  1393. // framework or bundle targets
  1394. if (isFrameworkTarget || isBundleTarget || isCFBundleTarget) {
  1395. using mapOfVectorOfSourceFiles =
  1396. std::map<std::string, std::vector<cmSourceFile*>>;
  1397. mapOfVectorOfSourceFiles bundleFiles;
  1398. for (auto sourceFile : commonSourceFiles) {
  1399. cmGeneratorTarget::SourceFileFlags tsFlags =
  1400. gtgt->GetTargetSourceFileFlags(sourceFile);
  1401. if (tsFlags.Type == cmGeneratorTarget::SourceFileTypeDeepResource) {
  1402. bundleFiles[tsFlags.MacFolder].push_back(sourceFile);
  1403. }
  1404. }
  1405. for (auto const& keySources : bundleFiles) {
  1406. cmXCodeObject* copyFilesBuildPhase =
  1407. this->CreateObject(cmXCodeObject::PBXCopyFilesBuildPhase);
  1408. copyFilesBuildPhase->SetComment("Copy files");
  1409. copyFilesBuildPhase->AddAttribute("buildActionMask",
  1410. this->CreateString("2147483647"));
  1411. copyFilesBuildPhase->AddAttribute("dstSubfolderSpec",
  1412. this->CreateString("7"));
  1413. copyFilesBuildPhase->AddAttribute("dstPath",
  1414. this->CreateString(keySources.first));
  1415. copyFilesBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1416. this->CreateString("0"));
  1417. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1418. copyFilesBuildPhase->AddAttribute("files", buildFiles);
  1419. for (auto sourceFile : keySources.second) {
  1420. cmXCodeObject* xsf = this->CreateXCodeSourceFile(
  1421. this->CurrentLocalGenerator, sourceFile, gtgt);
  1422. buildFiles->AddObject(xsf);
  1423. }
  1424. contentBuildPhases.push_back(copyFilesBuildPhase);
  1425. }
  1426. }
  1427. // Always create Link Binary With Libraries build phase
  1428. cmXCodeObject* frameworkBuildPhase = nullptr;
  1429. frameworkBuildPhase =
  1430. this->CreateObject(cmXCodeObject::PBXFrameworksBuildPhase);
  1431. frameworkBuildPhase->SetComment("Frameworks");
  1432. frameworkBuildPhase->AddAttribute("buildActionMask",
  1433. this->CreateString("2147483647"));
  1434. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1435. frameworkBuildPhase->AddAttribute("files", buildFiles);
  1436. // Add all collected .o files to this build phase
  1437. for (auto& externalObjFile : externalObjFiles) {
  1438. buildFiles->AddObject(externalObjFile);
  1439. }
  1440. frameworkBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1441. this->CreateString("0"));
  1442. // create list of build phases and create the Xcode target
  1443. cmXCodeObject* buildPhases = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1444. this->CreateCustomCommands(buildPhases, sourceBuildPhase, headerBuildPhase,
  1445. resourceBuildPhase, contentBuildPhases,
  1446. frameworkBuildPhase, gtgt);
  1447. targets.push_back(this->CreateXCodeTarget(gtgt, buildPhases));
  1448. return true;
  1449. }
  1450. void cmGlobalXCodeGenerator::ForceLinkerLanguages()
  1451. {
  1452. for (const auto& localGenerator : this->LocalGenerators) {
  1453. // All targets depend on the build-system check target.
  1454. for (const auto& tgt : localGenerator->GetGeneratorTargets()) {
  1455. // This makes sure all targets link using the proper language.
  1456. this->ForceLinkerLanguage(tgt.get());
  1457. }
  1458. }
  1459. }
  1460. void cmGlobalXCodeGenerator::ForceLinkerLanguage(cmGeneratorTarget* gtgt)
  1461. {
  1462. // This matters only for targets that link.
  1463. if (gtgt->GetType() != cmStateEnums::EXECUTABLE &&
  1464. gtgt->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1465. gtgt->GetType() != cmStateEnums::MODULE_LIBRARY) {
  1466. return;
  1467. }
  1468. std::string llang = gtgt->GetLinkerLanguage("NOCONFIG");
  1469. if (llang.empty()) {
  1470. return;
  1471. }
  1472. // If the language is compiled as a source trust Xcode to link with it.
  1473. for (auto const& Language :
  1474. gtgt->GetLinkImplementation("NOCONFIG")->Languages) {
  1475. if (Language == llang) {
  1476. return;
  1477. }
  1478. }
  1479. // Add an empty source file to the target that compiles with the
  1480. // linker language. This should convince Xcode to choose the proper
  1481. // language.
  1482. cmMakefile* mf = gtgt->Target->GetMakefile();
  1483. std::string fname = cmStrCat(
  1484. gtgt->GetLocalGenerator()->GetCurrentBinaryDirectory(), "/CMakeFiles/",
  1485. gtgt->GetName(), "-CMakeForceLinker.", cmSystemTools::LowerCase(llang));
  1486. {
  1487. cmGeneratedFileStream fout(fname);
  1488. fout << "\n";
  1489. }
  1490. if (cmSourceFile* sf = mf->GetOrCreateSource(fname)) {
  1491. sf->SetProperty("LANGUAGE", llang.c_str());
  1492. gtgt->AddSource(fname);
  1493. }
  1494. }
  1495. bool cmGlobalXCodeGenerator::IsHeaderFile(cmSourceFile* sf)
  1496. {
  1497. return cm::contains(this->CMakeInstance->GetHeaderExtensions(),
  1498. sf->GetExtension());
  1499. }
  1500. cmXCodeObject* cmGlobalXCodeGenerator::CreateLegacyRunScriptBuildPhase(
  1501. const char* name, const char* name2, cmGeneratorTarget* target,
  1502. const std::vector<cmCustomCommand>& commands)
  1503. {
  1504. if (commands.empty() && strcmp(name, "CMake ReRun") != 0) {
  1505. return nullptr;
  1506. }
  1507. cmXCodeObject* buildPhase =
  1508. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1509. buildPhase->AddAttribute("buildActionMask",
  1510. this->CreateString("2147483647"));
  1511. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1512. buildPhase->AddAttribute("files", buildFiles);
  1513. buildPhase->AddAttribute("name", this->CreateString(name));
  1514. buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1515. this->CreateString("0"));
  1516. buildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  1517. this->AddCommandsToBuildPhase(buildPhase, target, commands, name2);
  1518. return buildPhase;
  1519. }
  1520. void cmGlobalXCodeGenerator::CreateCustomCommands(
  1521. cmXCodeObject* buildPhases, cmXCodeObject* sourceBuildPhase,
  1522. cmXCodeObject* headerBuildPhase, cmXCodeObject* resourceBuildPhase,
  1523. std::vector<cmXCodeObject*> const& contentBuildPhases,
  1524. cmXCodeObject* frameworkBuildPhase, cmGeneratorTarget* gtgt)
  1525. {
  1526. std::vector<cmCustomCommand> const& prebuild = gtgt->GetPreBuildCommands();
  1527. std::vector<cmCustomCommand> const& prelink = gtgt->GetPreLinkCommands();
  1528. std::vector<cmCustomCommand> postbuild = gtgt->GetPostBuildCommands();
  1529. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY &&
  1530. !gtgt->IsFrameworkOnApple()) {
  1531. std::string str_file = cmStrCat("$<TARGET_FILE:", gtgt->GetName(), '>');
  1532. std::string str_so_file =
  1533. cmStrCat("$<TARGET_SONAME_FILE:", gtgt->GetName(), '>');
  1534. std::string str_link_file =
  1535. cmStrCat("$<TARGET_LINKER_FILE:", gtgt->GetName(), '>');
  1536. bool stdPipesUTF8 = true;
  1537. cmCustomCommandLines cmd = cmMakeSingleCommandLine(
  1538. { cmSystemTools::GetCMakeCommand(), "-E", "cmake_symlink_library",
  1539. str_file, str_so_file, str_link_file });
  1540. cmCustomCommand command(
  1541. std::vector<std::string>(), std::vector<std::string>(),
  1542. std::vector<std::string>(), cmd, this->CurrentMakefile->GetBacktrace(),
  1543. "Creating symlinks", "", stdPipesUTF8);
  1544. postbuild.push_back(std::move(command));
  1545. }
  1546. cmXCodeObject* legacyCustomCommandsBuildPhase = nullptr;
  1547. cmXCodeObject* preBuildPhase = nullptr;
  1548. cmXCodeObject* preLinkPhase = nullptr;
  1549. cmXCodeObject* postBuildPhase = nullptr;
  1550. if (this->XcodeBuildSystem >= BuildSystem::Twelve) {
  1551. // create prebuild phase
  1552. preBuildPhase =
  1553. this->CreateRunScriptBuildPhase("CMake PreBuild Rules", prebuild);
  1554. // create prelink phase
  1555. preLinkPhase =
  1556. this->CreateRunScriptBuildPhase("CMake PreLink Rules", prelink);
  1557. // create postbuild phase
  1558. postBuildPhase =
  1559. this->CreateRunScriptBuildPhase("CMake PostBuild Rules", postbuild);
  1560. } else {
  1561. std::vector<cmSourceFile*> classes;
  1562. if (!gtgt->GetConfigCommonSourceFiles(classes)) {
  1563. return;
  1564. }
  1565. // add all the sources
  1566. std::vector<cmCustomCommand> commands;
  1567. auto& visited = this->CommandsVisited[gtgt];
  1568. for (auto sourceFile : classes) {
  1569. if (sourceFile->GetCustomCommand() &&
  1570. visited.insert(sourceFile).second) {
  1571. commands.push_back(*sourceFile->GetCustomCommand());
  1572. if (this->XcodeBuildSystem >= BuildSystem::Twelve) {
  1573. this->CustomCommandRoots[sourceFile].insert(gtgt);
  1574. }
  1575. }
  1576. }
  1577. // create custom commands phase
  1578. legacyCustomCommandsBuildPhase = this->CreateLegacyRunScriptBuildPhase(
  1579. "CMake Rules", "cmakeRulesBuildPhase", gtgt, commands);
  1580. // create prebuild phase
  1581. preBuildPhase = this->CreateLegacyRunScriptBuildPhase(
  1582. "CMake PreBuild Rules", "preBuildCommands", gtgt, prebuild);
  1583. // create prelink phase
  1584. preLinkPhase = this->CreateLegacyRunScriptBuildPhase(
  1585. "CMake PreLink Rules", "preLinkCommands", gtgt, prelink);
  1586. // create postbuild phase
  1587. postBuildPhase = this->CreateLegacyRunScriptBuildPhase(
  1588. "CMake PostBuild Rules", "postBuildPhase", gtgt, postbuild);
  1589. }
  1590. // The order here is the order they will be built in.
  1591. // The order "headers, resources, sources" mimics a native project generated
  1592. // from an xcode template...
  1593. //
  1594. if (preBuildPhase) {
  1595. buildPhases->AddObject(preBuildPhase);
  1596. }
  1597. if (legacyCustomCommandsBuildPhase) {
  1598. buildPhases->AddObject(legacyCustomCommandsBuildPhase);
  1599. }
  1600. if (this->XcodeBuildSystem >= BuildSystem::Twelve) {
  1601. this->CreateRunScriptBuildPhases(buildPhases, gtgt);
  1602. }
  1603. if (headerBuildPhase) {
  1604. buildPhases->AddObject(headerBuildPhase);
  1605. }
  1606. if (resourceBuildPhase) {
  1607. buildPhases->AddObject(resourceBuildPhase);
  1608. }
  1609. for (auto obj : contentBuildPhases) {
  1610. buildPhases->AddObject(obj);
  1611. }
  1612. if (sourceBuildPhase) {
  1613. buildPhases->AddObject(sourceBuildPhase);
  1614. }
  1615. if (preLinkPhase) {
  1616. buildPhases->AddObject(preLinkPhase);
  1617. }
  1618. if (frameworkBuildPhase) {
  1619. buildPhases->AddObject(frameworkBuildPhase);
  1620. }
  1621. if (postBuildPhase) {
  1622. buildPhases->AddObject(postBuildPhase);
  1623. }
  1624. }
  1625. void cmGlobalXCodeGenerator::CreateRunScriptBuildPhases(
  1626. cmXCodeObject* buildPhases, cmGeneratorTarget const* gt)
  1627. {
  1628. std::vector<cmSourceFile*> sources;
  1629. if (!gt->GetConfigCommonSourceFiles(sources)) {
  1630. return;
  1631. }
  1632. auto& visited = this->CommandsVisited[gt];
  1633. for (auto sf : sources) {
  1634. this->CreateRunScriptBuildPhases(buildPhases, sf, gt, visited);
  1635. }
  1636. }
  1637. void cmGlobalXCodeGenerator::CreateRunScriptBuildPhases(
  1638. cmXCodeObject* buildPhases, cmSourceFile const* sf,
  1639. cmGeneratorTarget const* gt, std::set<cmSourceFile const*>& visited)
  1640. {
  1641. cmCustomCommand const* cc = sf->GetCustomCommand();
  1642. if (cc && visited.insert(sf).second) {
  1643. this->CustomCommandRoots[sf].insert(gt);
  1644. if (std::vector<cmSourceFile*> const* depends = gt->GetSourceDepends(sf)) {
  1645. for (cmSourceFile const* di : *depends) {
  1646. this->CreateRunScriptBuildPhases(buildPhases, di, gt, visited);
  1647. }
  1648. }
  1649. cmXCodeObject* buildPhase = this->CreateRunScriptBuildPhase(sf, gt, *cc);
  1650. buildPhases->AddObject(buildPhase);
  1651. }
  1652. }
  1653. cmXCodeObject* cmGlobalXCodeGenerator::CreateRunScriptBuildPhase(
  1654. cmSourceFile const* sf, cmGeneratorTarget const* gt,
  1655. cmCustomCommand const& cc)
  1656. {
  1657. std::set<std::string> allConfigInputs;
  1658. std::set<std::string> allConfigOutputs;
  1659. std::string shellScript = "set -e\n";
  1660. for (std::string const& configName : this->CurrentConfigurationTypes) {
  1661. cmCustomCommandGenerator ccg(cc, configName, this->CurrentLocalGenerator);
  1662. std::vector<std::string> realDepends;
  1663. realDepends.reserve(ccg.GetDepends().size());
  1664. for (auto const& d : ccg.GetDepends()) {
  1665. std::string dep;
  1666. if (this->CurrentLocalGenerator->GetRealDependency(d, configName, dep)) {
  1667. realDepends.emplace_back(std::move(dep));
  1668. }
  1669. }
  1670. allConfigInputs.insert(realDepends.begin(), realDepends.end());
  1671. allConfigOutputs.insert(ccg.GetByproducts().begin(),
  1672. ccg.GetByproducts().end());
  1673. allConfigOutputs.insert(ccg.GetOutputs().begin(), ccg.GetOutputs().end());
  1674. shellScript =
  1675. cmStrCat(shellScript, R"(if test "$CONFIGURATION" = ")", configName,
  1676. "\"; then :\n", this->ConstructScript(ccg), "fi\n");
  1677. }
  1678. cmXCodeObject* buildPhase =
  1679. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1680. buildPhase->AddAttribute("buildActionMask",
  1681. this->CreateString("2147483647"));
  1682. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1683. buildPhase->AddAttribute("files", buildFiles);
  1684. {
  1685. std::string name;
  1686. if (!allConfigOutputs.empty()) {
  1687. name = cmStrCat("Generate ",
  1688. this->RelativeToBinary(*allConfigOutputs.begin()));
  1689. } else {
  1690. name = sf->GetLocation().GetName();
  1691. }
  1692. buildPhase->AddAttribute("name", this->CreateString(name));
  1693. }
  1694. buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1695. this->CreateString("0"));
  1696. buildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  1697. buildPhase->AddAttribute("shellScript", this->CreateString(shellScript));
  1698. buildPhase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  1699. bool symbolic = false;
  1700. {
  1701. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1702. for (std::string const& i : allConfigInputs) {
  1703. inputPaths->AddUniqueObject(this->CreateString(i));
  1704. if (!symbolic) {
  1705. if (cmSourceFile* isf =
  1706. gt->GetLocalGenerator()->GetMakefile()->GetSource(
  1707. i, cmSourceFileLocationKind::Known)) {
  1708. symbolic = isf->GetPropertyAsBool("SYMBOLIC");
  1709. }
  1710. }
  1711. }
  1712. buildPhase->AddAttribute("inputPaths", inputPaths);
  1713. }
  1714. {
  1715. cmXCodeObject* outputPaths =
  1716. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1717. for (std::string const& o : allConfigOutputs) {
  1718. outputPaths->AddUniqueObject(this->CreateString(o));
  1719. if (!symbolic) {
  1720. if (cmSourceFile* osf =
  1721. gt->GetLocalGenerator()->GetMakefile()->GetSource(
  1722. o, cmSourceFileLocationKind::Known)) {
  1723. symbolic = osf->GetPropertyAsBool("SYMBOLIC");
  1724. }
  1725. }
  1726. }
  1727. buildPhase->AddAttribute("outputPaths", outputPaths);
  1728. }
  1729. if (symbolic) {
  1730. buildPhase->AddAttribute("alwaysOutOfDate", this->CreateString("1"));
  1731. }
  1732. return buildPhase;
  1733. }
  1734. cmXCodeObject* cmGlobalXCodeGenerator::CreateRunScriptBuildPhase(
  1735. std::string const& name, std::vector<cmCustomCommand> const& commands)
  1736. {
  1737. if (commands.empty()) {
  1738. return nullptr;
  1739. }
  1740. std::set<std::string> allConfigOutputs;
  1741. std::string shellScript = "set -e\n";
  1742. for (std::string const& configName : this->CurrentConfigurationTypes) {
  1743. shellScript = cmStrCat(shellScript, R"(if test "$CONFIGURATION" = ")",
  1744. configName, "\"; then :\n");
  1745. for (cmCustomCommand const& cc : commands) {
  1746. cmCustomCommandGenerator ccg(cc, configName,
  1747. this->CurrentLocalGenerator);
  1748. shellScript = cmStrCat(shellScript, this->ConstructScript(ccg));
  1749. allConfigOutputs.insert(ccg.GetByproducts().begin(),
  1750. ccg.GetByproducts().end());
  1751. }
  1752. shellScript = cmStrCat(shellScript, "fi\n");
  1753. }
  1754. cmXCodeObject* buildPhase =
  1755. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1756. buildPhase->AddAttribute("buildActionMask",
  1757. this->CreateString("2147483647"));
  1758. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1759. buildPhase->AddAttribute("files", buildFiles);
  1760. buildPhase->AddAttribute("name", this->CreateString(name));
  1761. buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1762. this->CreateString("0"));
  1763. buildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  1764. buildPhase->AddAttribute("shellScript", this->CreateString(shellScript));
  1765. buildPhase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  1766. {
  1767. cmXCodeObject* outputPaths =
  1768. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1769. for (std::string const& o : allConfigOutputs) {
  1770. outputPaths->AddUniqueObject(this->CreateString(o));
  1771. }
  1772. buildPhase->AddAttribute("outputPaths", outputPaths);
  1773. }
  1774. buildPhase->AddAttribute("alwaysOutOfDate", this->CreateString("1"));
  1775. return buildPhase;
  1776. }
  1777. namespace {
  1778. void ReplaceScriptVars(std::string& cmd)
  1779. {
  1780. cmSystemTools::ReplaceString(cmd, "$(CONFIGURATION)", "$CONFIGURATION");
  1781. cmSystemTools::ReplaceString(cmd, "$(EFFECTIVE_PLATFORM_NAME)",
  1782. "$EFFECTIVE_PLATFORM_NAME");
  1783. }
  1784. }
  1785. std::string cmGlobalXCodeGenerator::ConstructScript(
  1786. cmCustomCommandGenerator const& ccg)
  1787. {
  1788. std::string script;
  1789. cmLocalGenerator* lg = this->CurrentLocalGenerator;
  1790. std::string wd = ccg.GetWorkingDirectory();
  1791. if (wd.empty()) {
  1792. wd = lg->GetCurrentBinaryDirectory();
  1793. }
  1794. wd = lg->ConvertToOutputFormat(wd, cmOutputConverter::SHELL);
  1795. ReplaceScriptVars(wd);
  1796. script = cmStrCat(script, " cd ", wd, "\n");
  1797. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  1798. std::string cmd = ccg.GetCommand(c);
  1799. if (cmd.empty()) {
  1800. continue;
  1801. }
  1802. cmSystemTools::ReplaceString(cmd, "/./", "/");
  1803. cmd = lg->ConvertToOutputFormat(cmd, cmOutputConverter::SHELL);
  1804. ccg.AppendArguments(c, cmd);
  1805. ReplaceScriptVars(cmd);
  1806. script = cmStrCat(script, " ", cmd, '\n');
  1807. }
  1808. return script;
  1809. }
  1810. // This function removes each occurrence of the flag and returns the last one
  1811. // (i.e., the dominant flag in GCC)
  1812. std::string cmGlobalXCodeGenerator::ExtractFlag(const char* flag,
  1813. std::string& flags)
  1814. {
  1815. std::string retFlag;
  1816. std::string::size_type lastOccurancePos = flags.rfind(flag);
  1817. bool saved = false;
  1818. while (lastOccurancePos != std::string::npos) {
  1819. // increment pos, we use lastOccurancePos to reduce search space on next
  1820. // inc
  1821. std::string::size_type pos = lastOccurancePos;
  1822. if (pos == 0 || flags[pos - 1] == ' ') {
  1823. while (pos < flags.size() && flags[pos] != ' ') {
  1824. if (!saved) {
  1825. retFlag += flags[pos];
  1826. }
  1827. flags[pos] = ' ';
  1828. pos++;
  1829. }
  1830. saved = true;
  1831. }
  1832. // decrement lastOccurancePos while making sure we don't loop around
  1833. // and become a very large positive number since size_type is unsigned
  1834. lastOccurancePos = lastOccurancePos == 0 ? 0 : lastOccurancePos - 1;
  1835. lastOccurancePos = flags.rfind(flag, lastOccurancePos);
  1836. }
  1837. return retFlag;
  1838. }
  1839. // This function removes each matching occurrence of the expression and
  1840. // returns the last one (i.e., the dominant flag in GCC)
  1841. std::string cmGlobalXCodeGenerator::ExtractFlagRegex(const char* exp,
  1842. int matchIndex,
  1843. std::string& flags)
  1844. {
  1845. std::string retFlag;
  1846. cmsys::RegularExpression regex(exp);
  1847. assert(regex.is_valid());
  1848. if (!regex.is_valid()) {
  1849. return retFlag;
  1850. }
  1851. std::string::size_type offset = 0;
  1852. while (regex.find(&flags[offset])) {
  1853. const std::string::size_type startPos = offset + regex.start(matchIndex);
  1854. const std::string::size_type endPos = offset + regex.end(matchIndex);
  1855. const std::string::size_type size = endPos - startPos;
  1856. offset = startPos + 1;
  1857. retFlag.assign(flags, startPos, size);
  1858. flags.replace(startPos, size, size, ' ');
  1859. }
  1860. return retFlag;
  1861. }
  1862. //----------------------------------------------------------------------------
  1863. // This function strips off Xcode attributes that do not target the current
  1864. // configuration
  1865. void cmGlobalXCodeGenerator::FilterConfigurationAttribute(
  1866. std::string const& configName, std::string& attribute)
  1867. {
  1868. // Handle [variant=<config>] condition explicitly here.
  1869. std::string::size_type beginVariant = attribute.find("[variant=");
  1870. if (beginVariant == std::string::npos) {
  1871. // There is no variant in this attribute.
  1872. return;
  1873. }
  1874. std::string::size_type endVariant = attribute.find(']', beginVariant + 9);
  1875. if (endVariant == std::string::npos) {
  1876. // There is no terminating bracket.
  1877. return;
  1878. }
  1879. // Compare the variant to the configuration.
  1880. std::string variant =
  1881. attribute.substr(beginVariant + 9, endVariant - beginVariant - 9);
  1882. if (variant == configName) {
  1883. // The variant matches the configuration so use this
  1884. // attribute but drop the [variant=<config>] condition.
  1885. attribute.erase(beginVariant, endVariant - beginVariant + 1);
  1886. } else {
  1887. // The variant does not match the configuration so
  1888. // do not use this attribute.
  1889. attribute.clear();
  1890. }
  1891. }
  1892. void cmGlobalXCodeGenerator::AddCommandsToBuildPhase(
  1893. cmXCodeObject* buildphase, cmGeneratorTarget* target,
  1894. std::vector<cmCustomCommand> const& commands, const char* name)
  1895. {
  1896. std::string dir = cmStrCat(
  1897. this->CurrentLocalGenerator->GetCurrentBinaryDirectory(), "/CMakeScripts");
  1898. cmSystemTools::MakeDirectory(dir);
  1899. std::string makefile =
  1900. cmStrCat(dir, '/', target->GetName(), '_', name, ".make");
  1901. for (const auto& currentConfig : this->CurrentConfigurationTypes) {
  1902. this->CreateCustomRulesMakefile(makefile.c_str(), target, commands,
  1903. currentConfig);
  1904. }
  1905. std::string cdir = this->CurrentLocalGenerator->GetCurrentBinaryDirectory();
  1906. cdir = this->ConvertToRelativeForMake(cdir);
  1907. std::string makecmd =
  1908. cmStrCat("make -C ", cdir, " -f ",
  1909. this->ConvertToRelativeForMake((makefile + "$CONFIGURATION")),
  1910. " OBJDIR=$(basename \"$OBJECT_FILE_DIR_normal\") all");
  1911. buildphase->AddAttribute("shellScript", this->CreateString(makecmd));
  1912. buildphase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  1913. }
  1914. void cmGlobalXCodeGenerator::CreateCustomRulesMakefile(
  1915. const char* makefileBasename, cmGeneratorTarget* target,
  1916. std::vector<cmCustomCommand> const& commands, const std::string& configName)
  1917. {
  1918. std::string makefileName = cmStrCat(makefileBasename, configName);
  1919. cmGeneratedFileStream makefileStream(makefileName);
  1920. if (!makefileStream) {
  1921. return;
  1922. }
  1923. makefileStream.SetCopyIfDifferent(true);
  1924. makefileStream << "# Generated by CMake, DO NOT EDIT\n";
  1925. makefileStream << "# Custom rules for " << target->GetName() << "\n";
  1926. // disable the implicit rules
  1927. makefileStream << ".SUFFIXES: "
  1928. << "\n";
  1929. // have all depend on all outputs
  1930. makefileStream << "all: ";
  1931. std::map<const cmCustomCommand*, std::string> tname;
  1932. int count = 0;
  1933. for (auto const& command : commands) {
  1934. cmCustomCommandGenerator ccg(command, configName,
  1935. this->CurrentLocalGenerator);
  1936. if (ccg.GetNumberOfCommands() > 0) {
  1937. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1938. if (!outputs.empty()) {
  1939. for (auto const& output : outputs) {
  1940. makefileStream << "\\\n\t" << this->ConvertToRelativeForMake(output);
  1941. }
  1942. } else {
  1943. std::ostringstream str;
  1944. str << "_buildpart_" << count++;
  1945. tname[&ccg.GetCC()] = target->GetName() + str.str();
  1946. makefileStream << "\\\n\t" << tname[&ccg.GetCC()];
  1947. }
  1948. }
  1949. }
  1950. makefileStream << "\n\n";
  1951. for (auto const& command : commands) {
  1952. cmCustomCommandGenerator ccg(command, configName,
  1953. this->CurrentLocalGenerator);
  1954. std::vector<std::string> realDepends;
  1955. realDepends.reserve(ccg.GetDepends().size());
  1956. for (auto const& d : ccg.GetDepends()) {
  1957. std::string dep;
  1958. if (this->CurrentLocalGenerator->GetRealDependency(d, configName, dep)) {
  1959. realDepends.emplace_back(std::move(dep));
  1960. }
  1961. }
  1962. if (ccg.GetNumberOfCommands() > 0) {
  1963. makefileStream << "\n";
  1964. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1965. if (!outputs.empty()) {
  1966. // There is at least one output, start the rule for it
  1967. const char* sep = "";
  1968. for (auto const& output : outputs) {
  1969. makefileStream << sep << this->ConvertToRelativeForMake(output);
  1970. sep = " ";
  1971. }
  1972. makefileStream << ": ";
  1973. } else {
  1974. // There are no outputs. Use the generated force rule name.
  1975. makefileStream << tname[&ccg.GetCC()] << ": ";
  1976. }
  1977. for (auto const& dep : realDepends) {
  1978. makefileStream << "\\\n" << this->ConvertToRelativeForMake(dep);
  1979. }
  1980. makefileStream << "\n";
  1981. if (const char* comment = ccg.GetComment()) {
  1982. std::string echo_cmd =
  1983. cmStrCat("echo ",
  1984. (this->CurrentLocalGenerator->EscapeForShell(
  1985. comment, ccg.GetCC().GetEscapeAllowMakeVars())));
  1986. makefileStream << "\t" << echo_cmd << "\n";
  1987. }
  1988. // Add each command line to the set of commands.
  1989. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  1990. // Build the command line in a single string.
  1991. std::string cmd2 = ccg.GetCommand(c);
  1992. cmSystemTools::ReplaceString(cmd2, "/./", "/");
  1993. cmd2 = this->ConvertToRelativeForMake(cmd2);
  1994. std::string cmd;
  1995. std::string wd = ccg.GetWorkingDirectory();
  1996. if (!wd.empty()) {
  1997. cmd += "cd ";
  1998. cmd += this->ConvertToRelativeForMake(wd);
  1999. cmd += " && ";
  2000. }
  2001. cmd += cmd2;
  2002. ccg.AppendArguments(c, cmd);
  2003. makefileStream << "\t" << cmd << "\n";
  2004. }
  2005. // Symbolic inputs are not expected to exist, so add dummy rules.
  2006. for (auto const& dep : realDepends) {
  2007. if (cmSourceFile* dsf =
  2008. target->GetLocalGenerator()->GetMakefile()->GetSource(
  2009. dep, cmSourceFileLocationKind::Known)) {
  2010. if (dsf->GetPropertyAsBool("SYMBOLIC")) {
  2011. makefileStream << this->ConvertToRelativeForMake(dep) << ":\n";
  2012. }
  2013. }
  2014. }
  2015. }
  2016. }
  2017. }
  2018. void cmGlobalXCodeGenerator::AddPositionIndependentLinkAttribute(
  2019. cmGeneratorTarget* target, cmXCodeObject* buildSettings,
  2020. const std::string& configName)
  2021. {
  2022. // For now, only EXECUTABLE is concerned
  2023. if (target->GetType() != cmStateEnums::EXECUTABLE) {
  2024. return;
  2025. }
  2026. const char* PICValue = target->GetLinkPIEProperty(configName);
  2027. if (PICValue == nullptr) {
  2028. // POSITION_INDEPENDENT_CODE is not set
  2029. return;
  2030. }
  2031. buildSettings->AddAttribute(
  2032. "LD_NO_PIE", this->CreateString(cmIsOn(PICValue) ? "NO" : "YES"));
  2033. }
  2034. void cmGlobalXCodeGenerator::CreateBuildSettings(cmGeneratorTarget* gtgt,
  2035. cmXCodeObject* buildSettings,
  2036. const std::string& configName)
  2037. {
  2038. if (!gtgt->IsInBuildSystem()) {
  2039. return;
  2040. }
  2041. std::string defFlags;
  2042. bool shared = ((gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) ||
  2043. (gtgt->GetType() == cmStateEnums::MODULE_LIBRARY));
  2044. bool binary = ((gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) ||
  2045. (gtgt->GetType() == cmStateEnums::STATIC_LIBRARY) ||
  2046. (gtgt->GetType() == cmStateEnums::EXECUTABLE) || shared);
  2047. // Compute the compilation flags for each language.
  2048. std::set<std::string> languages;
  2049. gtgt->GetLanguages(languages, configName);
  2050. std::map<std::string, std::string> cflags;
  2051. for (auto const& lang : languages) {
  2052. std::string& flags = cflags[lang];
  2053. // Add language-specific flags.
  2054. this->CurrentLocalGenerator->AddLanguageFlags(flags, gtgt, lang,
  2055. configName);
  2056. if (gtgt->IsIPOEnabled(lang, configName)) {
  2057. this->CurrentLocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  2058. }
  2059. // Add shared-library flags if needed.
  2060. this->CurrentLocalGenerator->AddCMP0018Flags(flags, gtgt, lang,
  2061. configName);
  2062. this->CurrentLocalGenerator->AddVisibilityPresetFlags(flags, gtgt, lang);
  2063. this->CurrentLocalGenerator->AddCompileOptions(flags, gtgt, lang,
  2064. configName);
  2065. }
  2066. std::string llang = gtgt->GetLinkerLanguage(configName);
  2067. if (binary && llang.empty()) {
  2068. cmSystemTools::Error(
  2069. "CMake can not determine linker language for target: " +
  2070. gtgt->GetName());
  2071. return;
  2072. }
  2073. std::string const& langForPreprocessor = llang;
  2074. if (gtgt->IsIPOEnabled(llang, configName)) {
  2075. const char* ltoValue =
  2076. this->CurrentMakefile->IsOn("_CMAKE_LTO_THIN") ? "YES_THIN" : "YES";
  2077. buildSettings->AddAttribute("LLVM_LTO", this->CreateString(ltoValue));
  2078. }
  2079. // Handle PIE linker configuration
  2080. this->AddPositionIndependentLinkAttribute(gtgt, buildSettings, configName);
  2081. // Add define flags
  2082. this->CurrentLocalGenerator->AppendFlags(
  2083. defFlags, this->CurrentMakefile->GetDefineFlags());
  2084. // Add preprocessor definitions for this target and configuration.
  2085. BuildObjectListOrString ppDefs(this, true);
  2086. this->AppendDefines(
  2087. ppDefs, "CMAKE_INTDIR=\"$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)\"");
  2088. if (const std::string* exportMacro = gtgt->GetExportMacro()) {
  2089. // Add the export symbol definition for shared library objects.
  2090. this->AppendDefines(ppDefs, exportMacro->c_str());
  2091. }
  2092. std::vector<std::string> targetDefines;
  2093. if (!langForPreprocessor.empty()) {
  2094. gtgt->GetCompileDefinitions(targetDefines, configName,
  2095. langForPreprocessor);
  2096. }
  2097. this->AppendDefines(ppDefs, targetDefines);
  2098. buildSettings->AddAttribute("GCC_PREPROCESSOR_DEFINITIONS",
  2099. ppDefs.CreateList());
  2100. std::string extraLinkOptionsVar;
  2101. std::string extraLinkOptions;
  2102. if (gtgt->GetType() == cmStateEnums::EXECUTABLE) {
  2103. extraLinkOptionsVar = "CMAKE_EXE_LINKER_FLAGS";
  2104. } else if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  2105. extraLinkOptionsVar = "CMAKE_SHARED_LINKER_FLAGS";
  2106. } else if (gtgt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2107. extraLinkOptionsVar = "CMAKE_MODULE_LINKER_FLAGS";
  2108. }
  2109. if (!extraLinkOptionsVar.empty()) {
  2110. this->CurrentLocalGenerator->AddConfigVariableFlags(
  2111. extraLinkOptions, extraLinkOptionsVar, configName);
  2112. }
  2113. if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  2114. gtgt->GetType() == cmStateEnums::STATIC_LIBRARY) {
  2115. this->CurrentLocalGenerator->GetStaticLibraryFlags(
  2116. extraLinkOptions, configName, llang, gtgt);
  2117. } else {
  2118. cmProp targetLinkFlags = gtgt->GetProperty("LINK_FLAGS");
  2119. if (targetLinkFlags) {
  2120. this->CurrentLocalGenerator->AppendFlags(extraLinkOptions,
  2121. *targetLinkFlags);
  2122. }
  2123. if (!configName.empty()) {
  2124. std::string linkFlagsVar =
  2125. cmStrCat("LINK_FLAGS_", cmSystemTools::UpperCase(configName));
  2126. if (cmProp linkFlags = gtgt->GetProperty(linkFlagsVar)) {
  2127. this->CurrentLocalGenerator->AppendFlags(extraLinkOptions, *linkFlags);
  2128. }
  2129. }
  2130. std::vector<std::string> opts;
  2131. gtgt->GetLinkOptions(opts, configName, llang);
  2132. // LINK_OPTIONS are escaped.
  2133. this->CurrentLocalGenerator->AppendCompileOptions(extraLinkOptions, opts);
  2134. }
  2135. // Set target-specific architectures.
  2136. std::vector<std::string> archs;
  2137. gtgt->GetAppleArchs(configName, archs);
  2138. if (!archs.empty()) {
  2139. // Enable ARCHS attribute.
  2140. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH", this->CreateString("NO"));
  2141. // Store ARCHS value.
  2142. if (archs.size() == 1) {
  2143. buildSettings->AddAttribute("ARCHS", this->CreateString(archs[0]));
  2144. } else {
  2145. cmXCodeObject* archObjects =
  2146. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2147. for (auto& arch : archs) {
  2148. archObjects->AddObject(this->CreateString(arch));
  2149. }
  2150. buildSettings->AddAttribute("ARCHS", archObjects);
  2151. }
  2152. }
  2153. // Get the product name components.
  2154. std::string pnprefix;
  2155. std::string pnbase;
  2156. std::string pnsuffix;
  2157. gtgt->GetFullNameComponents(pnprefix, pnbase, pnsuffix, configName);
  2158. cmProp version = gtgt->GetProperty("VERSION");
  2159. cmProp soversion = gtgt->GetProperty("SOVERSION");
  2160. if (!gtgt->HasSOName(configName) || gtgt->IsFrameworkOnApple()) {
  2161. version = nullptr;
  2162. soversion = nullptr;
  2163. }
  2164. if (version && !soversion) {
  2165. soversion = version;
  2166. }
  2167. if (!version && soversion) {
  2168. version = soversion;
  2169. }
  2170. std::string realName = pnbase;
  2171. std::string soName = pnbase;
  2172. if (version && soversion) {
  2173. realName += ".";
  2174. realName += *version;
  2175. soName += ".";
  2176. soName += *soversion;
  2177. }
  2178. // Set attributes to specify the proper name for the target.
  2179. std::string pndir = this->CurrentLocalGenerator->GetCurrentBinaryDirectory();
  2180. if (gtgt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2181. gtgt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2182. gtgt->GetType() == cmStateEnums::MODULE_LIBRARY ||
  2183. gtgt->GetType() == cmStateEnums::EXECUTABLE) {
  2184. if (!gtgt->UsesDefaultOutputDir(configName,
  2185. cmStateEnums::RuntimeBinaryArtifact)) {
  2186. std::string pncdir = gtgt->GetDirectory(configName);
  2187. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  2188. this->CreateString(pncdir));
  2189. }
  2190. if (gtgt->IsFrameworkOnApple() || gtgt->IsCFBundleOnApple()) {
  2191. pnprefix = "";
  2192. }
  2193. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  2194. this->CreateString(pnprefix));
  2195. buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
  2196. this->CreateString(pnsuffix));
  2197. } else if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  2198. pnprefix = "lib";
  2199. pnbase = gtgt->GetName();
  2200. pnsuffix = ".a";
  2201. std::string pncdir = this->GetObjectsDirectory(
  2202. this->CurrentProject, configName, gtgt, OBJECT_LIBRARY_ARTIFACT_DIR);
  2203. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  2204. this->CreateString(pncdir));
  2205. }
  2206. // Store the product name for all target types.
  2207. buildSettings->AddAttribute("PRODUCT_NAME", this->CreateString(realName));
  2208. buildSettings->AddAttribute("SYMROOT", this->CreateString(pndir));
  2209. // Handle settings for each target type.
  2210. switch (gtgt->GetType()) {
  2211. case cmStateEnums::STATIC_LIBRARY:
  2212. if (gtgt->GetPropertyAsBool("FRAMEWORK")) {
  2213. std::string fw_version = gtgt->GetFrameworkVersion();
  2214. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  2215. this->CreateString(fw_version));
  2216. cmProp ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  2217. if (ext) {
  2218. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  2219. this->CreateString(*ext));
  2220. }
  2221. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2222. // Xcode will create the final version of Info.plist at build time,
  2223. // so let it replace the framework name. This avoids creating
  2224. // a per-configuration Info.plist file.
  2225. this->CurrentLocalGenerator->GenerateFrameworkInfoPList(
  2226. gtgt, "$(EXECUTABLE_NAME)", plist);
  2227. buildSettings->AddAttribute("INFOPLIST_FILE",
  2228. this->CreateString(plist));
  2229. buildSettings->AddAttribute("MACH_O_TYPE",
  2230. this->CreateString("staticlib"));
  2231. } else {
  2232. buildSettings->AddAttribute("LIBRARY_STYLE",
  2233. this->CreateString("STATIC"));
  2234. }
  2235. break;
  2236. case cmStateEnums::OBJECT_LIBRARY: {
  2237. buildSettings->AddAttribute("LIBRARY_STYLE",
  2238. this->CreateString("STATIC"));
  2239. break;
  2240. }
  2241. case cmStateEnums::MODULE_LIBRARY: {
  2242. buildSettings->AddAttribute("LIBRARY_STYLE",
  2243. this->CreateString("BUNDLE"));
  2244. if (gtgt->IsCFBundleOnApple()) {
  2245. // It turns out that a BUNDLE is basically the same
  2246. // in many ways as an application bundle, as far as
  2247. // link flags go
  2248. std::string createFlags = this->LookupFlags(
  2249. "CMAKE_SHARED_MODULE_CREATE_", llang, "_FLAGS", "-bundle");
  2250. if (!createFlags.empty()) {
  2251. extraLinkOptions += " ";
  2252. extraLinkOptions += createFlags;
  2253. }
  2254. cmProp ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  2255. if (ext) {
  2256. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  2257. this->CreateString(*ext));
  2258. }
  2259. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2260. // Xcode will create the final version of Info.plist at build time,
  2261. // so let it replace the cfbundle name. This avoids creating
  2262. // a per-configuration Info.plist file. The cfbundle plist
  2263. // is very similar to the application bundle plist
  2264. this->CurrentLocalGenerator->GenerateAppleInfoPList(
  2265. gtgt, "$(EXECUTABLE_NAME)", plist);
  2266. buildSettings->AddAttribute("INFOPLIST_FILE",
  2267. this->CreateString(plist));
  2268. } else {
  2269. buildSettings->AddAttribute("MACH_O_TYPE",
  2270. this->CreateString("mh_bundle"));
  2271. buildSettings->AddAttribute("GCC_DYNAMIC_NO_PIC",
  2272. this->CreateString("NO"));
  2273. // Add the flags to create an executable.
  2274. std::string createFlags =
  2275. this->LookupFlags("CMAKE_", llang, "_LINK_FLAGS", "");
  2276. if (!createFlags.empty()) {
  2277. extraLinkOptions += " ";
  2278. extraLinkOptions += createFlags;
  2279. }
  2280. }
  2281. break;
  2282. }
  2283. case cmStateEnums::SHARED_LIBRARY: {
  2284. if (gtgt->GetPropertyAsBool("FRAMEWORK")) {
  2285. std::string fw_version = gtgt->GetFrameworkVersion();
  2286. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  2287. this->CreateString(fw_version));
  2288. cmProp ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  2289. if (ext) {
  2290. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  2291. this->CreateString(*ext));
  2292. }
  2293. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2294. // Xcode will create the final version of Info.plist at build time,
  2295. // so let it replace the framework name. This avoids creating
  2296. // a per-configuration Info.plist file.
  2297. this->CurrentLocalGenerator->GenerateFrameworkInfoPList(
  2298. gtgt, "$(EXECUTABLE_NAME)", plist);
  2299. buildSettings->AddAttribute("INFOPLIST_FILE",
  2300. this->CreateString(plist));
  2301. } else {
  2302. // Add the flags to create a shared library.
  2303. std::string createFlags = this->LookupFlags(
  2304. "CMAKE_SHARED_LIBRARY_CREATE_", llang, "_FLAGS", "-dynamiclib");
  2305. if (!createFlags.empty()) {
  2306. extraLinkOptions += " ";
  2307. extraLinkOptions += createFlags;
  2308. }
  2309. }
  2310. buildSettings->AddAttribute("LIBRARY_STYLE",
  2311. this->CreateString("DYNAMIC"));
  2312. break;
  2313. }
  2314. case cmStateEnums::EXECUTABLE: {
  2315. // Add the flags to create an executable.
  2316. std::string createFlags =
  2317. this->LookupFlags("CMAKE_", llang, "_LINK_FLAGS", "");
  2318. if (!createFlags.empty()) {
  2319. extraLinkOptions += " ";
  2320. extraLinkOptions += createFlags;
  2321. }
  2322. // Handle bundles and normal executables separately.
  2323. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  2324. cmProp ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  2325. if (ext) {
  2326. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  2327. this->CreateString(*ext));
  2328. }
  2329. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2330. // Xcode will create the final version of Info.plist at build time,
  2331. // so let it replace the executable name. This avoids creating
  2332. // a per-configuration Info.plist file.
  2333. this->CurrentLocalGenerator->GenerateAppleInfoPList(
  2334. gtgt, "$(EXECUTABLE_NAME)", plist);
  2335. buildSettings->AddAttribute("INFOPLIST_FILE",
  2336. this->CreateString(plist));
  2337. }
  2338. } break;
  2339. default:
  2340. break;
  2341. }
  2342. BuildObjectListOrString dirs(this, true);
  2343. BuildObjectListOrString fdirs(this, true);
  2344. BuildObjectListOrString sysdirs(this, true);
  2345. BuildObjectListOrString sysfdirs(this, true);
  2346. const bool emitSystemIncludes = this->XcodeVersion >= 83;
  2347. std::vector<std::string> includes;
  2348. if (!langForPreprocessor.empty()) {
  2349. this->CurrentLocalGenerator->GetIncludeDirectories(
  2350. includes, gtgt, langForPreprocessor, configName);
  2351. }
  2352. std::set<std::string> emitted;
  2353. emitted.insert("/System/Library/Frameworks");
  2354. for (auto& include : includes) {
  2355. if (this->NameResolvesToFramework(include)) {
  2356. std::string frameworkDir = cmStrCat(include, "/../");
  2357. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  2358. if (emitted.insert(frameworkDir).second) {
  2359. std::string incpath = this->XCodeEscapePath(frameworkDir);
  2360. if (emitSystemIncludes &&
  2361. gtgt->IsSystemIncludeDirectory(frameworkDir, configName,
  2362. langForPreprocessor)) {
  2363. sysfdirs.Add(incpath);
  2364. } else {
  2365. fdirs.Add(incpath);
  2366. }
  2367. }
  2368. } else {
  2369. std::string incpath = this->XCodeEscapePath(include);
  2370. if (emitSystemIncludes &&
  2371. gtgt->IsSystemIncludeDirectory(include, configName,
  2372. langForPreprocessor)) {
  2373. sysdirs.Add(incpath);
  2374. } else {
  2375. dirs.Add(incpath);
  2376. }
  2377. }
  2378. }
  2379. // Add framework search paths needed for linking.
  2380. if (cmComputeLinkInformation* cli = gtgt->GetLinkInformation(configName)) {
  2381. for (auto const& fwDir : cli->GetFrameworkPaths()) {
  2382. if (emitted.insert(fwDir).second) {
  2383. std::string incpath = this->XCodeEscapePath(fwDir);
  2384. if (emitSystemIncludes &&
  2385. gtgt->IsSystemIncludeDirectory(fwDir, configName,
  2386. langForPreprocessor)) {
  2387. sysfdirs.Add(incpath);
  2388. } else {
  2389. fdirs.Add(incpath);
  2390. }
  2391. }
  2392. }
  2393. }
  2394. if (!fdirs.IsEmpty()) {
  2395. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS", fdirs.CreateList());
  2396. }
  2397. if (!dirs.IsEmpty()) {
  2398. buildSettings->AddAttribute("HEADER_SEARCH_PATHS", dirs.CreateList());
  2399. }
  2400. if (!sysfdirs.IsEmpty()) {
  2401. buildSettings->AddAttribute("SYSTEM_FRAMEWORK_SEARCH_PATHS",
  2402. sysfdirs.CreateList());
  2403. }
  2404. if (!sysdirs.IsEmpty()) {
  2405. buildSettings->AddAttribute("SYSTEM_HEADER_SEARCH_PATHS",
  2406. sysdirs.CreateList());
  2407. }
  2408. if (this->XcodeVersion >= 60 && !emitSystemIncludes) {
  2409. // Add those per-language flags in addition to HEADER_SEARCH_PATHS to gain
  2410. // system include directory awareness. We need to also keep on setting
  2411. // HEADER_SEARCH_PATHS to work around a missing compile options flag for
  2412. // GNU assembly files (#16449)
  2413. for (auto const& language : languages) {
  2414. std::string includeFlags = this->CurrentLocalGenerator->GetIncludeFlags(
  2415. includes, gtgt, language, true, false, configName);
  2416. if (!includeFlags.empty()) {
  2417. cflags[language] += " " + includeFlags;
  2418. }
  2419. }
  2420. }
  2421. bool same_gflags = true;
  2422. std::map<std::string, std::string> gflags;
  2423. std::string const* last_gflag = nullptr;
  2424. std::string optLevel = "0";
  2425. // Minimal map of flags to build settings.
  2426. for (auto const& language : languages) {
  2427. std::string& flags = cflags[language];
  2428. std::string& gflag = gflags[language];
  2429. std::string oflag =
  2430. this->ExtractFlagRegex("(^| )(-Ofast|-Os|-O[0-9]*)( |$)", 2, flags);
  2431. if (oflag.size() == 2) {
  2432. optLevel = "1";
  2433. } else if (oflag.size() > 2) {
  2434. optLevel = oflag.substr(2);
  2435. }
  2436. gflag = this->ExtractFlag("-g", flags);
  2437. // put back gdwarf-2 if used since there is no way
  2438. // to represent it in the gui, but we still want debug yes
  2439. if (gflag == "-gdwarf-2") {
  2440. flags += " ";
  2441. flags += gflag;
  2442. }
  2443. if (last_gflag && *last_gflag != gflag) {
  2444. same_gflags = false;
  2445. }
  2446. last_gflag = &gflag;
  2447. }
  2448. const char* debugStr = "YES";
  2449. if (!same_gflags) {
  2450. // We can't set the Xcode flag differently depending on the language,
  2451. // so put them back in this case.
  2452. for (auto const& language : languages) {
  2453. cflags[language] += " ";
  2454. cflags[language] += gflags[language];
  2455. }
  2456. debugStr = "NO";
  2457. } else if (last_gflag && (last_gflag->empty() || *last_gflag == "-g0")) {
  2458. debugStr = "NO";
  2459. }
  2460. // extract C++ stdlib
  2461. for (auto const& language : languages) {
  2462. if (language != "CXX" && language != "OBJCXX") {
  2463. continue;
  2464. }
  2465. std::string& flags = cflags[language];
  2466. auto stdlib =
  2467. this->ExtractFlagRegex("(^| )(-stdlib=[^ ]+)( |$)", 2, flags);
  2468. if (stdlib.size() > 8) {
  2469. const auto cxxLibrary = stdlib.substr(8);
  2470. if (language == "CXX" ||
  2471. !buildSettings->GetAttribute("CLANG_CXX_LIBRARY")) {
  2472. buildSettings->AddAttribute("CLANG_CXX_LIBRARY",
  2473. this->CreateString(cxxLibrary));
  2474. }
  2475. }
  2476. }
  2477. buildSettings->AddAttribute("COMBINE_HIDPI_IMAGES",
  2478. this->CreateString("YES"));
  2479. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  2480. this->CreateString(debugStr));
  2481. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  2482. this->CreateString(optLevel));
  2483. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  2484. this->CreateString("NO"));
  2485. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  2486. this->CreateString("NO"));
  2487. for (auto const& language : languages) {
  2488. std::string flags = cflags[language] + " " + defFlags;
  2489. if (language == "CXX" || language == "OBJCXX") {
  2490. if (language == "CXX" ||
  2491. !buildSettings->GetAttribute("OTHER_CPLUSPLUSFLAGS")) {
  2492. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  2493. this->CreateString(flags));
  2494. }
  2495. } else if (language == "Fortran") {
  2496. buildSettings->AddAttribute("IFORT_OTHER_FLAGS",
  2497. this->CreateString(flags));
  2498. } else if (language == "C" || language == "OBJC") {
  2499. if (language == "C" || !buildSettings->GetAttribute("OTHER_CFLAGS")) {
  2500. buildSettings->AddAttribute("OTHER_CFLAGS", this->CreateString(flags));
  2501. }
  2502. } else if (language == "Swift") {
  2503. buildSettings->AddAttribute("OTHER_SWIFT_FLAGS",
  2504. this->CreateString(flags));
  2505. }
  2506. }
  2507. // Add Fortran source format attribute if property is set.
  2508. const char* format = nullptr;
  2509. std::string const& tgtfmt = gtgt->GetSafeProperty("Fortran_FORMAT");
  2510. switch (cmOutputConverter::GetFortranFormat(tgtfmt)) {
  2511. case cmOutputConverter::FortranFormatFixed:
  2512. format = "fixed";
  2513. break;
  2514. case cmOutputConverter::FortranFormatFree:
  2515. format = "free";
  2516. break;
  2517. default:
  2518. break;
  2519. }
  2520. if (format) {
  2521. buildSettings->AddAttribute("IFORT_LANG_SRCFMT",
  2522. this->CreateString(format));
  2523. }
  2524. // Create the INSTALL_PATH attribute.
  2525. std::string install_name_dir;
  2526. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  2527. // Get the install_name directory for the build tree.
  2528. install_name_dir = gtgt->GetInstallNameDirForBuildTree(configName);
  2529. // Xcode doesn't create the correct install_name in some cases.
  2530. // That is, if the INSTALL_PATH is empty, or if we have versioning
  2531. // of dylib libraries, we want to specify the install_name.
  2532. // This is done by adding a link flag to create an install_name
  2533. // with just the library soname.
  2534. std::string install_name;
  2535. if (!install_name_dir.empty()) {
  2536. // Convert to a path for the native build tool.
  2537. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  2538. install_name += install_name_dir;
  2539. install_name += "/";
  2540. }
  2541. install_name += gtgt->GetSOName(configName);
  2542. if ((realName != soName) || install_name_dir.empty()) {
  2543. install_name_dir = "";
  2544. extraLinkOptions += " -install_name ";
  2545. extraLinkOptions += XCodeEscapePath(install_name);
  2546. }
  2547. }
  2548. buildSettings->AddAttribute("INSTALL_PATH",
  2549. this->CreateString(install_name_dir));
  2550. // Create the LD_RUNPATH_SEARCH_PATHS
  2551. cmComputeLinkInformation* pcli = gtgt->GetLinkInformation(configName);
  2552. if (pcli) {
  2553. std::string search_paths;
  2554. std::vector<std::string> runtimeDirs;
  2555. pcli->GetRPath(runtimeDirs, false);
  2556. // runpath dirs needs to be unique to prevent corruption
  2557. std::set<std::string> unique_dirs;
  2558. for (auto runpath : runtimeDirs) {
  2559. runpath = this->ExpandCFGIntDir(runpath, configName);
  2560. if (unique_dirs.find(runpath) == unique_dirs.end()) {
  2561. unique_dirs.insert(runpath);
  2562. if (!search_paths.empty()) {
  2563. search_paths += " ";
  2564. }
  2565. search_paths += this->XCodeEscapePath(runpath);
  2566. }
  2567. }
  2568. if (!search_paths.empty()) {
  2569. buildSettings->AddAttribute("LD_RUNPATH_SEARCH_PATHS",
  2570. this->CreateString(search_paths));
  2571. }
  2572. }
  2573. buildSettings->AddAttribute(this->GetTargetLinkFlagsVar(gtgt),
  2574. this->CreateString(extraLinkOptions));
  2575. buildSettings->AddAttribute("OTHER_REZFLAGS", this->CreateString(""));
  2576. buildSettings->AddAttribute("SECTORDER_FLAGS", this->CreateString(""));
  2577. buildSettings->AddAttribute("USE_HEADERMAP", this->CreateString("NO"));
  2578. cmXCodeObject* group = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2579. group->AddObject(this->CreateString("$(inherited)"));
  2580. buildSettings->AddAttribute("WARNING_CFLAGS", group);
  2581. // Runtime version information.
  2582. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  2583. int major;
  2584. int minor;
  2585. int patch;
  2586. // MACHO_CURRENT_VERSION or VERSION -> current_version
  2587. gtgt->GetTargetVersionFallback("MACHO_CURRENT_VERSION", "VERSION", major,
  2588. minor, patch);
  2589. std::ostringstream v;
  2590. // Xcode always wants at least 1.0.0 or nothing
  2591. if (!(major == 0 && minor == 0 && patch == 0)) {
  2592. v << major << "." << minor << "." << patch;
  2593. }
  2594. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  2595. this->CreateString(v.str()));
  2596. // MACHO_COMPATIBILITY_VERSION or SOVERSION -> compatibility_version
  2597. gtgt->GetTargetVersionFallback("MACHO_COMPATIBILITY_VERSION", "SOVERSION",
  2598. major, minor, patch);
  2599. std::ostringstream vso;
  2600. // Xcode always wants at least 1.0.0 or nothing
  2601. if (!(major == 0 && minor == 0 && patch == 0)) {
  2602. vso << major << "." << minor << "." << patch;
  2603. }
  2604. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  2605. this->CreateString(vso.str()));
  2606. }
  2607. // Precompile Headers
  2608. std::string pchHeader = gtgt->GetPchHeader(configName, llang);
  2609. if (!pchHeader.empty()) {
  2610. buildSettings->AddAttribute("GCC_PREFIX_HEADER",
  2611. this->CreateString(pchHeader));
  2612. buildSettings->AddAttribute("GCC_PRECOMPILE_PREFIX_HEADER",
  2613. this->CreateString("YES"));
  2614. }
  2615. // put this last so it can override existing settings
  2616. // Convert "XCODE_ATTRIBUTE_*" properties directly.
  2617. {
  2618. for (auto const& prop : gtgt->GetPropertyKeys()) {
  2619. if (cmHasLiteralPrefix(prop, "XCODE_ATTRIBUTE_")) {
  2620. std::string attribute = prop.substr(16);
  2621. this->FilterConfigurationAttribute(configName, attribute);
  2622. if (!attribute.empty()) {
  2623. std::string const& pr = gtgt->GetSafeProperty(prop);
  2624. std::string processed = cmGeneratorExpression::Evaluate(
  2625. pr, this->CurrentLocalGenerator, configName);
  2626. buildSettings->AddAttribute(attribute,
  2627. this->CreateString(processed));
  2628. }
  2629. }
  2630. }
  2631. }
  2632. }
  2633. cmXCodeObject* cmGlobalXCodeGenerator::CreateUtilityTarget(
  2634. cmGeneratorTarget* gtgt)
  2635. {
  2636. cmXCodeObject* shellBuildPhase =
  2637. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  2638. shellBuildPhase->AddAttribute("buildActionMask",
  2639. this->CreateString("2147483647"));
  2640. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2641. shellBuildPhase->AddAttribute("files", buildFiles);
  2642. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2643. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  2644. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2645. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  2646. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  2647. this->CreateString("0"));
  2648. shellBuildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  2649. shellBuildPhase->AddAttribute(
  2650. "shellScript", this->CreateString("# shell script goes here\nexit 0"));
  2651. shellBuildPhase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  2652. cmXCodeObject* target =
  2653. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  2654. target->SetComment(gtgt->GetName());
  2655. cmXCodeObject* buildPhases = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2656. std::vector<cmXCodeObject*> emptyContentVector;
  2657. this->CreateCustomCommands(buildPhases, nullptr, nullptr, nullptr,
  2658. emptyContentVector, nullptr, gtgt);
  2659. target->AddAttribute("buildPhases", buildPhases);
  2660. this->AddConfigurations(target, gtgt);
  2661. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2662. target->AddAttribute("dependencies", dependencies);
  2663. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2664. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2665. target->SetTarget(gtgt);
  2666. this->XCodeObjectMap[gtgt] = target;
  2667. // Add source files without build rules for editing convenience.
  2668. if (gtgt->GetType() != cmStateEnums::GLOBAL_TARGET &&
  2669. gtgt->GetName() != CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  2670. std::vector<cmSourceFile*> sources;
  2671. if (!gtgt->GetConfigCommonSourceFiles(sources)) {
  2672. return nullptr;
  2673. }
  2674. // Add CMakeLists.txt file for user convenience.
  2675. this->AddXCodeProjBuildRule(gtgt, sources);
  2676. for (auto sourceFile : sources) {
  2677. if (!sourceFile->GetIsGenerated()) {
  2678. this->CreateXCodeFileReference(sourceFile, gtgt);
  2679. }
  2680. }
  2681. }
  2682. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2683. return target;
  2684. }
  2685. std::string cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  2686. cmGeneratorTarget* gtgt)
  2687. {
  2688. std::vector<std::string> const configVector = cmExpandedList(
  2689. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES"));
  2690. cmXCodeObject* configlist =
  2691. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2692. cmXCodeObject* buildConfigurations =
  2693. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2694. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2695. std::string comment = cmStrCat("Build configuration list for ",
  2696. cmXCodeObject::PBXTypeNames[target->GetIsA()],
  2697. " \"", gtgt->GetName(), '"');
  2698. configlist->SetComment(comment);
  2699. target->AddAttribute("buildConfigurationList",
  2700. this->CreateObjectReference(configlist));
  2701. for (auto const& i : configVector) {
  2702. cmXCodeObject* config =
  2703. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2704. buildConfigurations->AddObject(config);
  2705. cmXCodeObject* buildSettings =
  2706. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2707. this->CreateBuildSettings(gtgt, buildSettings, i);
  2708. config->AddAttribute("name", this->CreateString(i));
  2709. config->SetComment(i);
  2710. config->AddAttribute("buildSettings", buildSettings);
  2711. }
  2712. if (!configVector.empty()) {
  2713. configlist->AddAttribute("defaultConfigurationName",
  2714. this->CreateString(configVector[0]));
  2715. configlist->AddAttribute("defaultConfigurationIsVisible",
  2716. this->CreateString("0"));
  2717. return configVector[0];
  2718. }
  2719. return "";
  2720. }
  2721. const char* cmGlobalXCodeGenerator::GetTargetLinkFlagsVar(
  2722. cmGeneratorTarget const* target) const
  2723. {
  2724. if (this->XcodeVersion >= 60 &&
  2725. (target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2726. target->GetType() == cmStateEnums::OBJECT_LIBRARY)) {
  2727. return "OTHER_LIBTOOLFLAGS";
  2728. }
  2729. return "OTHER_LDFLAGS";
  2730. }
  2731. const char* cmGlobalXCodeGenerator::GetTargetFileType(
  2732. cmGeneratorTarget* target)
  2733. {
  2734. if (cmProp e = target->GetProperty("XCODE_EXPLICIT_FILE_TYPE")) {
  2735. return e->c_str();
  2736. }
  2737. switch (target->GetType()) {
  2738. case cmStateEnums::OBJECT_LIBRARY:
  2739. return "archive.ar";
  2740. case cmStateEnums::STATIC_LIBRARY:
  2741. return (target->GetPropertyAsBool("FRAMEWORK") ? "wrapper.framework"
  2742. : "archive.ar");
  2743. case cmStateEnums::MODULE_LIBRARY:
  2744. if (target->IsXCTestOnApple()) {
  2745. return "wrapper.cfbundle";
  2746. }
  2747. if (target->IsCFBundleOnApple()) {
  2748. return "wrapper.plug-in";
  2749. }
  2750. return "compiled.mach-o.executable";
  2751. case cmStateEnums::SHARED_LIBRARY:
  2752. return (target->GetPropertyAsBool("FRAMEWORK")
  2753. ? "wrapper.framework"
  2754. : "compiled.mach-o.dylib");
  2755. case cmStateEnums::EXECUTABLE:
  2756. return "compiled.mach-o.executable";
  2757. default:
  2758. break;
  2759. }
  2760. return nullptr;
  2761. }
  2762. const char* cmGlobalXCodeGenerator::GetTargetProductType(
  2763. cmGeneratorTarget* target)
  2764. {
  2765. if (cmProp e = target->GetProperty("XCODE_PRODUCT_TYPE")) {
  2766. return e->c_str();
  2767. }
  2768. switch (target->GetType()) {
  2769. case cmStateEnums::OBJECT_LIBRARY:
  2770. return "com.apple.product-type.library.static";
  2771. case cmStateEnums::STATIC_LIBRARY:
  2772. return (target->GetPropertyAsBool("FRAMEWORK")
  2773. ? "com.apple.product-type.framework"
  2774. : "com.apple.product-type.library.static");
  2775. case cmStateEnums::MODULE_LIBRARY:
  2776. if (target->IsXCTestOnApple()) {
  2777. return "com.apple.product-type.bundle.unit-test";
  2778. } else if (target->IsCFBundleOnApple()) {
  2779. return "com.apple.product-type.bundle";
  2780. } else {
  2781. return "com.apple.product-type.tool";
  2782. }
  2783. case cmStateEnums::SHARED_LIBRARY:
  2784. return (target->GetPropertyAsBool("FRAMEWORK")
  2785. ? "com.apple.product-type.framework"
  2786. : "com.apple.product-type.library.dynamic");
  2787. case cmStateEnums::EXECUTABLE:
  2788. return (target->GetPropertyAsBool("MACOSX_BUNDLE")
  2789. ? "com.apple.product-type.application"
  2790. : "com.apple.product-type.tool");
  2791. default:
  2792. break;
  2793. }
  2794. return nullptr;
  2795. }
  2796. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeTarget(
  2797. cmGeneratorTarget* gtgt, cmXCodeObject* buildPhases)
  2798. {
  2799. if (!gtgt->IsInBuildSystem()) {
  2800. return nullptr;
  2801. }
  2802. cmXCodeObject* target = this->CreateObject(cmXCodeObject::PBXNativeTarget);
  2803. target->AddAttribute("buildPhases", buildPhases);
  2804. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2805. target->AddAttribute("buildRules", buildRules);
  2806. std::string defConfig;
  2807. defConfig = this->AddConfigurations(target, gtgt);
  2808. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2809. target->AddAttribute("dependencies", dependencies);
  2810. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2811. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2812. cmXCodeObject* fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  2813. if (const char* fileType = this->GetTargetFileType(gtgt)) {
  2814. fileRef->AddAttribute("explicitFileType", this->CreateString(fileType));
  2815. }
  2816. std::string fullName;
  2817. if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  2818. fullName = cmStrCat("lib", gtgt->GetName(), ".a");
  2819. } else {
  2820. fullName = gtgt->GetFullName(defConfig);
  2821. }
  2822. fileRef->AddAttribute("path", this->CreateString(fullName));
  2823. fileRef->AddAttribute("sourceTree",
  2824. this->CreateString("BUILT_PRODUCTS_DIR"));
  2825. fileRef->SetComment(gtgt->GetName());
  2826. target->AddAttribute("productReference",
  2827. this->CreateObjectReference(fileRef));
  2828. if (const char* productType = this->GetTargetProductType(gtgt)) {
  2829. target->AddAttribute("productType", this->CreateString(productType));
  2830. }
  2831. target->SetTarget(gtgt);
  2832. this->XCodeObjectMap[gtgt] = target;
  2833. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2834. return target;
  2835. }
  2836. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(
  2837. cmGeneratorTarget const* t)
  2838. {
  2839. if (!t) {
  2840. return nullptr;
  2841. }
  2842. auto const i = this->XCodeObjectMap.find(t);
  2843. if (i == this->XCodeObjectMap.end()) {
  2844. return nullptr;
  2845. }
  2846. return i->second;
  2847. }
  2848. std::string cmGlobalXCodeGenerator::GetObjectId()
  2849. {
  2850. std::string objectId;
  2851. char cUuid[40] = { 0 };
  2852. CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
  2853. CFStringRef s = CFUUIDCreateString(kCFAllocatorDefault, uuid);
  2854. CFStringGetCString(s, cUuid, sizeof(cUuid), kCFStringEncodingUTF8);
  2855. objectId = cUuid;
  2856. CFRelease(s);
  2857. CFRelease(uuid);
  2858. cmSystemTools::ReplaceString(objectId, "-", "");
  2859. if (objectId.size() > 24) {
  2860. objectId = objectId.substr(0, 24);
  2861. }
  2862. return objectId;
  2863. }
  2864. std::string cmGlobalXCodeGenerator::GetOrCreateId(const std::string& name,
  2865. const std::string& id)
  2866. {
  2867. std::string guidStoreName = cmStrCat(name, "_GUID_CMAKE");
  2868. cmProp storedGUID = this->CMakeInstance->GetCacheDefinition(guidStoreName);
  2869. if (storedGUID) {
  2870. return *storedGUID;
  2871. }
  2872. this->CMakeInstance->AddCacheEntry(guidStoreName, id.c_str(),
  2873. "Stored Xcode object GUID",
  2874. cmStateEnums::INTERNAL);
  2875. return id;
  2876. }
  2877. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  2878. cmXCodeObject* dependTarget)
  2879. {
  2880. // This is called once for every edge in the target dependency graph.
  2881. cmXCodeObject* container =
  2882. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  2883. container->SetComment("PBXContainerItemProxy");
  2884. container->AddAttribute("containerPortal",
  2885. this->CreateObjectReference(this->RootObject));
  2886. container->AddAttribute("proxyType", this->CreateString("1"));
  2887. container->AddAttribute("remoteGlobalIDString",
  2888. this->CreateObjectReference(dependTarget));
  2889. container->AddAttribute(
  2890. "remoteInfo", this->CreateString(dependTarget->GetTarget()->GetName()));
  2891. cmXCodeObject* targetdep =
  2892. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  2893. targetdep->SetComment("PBXTargetDependency");
  2894. targetdep->AddAttribute("target", this->CreateObjectReference(dependTarget));
  2895. targetdep->AddAttribute("targetProxy",
  2896. this->CreateObjectReference(container));
  2897. cmXCodeObject* depends = target->GetAttribute("dependencies");
  2898. if (!depends) {
  2899. cmSystemTools::Error(
  2900. "target does not have dependencies attribute error..");
  2901. } else {
  2902. depends->AddUniqueObject(targetdep);
  2903. }
  2904. }
  2905. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  2906. const char* attribute,
  2907. cmXCodeObject* value)
  2908. {
  2909. if (settings) {
  2910. cmXCodeObject* attr = settings->GetAttribute(attribute);
  2911. if (!attr) {
  2912. settings->AddAttribute(attribute, value);
  2913. } else {
  2914. if (value->GetType() != cmXCodeObject::OBJECT_LIST &&
  2915. value->GetType() != cmXCodeObject::STRING) {
  2916. cmSystemTools::Error("Unsupported value type for appending: " +
  2917. std::string(attribute));
  2918. return;
  2919. }
  2920. if (attr->GetType() == cmXCodeObject::OBJECT_LIST) {
  2921. if (value->GetType() == cmXCodeObject::OBJECT_LIST) {
  2922. for (auto* obj : value->GetObjectList()) {
  2923. attr->AddObject(obj);
  2924. }
  2925. } else {
  2926. attr->AddObject(value);
  2927. }
  2928. } else if (attr->GetType() == cmXCodeObject::STRING) {
  2929. if (value->GetType() == cmXCodeObject::OBJECT_LIST) {
  2930. // Add old value as a list item to new object list
  2931. // and replace the attribute with the new list
  2932. value->PrependObject(attr);
  2933. settings->AddAttribute(attribute, value);
  2934. } else {
  2935. std::string newValue =
  2936. cmStrCat(attr->GetString(), ' ', value->GetString());
  2937. attr->SetString(newValue);
  2938. }
  2939. } else {
  2940. cmSystemTools::Error("Unsupported attribute type for appending: " +
  2941. std::string(attribute));
  2942. }
  2943. }
  2944. }
  2945. }
  2946. void cmGlobalXCodeGenerator::AppendBuildSettingAttribute(
  2947. cmXCodeObject* target, const char* attribute, cmXCodeObject* value,
  2948. const std::string& configName)
  2949. {
  2950. // There are multiple configurations. Add the setting to the
  2951. // buildSettings of the configuration name given.
  2952. cmXCodeObject* configurationList =
  2953. target->GetAttribute("buildConfigurationList")->GetObject();
  2954. cmXCodeObject* buildConfigs =
  2955. configurationList->GetAttribute("buildConfigurations");
  2956. for (auto obj : buildConfigs->GetObjectList()) {
  2957. if (configName.empty() ||
  2958. obj->GetAttribute("name")->GetString() == configName) {
  2959. cmXCodeObject* settings = obj->GetAttribute("buildSettings");
  2960. this->AppendOrAddBuildSetting(settings, attribute, value);
  2961. }
  2962. }
  2963. }
  2964. void cmGlobalXCodeGenerator::AddDependAndLinkInformation(cmXCodeObject* target)
  2965. {
  2966. cmGeneratorTarget* gt = target->GetTarget();
  2967. if (!gt) {
  2968. cmSystemTools::Error("Error no target on xobject\n");
  2969. return;
  2970. }
  2971. if (!gt->IsInBuildSystem()) {
  2972. return;
  2973. }
  2974. // Add dependencies on other CMake targets.
  2975. for (const auto& dep : this->GetTargetDirectDepends(gt)) {
  2976. if (cmXCodeObject* dptarget = this->FindXCodeTarget(dep)) {
  2977. this->AddDependTarget(target, dptarget);
  2978. }
  2979. }
  2980. // Separate libraries into ones that can be linked using "Link Binary With
  2981. // Libraries" build phase and the ones that can't. Only targets that build
  2982. // Apple bundles (.app, .framework, .bundle), executables and dylibs can use
  2983. // this feature and only targets that represent actual libraries (object,
  2984. // static, dynamic or bundle, excluding executables) will be used. These are
  2985. // limitations imposed by CMake use-cases - otherwise a lot of things break.
  2986. // The rest will be linked using linker flags (OTHER_LDFLAGS setting in Xcode
  2987. // project).
  2988. std::map<std::string, std::vector<cmComputeLinkInformation::Item const*>>
  2989. configItemMap;
  2990. auto addToLinkerArguments =
  2991. [&configItemMap](const std::string& configName,
  2992. cmComputeLinkInformation::Item const* libItemPtr) {
  2993. auto& linkVector = configItemMap[configName];
  2994. if (std::find_if(linkVector.begin(), linkVector.end(),
  2995. [libItemPtr](cmComputeLinkInformation::Item const* p) {
  2996. return p == libItemPtr;
  2997. }) == linkVector.end()) {
  2998. linkVector.push_back(libItemPtr);
  2999. }
  3000. };
  3001. std::vector<cmComputeLinkInformation::Item const*> linkPhaseTargetVector;
  3002. std::map<std::string, std::vector<std::string>> targetConfigMap;
  3003. using ConfigItemPair =
  3004. std::pair<std::string, cmComputeLinkInformation::Item const*>;
  3005. std::map<std::string, std::vector<ConfigItemPair>> targetItemMap;
  3006. std::map<std::string, std::vector<std::string>> targetProductNameMap;
  3007. bool useLinkPhase = false;
  3008. bool forceLinkPhase = false;
  3009. cmProp prop =
  3010. target->GetTarget()->GetProperty("XCODE_LINK_BUILD_PHASE_MODE");
  3011. if (prop) {
  3012. if (*prop == "BUILT_ONLY") {
  3013. useLinkPhase = true;
  3014. } else if (*prop == "KNOWN_LOCATION") {
  3015. useLinkPhase = true;
  3016. forceLinkPhase = true;
  3017. } else if (*prop != "NONE") {
  3018. cmSystemTools::Error("Invalid value for XCODE_LINK_BUILD_PHASE_MODE: " +
  3019. *prop);
  3020. return;
  3021. }
  3022. }
  3023. for (auto const& configName : this->CurrentConfigurationTypes) {
  3024. cmComputeLinkInformation* cli = gt->GetLinkInformation(configName);
  3025. if (!cli) {
  3026. continue;
  3027. }
  3028. for (auto const& libItem : cli->GetItems()) {
  3029. // We want to put only static libraries, dynamic libraries, frameworks
  3030. // and bundles that are built from targets that are not imported in "Link
  3031. // Binary With Libraries" build phase. Except if the target property
  3032. // XCODE_LINK_BUILD_PHASE_MODE is KNOWN_LOCATION then all imported and
  3033. // non-target libraries will be added as well.
  3034. if (useLinkPhase &&
  3035. (gt->GetType() == cmStateEnums::EXECUTABLE ||
  3036. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3037. gt->GetType() == cmStateEnums::MODULE_LIBRARY) &&
  3038. ((libItem.Target &&
  3039. (!libItem.Target->IsImported() || forceLinkPhase) &&
  3040. (libItem.Target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3041. libItem.Target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3042. libItem.Target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  3043. libItem.Target->GetType() == cmStateEnums::UNKNOWN_LIBRARY)) ||
  3044. (!libItem.Target && libItem.IsPath && forceLinkPhase))) {
  3045. std::string libName;
  3046. bool canUseLinkPhase = true;
  3047. if (libItem.Target) {
  3048. if (libItem.Target->GetType() == cmStateEnums::UNKNOWN_LIBRARY) {
  3049. canUseLinkPhase = canUseLinkPhase && forceLinkPhase;
  3050. } else {
  3051. // If a library target uses custom build output directory Xcode
  3052. // won't pick it up so we have to resort back to linker flags, but
  3053. // that's OK as long as the custom output dir is absolute path.
  3054. for (auto const& libConfigName : this->CurrentConfigurationTypes) {
  3055. canUseLinkPhase = canUseLinkPhase &&
  3056. libItem.Target->UsesDefaultOutputDir(
  3057. libConfigName, cmStateEnums::RuntimeBinaryArtifact);
  3058. }
  3059. }
  3060. libName = libItem.Target->GetName();
  3061. } else {
  3062. libName = cmSystemTools::GetFilenameName(libItem.Value.Value);
  3063. // We don't want all the possible files here, just standard libraries
  3064. const auto libExt = cmSystemTools::GetFilenameExtension(libName);
  3065. if (!IsLinkPhaseLibraryExtension(libExt)) {
  3066. canUseLinkPhase = false;
  3067. }
  3068. }
  3069. if (canUseLinkPhase) {
  3070. // Add unique configuration name to target-config map for later
  3071. // checks
  3072. auto& configVector = targetConfigMap[libName];
  3073. if (std::find(configVector.begin(), configVector.end(),
  3074. configName) == configVector.end()) {
  3075. configVector.push_back(configName);
  3076. }
  3077. // Add a pair of config and item to target-item map
  3078. auto& itemVector = targetItemMap[libName];
  3079. itemVector.emplace_back(ConfigItemPair(configName, &libItem));
  3080. // Add product file-name to a lib-product map
  3081. auto productName =
  3082. cmSystemTools::GetFilenameName(libItem.Value.Value);
  3083. auto& productVector = targetProductNameMap[libName];
  3084. if (std::find(productVector.begin(), productVector.end(),
  3085. productName) == productVector.end()) {
  3086. productVector.push_back(productName);
  3087. }
  3088. continue;
  3089. }
  3090. }
  3091. // Add this library item to a regular linker flag list
  3092. addToLinkerArguments(configName, &libItem);
  3093. }
  3094. }
  3095. // Go through target library map and separate libraries that are linked
  3096. // in all configurations and produce only single product, from the rest.
  3097. // Only these will be linked through "Link Binary With Libraries" build
  3098. // phase.
  3099. for (auto const& targetLibConfigs : targetConfigMap) {
  3100. // Add this library to "Link Binary With Libraries" build phase if it's
  3101. // linked in all configurations and it has only one product name
  3102. auto& itemVector = targetItemMap[targetLibConfigs.first];
  3103. auto& productVector = targetProductNameMap[targetLibConfigs.first];
  3104. if (targetLibConfigs.second == this->CurrentConfigurationTypes &&
  3105. productVector.size() == 1) {
  3106. // Add this library to "Link Binary With Libraries" list
  3107. linkPhaseTargetVector.push_back(itemVector[0].second);
  3108. } else {
  3109. for (auto const& libItem : targetItemMap[targetLibConfigs.first]) {
  3110. // Add this library item to a regular linker flag list
  3111. addToLinkerArguments(libItem.first, libItem.second);
  3112. }
  3113. }
  3114. }
  3115. // Add libraries to "Link Binary With Libraries" build phase and collect
  3116. // their search paths. Xcode does not support per-configuration linking
  3117. // in this build phase so we don't have to do this for each configuration
  3118. // separately.
  3119. std::vector<std::string> linkSearchPaths;
  3120. std::vector<std::string> frameworkSearchPaths;
  3121. for (auto const& libItem : linkPhaseTargetVector) {
  3122. // Add target output directory as a library search path
  3123. std::string linkDir;
  3124. if (libItem->Target) {
  3125. linkDir = libItem->Target->GetLocationForBuild();
  3126. } else {
  3127. linkDir = libItem->Value.Value;
  3128. }
  3129. linkDir = GetLibraryOrFrameworkPath(linkDir);
  3130. bool isFramework = cmSystemTools::IsPathToFramework(linkDir);
  3131. linkDir = cmSystemTools::GetParentDirectory(linkDir);
  3132. if (isFramework) {
  3133. if (std::find(frameworkSearchPaths.begin(), frameworkSearchPaths.end(),
  3134. linkDir) == frameworkSearchPaths.end()) {
  3135. frameworkSearchPaths.push_back(linkDir);
  3136. }
  3137. } else {
  3138. if (std::find(linkSearchPaths.begin(), linkSearchPaths.end(), linkDir) ==
  3139. linkSearchPaths.end()) {
  3140. linkSearchPaths.push_back(linkDir);
  3141. }
  3142. }
  3143. // Add target dependency
  3144. if (libItem->Target && !libItem->Target->IsImported()) {
  3145. for (auto const& configName : this->CurrentConfigurationTypes) {
  3146. target->AddDependTarget(configName, libItem->Target->GetName());
  3147. }
  3148. }
  3149. // Get the library target
  3150. auto* libTarget = FindXCodeTarget(libItem->Target);
  3151. cmXCodeObject* buildFile;
  3152. if (!libTarget) {
  3153. if (libItem->IsPath) {
  3154. // Get or create a direct file ref in the root project
  3155. auto cleanPath = libItem->Value.Value;
  3156. if (cmSystemTools::FileIsFullPath(cleanPath)) {
  3157. // Some arguments are reported as paths, but they are actually not,
  3158. // so we can't collapse them, and neither can we collapse relative
  3159. // paths
  3160. cleanPath = cmSystemTools::CollapseFullPath(cleanPath);
  3161. }
  3162. auto it = this->ExternalLibRefs.find(cleanPath);
  3163. if (it == this->ExternalLibRefs.end()) {
  3164. buildFile = CreateXCodeBuildFileFromPath(cleanPath, gt, "", nullptr);
  3165. if (!buildFile) {
  3166. // Add this library item back to a regular linker flag list
  3167. for (const auto& conf : configItemMap) {
  3168. addToLinkerArguments(conf.first, libItem);
  3169. }
  3170. continue;
  3171. }
  3172. this->ExternalLibRefs.emplace(cleanPath, buildFile);
  3173. } else {
  3174. buildFile = it->second;
  3175. }
  3176. } else {
  3177. // Add this library item back to a regular linker flag list
  3178. for (const auto& conf : configItemMap) {
  3179. addToLinkerArguments(conf.first, libItem);
  3180. }
  3181. continue;
  3182. }
  3183. } else {
  3184. // Add the target output file as a build reference for other targets
  3185. // to link against
  3186. auto* fileRefObject = libTarget->GetAttribute("productReference");
  3187. if (!fileRefObject) {
  3188. // Add this library item back to a regular linker flag list
  3189. for (const auto& conf : configItemMap) {
  3190. addToLinkerArguments(conf.first, libItem);
  3191. }
  3192. continue;
  3193. }
  3194. auto it = FileRefToBuildFileMap.find(fileRefObject);
  3195. if (it == FileRefToBuildFileMap.end()) {
  3196. buildFile = this->CreateObject(cmXCodeObject::PBXBuildFile);
  3197. buildFile->AddAttribute("fileRef", fileRefObject);
  3198. FileRefToBuildFileMap[fileRefObject] = buildFile;
  3199. } else {
  3200. buildFile = it->second;
  3201. }
  3202. }
  3203. // Add this reference to current target
  3204. auto* buildPhases = target->GetAttribute("buildPhases");
  3205. if (!buildPhases) {
  3206. cmSystemTools::Error("Missing buildPhase of target");
  3207. continue;
  3208. }
  3209. auto* frameworkBuildPhase =
  3210. buildPhases->GetObject(cmXCodeObject::PBXFrameworksBuildPhase);
  3211. if (!frameworkBuildPhase) {
  3212. cmSystemTools::Error("Missing PBXFrameworksBuildPhase of buildPhase");
  3213. continue;
  3214. }
  3215. auto* buildFiles = frameworkBuildPhase->GetAttribute("files");
  3216. if (!buildFiles) {
  3217. cmSystemTools::Error("Missing files of PBXFrameworksBuildPhase");
  3218. continue;
  3219. }
  3220. if (buildFile && !buildFiles->HasObject(buildFile)) {
  3221. buildFiles->AddObject(buildFile);
  3222. }
  3223. }
  3224. // Loop over configuration types and set per-configuration info.
  3225. for (auto const& configName : this->CurrentConfigurationTypes) {
  3226. {
  3227. // Add object library contents as link flags.
  3228. BuildObjectListOrString libSearchPaths(this, true);
  3229. std::vector<cmSourceFile const*> objs;
  3230. gt->GetExternalObjects(objs, configName);
  3231. for (auto sourceFile : objs) {
  3232. if (sourceFile->GetObjectLibrary().empty()) {
  3233. continue;
  3234. }
  3235. libSearchPaths.Add(this->XCodeEscapePath(sourceFile->GetFullPath()));
  3236. }
  3237. this->AppendBuildSettingAttribute(
  3238. target, this->GetTargetLinkFlagsVar(gt), libSearchPaths.CreateList(),
  3239. configName);
  3240. }
  3241. // Skip link information for object libraries.
  3242. if (gt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3243. gt->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3244. continue;
  3245. }
  3246. // Compute the link library and directory information.
  3247. cmComputeLinkInformation* cli = gt->GetLinkInformation(configName);
  3248. if (!cli) {
  3249. continue;
  3250. }
  3251. // Add dependencies directly on library files.
  3252. for (auto const& libDep : cli->GetDepends()) {
  3253. target->AddDependLibrary(configName, libDep);
  3254. }
  3255. // add the library search paths
  3256. {
  3257. BuildObjectListOrString libSearchPaths(this, true);
  3258. std::string linkDirs;
  3259. for (auto const& libDir : cli->GetDirectories()) {
  3260. if (!libDir.empty() && libDir != "/usr/lib") {
  3261. libSearchPaths.Add(this->XCodeEscapePath(
  3262. libDir + "/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)"));
  3263. libSearchPaths.Add(this->XCodeEscapePath(libDir));
  3264. }
  3265. }
  3266. // Add previously collected paths where to look for libraries
  3267. // that were added to "Link Binary With Libraries"
  3268. for (auto& libDir : linkSearchPaths) {
  3269. libSearchPaths.Add(this->XCodeEscapePath(libDir));
  3270. }
  3271. // Add paths defined in project-wide build settings
  3272. libSearchPaths.Add("$(inherited)");
  3273. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  3274. libSearchPaths.CreateList(),
  3275. configName);
  3276. }
  3277. // add framework search paths
  3278. {
  3279. BuildObjectListOrString fwSearchPaths(this, true);
  3280. // Add previously collected paths where to look for frameworks
  3281. // that were added to "Link Binary With Libraries"
  3282. for (auto& fwDir : frameworkSearchPaths) {
  3283. fwSearchPaths.Add(this->XCodeEscapePath(fwDir));
  3284. }
  3285. // Add paths defined in project-wide build settings
  3286. fwSearchPaths.Add("$(inherited)");
  3287. this->AppendBuildSettingAttribute(target, "FRAMEWORK_SEARCH_PATHS",
  3288. fwSearchPaths.CreateList(),
  3289. configName);
  3290. }
  3291. // now add the left-over link libraries
  3292. {
  3293. BuildObjectListOrString libPaths(this, true);
  3294. for (auto const& libItem : configItemMap[configName]) {
  3295. auto const& libName = *libItem;
  3296. if (libName.IsPath) {
  3297. auto cleanPath = libName.Value.Value;
  3298. if (cmSystemTools::FileIsFullPath(cleanPath)) {
  3299. cleanPath = cmSystemTools::CollapseFullPath(cleanPath);
  3300. }
  3301. const auto libPath = GetLibraryOrFrameworkPath(cleanPath);
  3302. if (cmSystemTools::StringEndsWith(libPath.c_str(), ".framework")) {
  3303. const auto fwName =
  3304. cmSystemTools::GetFilenameWithoutExtension(libPath);
  3305. const auto fwDir = cmSystemTools::GetParentDirectory(libPath);
  3306. libPaths.Add("-F " + this->XCodeEscapePath(fwDir));
  3307. libPaths.Add("-framework " + fwName);
  3308. } else {
  3309. libPaths.Add(this->XCodeEscapePath(cleanPath));
  3310. }
  3311. if ((!libName.Target || libName.Target->IsImported()) &&
  3312. IsLinkPhaseLibraryExtension(libPath)) {
  3313. // Create file reference for embedding
  3314. auto it = this->ExternalLibRefs.find(cleanPath);
  3315. if (it == this->ExternalLibRefs.end()) {
  3316. auto* buildFile =
  3317. this->CreateXCodeBuildFileFromPath(cleanPath, gt, "", nullptr);
  3318. if (buildFile) {
  3319. this->ExternalLibRefs.emplace(cleanPath, buildFile);
  3320. }
  3321. }
  3322. }
  3323. } else if (!libName.Target ||
  3324. libName.Target->GetType() !=
  3325. cmStateEnums::INTERFACE_LIBRARY) {
  3326. libPaths.Add(libName.Value.Value);
  3327. }
  3328. if (libName.Target && !libName.Target->IsImported()) {
  3329. target->AddDependTarget(configName, libName.Target->GetName());
  3330. }
  3331. }
  3332. this->AppendBuildSettingAttribute(target,
  3333. this->GetTargetLinkFlagsVar(gt),
  3334. libPaths.CreateList(), configName);
  3335. }
  3336. }
  3337. }
  3338. bool cmGlobalXCodeGenerator::CreateGroups(
  3339. std::vector<cmLocalGenerator*>& generators)
  3340. {
  3341. for (auto& generator : generators) {
  3342. cmMakefile* mf = generator->GetMakefile();
  3343. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  3344. for (const auto& gtgt : generator->GetGeneratorTargets()) {
  3345. // Same skipping logic here as in CreateXCodeTargets so that we do not
  3346. // end up with (empty anyhow) ZERO_CHECK, install, or test source
  3347. // groups:
  3348. //
  3349. if (!gtgt->IsInBuildSystem() ||
  3350. gtgt->GetType() == cmStateEnums::GLOBAL_TARGET ||
  3351. gtgt->GetName() == CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  3352. continue;
  3353. }
  3354. auto addSourceToGroup = [this, mf, &gtgt,
  3355. &sourceGroups](std::string const& source) {
  3356. cmSourceGroup* sourceGroup = mf->FindSourceGroup(source, sourceGroups);
  3357. cmXCodeObject* pbxgroup =
  3358. this->CreateOrGetPBXGroup(gtgt.get(), sourceGroup);
  3359. std::string key = GetGroupMapKeyFromPath(gtgt.get(), source);
  3360. this->GroupMap[key] = pbxgroup;
  3361. };
  3362. // Put cmSourceFile instances in proper groups:
  3363. for (auto const& si : gtgt->GetAllConfigSources()) {
  3364. cmSourceFile const* sf = si.Source;
  3365. if (!sf->GetObjectLibrary().empty()) {
  3366. // Object library files go on the link line instead.
  3367. continue;
  3368. }
  3369. addSourceToGroup(sf->GetFullPath());
  3370. }
  3371. // Add CMakeLists.txt file for user convenience.
  3372. {
  3373. std::string listfile =
  3374. cmStrCat(gtgt->GetLocalGenerator()->GetCurrentSourceDirectory(),
  3375. "/CMakeLists.txt");
  3376. cmSourceFile* sf = gtgt->Makefile->GetOrCreateSource(
  3377. listfile, false, cmSourceFileLocationKind::Known);
  3378. addSourceToGroup(sf->ResolveFullPath());
  3379. }
  3380. // Add the Info.plist we are about to generate for an App Bundle.
  3381. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  3382. std::string plist = this->ComputeInfoPListLocation(gtgt.get());
  3383. cmSourceFile* sf = gtgt->Makefile->GetOrCreateSource(
  3384. plist, true, cmSourceFileLocationKind::Known);
  3385. addSourceToGroup(sf->ResolveFullPath());
  3386. }
  3387. }
  3388. }
  3389. return true;
  3390. }
  3391. cmXCodeObject* cmGlobalXCodeGenerator::CreatePBXGroup(cmXCodeObject* parent,
  3392. const std::string& name)
  3393. {
  3394. cmXCodeObject* parentChildren = nullptr;
  3395. if (parent) {
  3396. parentChildren = parent->GetAttribute("children");
  3397. }
  3398. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  3399. cmXCodeObject* groupChildren =
  3400. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3401. group->AddAttribute("name", this->CreateString(name));
  3402. group->AddAttribute("children", groupChildren);
  3403. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  3404. if (parentChildren) {
  3405. parentChildren->AddObject(group);
  3406. }
  3407. return group;
  3408. }
  3409. cmXCodeObject* cmGlobalXCodeGenerator::CreateOrGetPBXGroup(
  3410. cmGeneratorTarget* gtgt, cmSourceGroup* sg)
  3411. {
  3412. std::string s;
  3413. std::string target;
  3414. const std::string targetFolder = gtgt->GetEffectiveFolderName();
  3415. if (!targetFolder.empty()) {
  3416. target = cmStrCat(targetFolder, '/');
  3417. }
  3418. target += gtgt->GetName();
  3419. s = cmStrCat(target, '/', sg->GetFullName());
  3420. auto it = this->GroupNameMap.find(s);
  3421. if (it != this->GroupNameMap.end()) {
  3422. return it->second;
  3423. }
  3424. it = this->TargetGroup.find(target);
  3425. cmXCodeObject* tgroup = nullptr;
  3426. if (it != this->TargetGroup.end()) {
  3427. tgroup = it->second;
  3428. } else {
  3429. std::vector<std::string> tgt_folders = cmTokenize(target, "/");
  3430. std::string curr_tgt_folder;
  3431. for (std::vector<std::string>::size_type i = 0; i < tgt_folders.size();
  3432. i++) {
  3433. if (i != 0) {
  3434. curr_tgt_folder += "/";
  3435. }
  3436. curr_tgt_folder += tgt_folders[i];
  3437. it = this->TargetGroup.find(curr_tgt_folder);
  3438. if (it != this->TargetGroup.end()) {
  3439. tgroup = it->second;
  3440. continue;
  3441. }
  3442. tgroup = this->CreatePBXGroup(tgroup, tgt_folders[i]);
  3443. this->TargetGroup[curr_tgt_folder] = tgroup;
  3444. if (i == 0) {
  3445. this->MainGroupChildren->AddObject(tgroup);
  3446. }
  3447. }
  3448. }
  3449. this->TargetGroup[target] = tgroup;
  3450. // If it's the default source group (empty name) then put the source file
  3451. // directly in the tgroup...
  3452. //
  3453. if (sg->GetFullName().empty()) {
  3454. this->GroupNameMap[s] = tgroup;
  3455. return tgroup;
  3456. }
  3457. // It's a recursive folder structure, let's find the real parent group
  3458. if (sg->GetFullName() != sg->GetName()) {
  3459. std::string curr_folder = cmStrCat(target, '/');
  3460. for (auto const& folder : cmTokenize(sg->GetFullName(), "\\")) {
  3461. curr_folder += folder;
  3462. auto const i_folder = this->GroupNameMap.find(curr_folder);
  3463. // Create new folder
  3464. if (i_folder == this->GroupNameMap.end()) {
  3465. cmXCodeObject* group = this->CreatePBXGroup(tgroup, folder);
  3466. this->GroupNameMap[curr_folder] = group;
  3467. tgroup = group;
  3468. } else {
  3469. tgroup = i_folder->second;
  3470. }
  3471. curr_folder += "\\";
  3472. }
  3473. return tgroup;
  3474. }
  3475. cmXCodeObject* group = this->CreatePBXGroup(tgroup, sg->GetName());
  3476. this->GroupNameMap[s] = group;
  3477. return group;
  3478. }
  3479. bool cmGlobalXCodeGenerator::CreateXCodeObjects(
  3480. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  3481. {
  3482. this->ClearXCodeObjects();
  3483. this->RootObject = nullptr;
  3484. this->MainGroupChildren = nullptr;
  3485. this->FrameworkGroup = nullptr;
  3486. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  3487. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  3488. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3489. for (const std::string& CurrentConfigurationType :
  3490. this->CurrentConfigurationTypes) {
  3491. cmXCodeObject* buildStyle =
  3492. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  3493. const std::string& name = CurrentConfigurationType;
  3494. buildStyle->AddAttribute("name", this->CreateString(name));
  3495. buildStyle->SetComment(name);
  3496. cmXCodeObject* sgroup = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  3497. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  3498. buildStyle->AddAttribute("buildSettings", sgroup);
  3499. listObjs->AddObject(buildStyle);
  3500. }
  3501. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  3502. this->MainGroupChildren = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3503. mainGroup->AddAttribute("children", this->MainGroupChildren);
  3504. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  3505. // now create the cmake groups
  3506. if (!this->CreateGroups(generators)) {
  3507. return false;
  3508. }
  3509. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  3510. productGroup->AddAttribute("name", this->CreateString("Products"));
  3511. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  3512. cmXCodeObject* productGroupChildren =
  3513. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3514. productGroup->AddAttribute("children", productGroupChildren);
  3515. this->MainGroupChildren->AddObject(productGroup);
  3516. this->FrameworkGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  3517. this->FrameworkGroup->AddAttribute("name", this->CreateString("Frameworks"));
  3518. this->FrameworkGroup->AddAttribute("sourceTree",
  3519. this->CreateString("<group>"));
  3520. cmXCodeObject* frameworkGroupChildren =
  3521. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3522. this->FrameworkGroup->AddAttribute("children", frameworkGroupChildren);
  3523. this->MainGroupChildren->AddObject(this->FrameworkGroup);
  3524. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  3525. this->RootObject->SetComment("Project object");
  3526. std::string project_id = cmStrCat("PROJECT_", root->GetProjectName());
  3527. this->RootObject->SetId(
  3528. this->GetOrCreateId(project_id, this->RootObject->GetId()));
  3529. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  3530. this->RootObject->AddAttribute("mainGroup",
  3531. this->CreateObjectReference(mainGroup));
  3532. this->RootObject->AddAttribute("buildSettings", group);
  3533. this->RootObject->AddAttribute("buildStyles", listObjs);
  3534. this->RootObject->AddAttribute("hasScannedForEncodings",
  3535. this->CreateString("0"));
  3536. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  3537. group->AddAttribute("BuildIndependentTargetsInParallel",
  3538. this->CreateString("YES"));
  3539. std::ostringstream v;
  3540. v << std::setfill('0') << std::setw(4) << XcodeVersion * 10;
  3541. group->AddAttribute("LastUpgradeCheck", this->CreateString(v.str()));
  3542. this->RootObject->AddAttribute("attributes", group);
  3543. this->RootObject->AddAttribute("compatibilityVersion",
  3544. this->CreateString("Xcode 3.2"));
  3545. // Point Xcode at the top of the source tree.
  3546. {
  3547. std::string pdir =
  3548. this->RelativeToBinary(root->GetCurrentSourceDirectory());
  3549. this->RootObject->AddAttribute("projectDirPath", this->CreateString(pdir));
  3550. this->RootObject->AddAttribute("projectRoot", this->CreateString(""));
  3551. }
  3552. cmXCodeObject* configlist =
  3553. this->CreateObject(cmXCodeObject::XCConfigurationList);
  3554. cmXCodeObject* buildConfigurations =
  3555. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3556. using Configs = std::vector<std::pair<std::string, cmXCodeObject*>>;
  3557. Configs configs;
  3558. std::string defaultConfigName;
  3559. for (const auto& name : this->CurrentConfigurationTypes) {
  3560. if (defaultConfigName.empty()) {
  3561. defaultConfigName = name;
  3562. }
  3563. cmXCodeObject* config =
  3564. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  3565. config->AddAttribute("name", this->CreateString(name));
  3566. configs.push_back(std::make_pair(name, config));
  3567. }
  3568. if (defaultConfigName.empty()) {
  3569. defaultConfigName = "Debug";
  3570. }
  3571. for (auto& config : configs) {
  3572. buildConfigurations->AddObject(config.second);
  3573. }
  3574. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  3575. std::string comment = cmStrCat("Build configuration list for PBXProject \"",
  3576. this->CurrentProject, '"');
  3577. configlist->SetComment(comment);
  3578. configlist->AddAttribute("defaultConfigurationIsVisible",
  3579. this->CreateString("0"));
  3580. configlist->AddAttribute("defaultConfigurationName",
  3581. this->CreateString(defaultConfigName));
  3582. cmXCodeObject* buildSettings =
  3583. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  3584. cmProp sysroot = this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  3585. cmProp deploymentTarget =
  3586. this->CurrentMakefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  3587. if (sysroot) {
  3588. buildSettings->AddAttribute("SDKROOT", this->CreateString(*sysroot));
  3589. }
  3590. // recompute this as it may have been changed since enable language
  3591. this->ComputeArchitectures(this->CurrentMakefile);
  3592. std::string const archs = cmJoin(this->Architectures, " ");
  3593. if (archs.empty()) {
  3594. // Tell Xcode to use NATIVE_ARCH instead of ARCHS.
  3595. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH", this->CreateString("YES"));
  3596. // When targeting macOS, use only the host architecture.
  3597. if (this->SystemName == "Darwin"_s &&
  3598. (!cmNonempty(sysroot) ||
  3599. cmSystemTools::LowerCase(*sysroot).find("macos") !=
  3600. std::string::npos)) {
  3601. buildSettings->AddAttribute("ARCHS",
  3602. this->CreateString("$(NATIVE_ARCH_ACTUAL)"));
  3603. }
  3604. } else {
  3605. // Tell Xcode to use ARCHS (ONLY_ACTIVE_ARCH defaults to NO).
  3606. buildSettings->AddAttribute("ARCHS", this->CreateString(archs));
  3607. }
  3608. if (cmNonempty(deploymentTarget)) {
  3609. buildSettings->AddAttribute(GetDeploymentPlatform(root->GetMakefile()),
  3610. this->CreateString(*deploymentTarget));
  3611. }
  3612. if (!this->GeneratorToolset.empty()) {
  3613. buildSettings->AddAttribute("GCC_VERSION",
  3614. this->CreateString(this->GeneratorToolset));
  3615. }
  3616. if (this->GetLanguageEnabled("Swift")) {
  3617. std::string swiftVersion;
  3618. if (cmProp vers = this->CurrentMakefile->GetDefinition(
  3619. "CMAKE_Swift_LANGUAGE_VERSION")) {
  3620. swiftVersion = *vers;
  3621. } else if (this->XcodeVersion >= 102) {
  3622. swiftVersion = "4.0";
  3623. } else if (this->XcodeVersion >= 83) {
  3624. swiftVersion = "3.0";
  3625. } else {
  3626. swiftVersion = "2.3";
  3627. }
  3628. buildSettings->AddAttribute("SWIFT_VERSION",
  3629. this->CreateString(swiftVersion));
  3630. }
  3631. std::string symroot = cmStrCat(root->GetCurrentBinaryDirectory(), "/build");
  3632. buildSettings->AddAttribute("SYMROOT", this->CreateString(symroot));
  3633. // Inside a try_compile project, do not require signing on any platform.
  3634. if (this->CMakeInstance->GetIsInTryCompile()) {
  3635. buildSettings->AddAttribute("CODE_SIGNING_ALLOWED",
  3636. this->CreateString("NO"));
  3637. }
  3638. for (auto& config : configs) {
  3639. cmXCodeObject* buildSettingsForCfg = this->CreateFlatClone(buildSettings);
  3640. // Put this last so it can override existing settings
  3641. // Convert "CMAKE_XCODE_ATTRIBUTE_*" variables directly.
  3642. for (const auto& var : this->CurrentMakefile->GetDefinitions()) {
  3643. if (cmHasLiteralPrefix(var, "CMAKE_XCODE_ATTRIBUTE_")) {
  3644. std::string attribute = var.substr(22);
  3645. this->FilterConfigurationAttribute(config.first, attribute);
  3646. if (!attribute.empty()) {
  3647. std::string processed = cmGeneratorExpression::Evaluate(
  3648. this->CurrentMakefile->GetSafeDefinition(var),
  3649. this->CurrentLocalGenerator, config.first);
  3650. buildSettingsForCfg->AddAttribute(attribute,
  3651. this->CreateString(processed));
  3652. }
  3653. }
  3654. }
  3655. // store per-config buildSettings into configuration object
  3656. config.second->AddAttribute("buildSettings", buildSettingsForCfg);
  3657. }
  3658. this->RootObject->AddAttribute("buildConfigurationList",
  3659. this->CreateObjectReference(configlist));
  3660. std::vector<cmXCodeObject*> targets;
  3661. for (auto& generator : generators) {
  3662. if (!this->CreateXCodeTargets(generator, targets)) {
  3663. return false;
  3664. }
  3665. for (auto const& ccRoot : this->CustomCommandRoots) {
  3666. if (ccRoot.second.size() > 1) {
  3667. std::string e = "The custom command ";
  3668. std::vector<std::string> const& outputs =
  3669. ccRoot.first->GetCustomCommand()->GetOutputs();
  3670. if (!outputs.empty()) {
  3671. e = cmStrCat(e, "generating\n ", outputs[0]);
  3672. } else {
  3673. e = cmStrCat(e, "driven by\n ", ccRoot.first->GetFullPath());
  3674. }
  3675. e = cmStrCat(e, "\nis attached to multiple targets:");
  3676. for (cmGeneratorTarget const* gt : ccRoot.second) {
  3677. e = cmStrCat(e, "\n ", gt->GetName());
  3678. }
  3679. e = cmStrCat(
  3680. e,
  3681. "\nbut none of these is a common dependency of the other(s). "
  3682. "This is not allowed by the Xcode \"new build system\".");
  3683. generator->IssueMessage(MessageType::FATAL_ERROR, e);
  3684. return false;
  3685. }
  3686. }
  3687. this->CustomCommandRoots.clear();
  3688. }
  3689. // loop over all targets and add link and depend info
  3690. for (auto t : targets) {
  3691. this->AddDependAndLinkInformation(t);
  3692. }
  3693. if (this->XcodeBuildSystem == BuildSystem::One) {
  3694. this->CreateXCodeDependHackMakefile(targets);
  3695. }
  3696. // now add all targets to the root object
  3697. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3698. for (auto t : targets) {
  3699. allTargets->AddObject(t);
  3700. cmXCodeObject* productRef = t->GetAttribute("productReference");
  3701. if (productRef) {
  3702. productGroupChildren->AddObject(productRef->GetObject());
  3703. }
  3704. }
  3705. this->RootObject->AddAttribute("targets", allTargets);
  3706. return true;
  3707. }
  3708. std::string cmGlobalXCodeGenerator::GetObjectsDirectory(
  3709. const std::string& projName, const std::string& configName,
  3710. const cmGeneratorTarget* t, const std::string& variant) const
  3711. {
  3712. std::string dir = cmStrCat(
  3713. t->GetLocalGenerator()->GetCurrentBinaryDirectory(), '/', projName,
  3714. ".build/", configName, '/', t->GetName(), ".build/", variant);
  3715. return dir;
  3716. }
  3717. void cmGlobalXCodeGenerator::ComputeArchitectures(cmMakefile* mf)
  3718. {
  3719. this->Architectures.clear();
  3720. cmProp sysroot = mf->GetDefinition("CMAKE_OSX_SYSROOT");
  3721. if (sysroot) {
  3722. mf->GetDefExpandList("CMAKE_OSX_ARCHITECTURES", this->Architectures);
  3723. }
  3724. if (this->Architectures.empty()) {
  3725. mf->GetDefExpandList("_CMAKE_APPLE_ARCHS_DEFAULT", this->Architectures);
  3726. }
  3727. if (this->Architectures.empty()) {
  3728. // With no ARCHS we use ONLY_ACTIVE_ARCH and possibly a
  3729. // platform-specific default ARCHS placeholder value.
  3730. // Look up the arch that Xcode chooses in this case.
  3731. if (cmProp arch = mf->GetDefinition("CMAKE_XCODE_ARCHS")) {
  3732. this->ObjectDirArchDefault = *arch;
  3733. // We expect only one arch but choose the first just in case.
  3734. std::string::size_type pos = this->ObjectDirArchDefault.find(';');
  3735. if (pos != std::string::npos) {
  3736. this->ObjectDirArchDefault = this->ObjectDirArchDefault.substr(0, pos);
  3737. }
  3738. }
  3739. }
  3740. this->ComputeObjectDirArch(mf);
  3741. }
  3742. void cmGlobalXCodeGenerator::ComputeObjectDirArch(cmMakefile* mf)
  3743. {
  3744. if (this->Architectures.size() > 1 || this->UseEffectivePlatformName(mf)) {
  3745. this->ObjectDirArch = "$(CURRENT_ARCH)";
  3746. } else if (!this->Architectures.empty()) {
  3747. this->ObjectDirArch = this->Architectures[0];
  3748. } else {
  3749. this->ObjectDirArch = this->ObjectDirArchDefault;
  3750. }
  3751. }
  3752. void cmGlobalXCodeGenerator::CreateXCodeDependHackMakefile(
  3753. std::vector<cmXCodeObject*>& targets)
  3754. {
  3755. cmGeneratedFileStream makefileStream(this->CurrentXCodeHackMakefile);
  3756. if (!makefileStream) {
  3757. cmSystemTools::Error("Could not create " + this->CurrentXCodeHackMakefile);
  3758. return;
  3759. }
  3760. makefileStream.SetCopyIfDifferent(true);
  3761. // one more pass for external depend information not handled
  3762. // correctly by xcode
  3763. /* clang-format off */
  3764. makefileStream << "# DO NOT EDIT\n";
  3765. makefileStream << "# This makefile makes sure all linkable targets are\n";
  3766. makefileStream << "# up-to-date with anything they link to\n"
  3767. "default:\n"
  3768. "\techo \"Do not invoke directly\"\n"
  3769. "\n";
  3770. /* clang-format on */
  3771. std::set<std::string> dummyRules;
  3772. // Write rules to help Xcode relink things at the right time.
  3773. /* clang-format off */
  3774. makefileStream <<
  3775. "# Rules to remove targets that are older than anything to which they\n"
  3776. "# link. This forces Xcode to relink the targets from scratch. It\n"
  3777. "# does not seem to check these dependencies itself.\n";
  3778. /* clang-format on */
  3779. for (const auto& configName : this->CurrentConfigurationTypes) {
  3780. for (auto target : targets) {
  3781. cmGeneratorTarget* gt = target->GetTarget();
  3782. if (gt->GetType() == cmStateEnums::EXECUTABLE ||
  3783. gt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3784. gt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3785. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3786. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3787. // Declare an entry point for the target post-build phase.
  3788. makefileStream << this->PostBuildMakeTarget(gt->GetName(), configName)
  3789. << ":\n";
  3790. }
  3791. if (gt->GetType() == cmStateEnums::EXECUTABLE ||
  3792. gt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3793. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3794. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3795. std::string tfull = gt->GetFullPath(configName);
  3796. std::string trel = this->ConvertToRelativeForMake(tfull);
  3797. // Add this target to the post-build phases of its dependencies.
  3798. auto const y = target->GetDependTargets().find(configName);
  3799. if (y != target->GetDependTargets().end()) {
  3800. for (auto const& deptgt : y->second) {
  3801. makefileStream << this->PostBuildMakeTarget(deptgt, configName)
  3802. << ": " << trel << "\n";
  3803. }
  3804. }
  3805. std::vector<cmGeneratorTarget*> objlibs;
  3806. gt->GetObjectLibrariesCMP0026(objlibs);
  3807. for (auto objLib : objlibs) {
  3808. makefileStream << this->PostBuildMakeTarget(objLib->GetName(),
  3809. configName)
  3810. << ": " << trel << "\n";
  3811. }
  3812. // Create a rule for this target.
  3813. makefileStream << trel << ":";
  3814. // List dependencies if any exist.
  3815. auto const x = target->GetDependLibraries().find(configName);
  3816. if (x != target->GetDependLibraries().end()) {
  3817. for (auto const& deplib : x->second) {
  3818. std::string file = this->ConvertToRelativeForMake(deplib);
  3819. makefileStream << "\\\n\t" << file;
  3820. dummyRules.insert(file);
  3821. }
  3822. }
  3823. for (auto objLib : objlibs) {
  3824. const std::string objLibName = objLib->GetName();
  3825. std::string d = cmStrCat(
  3826. this->GetObjectsDirectory(this->CurrentProject, configName, objLib,
  3827. OBJECT_LIBRARY_ARTIFACT_DIR),
  3828. "lib", objLibName, ".a");
  3829. std::string dependency = this->ConvertToRelativeForMake(d);
  3830. makefileStream << "\\\n\t" << dependency;
  3831. dummyRules.insert(dependency);
  3832. }
  3833. // Write the action to remove the target if it is out of date.
  3834. makefileStream << "\n";
  3835. makefileStream << "\t/bin/rm -f "
  3836. << this->ConvertToRelativeForMake(tfull) << "\n";
  3837. // if building for more than one architecture
  3838. // then remove those executables as well
  3839. if (this->Architectures.size() > 1) {
  3840. std::string universal = this->GetObjectsDirectory(
  3841. this->CurrentProject, configName, gt, "$(OBJDIR)/");
  3842. for (const auto& architecture : this->Architectures) {
  3843. std::string universalFile = cmStrCat(universal, architecture, '/',
  3844. gt->GetFullName(configName));
  3845. makefileStream << "\t/bin/rm -f "
  3846. << this->ConvertToRelativeForMake(universalFile)
  3847. << "\n";
  3848. }
  3849. }
  3850. makefileStream << "\n\n";
  3851. }
  3852. }
  3853. }
  3854. makefileStream << "\n\n"
  3855. << "# For each target create a dummy rule"
  3856. << "so the target does not have to exist\n";
  3857. for (auto const& dummyRule : dummyRules) {
  3858. makefileStream << dummyRule << ":\n";
  3859. }
  3860. }
  3861. void cmGlobalXCodeGenerator::OutputXCodeProject(
  3862. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  3863. {
  3864. if (generators.empty()) {
  3865. return;
  3866. }
  3867. if (!this->CreateXCodeObjects(root, generators)) {
  3868. return;
  3869. }
  3870. std::string xcodeDir = cmStrCat(root->GetCurrentBinaryDirectory(), '/',
  3871. root->GetProjectName(), ".xcodeproj");
  3872. cmSystemTools::MakeDirectory(xcodeDir);
  3873. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  3874. cmGeneratedFileStream fout(xcodeProjFile);
  3875. fout.SetCopyIfDifferent(true);
  3876. if (!fout) {
  3877. return;
  3878. }
  3879. this->WriteXCodePBXProj(fout, root, generators);
  3880. bool hasGeneratedSchemes = this->OutputXCodeSharedSchemes(xcodeDir, root);
  3881. this->OutputXCodeWorkspaceSettings(xcodeDir, hasGeneratedSchemes);
  3882. this->ClearXCodeObjects();
  3883. // Since this call may have created new cache entries, save the cache:
  3884. //
  3885. root->GetMakefile()->GetCMakeInstance()->SaveCache(
  3886. root->GetBinaryDirectory());
  3887. }
  3888. bool cmGlobalXCodeGenerator::OutputXCodeSharedSchemes(
  3889. const std::string& xcProjDir, cmLocalGenerator* root)
  3890. {
  3891. // collect all tests for the targets
  3892. std::map<std::string, cmXCodeScheme::TestObjects> testables;
  3893. for (const auto& obj : this->XCodeObjects) {
  3894. if (obj->GetType() != cmXCodeObject::OBJECT ||
  3895. obj->GetIsA() != cmXCodeObject::PBXNativeTarget) {
  3896. continue;
  3897. }
  3898. if (!obj->GetTarget()->IsXCTestOnApple()) {
  3899. continue;
  3900. }
  3901. cmProp testee = obj->GetTarget()->GetProperty("XCTEST_TESTEE");
  3902. if (!testee) {
  3903. continue;
  3904. }
  3905. testables[*testee].push_back(obj.get());
  3906. }
  3907. // generate scheme
  3908. bool ret = false;
  3909. // Since the lowest available Xcode version for testing was 6.4,
  3910. // I'm setting this as a limit then
  3911. if (this->XcodeVersion >= 64) {
  3912. for (const auto& obj : this->XCodeObjects) {
  3913. if (obj->GetType() == cmXCodeObject::OBJECT &&
  3914. (obj->GetIsA() == cmXCodeObject::PBXNativeTarget ||
  3915. obj->GetIsA() == cmXCodeObject::PBXAggregateTarget) &&
  3916. (root->GetMakefile()->GetCMakeInstance()->GetIsInTryCompile() ||
  3917. obj->GetTarget()->GetPropertyAsBool("XCODE_GENERATE_SCHEME"))) {
  3918. const std::string& targetName = obj->GetTarget()->GetName();
  3919. cmXCodeScheme schm(root, obj.get(), testables[targetName],
  3920. this->CurrentConfigurationTypes,
  3921. this->XcodeVersion);
  3922. schm.WriteXCodeSharedScheme(xcProjDir,
  3923. this->RelativeToSource(xcProjDir));
  3924. ret = true;
  3925. }
  3926. }
  3927. }
  3928. return ret;
  3929. }
  3930. void cmGlobalXCodeGenerator::OutputXCodeWorkspaceSettings(
  3931. const std::string& xcProjDir, bool hasGeneratedSchemes)
  3932. {
  3933. std::string xcodeSharedDataDir =
  3934. cmStrCat(xcProjDir, "/project.xcworkspace/xcshareddata");
  3935. cmSystemTools::MakeDirectory(xcodeSharedDataDir);
  3936. std::string workspaceSettingsFile =
  3937. cmStrCat(xcodeSharedDataDir, "/WorkspaceSettings.xcsettings");
  3938. cmGeneratedFileStream fout(workspaceSettingsFile);
  3939. fout.SetCopyIfDifferent(true);
  3940. if (!fout) {
  3941. return;
  3942. }
  3943. cmXMLWriter xout(fout);
  3944. xout.StartDocument();
  3945. xout.Doctype("plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\""
  3946. "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
  3947. xout.StartElement("plist");
  3948. xout.Attribute("version", "1.0");
  3949. xout.StartElement("dict");
  3950. if (this->XcodeVersion >= 100) {
  3951. xout.Element("key", "BuildSystemType");
  3952. switch (this->XcodeBuildSystem) {
  3953. case BuildSystem::One:
  3954. xout.Element("string", "Original");
  3955. xout.Element("key", "DisableBuildSystemDeprecationWarning");
  3956. xout.Element("true");
  3957. break;
  3958. case BuildSystem::Twelve:
  3959. xout.Element("string", "Latest");
  3960. break;
  3961. }
  3962. }
  3963. if (hasGeneratedSchemes) {
  3964. xout.Element("key",
  3965. "IDEWorkspaceSharedSettings_AutocreateContextsIfNeeded");
  3966. xout.Element("false");
  3967. }
  3968. xout.EndElement(); // dict
  3969. xout.EndElement(); // plist
  3970. xout.EndDocument();
  3971. }
  3972. void cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  3973. cmLocalGenerator*,
  3974. std::vector<cmLocalGenerator*>&)
  3975. {
  3976. SortXCodeObjects();
  3977. fout << "// !$*UTF8*$!\n";
  3978. fout << "{\n";
  3979. cmXCodeObject::Indent(1, fout);
  3980. fout << "archiveVersion = 1;\n";
  3981. cmXCodeObject::Indent(1, fout);
  3982. fout << "classes = {\n";
  3983. cmXCodeObject::Indent(1, fout);
  3984. fout << "};\n";
  3985. cmXCodeObject::Indent(1, fout);
  3986. fout << "objectVersion = 46;\n";
  3987. cmXCode21Object::PrintList(this->XCodeObjects, fout);
  3988. cmXCodeObject::Indent(1, fout);
  3989. fout << "rootObject = " << this->RootObject->GetId()
  3990. << " /* Project object */;\n";
  3991. fout << "}\n";
  3992. }
  3993. const char* cmGlobalXCodeGenerator::GetCMakeCFGIntDir() const
  3994. {
  3995. return "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)";
  3996. }
  3997. std::string cmGlobalXCodeGenerator::ExpandCFGIntDir(
  3998. const std::string& str, const std::string& config) const
  3999. {
  4000. std::string replace1 = "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)";
  4001. std::string replace2 = "$(CONFIGURATION)";
  4002. std::string tmp = str;
  4003. for (std::string::size_type i = tmp.find(replace1); i != std::string::npos;
  4004. i = tmp.find(replace1, i)) {
  4005. tmp.replace(i, replace1.size(), config);
  4006. i += config.size();
  4007. }
  4008. for (std::string::size_type i = tmp.find(replace2); i != std::string::npos;
  4009. i = tmp.find(replace2, i)) {
  4010. tmp.replace(i, replace2.size(), config);
  4011. i += config.size();
  4012. }
  4013. return tmp;
  4014. }
  4015. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  4016. {
  4017. entry.Name = cmGlobalXCodeGenerator::GetActualName();
  4018. entry.Brief = "Generate Xcode project files.";
  4019. }
  4020. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(
  4021. std::string const& p)
  4022. {
  4023. return cmSystemTools::ConvertToOutputPath(p);
  4024. }
  4025. std::string cmGlobalXCodeGenerator::RelativeToSource(const std::string& p)
  4026. {
  4027. // We force conversion because Xcode breakpoints do not work unless
  4028. // they are in a file named relative to the source tree.
  4029. return cmSystemTools::ForceToRelativePath(
  4030. cmSystemTools::JoinPath(this->ProjectSourceDirectoryComponents), p);
  4031. }
  4032. std::string cmGlobalXCodeGenerator::RelativeToBinary(const std::string& p)
  4033. {
  4034. return this->CurrentLocalGenerator->MaybeConvertToRelativePath(
  4035. cmSystemTools::JoinPath(this->ProjectOutputDirectoryComponents), p);
  4036. }
  4037. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const std::string& p)
  4038. {
  4039. if (p.find_first_of(" []") != std::string::npos) {
  4040. std::string t = cmStrCat('"', p, '"');
  4041. return t;
  4042. }
  4043. return p;
  4044. }
  4045. void cmGlobalXCodeGenerator::AppendDirectoryForConfig(
  4046. const std::string& prefix, const std::string& config,
  4047. const std::string& suffix, std::string& dir)
  4048. {
  4049. if (!config.empty()) {
  4050. dir += prefix;
  4051. dir += config;
  4052. dir += suffix;
  4053. }
  4054. }
  4055. std::string cmGlobalXCodeGenerator::LookupFlags(
  4056. const std::string& varNamePrefix, const std::string& varNameLang,
  4057. const std::string& varNameSuffix, const std::string& default_flags)
  4058. {
  4059. if (!varNameLang.empty()) {
  4060. std::string varName = cmStrCat(varNamePrefix, varNameLang, varNameSuffix);
  4061. if (cmProp varValue = this->CurrentMakefile->GetDefinition(varName)) {
  4062. if (!varValue->empty()) {
  4063. return *varValue;
  4064. }
  4065. }
  4066. }
  4067. return default_flags;
  4068. }
  4069. void cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  4070. const char* defines_list,
  4071. bool dflag)
  4072. {
  4073. // Skip this if there are no definitions.
  4074. if (!defines_list) {
  4075. return;
  4076. }
  4077. // Expand the list of definitions.
  4078. std::vector<std::string> defines = cmExpandedList(defines_list);
  4079. // Store the definitions in the string.
  4080. this->AppendDefines(defs, defines, dflag);
  4081. }
  4082. void cmGlobalXCodeGenerator::AppendDefines(
  4083. BuildObjectListOrString& defs, std::vector<std::string> const& defines,
  4084. bool dflag)
  4085. {
  4086. // GCC_PREPROCESSOR_DEFINITIONS is a space-separated list of definitions.
  4087. std::string def;
  4088. for (auto const& define : defines) {
  4089. // Start with -D if requested.
  4090. def = cmStrCat(dflag ? "-D" : "", define);
  4091. // Append the flag with needed escapes.
  4092. std::string tmp;
  4093. this->AppendFlag(tmp, def);
  4094. defs.Add(tmp);
  4095. }
  4096. }
  4097. void cmGlobalXCodeGenerator::AppendFlag(std::string& flags,
  4098. std::string const& flag) const
  4099. {
  4100. // Short-circuit for an empty flag.
  4101. if (flag.empty()) {
  4102. return;
  4103. }
  4104. // Separate from previous flags.
  4105. if (!flags.empty()) {
  4106. flags += " ";
  4107. }
  4108. // Check if the flag needs quoting.
  4109. bool quoteFlag =
  4110. flag.find_first_of("`~!@#$%^&*()+={}[]|:;\"'<>,.? ") != std::string::npos;
  4111. // We escape a flag as follows:
  4112. // - Place each flag in single quotes ''
  4113. // - Escape a single quote as \'
  4114. // - Escape a backslash as \\ since it itself is an escape
  4115. // Note that in the code below we need one more level of escapes for
  4116. // C string syntax in this source file.
  4117. //
  4118. // The final level of escaping is done when the string is stored
  4119. // into the project file by cmXCodeObject::PrintString.
  4120. if (quoteFlag) {
  4121. // Open single quote.
  4122. flags += "'";
  4123. }
  4124. // Flag value with escaped quotes and backslashes.
  4125. for (auto c : flag) {
  4126. if (c == '\'') {
  4127. flags += "'\\''";
  4128. } else if (c == '\\') {
  4129. flags += "\\\\";
  4130. } else {
  4131. flags += c;
  4132. }
  4133. }
  4134. if (quoteFlag) {
  4135. // Close single quote.
  4136. flags += "'";
  4137. }
  4138. }
  4139. std::string cmGlobalXCodeGenerator::ComputeInfoPListLocation(
  4140. cmGeneratorTarget* target)
  4141. {
  4142. std::string plist =
  4143. cmStrCat(target->GetLocalGenerator()->GetCurrentBinaryDirectory(),
  4144. "/CMakeFiles/", target->GetName(), ".dir/Info.plist");
  4145. return plist;
  4146. }
  4147. // Return true if the generated build tree may contain multiple builds.
  4148. // i.e. "Can I build Debug and Release in the same tree?"
  4149. bool cmGlobalXCodeGenerator::IsMultiConfig() const
  4150. {
  4151. // Newer Xcode versions are multi config:
  4152. return true;
  4153. }
  4154. bool cmGlobalXCodeGenerator::HasKnownObjectFileLocation(
  4155. std::string* reason) const
  4156. {
  4157. if (this->ObjectDirArch.find('$') != std::string::npos) {
  4158. if (reason != nullptr) {
  4159. *reason = " under Xcode with multiple architectures";
  4160. }
  4161. return false;
  4162. }
  4163. return true;
  4164. }
  4165. bool cmGlobalXCodeGenerator::UseEffectivePlatformName(cmMakefile* mf) const
  4166. {
  4167. cmProp epnValue = this->GetCMakeInstance()->GetState()->GetGlobalProperty(
  4168. "XCODE_EMIT_EFFECTIVE_PLATFORM_NAME");
  4169. if (!epnValue) {
  4170. return mf->PlatformIsAppleEmbedded();
  4171. }
  4172. return cmIsOn(*epnValue);
  4173. }
  4174. bool cmGlobalXCodeGenerator::ShouldStripResourcePath(cmMakefile*) const
  4175. {
  4176. // Xcode determines Resource location itself
  4177. return true;
  4178. }
  4179. void cmGlobalXCodeGenerator::ComputeTargetObjectDirectory(
  4180. cmGeneratorTarget* gt) const
  4181. {
  4182. std::string configName = this->GetCMakeCFGIntDir();
  4183. std::string dir =
  4184. cmStrCat(this->GetObjectsDirectory("$(PROJECT_NAME)", configName, gt,
  4185. "$(OBJECT_FILE_DIR_normal:base)/"),
  4186. this->ObjectDirArch, '/');
  4187. gt->ObjectDirectory = dir;
  4188. }
  4189. std::string cmGlobalXCodeGenerator::GetDeploymentPlatform(const cmMakefile* mf)
  4190. {
  4191. switch (mf->GetAppleSDKType()) {
  4192. case cmMakefile::AppleSDK::AppleTVOS:
  4193. case cmMakefile::AppleSDK::AppleTVSimulator:
  4194. return "TVOS_DEPLOYMENT_TARGET";
  4195. case cmMakefile::AppleSDK::IPhoneOS:
  4196. case cmMakefile::AppleSDK::IPhoneSimulator:
  4197. return "IPHONEOS_DEPLOYMENT_TARGET";
  4198. case cmMakefile::AppleSDK::WatchOS:
  4199. case cmMakefile::AppleSDK::WatchSimulator:
  4200. return "WATCHOS_DEPLOYMENT_TARGET";
  4201. case cmMakefile::AppleSDK::MacOS:
  4202. default:
  4203. return "MACOSX_DEPLOYMENT_TARGET";
  4204. }
  4205. }