cmGlobalXCodeGenerator.cxx 88 KB

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