cmGlobalXCodeGenerator.cxx 134 KB

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