cmGlobalXCodeGenerator.cxx 101 KB

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