cmGlobalXCodeGenerator.cxx 134 KB

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