cmGlobalXCodeGenerator.cxx 94 KB

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