cmGlobalXCodeGenerator.cxx 129 KB

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