cmGlobalXCodeGenerator.cxx 100 KB

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