cmGlobalXCodeGenerator.cxx 129 KB

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