cmGlobalXCodeGenerator.cxx 111 KB

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