cmGlobalXCodeGenerator.cxx 102 KB

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