cmGlobalXCodeGenerator.cxx 125 KB

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