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