cmGlobalXCodeGenerator.cxx 79 KB

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