cmGlobalXCodeGenerator.cxx 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667
  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. if(target.GetPropertyAsBool("FRAMEWORK"))
  1095. {
  1096. pndir += "/..";
  1097. pndir = cmSystemTools::CollapseFullPath(pndir.c_str());
  1098. }
  1099. buildSettings->AddAttribute("SYMROOT",
  1100. this->CreateString(pndir.c_str()));
  1101. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  1102. this->CreateString(pnprefix.c_str()));
  1103. buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
  1104. this->CreateString(pnsuffix.c_str()));
  1105. }
  1106. // Handle settings for each target type.
  1107. switch(target.GetType())
  1108. {
  1109. case cmTarget::STATIC_LIBRARY:
  1110. {
  1111. fileType = "archive.ar";
  1112. productType = "com.apple.product-type.library.static";
  1113. buildSettings->AddAttribute("LIBRARY_STYLE",
  1114. this->CreateString("STATIC"));
  1115. break;
  1116. }
  1117. case cmTarget::MODULE_LIBRARY:
  1118. {
  1119. buildSettings->AddAttribute("LIBRARY_STYLE",
  1120. this->CreateString("BUNDLE"));
  1121. if(this->XcodeVersion >= 22)
  1122. {
  1123. fileType = "compiled.mach-o.executable";
  1124. productType = "com.apple.product-type.tool";
  1125. buildSettings->AddAttribute("MACH_O_TYPE",
  1126. this->CreateString("mh_bundle"));
  1127. buildSettings->AddAttribute("GCC_DYNAMIC_NO_PIC",
  1128. this->CreateString("NO"));
  1129. // Add the flags to create an executable.
  1130. std::string createFlags =
  1131. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1132. if(!createFlags.empty())
  1133. {
  1134. extraLinkOptions += " ";
  1135. extraLinkOptions += createFlags;
  1136. }
  1137. }
  1138. else
  1139. {
  1140. fileType = "compiled.mach-o.dylib";
  1141. productType = "com.apple.product-type.library.dynamic";
  1142. // Add the flags to create a module.
  1143. std::string createFlags =
  1144. this->LookupFlags("CMAKE_SHARED_MODULE_CREATE_", lang, "_FLAGS",
  1145. "-bundle");
  1146. if(!createFlags.empty())
  1147. {
  1148. extraLinkOptions += " ";
  1149. extraLinkOptions += createFlags;
  1150. }
  1151. }
  1152. break;
  1153. }
  1154. case cmTarget::SHARED_LIBRARY:
  1155. {
  1156. if(target.GetPropertyAsBool("FRAMEWORK"))
  1157. {
  1158. fileType = "wrapper.framework";
  1159. productType = "com.apple.product-type.framework";
  1160. break;
  1161. }
  1162. fileType = "compiled.mach-o.dylib";
  1163. productType = "com.apple.product-type.library.dynamic";
  1164. buildSettings->AddAttribute("LIBRARY_STYLE",
  1165. this->CreateString("DYNAMIC"));
  1166. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  1167. this->CreateString("1"));
  1168. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  1169. this->CreateString("1"));
  1170. // Add the flags to create a shared library.
  1171. std::string createFlags =
  1172. this->LookupFlags("CMAKE_SHARED_LIBRARY_CREATE_", lang, "_FLAGS",
  1173. "-dynamiclib");
  1174. if(!createFlags.empty())
  1175. {
  1176. extraLinkOptions += " ";
  1177. extraLinkOptions += createFlags;
  1178. }
  1179. break;
  1180. }
  1181. case cmTarget::EXECUTABLE:
  1182. {
  1183. fileType = "compiled.mach-o.executable";
  1184. // Add the flags to create an executable.
  1185. std::string createFlags =
  1186. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1187. if(!createFlags.empty())
  1188. {
  1189. extraLinkOptions += " ";
  1190. extraLinkOptions += createFlags;
  1191. }
  1192. // Handle bundles and normal executables separately.
  1193. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  1194. {
  1195. productType = "com.apple.product-type.application";
  1196. std::string f1 =
  1197. this->CurrentMakefile->GetModulesFile("MacOSXBundleInfo.plist.in");
  1198. if ( f1.size() == 0 )
  1199. {
  1200. cmSystemTools::Error("could not find Mac OSX bundle template file.");
  1201. }
  1202. std::string f2 = this->CurrentMakefile->GetCurrentOutputDirectory();
  1203. f2 += "/Info.plist";
  1204. this->CurrentMakefile->ConfigureFile(f1.c_str(), f2.c_str(),
  1205. false, false, false);
  1206. std::string path =
  1207. this->ConvertToRelativeForXCode(f2.c_str());
  1208. buildSettings->AddAttribute("INFOPLIST_FILE",
  1209. this->CreateString(path.c_str()));
  1210. }
  1211. else
  1212. {
  1213. productType = "com.apple.product-type.tool";
  1214. }
  1215. }
  1216. break;
  1217. default:
  1218. break;
  1219. }
  1220. if(this->XcodeVersion >= 22)
  1221. {
  1222. buildSettings->AddAttribute("PREBINDING",
  1223. this->CreateString("NO"));
  1224. }
  1225. std::string dirs;
  1226. std::vector<std::string> includes;
  1227. this->CurrentLocalGenerator->GetIncludeDirectories(includes);
  1228. std::vector<std::string>::iterator i = includes.begin();
  1229. std::string fdirs;
  1230. std::set<cmStdString> emitted;
  1231. emitted.insert("/System/Library/Frameworks");
  1232. for(;i != includes.end(); ++i)
  1233. {
  1234. if(cmSystemTools::IsPathToFramework(i->c_str()))
  1235. {
  1236. std::string frameworkDir = *i;
  1237. frameworkDir += "/../";
  1238. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1239. if(emitted.insert(frameworkDir).second)
  1240. {
  1241. fdirs += this->XCodeEscapePath(frameworkDir.c_str());
  1242. fdirs += " ";
  1243. }
  1244. }
  1245. else
  1246. {
  1247. std::string incpath =
  1248. this->XCodeEscapePath(i->c_str());
  1249. dirs += incpath + " ";
  1250. }
  1251. }
  1252. std::vector<std::string>& frameworks = target.GetFrameworks();
  1253. if(frameworks.size())
  1254. {
  1255. for(std::vector<std::string>::iterator fmIt = frameworks.begin();
  1256. fmIt != frameworks.end(); ++fmIt)
  1257. {
  1258. if(emitted.insert(*fmIt).second)
  1259. {
  1260. fdirs += this->XCodeEscapePath(fmIt->c_str());
  1261. fdirs += " ";
  1262. }
  1263. }
  1264. }
  1265. if(fdirs.size())
  1266. {
  1267. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS",
  1268. this->CreateString(fdirs.c_str()));
  1269. }
  1270. if(dirs.size())
  1271. {
  1272. buildSettings->AddAttribute("HEADER_SEARCH_PATHS",
  1273. this->CreateString(dirs.c_str()));
  1274. }
  1275. std::string oflagc = this->ExtractFlag("-O", cflags);
  1276. char optLevel[2];
  1277. optLevel[0] = '0';
  1278. optLevel[1] = 0;
  1279. if(oflagc.size() == 3)
  1280. {
  1281. optLevel[0] = oflagc[2];
  1282. }
  1283. if(oflagc.size() == 2)
  1284. {
  1285. optLevel[0] = '1';
  1286. }
  1287. std::string oflag = this->ExtractFlag("-O", flags);
  1288. if(oflag.size() == 3)
  1289. {
  1290. optLevel[0] = oflag[2];
  1291. }
  1292. if(oflag.size() == 2)
  1293. {
  1294. optLevel[0] = '1';
  1295. }
  1296. std::string gflagc = this->ExtractFlag("-g", cflags);
  1297. // put back gdwarf-2 if used since there is no way
  1298. // to represent it in the gui, but we still want debug yes
  1299. if(gflagc == "-gdwarf-2")
  1300. {
  1301. cflags += " ";
  1302. cflags += gflagc;
  1303. }
  1304. std::string gflag = this->ExtractFlag("-g", flags);
  1305. if(gflag == "-gdwarf-2")
  1306. {
  1307. flags += " ";
  1308. flags += gflag;
  1309. }
  1310. const char* debugStr = "YES";
  1311. if(gflagc.size() ==0 && gflag.size() == 0)
  1312. {
  1313. debugStr = "NO";
  1314. }
  1315. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  1316. this->CreateString(debugStr));
  1317. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  1318. this->CreateString(optLevel));
  1319. buildSettings->AddAttribute("OPTIMIZATION_CFLAGS",
  1320. this->CreateString(oflagc.c_str()));
  1321. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  1322. this->CreateString("NO"));
  1323. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  1324. this->CreateString("NO"));
  1325. if(lang && strcmp(lang, "CXX") == 0)
  1326. {
  1327. flags += " ";
  1328. flags += defFlags;
  1329. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  1330. this->CreateString(flags.c_str()));
  1331. cflags += " ";
  1332. cflags += defFlags;
  1333. buildSettings->AddAttribute("OTHER_CFLAGS",
  1334. this->CreateString(cflags.c_str()));
  1335. }
  1336. else
  1337. {
  1338. flags += " ";
  1339. flags += defFlags;
  1340. buildSettings->AddAttribute("OTHER_CFLAGS",
  1341. this->CreateString(flags.c_str()));
  1342. }
  1343. // Create the INSTALL_PATH attribute.
  1344. std::string install_name_dir;
  1345. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1346. {
  1347. // Get the install_name directory for the build tree.
  1348. install_name_dir = target.GetInstallNameDirForBuildTree(configName);
  1349. if(install_name_dir.empty())
  1350. {
  1351. // Xcode will not pass the -install_name option at all if INSTALL_PATH
  1352. // is not given or is empty. We must explicitly put the flag in the
  1353. // link flags to create an install_name with just the library soname.
  1354. extraLinkOptions += " -install_name ";
  1355. extraLinkOptions += productName;
  1356. }
  1357. else
  1358. {
  1359. // Convert to a path for the native build tool.
  1360. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  1361. // do not escape spaces on this since it is only a single path
  1362. }
  1363. }
  1364. buildSettings->AddAttribute("INSTALL_PATH",
  1365. this->CreateString(install_name_dir.c_str()));
  1366. buildSettings->AddAttribute("OTHER_LDFLAGS",
  1367. this->CreateString(extraLinkOptions.c_str()));
  1368. buildSettings->AddAttribute("OTHER_REZFLAGS",
  1369. this->CreateString(""));
  1370. buildSettings->AddAttribute("SECTORDER_FLAGS",
  1371. this->CreateString(""));
  1372. buildSettings->AddAttribute("USE_HEADERMAP",
  1373. this->CreateString("NO"));
  1374. buildSettings->AddAttribute("WARNING_CFLAGS",
  1375. this->CreateString(
  1376. "-Wmost -Wno-four-char-constants"
  1377. " -Wno-unknown-pragmas"));
  1378. }
  1379. //----------------------------------------------------------------------------
  1380. cmXCodeObject*
  1381. cmGlobalXCodeGenerator::CreateUtilityTarget(cmTarget& cmtarget)
  1382. {
  1383. cmXCodeObject* shellBuildPhase =
  1384. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1385. shellBuildPhase->AddAttribute("buildActionMask",
  1386. this->CreateString("2147483647"));
  1387. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1388. shellBuildPhase->AddAttribute("files", buildFiles);
  1389. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1390. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  1391. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1392. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  1393. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1394. this->CreateString("0"));
  1395. shellBuildPhase->AddAttribute("shellPath",
  1396. this->CreateString("/bin/sh"));
  1397. shellBuildPhase->AddAttribute("shellScript",
  1398. this->CreateString(
  1399. "# shell script goes here\nexit 0"));
  1400. cmXCodeObject* target =
  1401. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  1402. target->SetComment(cmtarget.GetName());
  1403. cmXCodeObject* buildPhases =
  1404. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1405. this->CreateCustomCommands(buildPhases, 0, 0, 0, cmtarget);
  1406. target->AddAttribute("buildPhases", buildPhases);
  1407. cmXCodeObject* buildSettings =
  1408. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1409. std::string fileTypeString;
  1410. std::string productTypeString;
  1411. std::string productName;
  1412. const char* globalConfig = 0;
  1413. if(this->XcodeVersion > 20)
  1414. {
  1415. this->AddConfigurations(target, cmtarget);
  1416. }
  1417. else
  1418. {
  1419. globalConfig = this->CurrentMakefile->GetDefinition("CMAKE_BUILD_TYPE");
  1420. }
  1421. this->CreateBuildSettings(cmtarget,
  1422. buildSettings, fileTypeString,
  1423. productTypeString, productName, globalConfig);
  1424. target->AddAttribute("buildSettings", buildSettings);
  1425. cmXCodeObject* dependencies =
  1426. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1427. target->AddAttribute("dependencies", dependencies);
  1428. target->AddAttribute("name", this->CreateString(productName.c_str()));
  1429. target->AddAttribute("productName",this->CreateString(productName.c_str()));
  1430. target->SetTarget(&cmtarget);
  1431. return target;
  1432. }
  1433. //----------------------------------------------------------------------------
  1434. void cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  1435. cmTarget& cmtarget)
  1436. {
  1437. std::string configTypes =
  1438. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES");
  1439. std::vector<std::string> configVectorIn;
  1440. std::vector<std::string> configVector;
  1441. configVectorIn.push_back(configTypes);
  1442. cmSystemTools::ExpandList(configVectorIn, configVector);
  1443. cmXCodeObject* configlist =
  1444. this->CreateObject(cmXCodeObject::XCConfigurationList);
  1445. cmXCodeObject* buildConfigurations =
  1446. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1447. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  1448. std::string comment = "Build configuration list for ";
  1449. comment += cmXCodeObject::PBXTypeNames[target->GetIsA()];
  1450. comment += " \"";
  1451. comment += cmtarget.GetName();
  1452. comment += "\"";
  1453. configlist->SetComment(comment.c_str());
  1454. target->AddAttribute("buildConfigurationList",
  1455. this->CreateObjectReference(configlist));
  1456. for(unsigned int i = 0; i < configVector.size(); ++i)
  1457. {
  1458. cmXCodeObject* config =
  1459. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  1460. buildConfigurations->AddObject(config);
  1461. cmXCodeObject* buildSettings =
  1462. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1463. std::string fileTypeString;
  1464. std::string productTypeString;
  1465. std::string productName;
  1466. this->CreateBuildSettings(cmtarget,
  1467. buildSettings, fileTypeString,
  1468. productTypeString, productName,
  1469. configVector[i].c_str());
  1470. config->AddAttribute("name", this->CreateString(configVector[i].c_str()));
  1471. config->SetComment(configVector[i].c_str());
  1472. config->AddAttribute("buildSettings", buildSettings);
  1473. }
  1474. if(configVector.size())
  1475. {
  1476. configlist->AddAttribute("defaultConfigurationName",
  1477. this->CreateString(configVector[0].c_str()));
  1478. configlist->AddAttribute("defaultConfigurationIsVisible",
  1479. this->CreateString("0"));
  1480. }
  1481. }
  1482. //----------------------------------------------------------------------------
  1483. cmXCodeObject*
  1484. cmGlobalXCodeGenerator::CreateXCodeTarget(cmTarget& cmtarget,
  1485. cmXCodeObject* buildPhases)
  1486. {
  1487. cmXCodeObject* target =
  1488. this->CreateObject(cmXCodeObject::PBXNativeTarget);
  1489. target->AddAttribute("buildPhases", buildPhases);
  1490. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1491. target->AddAttribute("buildRules", buildRules);
  1492. cmXCodeObject* buildSettings =
  1493. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1494. std::string fileTypeString;
  1495. std::string productTypeString;
  1496. std::string productName;
  1497. const char* globalConfig = 0;
  1498. if(this->XcodeVersion > 20)
  1499. {
  1500. this->AddConfigurations(target, cmtarget);
  1501. }
  1502. else
  1503. {
  1504. globalConfig = this->CurrentMakefile->GetDefinition("CMAKE_BUILD_TYPE");
  1505. }
  1506. this->CreateBuildSettings(cmtarget,
  1507. buildSettings, fileTypeString,
  1508. productTypeString, productName, globalConfig);
  1509. target->AddAttribute("buildSettings", buildSettings);
  1510. cmXCodeObject* dependencies =
  1511. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1512. target->AddAttribute("dependencies", dependencies);
  1513. target->AddAttribute("name", this->CreateString(productName.c_str()));
  1514. target->AddAttribute("productName",this->CreateString(productName.c_str()));
  1515. cmXCodeObject* fileRef =
  1516. this->CreateObject(cmXCodeObject::PBXFileReference);
  1517. fileRef->AddAttribute("explicitFileType",
  1518. this->CreateString(fileTypeString.c_str()));
  1519. fileRef->AddAttribute("path", this->CreateString(productName.c_str()));
  1520. fileRef->AddAttribute("refType", this->CreateString("0"));
  1521. fileRef->AddAttribute("sourceTree",
  1522. this->CreateString("BUILT_PRODUCTS_DIR"));
  1523. fileRef->SetComment(cmtarget.GetName());
  1524. target->AddAttribute("productReference",
  1525. this->CreateObjectReference(fileRef));
  1526. target->AddAttribute("productType",
  1527. this->CreateString(productTypeString.c_str()));
  1528. target->SetTarget(&cmtarget);
  1529. return target;
  1530. }
  1531. //----------------------------------------------------------------------------
  1532. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(cmTarget* t)
  1533. {
  1534. if(!t)
  1535. {
  1536. return 0;
  1537. }
  1538. for(std::vector<cmXCodeObject*>::iterator i = this->XCodeObjects.begin();
  1539. i != this->XCodeObjects.end(); ++i)
  1540. {
  1541. cmXCodeObject* o = *i;
  1542. if(o->GetTarget() == t)
  1543. {
  1544. return o;
  1545. }
  1546. }
  1547. return 0;
  1548. }
  1549. //----------------------------------------------------------------------------
  1550. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  1551. cmXCodeObject* dependTarget)
  1552. {
  1553. // make sure a target does not depend on itself
  1554. if(target == dependTarget)
  1555. {
  1556. return;
  1557. }
  1558. // now avoid circular references if dependTarget already
  1559. // depends on target then skip it. Circular references crashes
  1560. // xcode
  1561. cmXCodeObject* dependTargetDepends =
  1562. dependTarget->GetObject("dependencies");
  1563. if(dependTargetDepends)
  1564. {
  1565. if(dependTargetDepends->HasObject(target->GetPBXTargetDependency()))
  1566. {
  1567. return;
  1568. }
  1569. }
  1570. cmXCodeObject* targetdep = dependTarget->GetPBXTargetDependency();
  1571. if(!targetdep)
  1572. {
  1573. cmXCodeObject* container =
  1574. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  1575. container->SetComment("PBXContainerItemProxy");
  1576. container->AddAttribute("containerPortal",
  1577. this->CreateObjectReference(this->RootObject));
  1578. container->AddAttribute("proxyType", this->CreateString("1"));
  1579. container->AddAttribute("remoteGlobalIDString",
  1580. this->CreateObjectReference(dependTarget));
  1581. container->AddAttribute("remoteInfo",
  1582. this->CreateString(
  1583. dependTarget->GetTarget()->GetName()));
  1584. targetdep =
  1585. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  1586. targetdep->SetComment("PBXTargetDependency");
  1587. targetdep->AddAttribute("target",
  1588. this->CreateObjectReference(dependTarget));
  1589. targetdep->AddAttribute("targetProxy",
  1590. this->CreateObjectReference(container));
  1591. dependTarget->SetPBXTargetDependency(targetdep);
  1592. }
  1593. cmXCodeObject* depends = target->GetObject("dependencies");
  1594. if(!depends)
  1595. {
  1596. cmSystemTools::
  1597. Error("target does not have dependencies attribute error..");
  1598. }
  1599. else
  1600. {
  1601. depends->AddUniqueObject(targetdep);
  1602. }
  1603. }
  1604. //----------------------------------------------------------------------------
  1605. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  1606. const char* attribute,
  1607. const char* value)
  1608. {
  1609. if(settings)
  1610. {
  1611. cmXCodeObject* attr = settings->GetObject(attribute);
  1612. if(!attr)
  1613. {
  1614. settings->AddAttribute(attribute, this->CreateString(value));
  1615. }
  1616. else
  1617. {
  1618. std::string oldValue = attr->GetString();
  1619. cmSystemTools::ReplaceString(oldValue, "\"", "");
  1620. oldValue += " ";
  1621. oldValue += value;
  1622. attr->SetString(oldValue.c_str());
  1623. }
  1624. }
  1625. }
  1626. //----------------------------------------------------------------------------
  1627. void cmGlobalXCodeGenerator
  1628. ::AppendBuildSettingAttribute(cmXCodeObject* target,
  1629. const char* attribute,
  1630. const char* value,
  1631. const char* configName)
  1632. {
  1633. if(this->XcodeVersion < 21)
  1634. {
  1635. // There is only one configuration. Add the setting to the buildSettings
  1636. // of the target.
  1637. this->AppendOrAddBuildSetting(target->GetObject("buildSettings"),
  1638. attribute, value);
  1639. }
  1640. else
  1641. {
  1642. // There are multiple configurations. Add the setting to the
  1643. // buildSettings of the configuration name given.
  1644. cmXCodeObject* configurationList =
  1645. target->GetObject("buildConfigurationList")->GetObject();
  1646. cmXCodeObject* buildConfigs =
  1647. configurationList->GetObject("buildConfigurations");
  1648. std::vector<cmXCodeObject*> list = buildConfigs->GetObjectList();
  1649. // each configuration and the target itself has a buildSettings in it
  1650. //list.push_back(target);
  1651. for(std::vector<cmXCodeObject*>::iterator i = list.begin();
  1652. i != list.end(); ++i)
  1653. {
  1654. if(configName)
  1655. {
  1656. if(strcmp((*i)->GetObject("name")->GetString(), configName) == 0)
  1657. {
  1658. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  1659. this->AppendOrAddBuildSetting(settings, attribute, value);
  1660. }
  1661. }
  1662. else
  1663. {
  1664. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  1665. this->AppendOrAddBuildSetting(settings, attribute, value);
  1666. }
  1667. }
  1668. }
  1669. }
  1670. //----------------------------------------------------------------------------
  1671. void cmGlobalXCodeGenerator
  1672. ::AddDependAndLinkInformation(cmXCodeObject* target)
  1673. {
  1674. cmTarget* cmtarget = target->GetTarget();
  1675. if(!cmtarget)
  1676. {
  1677. cmSystemTools::Error("Error no target on xobject\n");
  1678. return;
  1679. }
  1680. // Add dependencies on other CMake targets.
  1681. if(cmtarget->GetType() != cmTarget::STATIC_LIBRARY)
  1682. {
  1683. // Keep track of dependencies already listed.
  1684. std::set<cmStdString> emitted;
  1685. // A target should not depend on itself.
  1686. emitted.insert(cmtarget->GetName());
  1687. // Loop over all library dependencies.
  1688. const cmTarget::LinkLibraryVectorType& tlibs =
  1689. cmtarget->GetLinkLibraries();
  1690. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  1691. lib != tlibs.end(); ++lib)
  1692. {
  1693. // Don't emit the same library twice for this target.
  1694. if(emitted.insert(lib->first).second)
  1695. {
  1696. // Add this dependency.
  1697. cmTarget* t = this->FindTarget(this->CurrentProject.c_str(),
  1698. lib->first.c_str());
  1699. cmXCodeObject* dptarget = this->FindXCodeTarget(t);
  1700. if(dptarget)
  1701. {
  1702. this->AddDependTarget(target, dptarget);
  1703. }
  1704. }
  1705. }
  1706. }
  1707. // write utility dependencies.
  1708. for(std::set<cmStdString>::const_iterator i
  1709. = cmtarget->GetUtilities().begin();
  1710. i != cmtarget->GetUtilities().end(); ++i)
  1711. {
  1712. cmTarget* t = this->FindTarget(this->CurrentProject.c_str(),
  1713. i->c_str());
  1714. // if the target is in this project then make target depend
  1715. // on it. It may not be in this project if this is a sub
  1716. // project from the top.
  1717. if(t)
  1718. {
  1719. cmXCodeObject* dptarget = this->FindXCodeTarget(t);
  1720. if(dptarget)
  1721. {
  1722. this->AddDependTarget(target, dptarget);
  1723. }
  1724. else
  1725. {
  1726. std::string m = "Error Utility: ";
  1727. m += i->c_str();
  1728. m += "\n";
  1729. m += "cmtarget ";
  1730. if(t)
  1731. {
  1732. m += t->GetName();
  1733. }
  1734. m += "\n";
  1735. m += "Is on the target ";
  1736. m += cmtarget->GetName();
  1737. m += "\n";
  1738. m += "But it has no xcode target created yet??\n";
  1739. m += "Current project is ";
  1740. m += this->CurrentProject.c_str();
  1741. cmSystemTools::Error(m.c_str());
  1742. }
  1743. }
  1744. }
  1745. // Loop over configuration types and set per-configuration info.
  1746. for(std::vector<std::string>::iterator i =
  1747. this->CurrentConfigurationTypes.begin();
  1748. i != this->CurrentConfigurationTypes.end(); ++i)
  1749. {
  1750. // Get the current configuration name.
  1751. const char* configName = i->c_str();
  1752. if(!*configName)
  1753. {
  1754. configName = 0;
  1755. }
  1756. // Compute the link library and directory information.
  1757. std::vector<cmStdString> libNames;
  1758. std::vector<cmStdString> libDirs;
  1759. std::vector<cmStdString> fullPathLibs;
  1760. this->CurrentLocalGenerator->ComputeLinkInformation(*cmtarget, configName,
  1761. libNames, libDirs,
  1762. &fullPathLibs);
  1763. // Add dependencies directly on library files.
  1764. for(std::vector<cmStdString>::iterator j = fullPathLibs.begin();
  1765. j != fullPathLibs.end(); ++j)
  1766. {
  1767. target->AddDependLibrary(configName, j->c_str());
  1768. }
  1769. std::string linkDirs;
  1770. // add the library search paths
  1771. for(std::vector<cmStdString>::const_iterator libDir = libDirs.begin();
  1772. libDir != libDirs.end(); ++libDir)
  1773. {
  1774. if(libDir->size() && *libDir != "/usr/lib")
  1775. {
  1776. if(this->XcodeVersion > 15)
  1777. {
  1778. // now add the same one but append $(CONFIGURATION) to it:
  1779. linkDirs += " ";
  1780. linkDirs += this->XCodeEscapePath(libDir->c_str());
  1781. linkDirs += "/$(CONFIGURATION)";
  1782. }
  1783. linkDirs += " ";
  1784. linkDirs += this->XCodeEscapePath(libDir->c_str());
  1785. }
  1786. }
  1787. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  1788. linkDirs.c_str(), configName);
  1789. // now add the link libraries
  1790. if(cmtarget->GetType() != cmTarget::STATIC_LIBRARY)
  1791. {
  1792. std::string fdirs;
  1793. std::set<cmStdString> emitted;
  1794. emitted.insert("/System/Library/Frameworks");
  1795. for(std::vector<cmStdString>::iterator lib = libNames.begin();
  1796. lib != libNames.end(); ++lib)
  1797. {
  1798. std::string& libString = *lib;
  1799. // check to see if this is a -F framework path and extract it if it is
  1800. // -F framework stuff should be in the FRAMEWORK_SEARCH_PATHS and not
  1801. // OTHER_LDFLAGS
  1802. if(libString.size() > 2 && libString[0] == '-'
  1803. && libString[1] == 'F')
  1804. {
  1805. std::string path = libString.substr(2);
  1806. // remove escaped spaces from the path
  1807. cmSystemTools::ReplaceString(path, "\\ ", " ");
  1808. if(emitted.insert(path).second)
  1809. {
  1810. if(fdirs.size())
  1811. {
  1812. fdirs += " ";
  1813. }
  1814. fdirs += this->XCodeEscapePath(path.c_str());
  1815. }
  1816. }
  1817. else
  1818. {
  1819. this->AppendBuildSettingAttribute(target, "OTHER_LDFLAGS",
  1820. lib->c_str(), configName);
  1821. }
  1822. }
  1823. if(fdirs.size())
  1824. {
  1825. this->AppendBuildSettingAttribute(target, "FRAMEWORK_SEARCH_PATHS",
  1826. fdirs.c_str(), configName);
  1827. }
  1828. }
  1829. }
  1830. }
  1831. //----------------------------------------------------------------------------
  1832. void cmGlobalXCodeGenerator::CreateGroups(cmLocalGenerator* root,
  1833. std::vector<cmLocalGenerator*>&
  1834. generators)
  1835. {
  1836. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  1837. i != generators.end(); ++i)
  1838. {
  1839. if(this->IsExcluded(root, *i))
  1840. {
  1841. continue;
  1842. }
  1843. cmMakefile* mf = (*i)->GetMakefile();
  1844. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  1845. cmTargets &tgts = mf->GetTargets();
  1846. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1847. {
  1848. cmTarget& cmtarget = l->second;
  1849. // add the soon to be generated Info.plist file as a source for a
  1850. // MACOSX_BUNDLE file
  1851. if(cmtarget.GetPropertyAsBool("MACOSX_BUNDLE"))
  1852. {
  1853. cmSourceFile file;
  1854. file.SetName("Info",
  1855. this->CurrentMakefile->GetCurrentOutputDirectory(),
  1856. "plist", false);
  1857. cmtarget.GetSourceFiles().push_back
  1858. (this->CurrentMakefile->AddSource(file));
  1859. }
  1860. std::vector<cmSourceFile*> & classes = cmtarget.GetSourceFiles();
  1861. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  1862. s != classes.end(); s++)
  1863. {
  1864. cmSourceFile* sf = *s;
  1865. // Add the file to the list of sources.
  1866. std::string const& source = sf->GetFullPath();
  1867. cmSourceGroup& sourceGroup =
  1868. mf->FindSourceGroup(source.c_str(), sourceGroups);
  1869. cmXCodeObject* pbxgroup =
  1870. this->CreateOrGetPBXGroup(cmtarget, &sourceGroup);
  1871. this->GroupMap[sf] = pbxgroup;
  1872. }
  1873. }
  1874. }
  1875. }
  1876. //----------------------------------------------------------------------------
  1877. cmXCodeObject* cmGlobalXCodeGenerator
  1878. ::CreateOrGetPBXGroup(cmTarget& cmtarget, cmSourceGroup* sg)
  1879. {
  1880. cmStdString s = cmtarget.GetName();
  1881. s += "/";
  1882. s += sg->GetName();
  1883. std::map<cmStdString, cmXCodeObject* >::iterator i =
  1884. this->GroupNameMap.find(s);
  1885. if(i != this->GroupNameMap.end())
  1886. {
  1887. return i->second;
  1888. }
  1889. i = this->TargetGroup.find(cmtarget.GetName());
  1890. cmXCodeObject* tgroup = 0;
  1891. if(i != this->TargetGroup.end())
  1892. {
  1893. tgroup = i->second;
  1894. }
  1895. else
  1896. {
  1897. tgroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1898. this->TargetGroup[cmtarget.GetName()] = tgroup;
  1899. cmXCodeObject* tgroupChildren =
  1900. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1901. tgroup->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  1902. tgroup->AddAttribute("children", tgroupChildren);
  1903. if(this->XcodeVersion == 15)
  1904. {
  1905. tgroup->AddAttribute("refType", this->CreateString("4"));
  1906. }
  1907. tgroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1908. this->SourcesGroupChildren->AddObject(tgroup);
  1909. }
  1910. cmXCodeObject* tgroupChildren = tgroup->GetObject("children");
  1911. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  1912. cmXCodeObject* groupChildren =
  1913. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1914. group->AddAttribute("name", this->CreateString(sg->GetName()));
  1915. group->AddAttribute("children", groupChildren);
  1916. if(this->XcodeVersion == 15)
  1917. {
  1918. group->AddAttribute("refType", this->CreateString("4"));
  1919. }
  1920. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  1921. tgroupChildren->AddObject(group);
  1922. this->GroupNameMap[s] = group;
  1923. return group;
  1924. }
  1925. //----------------------------------------------------------------------------
  1926. void cmGlobalXCodeGenerator
  1927. ::CreateXCodeObjects(cmLocalGenerator* root,
  1928. std::vector<cmLocalGenerator*>&
  1929. generators)
  1930. {
  1931. this->ClearXCodeObjects();
  1932. this->RootObject = 0;
  1933. this->SourcesGroupChildren = 0;
  1934. this->MainGroupChildren = 0;
  1935. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1936. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  1937. cmXCodeObject* developBuildStyle =
  1938. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  1939. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1940. if(this->XcodeVersion == 15)
  1941. {
  1942. developBuildStyle->AddAttribute("name",
  1943. this->CreateString("Development"));
  1944. developBuildStyle->AddAttribute("buildSettings", group);
  1945. listObjs->AddObject(developBuildStyle);
  1946. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1947. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("YES"));
  1948. cmXCodeObject* deployBuildStyle =
  1949. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  1950. deployBuildStyle->AddAttribute("name", this->CreateString("Deployment"));
  1951. deployBuildStyle->AddAttribute("buildSettings", group);
  1952. listObjs->AddObject(deployBuildStyle);
  1953. }
  1954. else
  1955. {
  1956. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  1957. {
  1958. cmXCodeObject* buildStyle =
  1959. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  1960. const char* name = this->CurrentConfigurationTypes[i].c_str();
  1961. buildStyle->AddAttribute("name", this->CreateString(name));
  1962. buildStyle->SetComment(name);
  1963. cmXCodeObject* sgroup =
  1964. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1965. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  1966. buildStyle->AddAttribute("buildSettings", sgroup);
  1967. listObjs->AddObject(buildStyle);
  1968. }
  1969. }
  1970. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1971. this->MainGroupChildren =
  1972. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1973. mainGroup->AddAttribute("children", this->MainGroupChildren);
  1974. if(this->XcodeVersion == 15)
  1975. {
  1976. mainGroup->AddAttribute("refType", this->CreateString("4"));
  1977. }
  1978. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1979. cmXCodeObject* sourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1980. this->SourcesGroupChildren =
  1981. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1982. sourcesGroup->AddAttribute("name", this->CreateString("Sources"));
  1983. sourcesGroup->AddAttribute("children", this->SourcesGroupChildren);
  1984. if(this->XcodeVersion == 15)
  1985. {
  1986. sourcesGroup->AddAttribute("refType", this->CreateString("4"));
  1987. }
  1988. sourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1989. this->MainGroupChildren->AddObject(sourcesGroup);
  1990. // now create the cmake groups
  1991. this->CreateGroups(root, generators);
  1992. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1993. productGroup->AddAttribute("name", this->CreateString("Products"));
  1994. if(this->XcodeVersion == 15)
  1995. {
  1996. productGroup->AddAttribute("refType", this->CreateString("4"));
  1997. }
  1998. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1999. cmXCodeObject* productGroupChildren =
  2000. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2001. productGroup->AddAttribute("children", productGroupChildren);
  2002. this->MainGroupChildren->AddObject(productGroup);
  2003. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  2004. this->RootObject->SetComment("Project object");
  2005. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2006. this->RootObject->AddAttribute("mainGroup",
  2007. this->CreateObjectReference(mainGroup));
  2008. this->RootObject->AddAttribute("buildSettings", group);
  2009. this->RootObject->AddAttribute("buildStyles", listObjs);
  2010. this->RootObject->AddAttribute("hasScannedForEncodings",
  2011. this->CreateString("0"));
  2012. cmXCodeObject* configlist =
  2013. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2014. cmXCodeObject* buildConfigurations =
  2015. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2016. std::vector<cmXCodeObject*> configs;
  2017. if(this->XcodeVersion == 15)
  2018. {
  2019. cmXCodeObject* configDebug =
  2020. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2021. configDebug->AddAttribute("name", this->CreateString("Debug"));
  2022. configs.push_back(configDebug);
  2023. cmXCodeObject* configRelease =
  2024. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2025. configRelease->AddAttribute("name", this->CreateString("Release"));
  2026. configs.push_back(configRelease);
  2027. }
  2028. else
  2029. {
  2030. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  2031. {
  2032. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2033. cmXCodeObject* config =
  2034. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2035. config->AddAttribute("name", this->CreateString(name));
  2036. configs.push_back(config);
  2037. }
  2038. }
  2039. for(std::vector<cmXCodeObject*>::iterator c = configs.begin();
  2040. c != configs.end(); ++c)
  2041. {
  2042. buildConfigurations->AddObject(*c);
  2043. }
  2044. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2045. std::string comment = "Build configuration list for PBXProject ";
  2046. comment += " \"";
  2047. comment += this->CurrentProject;
  2048. comment += "\"";
  2049. configlist->SetComment(comment.c_str());
  2050. configlist->AddAttribute("defaultConfigurationIsVisible",
  2051. this->CreateString("0"));
  2052. configlist->AddAttribute("defaultConfigurationName",
  2053. this->CreateString("Debug"));
  2054. cmXCodeObject* buildSettings =
  2055. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2056. const char* osxArch =
  2057. this->CurrentMakefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  2058. const char* sysroot =
  2059. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  2060. if(osxArch && sysroot)
  2061. {
  2062. // recompute this as it may have been changed since enable language
  2063. this->Architectures.clear();
  2064. cmSystemTools::ExpandListArgument(std::string(osxArch),
  2065. this->Architectures);
  2066. if(this->Architectures.size() > 1)
  2067. {
  2068. buildSettings->AddAttribute("SDKROOT",
  2069. this->CreateString(sysroot));
  2070. std::string archString;
  2071. for( std::vector<std::string>::iterator i =
  2072. this->Architectures.begin();
  2073. i != this->Architectures.end(); ++i)
  2074. {
  2075. archString += *i;
  2076. archString += " ";
  2077. }
  2078. buildSettings->AddAttribute("ARCHS",
  2079. this->CreateString(archString.c_str()));
  2080. }
  2081. }
  2082. for( std::vector<cmXCodeObject*>::iterator i = configs.begin();
  2083. i != configs.end(); ++i)
  2084. {
  2085. (*i)->AddAttribute("buildSettings", buildSettings);
  2086. }
  2087. this->RootObject->AddAttribute("buildConfigurationList",
  2088. this->CreateObjectReference(configlist));
  2089. std::vector<cmXCodeObject*> targets;
  2090. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2091. i != generators.end(); ++i)
  2092. {
  2093. if(!this->IsExcluded(root, *i))
  2094. {
  2095. this->CreateXCodeTargets(*i, targets);
  2096. }
  2097. }
  2098. // loop over all targets and add link and depend info
  2099. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2100. i != targets.end(); ++i)
  2101. {
  2102. cmXCodeObject* t = *i;
  2103. this->AddDependAndLinkInformation(t);
  2104. }
  2105. // now create xcode depend hack makefile
  2106. this->CreateXCodeDependHackTarget(targets);
  2107. // now add all targets to the root object
  2108. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2109. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2110. i != targets.end(); ++i)
  2111. {
  2112. cmXCodeObject* t = *i;
  2113. allTargets->AddObject(t);
  2114. cmXCodeObject* productRef = t->GetObject("productReference");
  2115. if(productRef)
  2116. {
  2117. productGroupChildren->AddObject(productRef->GetObject());
  2118. }
  2119. }
  2120. this->RootObject->AddAttribute("targets", allTargets);
  2121. }
  2122. //----------------------------------------------------------------------------
  2123. void
  2124. cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  2125. std::vector<cmXCodeObject*>& targets)
  2126. {
  2127. cmGeneratedFileStream
  2128. makefileStream(this->CurrentXCodeHackMakefile.c_str());
  2129. if(!makefileStream)
  2130. {
  2131. cmSystemTools::Error("Could not create",
  2132. this->CurrentXCodeHackMakefile.c_str());
  2133. return;
  2134. }
  2135. makefileStream.SetCopyIfDifferent(true);
  2136. // one more pass for external depend information not handled
  2137. // correctly by xcode
  2138. makefileStream << "# DO NOT EDIT\n";
  2139. makefileStream << "# This makefile makes sure all linkable targets are \n";
  2140. makefileStream << "# up-to-date with anything they link to,avoiding a "
  2141. "bug in XCode 1.5\n";
  2142. for(std::vector<std::string>::const_iterator
  2143. ct = this->CurrentConfigurationTypes.begin();
  2144. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2145. {
  2146. if(this->XcodeVersion < 21 || ct->empty())
  2147. {
  2148. makefileStream << "all: ";
  2149. }
  2150. else
  2151. {
  2152. makefileStream << "all." << *ct << ": ";
  2153. }
  2154. const char* configName = 0;
  2155. if(!ct->empty())
  2156. {
  2157. configName = ct->c_str();
  2158. }
  2159. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2160. i != targets.end(); ++i)
  2161. {
  2162. cmXCodeObject* target = *i;
  2163. cmTarget* t =target->GetTarget();
  2164. if(t->GetType() == cmTarget::EXECUTABLE ||
  2165. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2166. t->GetType() == cmTarget::MODULE_LIBRARY)
  2167. {
  2168. makefileStream << "\\\n\t" <<
  2169. this->ConvertToRelativeForMake(
  2170. t->GetFullPath(configName).c_str());
  2171. }
  2172. }
  2173. makefileStream << "\n\n";
  2174. }
  2175. makefileStream
  2176. << "# For each target create a dummy rule "
  2177. "so the target does not have to exist\n";
  2178. std::set<cmStdString> emitted;
  2179. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2180. i != targets.end(); ++i)
  2181. {
  2182. cmXCodeObject* target = *i;
  2183. std::map<cmStdString, cmXCodeObject::StringVec> const& deplibs =
  2184. target->GetDependLibraries();
  2185. for(std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator ci
  2186. = deplibs.begin(); ci != deplibs.end(); ++ci)
  2187. {
  2188. for(cmXCodeObject::StringVec::const_iterator d = ci->second.begin();
  2189. d != ci->second.end(); ++d)
  2190. {
  2191. if(emitted.insert(*d).second)
  2192. {
  2193. makefileStream <<
  2194. this->ConvertToRelativeForMake(d->c_str()) << ":\n";
  2195. }
  2196. }
  2197. }
  2198. }
  2199. makefileStream << "\n\n";
  2200. // Write rules to help Xcode relink things at the right time.
  2201. makefileStream <<
  2202. "# Rules to remove targets that are older than anything to which they\n"
  2203. "# link. This forces Xcode to relink the targets from scratch. It\n"
  2204. "# does not seem to check these dependencies itself.\n";
  2205. for(std::vector<std::string>::const_iterator
  2206. ct = this->CurrentConfigurationTypes.begin();
  2207. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2208. {
  2209. const char* configName = 0;
  2210. if(!ct->empty())
  2211. {
  2212. configName = ct->c_str();
  2213. }
  2214. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2215. i != targets.end(); ++i)
  2216. {
  2217. cmXCodeObject* target = *i;
  2218. cmTarget* t =target->GetTarget();
  2219. if(t->GetType() == cmTarget::EXECUTABLE ||
  2220. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2221. t->GetType() == cmTarget::MODULE_LIBRARY)
  2222. {
  2223. // Create a rule for this target.
  2224. std::string tfull = t->GetFullPath(configName);
  2225. makefileStream << this->ConvertToRelativeForMake(tfull.c_str())
  2226. << ":";
  2227. // List dependencies if any exist.
  2228. std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator
  2229. x = target->GetDependLibraries().find(*ct);
  2230. if(x != target->GetDependLibraries().end())
  2231. {
  2232. std::vector<cmStdString> const& deplibs = x->second;
  2233. for(std::vector<cmStdString>::const_iterator d = deplibs.begin();
  2234. d != deplibs.end(); ++d)
  2235. {
  2236. makefileStream << "\\\n\t" <<
  2237. this->ConvertToRelativeForMake(d->c_str());
  2238. }
  2239. }
  2240. // Write the action to remove the target if it is out of date.
  2241. makefileStream << "\n";
  2242. makefileStream << "\t/bin/rm -f "
  2243. << this->ConvertToRelativeForMake(tfull.c_str())
  2244. << "\n";
  2245. // if building for more than one architecture
  2246. // then remove those exectuables as well
  2247. if(this->Architectures.size() > 1)
  2248. {
  2249. std::string universal = t->GetDirectory();
  2250. universal += "/";
  2251. universal += this->CurrentMakefile->GetProjectName();
  2252. universal += ".build/";
  2253. universal += configName;
  2254. universal += "/";
  2255. universal += t->GetName();
  2256. universal += ".build/Objects-normal/";
  2257. for( std::vector<std::string>::iterator arch =
  2258. this->Architectures.begin();
  2259. arch != this->Architectures.end(); ++arch)
  2260. {
  2261. std::string universalFile = universal;
  2262. universalFile += *arch;
  2263. universalFile += "/";
  2264. universalFile += t->GetName();
  2265. makefileStream << "\t/bin/rm -f "
  2266. <<
  2267. this->ConvertToRelativeForMake(universalFile.c_str())
  2268. << "\n";
  2269. }
  2270. }
  2271. makefileStream << "\n\n";
  2272. }
  2273. }
  2274. }
  2275. }
  2276. //----------------------------------------------------------------------------
  2277. void
  2278. cmGlobalXCodeGenerator::OutputXCodeProject(cmLocalGenerator* root,
  2279. std::vector<cmLocalGenerator*>&
  2280. generators)
  2281. {
  2282. if(generators.size() == 0)
  2283. {
  2284. return;
  2285. }
  2286. #if 1
  2287. // TODO: This block should be moved to a central location for all
  2288. // generators. It is duplicated in every generator.
  2289. for(std::vector<cmLocalGenerator*>::iterator g = generators.begin();
  2290. g != generators.end(); ++g)
  2291. {
  2292. if(this->IsExcluded(root, *g))
  2293. {
  2294. continue;
  2295. }
  2296. cmMakefile* mf = (*g)->GetMakefile();
  2297. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  2298. cmTargets &tgts = mf->GetTargets();
  2299. // Call TraceVSDependencies on all targets
  2300. for(cmTargets::iterator l = tgts.begin();
  2301. l != tgts.end(); l++)
  2302. {
  2303. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  2304. // so don't build a projectfile for it
  2305. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  2306. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  2307. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  2308. {
  2309. cmTarget& target = l->second;
  2310. target.TraceVSDependencies(target.GetName(), mf);
  2311. }
  2312. }
  2313. // now for all custom commands that are not used directly in a
  2314. // target, add them to all targets in the current directory or
  2315. // makefile
  2316. std::set<cmStdString> banned;
  2317. banned.insert("ALL_BUILD");
  2318. banned.insert("XCODE_DEPEND_HELPER");
  2319. banned.insert("install");
  2320. std::vector<cmSourceFile*> & classesmf = mf->GetSourceFiles();
  2321. for(std::vector<cmSourceFile*>::const_iterator i = classesmf.begin();
  2322. i != classesmf.end(); i++)
  2323. {
  2324. if(cmCustomCommand* cc = (*i)->GetCustomCommand())
  2325. {
  2326. if(!cc->IsUsed())
  2327. {
  2328. for(cmTargets::iterator l = tgts.begin();
  2329. l != tgts.end(); l++)
  2330. {
  2331. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  2332. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  2333. && (strncmp(l->first.c_str(),
  2334. "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
  2335. && banned.find(l->second.GetName()) == banned.end())
  2336. {
  2337. cmTarget& target = l->second;
  2338. bool sameAsTarget = false;
  2339. // make sure we don't add a custom command that depends on
  2340. // this target
  2341. for(unsigned int k =0; k < cc->GetDepends().size(); k++)
  2342. {
  2343. if(cmSystemTools::GetFilenameName
  2344. (cc->GetDepends()[k]) == target.GetFullName())
  2345. {
  2346. sameAsTarget = true;
  2347. }
  2348. }
  2349. if(!sameAsTarget)
  2350. {
  2351. target.GetSourceFiles().push_back(*i);
  2352. }
  2353. }
  2354. }
  2355. }
  2356. }
  2357. }
  2358. }
  2359. #endif
  2360. this->CreateXCodeObjects(root,
  2361. generators);
  2362. std::string xcodeDir = root->GetMakefile()->GetStartOutputDirectory();
  2363. xcodeDir += "/";
  2364. xcodeDir += root->GetMakefile()->GetProjectName();
  2365. xcodeDir += ".xcode";
  2366. if(this->XcodeVersion > 20)
  2367. {
  2368. xcodeDir += "proj";
  2369. }
  2370. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  2371. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  2372. cmGeneratedFileStream fout(xcodeProjFile.c_str());
  2373. fout.SetCopyIfDifferent(true);
  2374. if(!fout)
  2375. {
  2376. return;
  2377. }
  2378. this->WriteXCodePBXProj(fout, root, generators);
  2379. this->ClearXCodeObjects();
  2380. }
  2381. //----------------------------------------------------------------------------
  2382. void
  2383. cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  2384. cmLocalGenerator* ,
  2385. std::vector<cmLocalGenerator*>& )
  2386. {
  2387. fout << "// !$*UTF8*$!\n";
  2388. fout << "{\n";
  2389. cmXCodeObject::Indent(1, fout);
  2390. fout << "archiveVersion = 1;\n";
  2391. cmXCodeObject::Indent(1, fout);
  2392. fout << "classes = {\n";
  2393. cmXCodeObject::Indent(1, fout);
  2394. fout << "};\n";
  2395. cmXCodeObject::Indent(1, fout);
  2396. fout << "objectVersion = 39;\n";
  2397. cmXCodeObject::PrintList(this->XCodeObjects, fout);
  2398. cmXCodeObject::Indent(1, fout);
  2399. fout << "rootObject = " << this->RootObject->GetId() << ";\n";
  2400. fout << "}\n";
  2401. }
  2402. //----------------------------------------------------------------------------
  2403. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  2404. const
  2405. {
  2406. entry.name = this->GetName();
  2407. entry.brief = "Generate XCode project files.";
  2408. entry.full = "";
  2409. }
  2410. //----------------------------------------------------------------------------
  2411. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(const char* p)
  2412. {
  2413. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2414. {
  2415. return cmSystemTools::ConvertToOutputPath(p);
  2416. }
  2417. else
  2418. {
  2419. std::string ret =
  2420. this->CurrentLocalGenerator->
  2421. ConvertToRelativePath(this->CurrentOutputDirectoryComponents, p);
  2422. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2423. }
  2424. }
  2425. //----------------------------------------------------------------------------
  2426. std::string cmGlobalXCodeGenerator::ConvertToRelativeForXCode(const char* p)
  2427. {
  2428. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2429. {
  2430. return cmSystemTools::ConvertToOutputPath(p);
  2431. }
  2432. else
  2433. {
  2434. std::string ret =
  2435. this->CurrentLocalGenerator->
  2436. ConvertToRelativePath(this->ProjectOutputDirectoryComponents, p);
  2437. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2438. }
  2439. }
  2440. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const char* p)
  2441. {
  2442. std::string ret = p;
  2443. if(ret.find(' ') != ret.npos)
  2444. {
  2445. std::string t = ret;
  2446. ret = "\"";
  2447. ret += t;
  2448. ret += "\"";
  2449. }
  2450. return ret;
  2451. }
  2452. void cmGlobalXCodeGenerator::
  2453. GetTargetObjectFileDirectories(cmTarget* target,
  2454. std::vector<std::string>&
  2455. dirs)
  2456. {
  2457. std::string dir = this->CurrentMakefile->GetCurrentOutputDirectory();
  2458. dir += "/";
  2459. dir += this->CurrentMakefile->GetProjectName();
  2460. dir += ".build/";
  2461. dir += this->GetCMakeCFGInitDirectory();
  2462. dir += "/";
  2463. if(target->GetType() != cmTarget::EXECUTABLE)
  2464. {
  2465. dir += "lib";
  2466. }
  2467. dir += target->GetName();
  2468. if(target->GetType() == cmTarget::STATIC_LIBRARY)
  2469. {
  2470. dir += ".a";
  2471. }
  2472. if(target->GetType() == cmTarget::SHARED_LIBRARY)
  2473. {
  2474. dir += ".dylib";
  2475. }
  2476. if(target->GetType() == cmTarget::MODULE_LIBRARY)
  2477. {
  2478. dir += ".so";
  2479. }
  2480. dir += ".build/Objects-normal/";
  2481. std::string dirsave = dir;
  2482. if(this->Architectures.size())
  2483. {
  2484. for(std::vector<std::string>::iterator i = this->Architectures.begin();
  2485. i != this->Architectures.end(); ++i)
  2486. {
  2487. dir += *i;
  2488. dirs.push_back(dir);
  2489. dir = dirsave;
  2490. }
  2491. }
  2492. else
  2493. {
  2494. dirs.push_back(dir);
  2495. }
  2496. }
  2497. //----------------------------------------------------------------------------
  2498. void
  2499. cmGlobalXCodeGenerator
  2500. ::AppendDirectoryForConfig(const char* prefix,
  2501. const char* config,
  2502. const char* suffix,
  2503. std::string& dir)
  2504. {
  2505. if(this->XcodeVersion > 20)
  2506. {
  2507. if(config)
  2508. {
  2509. if(dir.find(".framework") != dir.npos)
  2510. {
  2511. std::string::size_type pos = dir.rfind("/");
  2512. std::string framework = dir.substr(pos);
  2513. std::string newDir;
  2514. newDir = dir.substr(0, pos);
  2515. newDir += "/";
  2516. newDir += config;
  2517. dir = newDir;
  2518. }
  2519. else
  2520. {
  2521. dir += prefix;
  2522. dir += config;
  2523. dir += suffix;
  2524. }
  2525. }
  2526. }
  2527. }
  2528. //----------------------------------------------------------------------------
  2529. std::string cmGlobalXCodeGenerator::LookupFlags(const char* varNamePrefix,
  2530. const char* varNameLang,
  2531. const char* varNameSuffix,
  2532. const char* default_flags)
  2533. {
  2534. if(varNameLang)
  2535. {
  2536. std::string varName = varNamePrefix;
  2537. varName += varNameLang;
  2538. varName += varNameSuffix;
  2539. if(const char* varValue =
  2540. this->CurrentMakefile->GetDefinition(varName.c_str()))
  2541. {
  2542. if(*varValue)
  2543. {
  2544. return varValue;
  2545. }
  2546. }
  2547. }
  2548. return default_flags;
  2549. }