cmGlobalXCodeGenerator.cxx 88 KB

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