cmGlobalXCodeGenerator.cxx 133 KB

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