cmGlobalXCodeGenerator.cxx 92 KB

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