cmGlobalXCodeGenerator.cxx 130 KB

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