cmGlobalXCodeGenerator.cxx 81 KB

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