cmGlobalXCodeGenerator.cxx 87 KB

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