cmGlobalXCodeGenerator.cxx 133 KB

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