cmGlobalXCodeGenerator.cxx 112 KB

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