cmGlobalXCodeGenerator.cxx 88 KB

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