cmGlobalXCodeGenerator.cxx 100 KB

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