cmGlobalXCodeGenerator.cxx 133 KB

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