cmGlobalXCodeGenerator.cxx 98 KB

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