cmGlobalXCodeGenerator.cxx 135 KB

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