cmGlobalXCodeGenerator.cxx 88 KB

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