1
0

cmGlobalXCodeGenerator.cxx 134 KB

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