cmGlobalXCodeGenerator.cxx 134 KB

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