cmGlobalXCodeGenerator.cxx 134 KB

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