cmGlobalXCodeGenerator.cxx 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336
  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. // disable the implicit rules
  1201. makefileStream << ".SUFFIXES: " << "\n";
  1202. // have all depend on all outputs
  1203. makefileStream << "all: ";
  1204. std::map<const cmCustomCommand*, cmStdString> tname;
  1205. int count = 0;
  1206. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1207. i != commands.end(); ++i)
  1208. {
  1209. cmCustomCommand const& cc = *i;
  1210. if(!cc.GetCommandLines().empty())
  1211. {
  1212. const std::vector<std::string>& outputs = cc.GetOutputs();
  1213. if(!outputs.empty())
  1214. {
  1215. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1216. o != outputs.end(); ++o)
  1217. {
  1218. makefileStream
  1219. << "\\\n\t" << this->ConvertToRelativeForMake(o->c_str());
  1220. }
  1221. }
  1222. else
  1223. {
  1224. cmOStringStream str;
  1225. str << "_buildpart_" << count++ ;
  1226. tname[&cc] = std::string(target.GetName()) + str.str();
  1227. makefileStream << "\\\n\t" << tname[&cc];
  1228. }
  1229. }
  1230. }
  1231. makefileStream << "\n\n";
  1232. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1233. i != commands.end(); ++i)
  1234. {
  1235. cmCustomCommand const& cc = *i;
  1236. if(!cc.GetCommandLines().empty())
  1237. {
  1238. cmCustomCommandGenerator ccg(cc, configName, this->CurrentMakefile);
  1239. makefileStream << "\n";
  1240. const std::vector<std::string>& outputs = cc.GetOutputs();
  1241. if(!outputs.empty())
  1242. {
  1243. // There is at least one output, start the rule for it
  1244. std::string primary_output =
  1245. this->ConvertToRelativeForMake(outputs.begin()->c_str());
  1246. makefileStream << primary_output << ": ";
  1247. }
  1248. else
  1249. {
  1250. // There are no outputs. Use the generated force rule name.
  1251. makefileStream << tname[&cc] << ": ";
  1252. }
  1253. for(std::vector<std::string>::const_iterator d =
  1254. cc.GetDepends().begin();
  1255. d != cc.GetDepends().end(); ++d)
  1256. {
  1257. std::string dep;
  1258. if(this->CurrentLocalGenerator
  1259. ->GetRealDependency(d->c_str(), configName, dep))
  1260. {
  1261. makefileStream << "\\\n" <<
  1262. this->ConvertToRelativeForMake(dep.c_str());
  1263. }
  1264. }
  1265. makefileStream << "\n";
  1266. if(const char* comment = cc.GetComment())
  1267. {
  1268. std::string echo_cmd = "echo ";
  1269. echo_cmd += (this->CurrentLocalGenerator->
  1270. EscapeForShell(comment, cc.GetEscapeAllowMakeVars()));
  1271. makefileStream << "\t" << echo_cmd.c_str() << "\n";
  1272. }
  1273. // Add each command line to the set of commands.
  1274. for(unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c)
  1275. {
  1276. // Build the command line in a single string.
  1277. std::string cmd2 = ccg.GetCommand(c);
  1278. cmSystemTools::ReplaceString(cmd2, "/./", "/");
  1279. cmd2 = this->ConvertToRelativeForMake(cmd2.c_str());
  1280. std::string cmd;
  1281. if(cc.GetWorkingDirectory())
  1282. {
  1283. cmd += "cd ";
  1284. cmd += this->ConvertToRelativeForMake(cc.GetWorkingDirectory());
  1285. cmd += " && ";
  1286. }
  1287. cmd += cmd2;
  1288. ccg.AppendArguments(c, cmd);
  1289. makefileStream << "\t" << cmd.c_str() << "\n";
  1290. }
  1291. }
  1292. }
  1293. // Add rules to deal with multiple outputs of custom commands.
  1294. if(!multipleOutputPairs.empty())
  1295. {
  1296. makefileStream <<
  1297. "\n# Dependencies of multiple outputs to their primary outputs \n";
  1298. for(std::map<cmStdString, cmStdString>::const_iterator o =
  1299. multipleOutputPairs.begin(); o != multipleOutputPairs.end(); ++o)
  1300. {
  1301. makefileStream << o->first << ": " << o->second << "\n";
  1302. }
  1303. makefileStream <<
  1304. "\n"
  1305. "cmake_check_multiple_outputs:\n";
  1306. for(std::map<cmStdString, cmStdString>::const_iterator o =
  1307. multipleOutputPairs.begin(); o != multipleOutputPairs.end(); ++o)
  1308. {
  1309. makefileStream << "\t@if [ ! -f "
  1310. << o->first << " ]; then rm -f "
  1311. << o->second << "; fi\n";
  1312. }
  1313. }
  1314. }
  1315. //----------------------------------------------------------------------------
  1316. void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
  1317. cmXCodeObject* buildSettings,
  1318. const char* configName)
  1319. {
  1320. std::string flags;
  1321. std::string defFlags;
  1322. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  1323. (target.GetType() == cmTarget::MODULE_LIBRARY));
  1324. bool binary = ((target.GetType() == cmTarget::STATIC_LIBRARY) ||
  1325. (target.GetType() == cmTarget::EXECUTABLE) ||
  1326. shared);
  1327. const char* lang = target.GetLinkerLanguage(configName);
  1328. std::string cflags;
  1329. if(lang)
  1330. {
  1331. // for c++ projects get the c flags as well
  1332. if(strcmp(lang, "CXX") == 0)
  1333. {
  1334. this->CurrentLocalGenerator->AddLanguageFlags(cflags, "C", configName);
  1335. this->CurrentLocalGenerator->AddSharedFlags(cflags, lang, shared);
  1336. }
  1337. // Add language-specific flags.
  1338. this->CurrentLocalGenerator->AddLanguageFlags(flags, lang, configName);
  1339. // Add shared-library flags if needed.
  1340. this->CurrentLocalGenerator->AddSharedFlags(flags, lang, shared);
  1341. }
  1342. else if(binary)
  1343. {
  1344. cmSystemTools::Error
  1345. ("CMake can not determine linker language for target:",
  1346. target.GetName());
  1347. return;
  1348. }
  1349. // Add define flags
  1350. this->CurrentLocalGenerator->
  1351. AppendFlags(defFlags,
  1352. this->CurrentMakefile->GetDefineFlags());
  1353. // Add preprocessor definitions for this target and configuration.
  1354. BuildObjectListOrString ppDefs(this, this->XcodeVersion >= 30);
  1355. if(this->XcodeVersion > 15)
  1356. {
  1357. this->AppendDefines(ppDefs, "CMAKE_INTDIR=\"$(CONFIGURATION)\"");
  1358. }
  1359. if(const char* exportMacro = target.GetExportMacro())
  1360. {
  1361. // Add the export symbol definition for shared library objects.
  1362. this->AppendDefines(ppDefs, exportMacro);
  1363. }
  1364. this->AppendDefines
  1365. (ppDefs, this->CurrentMakefile->GetProperty("COMPILE_DEFINITIONS"));
  1366. this->AppendDefines(ppDefs, target.GetProperty("COMPILE_DEFINITIONS"));
  1367. if(configName)
  1368. {
  1369. std::string defVarName = "COMPILE_DEFINITIONS_";
  1370. defVarName += cmSystemTools::UpperCase(configName);
  1371. this->AppendDefines
  1372. (ppDefs, this->CurrentMakefile->GetProperty(defVarName.c_str()));
  1373. this->AppendDefines(ppDefs, target.GetProperty(defVarName.c_str()));
  1374. }
  1375. buildSettings->AddAttribute
  1376. ("GCC_PREPROCESSOR_DEFINITIONS", ppDefs.CreateList());
  1377. std::string extraLinkOptions;
  1378. if(target.GetType() == cmTarget::EXECUTABLE)
  1379. {
  1380. extraLinkOptions =
  1381. this->CurrentMakefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  1382. std::string var = "CMAKE_EXE_LINKER_FLAGS_";
  1383. var += cmSystemTools::UpperCase(configName);
  1384. std::string val =
  1385. this->CurrentMakefile->GetSafeDefinition(var.c_str());
  1386. if(val.size())
  1387. {
  1388. extraLinkOptions += " ";
  1389. extraLinkOptions += val;
  1390. }
  1391. }
  1392. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1393. {
  1394. extraLinkOptions = this->CurrentMakefile->
  1395. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1396. }
  1397. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1398. {
  1399. extraLinkOptions = this->CurrentMakefile->
  1400. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1401. }
  1402. const char* linkFlagsProp = "LINK_FLAGS";
  1403. if(target.GetType() == cmTarget::STATIC_LIBRARY)
  1404. {
  1405. linkFlagsProp = "STATIC_LIBRARY_FLAGS";
  1406. }
  1407. const char* targetLinkFlags = target.GetProperty(linkFlagsProp);
  1408. if(targetLinkFlags)
  1409. {
  1410. extraLinkOptions += " ";
  1411. extraLinkOptions += targetLinkFlags;
  1412. }
  1413. if(configName && *configName)
  1414. {
  1415. std::string linkFlagsVar = linkFlagsProp;
  1416. linkFlagsVar += "_";
  1417. linkFlagsVar += cmSystemTools::UpperCase(configName);
  1418. if(const char* linkFlags = target.GetProperty(linkFlagsVar.c_str()))
  1419. {
  1420. extraLinkOptions += " ";
  1421. extraLinkOptions += linkFlags;
  1422. }
  1423. }
  1424. // Set target-specific architectures.
  1425. std::vector<std::string> archs;
  1426. target.GetAppleArchs(configName, archs);
  1427. if(!archs.empty())
  1428. {
  1429. // Enable ARCHS attribute.
  1430. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH",
  1431. this->CreateString("NO"));
  1432. // Store ARCHS value.
  1433. if(archs.size() == 1)
  1434. {
  1435. buildSettings->AddAttribute("ARCHS",
  1436. this->CreateString(archs[0].c_str()));
  1437. }
  1438. else
  1439. {
  1440. cmXCodeObject* archObjects =
  1441. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1442. for(std::vector<std::string>::iterator i = archs.begin();
  1443. i != archs.end(); i++)
  1444. {
  1445. archObjects->AddObject(this->CreateString((*i).c_str()));
  1446. }
  1447. buildSettings->AddAttribute("ARCHS", archObjects);
  1448. }
  1449. }
  1450. // Get the product name components.
  1451. std::string pnprefix;
  1452. std::string pnbase;
  1453. std::string pnsuffix;
  1454. target.GetFullNameComponents(pnprefix, pnbase, pnsuffix, configName);
  1455. // Store the product name for all target types.
  1456. buildSettings->AddAttribute("PRODUCT_NAME",
  1457. this->CreateString(pnbase.c_str()));
  1458. // Set attributes to specify the proper name for the target.
  1459. std::string pndir = this->CurrentMakefile->GetCurrentOutputDirectory();
  1460. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  1461. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1462. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1463. target.GetType() == cmTarget::EXECUTABLE)
  1464. {
  1465. if(this->XcodeVersion >= 21)
  1466. {
  1467. std::string pncdir = target.GetDirectory(configName);
  1468. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  1469. this->CreateString(pncdir.c_str()));
  1470. }
  1471. else
  1472. {
  1473. buildSettings->AddAttribute("OBJROOT",
  1474. this->CreateString(pndir.c_str()));
  1475. pndir = target.GetDirectory(configName);
  1476. }
  1477. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  1478. this->CreateString(pnprefix.c_str()));
  1479. buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
  1480. this->CreateString(pnsuffix.c_str()));
  1481. }
  1482. buildSettings->AddAttribute("SYMROOT",
  1483. this->CreateString(pndir.c_str()));
  1484. // Handle settings for each target type.
  1485. switch(target.GetType())
  1486. {
  1487. case cmTarget::STATIC_LIBRARY:
  1488. {
  1489. buildSettings->AddAttribute("LIBRARY_STYLE",
  1490. this->CreateString("STATIC"));
  1491. break;
  1492. }
  1493. case cmTarget::MODULE_LIBRARY:
  1494. {
  1495. buildSettings->AddAttribute("LIBRARY_STYLE",
  1496. this->CreateString("BUNDLE"));
  1497. if(this->XcodeVersion >= 22)
  1498. {
  1499. buildSettings->AddAttribute("MACH_O_TYPE",
  1500. this->CreateString("mh_bundle"));
  1501. buildSettings->AddAttribute("GCC_DYNAMIC_NO_PIC",
  1502. this->CreateString("NO"));
  1503. // Add the flags to create an executable.
  1504. std::string createFlags =
  1505. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1506. if(!createFlags.empty())
  1507. {
  1508. extraLinkOptions += " ";
  1509. extraLinkOptions += createFlags;
  1510. }
  1511. }
  1512. else
  1513. {
  1514. // Add the flags to create a module.
  1515. std::string createFlags =
  1516. this->LookupFlags("CMAKE_SHARED_MODULE_CREATE_", lang, "_FLAGS",
  1517. "-bundle");
  1518. if(!createFlags.empty())
  1519. {
  1520. extraLinkOptions += " ";
  1521. extraLinkOptions += createFlags;
  1522. }
  1523. }
  1524. break;
  1525. }
  1526. case cmTarget::SHARED_LIBRARY:
  1527. {
  1528. if(target.GetPropertyAsBool("FRAMEWORK"))
  1529. {
  1530. std::string version = target.GetFrameworkVersion();
  1531. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  1532. this->CreateString(version.c_str()));
  1533. std::string plist = this->ComputeInfoPListLocation(target);
  1534. // Xcode will create the final version of Info.plist at build time,
  1535. // so let it replace the framework name. This avoids creating
  1536. // a per-configuration Info.plist file.
  1537. this->CurrentLocalGenerator
  1538. ->GenerateFrameworkInfoPList(&target, "$(EXECUTABLE_NAME)",
  1539. plist.c_str());
  1540. std::string path =
  1541. this->ConvertToRelativeForXCode(plist.c_str());
  1542. buildSettings->AddAttribute("INFOPLIST_FILE",
  1543. this->CreateString(path.c_str()));
  1544. }
  1545. else
  1546. {
  1547. // Add the flags to create a shared library.
  1548. std::string createFlags =
  1549. this->LookupFlags("CMAKE_SHARED_LIBRARY_CREATE_", lang, "_FLAGS",
  1550. "-dynamiclib");
  1551. if(!createFlags.empty())
  1552. {
  1553. extraLinkOptions += " ";
  1554. extraLinkOptions += createFlags;
  1555. }
  1556. }
  1557. buildSettings->AddAttribute("LIBRARY_STYLE",
  1558. this->CreateString("DYNAMIC"));
  1559. break;
  1560. }
  1561. case cmTarget::EXECUTABLE:
  1562. {
  1563. // Add the flags to create an executable.
  1564. std::string createFlags =
  1565. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1566. if(!createFlags.empty())
  1567. {
  1568. extraLinkOptions += " ";
  1569. extraLinkOptions += createFlags;
  1570. }
  1571. // Handle bundles and normal executables separately.
  1572. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  1573. {
  1574. std::string plist = this->ComputeInfoPListLocation(target);
  1575. // Xcode will create the final version of Info.plist at build time,
  1576. // so let it replace the executable name. This avoids creating
  1577. // a per-configuration Info.plist file.
  1578. this->CurrentLocalGenerator
  1579. ->GenerateAppleInfoPList(&target, "$(EXECUTABLE_NAME)",
  1580. plist.c_str());
  1581. std::string path =
  1582. this->ConvertToRelativeForXCode(plist.c_str());
  1583. buildSettings->AddAttribute("INFOPLIST_FILE",
  1584. this->CreateString(path.c_str()));
  1585. }
  1586. }
  1587. break;
  1588. default:
  1589. break;
  1590. }
  1591. if(this->XcodeVersion >= 22)
  1592. {
  1593. buildSettings->AddAttribute("PREBINDING",
  1594. this->CreateString("NO"));
  1595. }
  1596. BuildObjectListOrString dirs(this, this->XcodeVersion >= 30);
  1597. BuildObjectListOrString fdirs(this, this->XcodeVersion >= 30);
  1598. std::vector<std::string> includes;
  1599. this->CurrentLocalGenerator->GetIncludeDirectories(includes);
  1600. std::set<cmStdString> emitted;
  1601. emitted.insert("/System/Library/Frameworks");
  1602. for(std::vector<std::string>::iterator i = includes.begin();
  1603. i != includes.end(); ++i)
  1604. {
  1605. if(this->NameResolvesToFramework(i->c_str()))
  1606. {
  1607. std::string frameworkDir = *i;
  1608. frameworkDir += "/../";
  1609. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1610. if(emitted.insert(frameworkDir).second)
  1611. {
  1612. fdirs.Add(this->XCodeEscapePath(frameworkDir.c_str()).c_str());
  1613. }
  1614. }
  1615. else
  1616. {
  1617. std::string incpath =
  1618. this->XCodeEscapePath(i->c_str());
  1619. dirs.Add(incpath.c_str());
  1620. }
  1621. }
  1622. std::vector<std::string>& frameworks = target.GetFrameworks();
  1623. if(frameworks.size())
  1624. {
  1625. for(std::vector<std::string>::iterator fmIt = frameworks.begin();
  1626. fmIt != frameworks.end(); ++fmIt)
  1627. {
  1628. if(emitted.insert(*fmIt).second)
  1629. {
  1630. fdirs.Add(this->XCodeEscapePath(fmIt->c_str()).c_str());
  1631. }
  1632. }
  1633. }
  1634. if(!fdirs.IsEmpty())
  1635. {
  1636. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS",
  1637. fdirs.CreateList());
  1638. }
  1639. if(!dirs.IsEmpty())
  1640. {
  1641. buildSettings->AddAttribute("HEADER_SEARCH_PATHS",
  1642. dirs.CreateList());
  1643. }
  1644. std::string oflagc = this->ExtractFlag("-O", cflags);
  1645. char optLevel[2];
  1646. optLevel[0] = '0';
  1647. optLevel[1] = 0;
  1648. if(oflagc.size() == 3)
  1649. {
  1650. optLevel[0] = oflagc[2];
  1651. }
  1652. if(oflagc.size() == 2)
  1653. {
  1654. optLevel[0] = '1';
  1655. }
  1656. std::string oflag = this->ExtractFlag("-O", flags);
  1657. if(oflag.size() == 3)
  1658. {
  1659. optLevel[0] = oflag[2];
  1660. }
  1661. if(oflag.size() == 2)
  1662. {
  1663. optLevel[0] = '1';
  1664. }
  1665. std::string gflagc = this->ExtractFlag("-g", cflags);
  1666. // put back gdwarf-2 if used since there is no way
  1667. // to represent it in the gui, but we still want debug yes
  1668. if(gflagc == "-gdwarf-2")
  1669. {
  1670. cflags += " ";
  1671. cflags += gflagc;
  1672. }
  1673. std::string gflag = this->ExtractFlag("-g", flags);
  1674. if(gflag == "-gdwarf-2")
  1675. {
  1676. flags += " ";
  1677. flags += gflag;
  1678. }
  1679. const char* debugStr = "YES";
  1680. if(gflagc.size() ==0 && gflag.size() == 0)
  1681. {
  1682. debugStr = "NO";
  1683. }
  1684. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  1685. this->CreateString(debugStr));
  1686. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  1687. this->CreateString(optLevel));
  1688. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  1689. this->CreateString("NO"));
  1690. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  1691. this->CreateString("NO"));
  1692. if(lang && strcmp(lang, "CXX") == 0)
  1693. {
  1694. flags += " ";
  1695. flags += defFlags;
  1696. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  1697. this->CreateString(flags.c_str()));
  1698. cflags += " ";
  1699. cflags += defFlags;
  1700. buildSettings->AddAttribute("OTHER_CFLAGS",
  1701. this->CreateString(cflags.c_str()));
  1702. }
  1703. else
  1704. {
  1705. flags += " ";
  1706. flags += defFlags;
  1707. buildSettings->AddAttribute("OTHER_CFLAGS",
  1708. this->CreateString(flags.c_str()));
  1709. }
  1710. // Create the INSTALL_PATH attribute.
  1711. std::string install_name_dir;
  1712. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1713. {
  1714. // Get the install_name directory for the build tree.
  1715. install_name_dir = target.GetInstallNameDirForBuildTree(configName, true);
  1716. if(install_name_dir.empty())
  1717. {
  1718. // Xcode will not pass the -install_name option at all if INSTALL_PATH
  1719. // is not given or is empty. We must explicitly put the flag in the
  1720. // link flags to create an install_name with just the library soname.
  1721. extraLinkOptions += " -install_name ";
  1722. extraLinkOptions += target.GetFullName(configName);
  1723. }
  1724. else
  1725. {
  1726. // Convert to a path for the native build tool.
  1727. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  1728. // do not escape spaces on this since it is only a single path
  1729. }
  1730. }
  1731. buildSettings->AddAttribute("INSTALL_PATH",
  1732. this->CreateString(install_name_dir.c_str()));
  1733. buildSettings->AddAttribute("OTHER_LDFLAGS",
  1734. this->CreateString(extraLinkOptions.c_str()));
  1735. buildSettings->AddAttribute("OTHER_REZFLAGS",
  1736. this->CreateString(""));
  1737. buildSettings->AddAttribute("SECTORDER_FLAGS",
  1738. this->CreateString(""));
  1739. buildSettings->AddAttribute("USE_HEADERMAP",
  1740. this->CreateString("NO"));
  1741. if (this->XcodeVersion >= 30)
  1742. {
  1743. cmXCodeObject *group = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1744. group->AddObject(this->CreateString("-Wmost"));
  1745. group->AddObject(this->CreateString("-Wno-four-char-constants"));
  1746. group->AddObject(this->CreateString("-Wno-unknown-pragmas"));
  1747. buildSettings->AddAttribute("WARNING_CFLAGS", group);
  1748. }
  1749. else
  1750. {
  1751. buildSettings->AddAttribute("WARNING_CFLAGS",
  1752. this->CreateString(
  1753. "-Wmost -Wno-four-char-constants"
  1754. " -Wno-unknown-pragmas"));
  1755. }
  1756. // Runtime version information.
  1757. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1758. {
  1759. int major;
  1760. int minor;
  1761. int patch;
  1762. // VERSION -> current_version
  1763. target.GetTargetVersion(false, major, minor, patch);
  1764. cmOStringStream v;
  1765. // Xcode always wants at least 1.0.0 or nothing
  1766. if(!(major == 0 && minor == 0 && patch == 0))
  1767. {
  1768. v << major << "." << minor << "." << patch;
  1769. }
  1770. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  1771. this->CreateString(v.str().c_str()));
  1772. // SOVERSION -> compatibility_version
  1773. target.GetTargetVersion(true, major, minor, patch);
  1774. cmOStringStream vso;
  1775. // Xcode always wants at least 1.0.0 or nothing
  1776. if(!(major == 0 && minor == 0 && patch == 0))
  1777. {
  1778. vso << major << "." << minor << "." << patch;
  1779. }
  1780. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  1781. this->CreateString(vso.str().c_str()));
  1782. }
  1783. // put this last so it can override existing settings
  1784. // Convert "XCODE_ATTRIBUTE_*" properties directly.
  1785. {
  1786. cmPropertyMap const& props = target.GetProperties();
  1787. for(cmPropertyMap::const_iterator i = props.begin();
  1788. i != props.end(); ++i)
  1789. {
  1790. if(i->first.find("XCODE_ATTRIBUTE_") == 0)
  1791. {
  1792. buildSettings->AddAttribute(i->first.substr(16).c_str(),
  1793. this->CreateString(i->second.GetValue()));
  1794. }
  1795. }
  1796. }
  1797. }
  1798. //----------------------------------------------------------------------------
  1799. cmXCodeObject*
  1800. cmGlobalXCodeGenerator::CreateUtilityTarget(cmTarget& cmtarget)
  1801. {
  1802. cmXCodeObject* shellBuildPhase =
  1803. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1804. shellBuildPhase->AddAttribute("buildActionMask",
  1805. this->CreateString("2147483647"));
  1806. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1807. shellBuildPhase->AddAttribute("files", buildFiles);
  1808. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1809. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  1810. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1811. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  1812. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1813. this->CreateString("0"));
  1814. shellBuildPhase->AddAttribute("shellPath",
  1815. this->CreateString("/bin/sh"));
  1816. shellBuildPhase->AddAttribute("shellScript",
  1817. this->CreateString(
  1818. "# shell script goes here\nexit 0"));
  1819. cmXCodeObject* target =
  1820. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  1821. target->SetComment(cmtarget.GetName());
  1822. cmXCodeObject* buildPhases =
  1823. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1824. std::vector<cmXCodeObject*> emptyContentVector;
  1825. this->CreateCustomCommands(buildPhases, 0, 0, 0, emptyContentVector, 0,
  1826. cmtarget);
  1827. target->AddAttribute("buildPhases", buildPhases);
  1828. if(this->XcodeVersion > 20)
  1829. {
  1830. this->AddConfigurations(target, cmtarget);
  1831. }
  1832. else
  1833. {
  1834. const char* theConfig =
  1835. this->CurrentMakefile->GetDefinition("CMAKE_BUILD_TYPE");
  1836. cmXCodeObject* buildSettings =
  1837. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1838. this->CreateBuildSettings(cmtarget, buildSettings, theConfig);
  1839. target->AddAttribute("buildSettings", buildSettings);
  1840. }
  1841. cmXCodeObject* dependencies =
  1842. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1843. target->AddAttribute("dependencies", dependencies);
  1844. target->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  1845. target->AddAttribute("productName",this->CreateString(cmtarget.GetName()));
  1846. target->SetTarget(&cmtarget);
  1847. // Add source files without build rules for editing convenience.
  1848. if(cmtarget.GetType() == cmTarget::UTILITY)
  1849. {
  1850. std::vector<cmSourceFile*> const& sources = cmtarget.GetSourceFiles();
  1851. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1852. i != sources.end(); ++i)
  1853. {
  1854. if(!(*i)->GetPropertyAsBool("GENERATED"))
  1855. {
  1856. this->CreateXCodeFileReference(*i, cmtarget);
  1857. }
  1858. }
  1859. }
  1860. return target;
  1861. }
  1862. //----------------------------------------------------------------------------
  1863. std::string cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  1864. cmTarget& cmtarget)
  1865. {
  1866. std::string configTypes =
  1867. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES");
  1868. std::vector<std::string> configVectorIn;
  1869. std::vector<std::string> configVector;
  1870. configVectorIn.push_back(configTypes);
  1871. cmSystemTools::ExpandList(configVectorIn, configVector);
  1872. cmXCodeObject* configlist =
  1873. this->CreateObject(cmXCodeObject::XCConfigurationList);
  1874. cmXCodeObject* buildConfigurations =
  1875. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1876. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  1877. std::string comment = "Build configuration list for ";
  1878. comment += cmXCodeObject::PBXTypeNames[target->GetIsA()];
  1879. comment += " \"";
  1880. comment += cmtarget.GetName();
  1881. comment += "\"";
  1882. configlist->SetComment(comment.c_str());
  1883. target->AddAttribute("buildConfigurationList",
  1884. this->CreateObjectReference(configlist));
  1885. for(unsigned int i = 0; i < configVector.size(); ++i)
  1886. {
  1887. cmXCodeObject* config =
  1888. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  1889. buildConfigurations->AddObject(config);
  1890. cmXCodeObject* buildSettings =
  1891. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1892. this->CreateBuildSettings(cmtarget, buildSettings,
  1893. configVector[i].c_str());
  1894. config->AddAttribute("name", this->CreateString(configVector[i].c_str()));
  1895. config->SetComment(configVector[i].c_str());
  1896. config->AddAttribute("buildSettings", buildSettings);
  1897. }
  1898. if(configVector.size())
  1899. {
  1900. configlist->AddAttribute("defaultConfigurationName",
  1901. this->CreateString(configVector[0].c_str()));
  1902. configlist->AddAttribute("defaultConfigurationIsVisible",
  1903. this->CreateString("0"));
  1904. return configVector[0];
  1905. }
  1906. return "";
  1907. }
  1908. //----------------------------------------------------------------------------
  1909. const char* cmGlobalXCodeGenerator::GetTargetFileType(cmTarget& cmtarget)
  1910. {
  1911. switch(cmtarget.GetType())
  1912. {
  1913. case cmTarget::STATIC_LIBRARY:
  1914. return "archive.ar";
  1915. case cmTarget::MODULE_LIBRARY:
  1916. return ((this->XcodeVersion >= 22)?
  1917. "compiled.mach-o.executable" : "compiled.mach-o.dylib");
  1918. case cmTarget::SHARED_LIBRARY:
  1919. return (cmtarget.GetPropertyAsBool("FRAMEWORK")?
  1920. "wrapper.framework" : "compiled.mach-o.dylib");
  1921. case cmTarget::EXECUTABLE:
  1922. return "compiled.mach-o.executable";
  1923. default: break;
  1924. }
  1925. return 0;
  1926. }
  1927. //----------------------------------------------------------------------------
  1928. const char* cmGlobalXCodeGenerator::GetTargetProductType(cmTarget& cmtarget)
  1929. {
  1930. switch(cmtarget.GetType())
  1931. {
  1932. case cmTarget::STATIC_LIBRARY:
  1933. return "com.apple.product-type.library.static";
  1934. case cmTarget::MODULE_LIBRARY:
  1935. return ((this->XcodeVersion >= 22)? "com.apple.product-type.tool" :
  1936. "com.apple.product-type.library.dynamic");
  1937. case cmTarget::SHARED_LIBRARY:
  1938. return (cmtarget.GetPropertyAsBool("FRAMEWORK")?
  1939. "com.apple.product-type.framework" :
  1940. "com.apple.product-type.library.dynamic");
  1941. case cmTarget::EXECUTABLE:
  1942. return (cmtarget.GetPropertyAsBool("MACOSX_BUNDLE")?
  1943. "com.apple.product-type.application" :
  1944. "com.apple.product-type.tool");
  1945. default: break;
  1946. }
  1947. return 0;
  1948. }
  1949. //----------------------------------------------------------------------------
  1950. cmXCodeObject*
  1951. cmGlobalXCodeGenerator::CreateXCodeTarget(cmTarget& cmtarget,
  1952. cmXCodeObject* buildPhases)
  1953. {
  1954. cmXCodeObject* target =
  1955. this->CreateObject(cmXCodeObject::PBXNativeTarget);
  1956. target->AddAttribute("buildPhases", buildPhases);
  1957. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1958. target->AddAttribute("buildRules", buildRules);
  1959. std::string defConfig;
  1960. if(this->XcodeVersion > 20)
  1961. {
  1962. defConfig = this->AddConfigurations(target, cmtarget);
  1963. }
  1964. else
  1965. {
  1966. cmXCodeObject* buildSettings =
  1967. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1968. defConfig = this->CurrentMakefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1969. this->CreateBuildSettings(cmtarget, buildSettings, defConfig.c_str());
  1970. target->AddAttribute("buildSettings", buildSettings);
  1971. }
  1972. cmXCodeObject* dependencies =
  1973. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1974. target->AddAttribute("dependencies", dependencies);
  1975. target->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  1976. target->AddAttribute("productName",this->CreateString(cmtarget.GetName()));
  1977. cmXCodeObject* fileRef =
  1978. this->CreateObject(cmXCodeObject::PBXFileReference);
  1979. if(const char* fileType = this->GetTargetFileType(cmtarget))
  1980. {
  1981. fileRef->AddAttribute("explicitFileType", this->CreateString(fileType));
  1982. }
  1983. std::string fullName = cmtarget.GetFullName(defConfig.c_str());
  1984. fileRef->AddAttribute("path", this->CreateString(fullName.c_str()));
  1985. fileRef->AddAttribute("refType", this->CreateString("0"));
  1986. fileRef->AddAttribute("sourceTree",
  1987. this->CreateString("BUILT_PRODUCTS_DIR"));
  1988. fileRef->SetComment(cmtarget.GetName());
  1989. target->AddAttribute("productReference",
  1990. this->CreateObjectReference(fileRef));
  1991. if(const char* productType = this->GetTargetProductType(cmtarget))
  1992. {
  1993. target->AddAttribute("productType", this->CreateString(productType));
  1994. }
  1995. target->SetTarget(&cmtarget);
  1996. return target;
  1997. }
  1998. //----------------------------------------------------------------------------
  1999. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(cmTarget* t)
  2000. {
  2001. if(!t)
  2002. {
  2003. return 0;
  2004. }
  2005. for(std::vector<cmXCodeObject*>::iterator i = this->XCodeObjects.begin();
  2006. i != this->XCodeObjects.end(); ++i)
  2007. {
  2008. cmXCodeObject* o = *i;
  2009. if(o->GetTarget() == t)
  2010. {
  2011. return o;
  2012. }
  2013. }
  2014. return 0;
  2015. }
  2016. //----------------------------------------------------------------------------
  2017. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  2018. cmXCodeObject* dependTarget)
  2019. {
  2020. // make sure a target does not depend on itself
  2021. if(target == dependTarget)
  2022. {
  2023. return;
  2024. }
  2025. // now avoid circular references if dependTarget already
  2026. // depends on target then skip it. Circular references crashes
  2027. // xcode
  2028. cmXCodeObject* dependTargetDepends =
  2029. dependTarget->GetObject("dependencies");
  2030. if(dependTargetDepends)
  2031. {
  2032. if(dependTargetDepends->HasObject(target->GetPBXTargetDependency()))
  2033. {
  2034. return;
  2035. }
  2036. }
  2037. cmXCodeObject* targetdep = dependTarget->GetPBXTargetDependency();
  2038. if(!targetdep)
  2039. {
  2040. cmXCodeObject* container =
  2041. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  2042. container->SetComment("PBXContainerItemProxy");
  2043. container->AddAttribute("containerPortal",
  2044. this->CreateObjectReference(this->RootObject));
  2045. container->AddAttribute("proxyType", this->CreateString("1"));
  2046. container->AddAttribute("remoteGlobalIDString",
  2047. this->CreateObjectReference(dependTarget));
  2048. container->AddAttribute("remoteInfo",
  2049. this->CreateString(
  2050. dependTarget->GetTarget()->GetName()));
  2051. targetdep =
  2052. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  2053. targetdep->SetComment("PBXTargetDependency");
  2054. targetdep->AddAttribute("target",
  2055. this->CreateObjectReference(dependTarget));
  2056. targetdep->AddAttribute("targetProxy",
  2057. this->CreateObjectReference(container));
  2058. dependTarget->SetPBXTargetDependency(targetdep);
  2059. }
  2060. cmXCodeObject* depends = target->GetObject("dependencies");
  2061. if(!depends)
  2062. {
  2063. cmSystemTools::
  2064. Error("target does not have dependencies attribute error..");
  2065. }
  2066. else
  2067. {
  2068. depends->AddUniqueObject(targetdep);
  2069. }
  2070. }
  2071. //----------------------------------------------------------------------------
  2072. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  2073. const char* attribute,
  2074. const char* value)
  2075. {
  2076. if(settings)
  2077. {
  2078. cmXCodeObject* attr = settings->GetObject(attribute);
  2079. if(!attr)
  2080. {
  2081. settings->AddAttribute(attribute, this->CreateString(value));
  2082. }
  2083. else
  2084. {
  2085. std::string oldValue = attr->GetString();
  2086. oldValue += " ";
  2087. oldValue += value;
  2088. attr->SetString(oldValue.c_str());
  2089. }
  2090. }
  2091. }
  2092. //----------------------------------------------------------------------------
  2093. void cmGlobalXCodeGenerator
  2094. ::AppendBuildSettingAttribute(cmXCodeObject* target,
  2095. const char* attribute,
  2096. const char* value,
  2097. const char* configName)
  2098. {
  2099. if(this->XcodeVersion < 21)
  2100. {
  2101. // There is only one configuration. Add the setting to the buildSettings
  2102. // of the target.
  2103. this->AppendOrAddBuildSetting(target->GetObject("buildSettings"),
  2104. attribute, value);
  2105. }
  2106. else
  2107. {
  2108. // There are multiple configurations. Add the setting to the
  2109. // buildSettings of the configuration name given.
  2110. cmXCodeObject* configurationList =
  2111. target->GetObject("buildConfigurationList")->GetObject();
  2112. cmXCodeObject* buildConfigs =
  2113. configurationList->GetObject("buildConfigurations");
  2114. std::vector<cmXCodeObject*> list = buildConfigs->GetObjectList();
  2115. // each configuration and the target itself has a buildSettings in it
  2116. //list.push_back(target);
  2117. for(std::vector<cmXCodeObject*>::iterator i = list.begin();
  2118. i != list.end(); ++i)
  2119. {
  2120. if(configName)
  2121. {
  2122. if(strcmp((*i)->GetObject("name")->GetString(), configName) == 0)
  2123. {
  2124. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2125. this->AppendOrAddBuildSetting(settings, attribute, value);
  2126. }
  2127. }
  2128. else
  2129. {
  2130. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2131. this->AppendOrAddBuildSetting(settings, attribute, value);
  2132. }
  2133. }
  2134. }
  2135. }
  2136. //----------------------------------------------------------------------------
  2137. void cmGlobalXCodeGenerator
  2138. ::AddDependAndLinkInformation(cmXCodeObject* target)
  2139. {
  2140. cmTarget* cmtarget = target->GetTarget();
  2141. if(!cmtarget)
  2142. {
  2143. cmSystemTools::Error("Error no target on xobject\n");
  2144. return;
  2145. }
  2146. // Add dependencies on other CMake targets.
  2147. TargetDependSet const& deps = this->GetTargetDirectDepends(*cmtarget);
  2148. for(TargetDependSet::const_iterator i = deps.begin(); i != deps.end(); ++i)
  2149. {
  2150. if(cmXCodeObject* dptarget = this->FindXCodeTarget(*i))
  2151. {
  2152. this->AddDependTarget(target, dptarget);
  2153. }
  2154. }
  2155. // Skip link information for static libraries.
  2156. if(cmtarget->GetType() == cmTarget::STATIC_LIBRARY)
  2157. {
  2158. return;
  2159. }
  2160. // Loop over configuration types and set per-configuration info.
  2161. for(std::vector<std::string>::iterator i =
  2162. this->CurrentConfigurationTypes.begin();
  2163. i != this->CurrentConfigurationTypes.end(); ++i)
  2164. {
  2165. // Get the current configuration name.
  2166. const char* configName = i->c_str();
  2167. if(!*configName)
  2168. {
  2169. configName = 0;
  2170. }
  2171. // Compute the link library and directory information.
  2172. cmComputeLinkInformation* pcli = cmtarget->GetLinkInformation(configName);
  2173. if(!pcli)
  2174. {
  2175. continue;
  2176. }
  2177. cmComputeLinkInformation& cli = *pcli;
  2178. // Add dependencies directly on library files.
  2179. {
  2180. std::vector<std::string> const& libDeps = cli.GetDepends();
  2181. for(std::vector<std::string>::const_iterator j = libDeps.begin();
  2182. j != libDeps.end(); ++j)
  2183. {
  2184. target->AddDependLibrary(configName, j->c_str());
  2185. }
  2186. }
  2187. // add the library search paths
  2188. {
  2189. std::vector<std::string> const& libDirs = cli.GetDirectories();
  2190. std::string linkDirs;
  2191. for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
  2192. libDir != libDirs.end(); ++libDir)
  2193. {
  2194. if(libDir->size() && *libDir != "/usr/lib")
  2195. {
  2196. if(this->XcodeVersion > 15)
  2197. {
  2198. // now add the same one but append $(CONFIGURATION) to it:
  2199. linkDirs += " ";
  2200. linkDirs += this->XCodeEscapePath(
  2201. (*libDir + "/$(CONFIGURATION)").c_str());
  2202. }
  2203. linkDirs += " ";
  2204. linkDirs += this->XCodeEscapePath(libDir->c_str());
  2205. }
  2206. }
  2207. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  2208. linkDirs.c_str(), configName);
  2209. }
  2210. // add the framework search paths
  2211. {
  2212. const char* sep = "";
  2213. std::string fdirs;
  2214. std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
  2215. for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  2216. fdi != fwDirs.end(); ++fdi)
  2217. {
  2218. fdirs += sep;
  2219. sep = " ";
  2220. fdirs += this->XCodeEscapePath(fdi->c_str());
  2221. }
  2222. if(!fdirs.empty())
  2223. {
  2224. this->AppendBuildSettingAttribute(target, "FRAMEWORK_SEARCH_PATHS",
  2225. fdirs.c_str(), configName);
  2226. }
  2227. }
  2228. // now add the link libraries
  2229. {
  2230. std::string linkLibs;
  2231. const char* sep = "";
  2232. typedef cmComputeLinkInformation::ItemVector ItemVector;
  2233. ItemVector const& libNames = cli.GetItems();
  2234. for(ItemVector::const_iterator li = libNames.begin();
  2235. li != libNames.end(); ++li)
  2236. {
  2237. linkLibs += sep;
  2238. sep = " ";
  2239. if(li->IsPath)
  2240. {
  2241. linkLibs += this->XCodeEscapePath(li->Value.c_str());
  2242. }
  2243. else
  2244. {
  2245. linkLibs += li->Value;
  2246. }
  2247. }
  2248. this->AppendBuildSettingAttribute(target, "OTHER_LDFLAGS",
  2249. linkLibs.c_str(), configName);
  2250. }
  2251. }
  2252. }
  2253. //----------------------------------------------------------------------------
  2254. void cmGlobalXCodeGenerator::CreateGroups(cmLocalGenerator* root,
  2255. std::vector<cmLocalGenerator*>&
  2256. generators)
  2257. {
  2258. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2259. i != generators.end(); ++i)
  2260. {
  2261. if(this->IsExcluded(root, *i))
  2262. {
  2263. continue;
  2264. }
  2265. cmMakefile* mf = (*i)->GetMakefile();
  2266. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  2267. cmTargets &tgts = mf->GetTargets();
  2268. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  2269. {
  2270. cmTarget& cmtarget = l->second;
  2271. // Same skipping logic here as in CreateXCodeTargets so that we do not
  2272. // end up with (empty anyhow) ALL_BUILD and XCODE_DEPEND_HELPER source
  2273. // groups:
  2274. //
  2275. if(cmtarget.GetType() == cmTarget::GLOBAL_TARGET)
  2276. {
  2277. continue;
  2278. }
  2279. // add the soon to be generated Info.plist file as a source for a
  2280. // MACOSX_BUNDLE file
  2281. if(cmtarget.GetPropertyAsBool("MACOSX_BUNDLE"))
  2282. {
  2283. std::string plist = this->ComputeInfoPListLocation(cmtarget);
  2284. cmSourceFile* sf = mf->GetOrCreateSource(plist.c_str(), true);
  2285. cmtarget.AddSourceFile(sf);
  2286. }
  2287. std::vector<cmSourceFile*> classes = cmtarget.GetSourceFiles();
  2288. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  2289. s != classes.end(); s++)
  2290. {
  2291. cmSourceFile* sf = *s;
  2292. // Add the file to the list of sources.
  2293. std::string const& source = sf->GetFullPath();
  2294. cmSourceGroup& sourceGroup =
  2295. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2296. cmXCodeObject* pbxgroup =
  2297. this->CreateOrGetPBXGroup(cmtarget, &sourceGroup);
  2298. cmStdString key = GetGroupMapKey(cmtarget, sf);
  2299. this->GroupMap[key] = pbxgroup;
  2300. }
  2301. }
  2302. }
  2303. }
  2304. //----------------------------------------------------------------------------
  2305. cmXCodeObject* cmGlobalXCodeGenerator
  2306. ::CreateOrGetPBXGroup(cmTarget& cmtarget, cmSourceGroup* sg)
  2307. {
  2308. cmStdString s = cmtarget.GetName();
  2309. s += "/";
  2310. s += sg->GetName();
  2311. std::map<cmStdString, cmXCodeObject* >::iterator i =
  2312. this->GroupNameMap.find(s);
  2313. if(i != this->GroupNameMap.end())
  2314. {
  2315. return i->second;
  2316. }
  2317. i = this->TargetGroup.find(cmtarget.GetName());
  2318. cmXCodeObject* tgroup = 0;
  2319. if(i != this->TargetGroup.end())
  2320. {
  2321. tgroup = i->second;
  2322. }
  2323. else
  2324. {
  2325. tgroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2326. this->TargetGroup[cmtarget.GetName()] = tgroup;
  2327. cmXCodeObject* tgroupChildren =
  2328. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2329. tgroup->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  2330. tgroup->AddAttribute("children", tgroupChildren);
  2331. if(this->XcodeVersion == 15)
  2332. {
  2333. tgroup->AddAttribute("refType", this->CreateString("4"));
  2334. }
  2335. tgroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2336. this->SourcesGroupChildren->AddObject(tgroup);
  2337. }
  2338. // If it's the default source group (empty name) then put the source file
  2339. // directly in the tgroup...
  2340. //
  2341. if (cmStdString(sg->GetName()) == "")
  2342. {
  2343. this->GroupNameMap[s] = tgroup;
  2344. return tgroup;
  2345. }
  2346. cmXCodeObject* tgroupChildren = tgroup->GetObject("children");
  2347. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  2348. cmXCodeObject* groupChildren =
  2349. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2350. group->AddAttribute("name", this->CreateString(sg->GetName()));
  2351. group->AddAttribute("children", groupChildren);
  2352. if(this->XcodeVersion == 15)
  2353. {
  2354. group->AddAttribute("refType", this->CreateString("4"));
  2355. }
  2356. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  2357. tgroupChildren->AddObject(group);
  2358. this->GroupNameMap[s] = group;
  2359. return group;
  2360. }
  2361. //----------------------------------------------------------------------------
  2362. void cmGlobalXCodeGenerator
  2363. ::CreateXCodeObjects(cmLocalGenerator* root,
  2364. std::vector<cmLocalGenerator*>&
  2365. generators)
  2366. {
  2367. this->ClearXCodeObjects();
  2368. this->RootObject = 0;
  2369. this->SourcesGroupChildren = 0;
  2370. this->ResourcesGroupChildren = 0;
  2371. this->MainGroupChildren = 0;
  2372. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2373. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2374. cmXCodeObject* developBuildStyle =
  2375. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2376. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2377. if(this->XcodeVersion == 15)
  2378. {
  2379. developBuildStyle->AddAttribute("name",
  2380. this->CreateString("Development"));
  2381. developBuildStyle->AddAttribute("buildSettings", group);
  2382. listObjs->AddObject(developBuildStyle);
  2383. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2384. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("YES"));
  2385. cmXCodeObject* deployBuildStyle =
  2386. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2387. deployBuildStyle->AddAttribute("name", this->CreateString("Deployment"));
  2388. deployBuildStyle->AddAttribute("buildSettings", group);
  2389. listObjs->AddObject(deployBuildStyle);
  2390. }
  2391. else
  2392. {
  2393. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  2394. {
  2395. cmXCodeObject* buildStyle =
  2396. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2397. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2398. buildStyle->AddAttribute("name", this->CreateString(name));
  2399. buildStyle->SetComment(name);
  2400. cmXCodeObject* sgroup =
  2401. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2402. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2403. buildStyle->AddAttribute("buildSettings", sgroup);
  2404. listObjs->AddObject(buildStyle);
  2405. }
  2406. }
  2407. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2408. this->MainGroupChildren =
  2409. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2410. mainGroup->AddAttribute("children", this->MainGroupChildren);
  2411. if(this->XcodeVersion == 15)
  2412. {
  2413. mainGroup->AddAttribute("refType", this->CreateString("4"));
  2414. }
  2415. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2416. cmXCodeObject* sourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2417. this->SourcesGroupChildren =
  2418. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2419. sourcesGroup->AddAttribute("name", this->CreateString("Sources"));
  2420. sourcesGroup->AddAttribute("children", this->SourcesGroupChildren);
  2421. if(this->XcodeVersion == 15)
  2422. {
  2423. sourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2424. }
  2425. sourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2426. this->MainGroupChildren->AddObject(sourcesGroup);
  2427. cmXCodeObject* resourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2428. this->ResourcesGroupChildren =
  2429. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2430. resourcesGroup->AddAttribute("name", this->CreateString("Resources"));
  2431. resourcesGroup->AddAttribute("children", this->ResourcesGroupChildren);
  2432. if(this->XcodeVersion == 15)
  2433. {
  2434. resourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2435. }
  2436. resourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2437. this->MainGroupChildren->AddObject(resourcesGroup);
  2438. // now create the cmake groups
  2439. this->CreateGroups(root, generators);
  2440. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2441. productGroup->AddAttribute("name", this->CreateString("Products"));
  2442. if(this->XcodeVersion == 15)
  2443. {
  2444. productGroup->AddAttribute("refType", this->CreateString("4"));
  2445. }
  2446. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2447. cmXCodeObject* productGroupChildren =
  2448. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2449. productGroup->AddAttribute("children", productGroupChildren);
  2450. this->MainGroupChildren->AddObject(productGroup);
  2451. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  2452. this->RootObject->SetComment("Project object");
  2453. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2454. this->RootObject->AddAttribute("mainGroup",
  2455. this->CreateObjectReference(mainGroup));
  2456. this->RootObject->AddAttribute("buildSettings", group);
  2457. this->RootObject->AddAttribute("buildStyles", listObjs);
  2458. this->RootObject->AddAttribute("hasScannedForEncodings",
  2459. this->CreateString("0"));
  2460. if (this->XcodeVersion >= 30)
  2461. {
  2462. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2463. group->AddAttribute("BuildIndependentTargetsInParallel",
  2464. this->CreateString("YES"));
  2465. this->RootObject->AddAttribute("attributes", group);
  2466. if (this->XcodeVersion >= 32)
  2467. this->RootObject->AddAttribute("compatibilityVersion",
  2468. this->CreateString("Xcode 3.2"));
  2469. else if (this->XcodeVersion >= 31)
  2470. this->RootObject->AddAttribute("compatibilityVersion",
  2471. this->CreateString("Xcode 3.1"));
  2472. else
  2473. this->RootObject->AddAttribute("compatibilityVersion",
  2474. this->CreateString("Xcode 3.0"));
  2475. }
  2476. // Point Xcode at the top of the source tree.
  2477. {
  2478. std::string pdir =
  2479. this->RelativeToBinary(root->GetMakefile()->GetCurrentDirectory());
  2480. this->RootObject->AddAttribute("projectDirPath",
  2481. this->CreateString(pdir.c_str()));
  2482. this->RootObject->AddAttribute("projectRoot", this->CreateString(""));
  2483. }
  2484. cmXCodeObject* configlist =
  2485. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2486. cmXCodeObject* buildConfigurations =
  2487. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2488. std::vector<cmXCodeObject*> configs;
  2489. if(this->XcodeVersion == 15)
  2490. {
  2491. cmXCodeObject* configDebug =
  2492. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2493. configDebug->AddAttribute("name", this->CreateString("Debug"));
  2494. configs.push_back(configDebug);
  2495. cmXCodeObject* configRelease =
  2496. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2497. configRelease->AddAttribute("name", this->CreateString("Release"));
  2498. configs.push_back(configRelease);
  2499. }
  2500. else
  2501. {
  2502. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  2503. {
  2504. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2505. cmXCodeObject* config =
  2506. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2507. config->AddAttribute("name", this->CreateString(name));
  2508. configs.push_back(config);
  2509. }
  2510. }
  2511. for(std::vector<cmXCodeObject*>::iterator c = configs.begin();
  2512. c != configs.end(); ++c)
  2513. {
  2514. buildConfigurations->AddObject(*c);
  2515. }
  2516. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2517. std::string comment = "Build configuration list for PBXProject ";
  2518. comment += " \"";
  2519. comment += this->CurrentProject;
  2520. comment += "\"";
  2521. configlist->SetComment(comment.c_str());
  2522. configlist->AddAttribute("defaultConfigurationIsVisible",
  2523. this->CreateString("0"));
  2524. configlist->AddAttribute("defaultConfigurationName",
  2525. this->CreateString("Debug"));
  2526. cmXCodeObject* buildSettings =
  2527. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2528. const char* osxArch =
  2529. this->CurrentMakefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  2530. if(strlen(osxArch) == 0)
  2531. {
  2532. if(this->XcodeVersion >= 32)
  2533. {
  2534. osxArch = "$(ARCHS_STANDARD_32_64_BIT)";
  2535. }
  2536. else if(this->XcodeVersion == 31)
  2537. {
  2538. osxArch = "$(ARCHS_STANDARD_32_BIT)";
  2539. }
  2540. else if(this->XcodeVersion <= 30)
  2541. {
  2542. #ifdef __ppc__
  2543. osxArch = "ppc";
  2544. #endif
  2545. #ifdef __i386
  2546. osxArch = "i386";
  2547. #endif
  2548. }
  2549. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH",
  2550. this->CreateString("YES"));
  2551. }
  2552. const char* sysroot =
  2553. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  2554. const char* sysrootDefault =
  2555. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT_DEFAULT");
  2556. const char* deploymentTarget =
  2557. this->CurrentMakefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  2558. if(osxArch && sysroot)
  2559. {
  2560. bool flagsUsed = false;
  2561. // recompute this as it may have been changed since enable language
  2562. this->Architectures.clear();
  2563. cmSystemTools::ExpandListArgument(std::string(osxArch),
  2564. this->Architectures);
  2565. flagsUsed = true;
  2566. buildSettings->AddAttribute("SDKROOT",
  2567. this->CreateString(sysroot));
  2568. std::string archString;
  2569. const char* sep = "";
  2570. for( std::vector<std::string>::iterator i =
  2571. this->Architectures.begin();
  2572. i != this->Architectures.end(); ++i)
  2573. {
  2574. archString += sep;
  2575. archString += *i;
  2576. sep = " ";
  2577. }
  2578. buildSettings->AddAttribute("ARCHS",
  2579. this->CreateString(archString.c_str()));
  2580. if(!flagsUsed && sysrootDefault &&
  2581. strcmp(sysroot, sysrootDefault) != 0)
  2582. {
  2583. buildSettings->AddAttribute("SDKROOT",
  2584. this->CreateString(sysroot));
  2585. }
  2586. }
  2587. if(deploymentTarget && *deploymentTarget)
  2588. {
  2589. buildSettings->AddAttribute("MACOSX_DEPLOYMENT_TARGET",
  2590. this->CreateString(deploymentTarget));
  2591. }
  2592. // Put this last so it can override existing settings
  2593. // Convert "CMAKE_XCODE_ATTRIBUTE_*" variables directly.
  2594. {
  2595. std::vector<std::string> vars = this->CurrentMakefile->GetDefinitions();
  2596. for(std::vector<std::string>::const_iterator i = vars.begin();
  2597. i != vars.end(); ++i)
  2598. {
  2599. if(i->find("CMAKE_XCODE_ATTRIBUTE_") == 0)
  2600. {
  2601. buildSettings->AddAttribute(i->substr(22).c_str(),
  2602. this->CreateString(
  2603. this->CurrentMakefile->GetDefinition(i->c_str())));
  2604. }
  2605. }
  2606. }
  2607. std::string symroot = root->GetMakefile()->GetCurrentOutputDirectory();
  2608. symroot += "/build";
  2609. buildSettings->AddAttribute("SYMROOT", this->CreateString(symroot.c_str()));
  2610. for( std::vector<cmXCodeObject*>::iterator i = configs.begin();
  2611. i != configs.end(); ++i)
  2612. {
  2613. (*i)->AddAttribute("buildSettings", buildSettings);
  2614. }
  2615. this->RootObject->AddAttribute("buildConfigurationList",
  2616. this->CreateObjectReference(configlist));
  2617. std::vector<cmXCodeObject*> targets;
  2618. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2619. i != generators.end(); ++i)
  2620. {
  2621. if(!this->IsExcluded(root, *i))
  2622. {
  2623. this->CreateXCodeTargets(*i, targets);
  2624. }
  2625. }
  2626. // loop over all targets and add link and depend info
  2627. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2628. i != targets.end(); ++i)
  2629. {
  2630. cmXCodeObject* t = *i;
  2631. this->AddDependAndLinkInformation(t);
  2632. }
  2633. // now create xcode depend hack makefile
  2634. this->CreateXCodeDependHackTarget(targets);
  2635. // now add all targets to the root object
  2636. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2637. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2638. i != targets.end(); ++i)
  2639. {
  2640. cmXCodeObject* t = *i;
  2641. allTargets->AddObject(t);
  2642. cmXCodeObject* productRef = t->GetObject("productReference");
  2643. if(productRef)
  2644. {
  2645. productGroupChildren->AddObject(productRef->GetObject());
  2646. }
  2647. }
  2648. this->RootObject->AddAttribute("targets", allTargets);
  2649. }
  2650. //----------------------------------------------------------------------------
  2651. void
  2652. cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  2653. std::vector<cmXCodeObject*>& targets)
  2654. {
  2655. cmGeneratedFileStream
  2656. makefileStream(this->CurrentXCodeHackMakefile.c_str());
  2657. if(!makefileStream)
  2658. {
  2659. cmSystemTools::Error("Could not create",
  2660. this->CurrentXCodeHackMakefile.c_str());
  2661. return;
  2662. }
  2663. makefileStream.SetCopyIfDifferent(true);
  2664. // one more pass for external depend information not handled
  2665. // correctly by xcode
  2666. makefileStream << "# DO NOT EDIT\n";
  2667. makefileStream << "# This makefile makes sure all linkable targets are\n";
  2668. makefileStream << "# up-to-date with anything they link to, avoiding a "
  2669. "bug in XCode 1.5\n";
  2670. for(std::vector<std::string>::const_iterator
  2671. ct = this->CurrentConfigurationTypes.begin();
  2672. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2673. {
  2674. if(this->XcodeVersion < 21 || ct->empty())
  2675. {
  2676. makefileStream << "all: ";
  2677. }
  2678. else
  2679. {
  2680. makefileStream << "all." << *ct << ": ";
  2681. }
  2682. const char* configName = 0;
  2683. if(!ct->empty())
  2684. {
  2685. configName = ct->c_str();
  2686. }
  2687. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2688. i != targets.end(); ++i)
  2689. {
  2690. cmXCodeObject* target = *i;
  2691. cmTarget* t =target->GetTarget();
  2692. if(t->GetType() == cmTarget::EXECUTABLE ||
  2693. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2694. t->GetType() == cmTarget::MODULE_LIBRARY)
  2695. {
  2696. std::string tfull = t->GetFullPath(configName);
  2697. makefileStream << "\\\n\t" <<
  2698. this->ConvertToRelativeForMake(tfull.c_str());
  2699. }
  2700. }
  2701. makefileStream << "\n\n";
  2702. }
  2703. makefileStream
  2704. << "# For each target create a dummy rule "
  2705. "so the target does not have to exist\n";
  2706. std::set<cmStdString> emitted;
  2707. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2708. i != targets.end(); ++i)
  2709. {
  2710. cmXCodeObject* target = *i;
  2711. std::map<cmStdString, cmXCodeObject::StringVec> const& deplibs =
  2712. target->GetDependLibraries();
  2713. for(std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator ci
  2714. = deplibs.begin(); ci != deplibs.end(); ++ci)
  2715. {
  2716. for(cmXCodeObject::StringVec::const_iterator d = ci->second.begin();
  2717. d != ci->second.end(); ++d)
  2718. {
  2719. if(emitted.insert(*d).second)
  2720. {
  2721. makefileStream <<
  2722. this->ConvertToRelativeForMake(d->c_str()) << ":\n";
  2723. }
  2724. }
  2725. }
  2726. }
  2727. makefileStream << "\n\n";
  2728. // Write rules to help Xcode relink things at the right time.
  2729. makefileStream <<
  2730. "# Rules to remove targets that are older than anything to which they\n"
  2731. "# link. This forces Xcode to relink the targets from scratch. It\n"
  2732. "# does not seem to check these dependencies itself.\n";
  2733. for(std::vector<std::string>::const_iterator
  2734. ct = this->CurrentConfigurationTypes.begin();
  2735. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2736. {
  2737. const char* configName = 0;
  2738. if(!ct->empty())
  2739. {
  2740. configName = ct->c_str();
  2741. }
  2742. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2743. i != targets.end(); ++i)
  2744. {
  2745. cmXCodeObject* target = *i;
  2746. cmTarget* t =target->GetTarget();
  2747. if(t->GetType() == cmTarget::EXECUTABLE ||
  2748. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2749. t->GetType() == cmTarget::MODULE_LIBRARY)
  2750. {
  2751. // Create a rule for this target.
  2752. std::string tfull = t->GetFullPath(configName);
  2753. makefileStream << this->ConvertToRelativeForMake(tfull.c_str())
  2754. << ":";
  2755. // List dependencies if any exist.
  2756. std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator
  2757. x = target->GetDependLibraries().find(*ct);
  2758. if(x != target->GetDependLibraries().end())
  2759. {
  2760. std::vector<cmStdString> const& deplibs = x->second;
  2761. for(std::vector<cmStdString>::const_iterator d = deplibs.begin();
  2762. d != deplibs.end(); ++d)
  2763. {
  2764. makefileStream << "\\\n\t" <<
  2765. this->ConvertToRelativeForMake(d->c_str());
  2766. }
  2767. }
  2768. // Write the action to remove the target if it is out of date.
  2769. makefileStream << "\n";
  2770. makefileStream << "\t/bin/rm -f "
  2771. << this->ConvertToRelativeForMake(tfull.c_str())
  2772. << "\n";
  2773. // if building for more than one architecture
  2774. // then remove those exectuables as well
  2775. if(this->Architectures.size() > 1)
  2776. {
  2777. std::string universal =
  2778. t->GetMakefile()->GetCurrentOutputDirectory();
  2779. universal += "/";
  2780. universal += this->CurrentProject;
  2781. universal += ".build/";
  2782. universal += configName;
  2783. universal += "/";
  2784. universal += t->GetName();
  2785. universal += ".build/Objects-normal/";
  2786. for( std::vector<std::string>::iterator arch =
  2787. this->Architectures.begin();
  2788. arch != this->Architectures.end(); ++arch)
  2789. {
  2790. std::string universalFile = universal;
  2791. universalFile += *arch;
  2792. universalFile += "/";
  2793. universalFile += t->GetFullName(configName);
  2794. makefileStream << "\t/bin/rm -f "
  2795. <<
  2796. this->ConvertToRelativeForMake(universalFile.c_str())
  2797. << "\n";
  2798. }
  2799. }
  2800. makefileStream << "\n\n";
  2801. }
  2802. }
  2803. }
  2804. }
  2805. //----------------------------------------------------------------------------
  2806. void
  2807. cmGlobalXCodeGenerator::OutputXCodeProject(cmLocalGenerator* root,
  2808. std::vector<cmLocalGenerator*>&
  2809. generators)
  2810. {
  2811. if(generators.size() == 0)
  2812. {
  2813. return;
  2814. }
  2815. // Skip local generators that are excluded from this project.
  2816. for(std::vector<cmLocalGenerator*>::iterator g = generators.begin();
  2817. g != generators.end(); ++g)
  2818. {
  2819. if(this->IsExcluded(root, *g))
  2820. {
  2821. continue;
  2822. }
  2823. }
  2824. this->CreateXCodeObjects(root,
  2825. generators);
  2826. std::string xcodeDir = root->GetMakefile()->GetStartOutputDirectory();
  2827. xcodeDir += "/";
  2828. xcodeDir += root->GetMakefile()->GetProjectName();
  2829. xcodeDir += ".xcode";
  2830. if(this->XcodeVersion > 20)
  2831. {
  2832. xcodeDir += "proj";
  2833. }
  2834. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  2835. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  2836. cmGeneratedFileStream fout(xcodeProjFile.c_str());
  2837. fout.SetCopyIfDifferent(true);
  2838. if(!fout)
  2839. {
  2840. return;
  2841. }
  2842. this->WriteXCodePBXProj(fout, root, generators);
  2843. this->ClearXCodeObjects();
  2844. }
  2845. //----------------------------------------------------------------------------
  2846. void
  2847. cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  2848. cmLocalGenerator* ,
  2849. std::vector<cmLocalGenerator*>& )
  2850. {
  2851. fout << "// !$*UTF8*$!\n";
  2852. fout << "{\n";
  2853. cmXCodeObject::Indent(1, fout);
  2854. fout << "archiveVersion = 1;\n";
  2855. cmXCodeObject::Indent(1, fout);
  2856. fout << "classes = {\n";
  2857. cmXCodeObject::Indent(1, fout);
  2858. fout << "};\n";
  2859. cmXCodeObject::Indent(1, fout);
  2860. if(this->XcodeVersion >= 21)
  2861. {
  2862. if (this->XcodeVersion >= 32)
  2863. fout << "objectVersion = 46;\n";
  2864. else if (this->XcodeVersion >= 31)
  2865. fout << "objectVersion = 45;\n";
  2866. else if (this->XcodeVersion >= 30)
  2867. fout << "objectVersion = 44;\n";
  2868. else
  2869. fout << "objectVersion = 42;\n";
  2870. cmXCode21Object::PrintList(this->XCodeObjects, fout);
  2871. }
  2872. else
  2873. {
  2874. fout << "objectVersion = 39;\n";
  2875. cmXCodeObject::PrintList(this->XCodeObjects, fout);
  2876. }
  2877. cmXCodeObject::Indent(1, fout);
  2878. fout << "rootObject = " << this->RootObject->GetId() << ";\n";
  2879. fout << "}\n";
  2880. }
  2881. //----------------------------------------------------------------------------
  2882. const char* cmGlobalXCodeGenerator::GetCMakeCFGInitDirectory()
  2883. {
  2884. return this->XcodeVersion >= 21? "$(CONFIGURATION)" : ".";
  2885. }
  2886. //----------------------------------------------------------------------------
  2887. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  2888. const
  2889. {
  2890. entry.Name = this->GetName();
  2891. entry.Brief = "Generate XCode project files.";
  2892. entry.Full = "";
  2893. }
  2894. //----------------------------------------------------------------------------
  2895. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(const char* p)
  2896. {
  2897. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2898. {
  2899. return cmSystemTools::ConvertToOutputPath(p);
  2900. }
  2901. else
  2902. {
  2903. std::string ret =
  2904. this->CurrentLocalGenerator->
  2905. ConvertToRelativePath(this->CurrentOutputDirectoryComponents, p);
  2906. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2907. }
  2908. }
  2909. //----------------------------------------------------------------------------
  2910. std::string cmGlobalXCodeGenerator::ConvertToRelativeForXCode(const char* p)
  2911. {
  2912. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2913. {
  2914. return cmSystemTools::ConvertToOutputPath(p);
  2915. }
  2916. else
  2917. {
  2918. std::string ret =
  2919. this->CurrentLocalGenerator->
  2920. ConvertToRelativePath(this->ProjectOutputDirectoryComponents, p);
  2921. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2922. }
  2923. }
  2924. //----------------------------------------------------------------------------
  2925. std::string cmGlobalXCodeGenerator::RelativeToSource(const char* p)
  2926. {
  2927. // We force conversion because Xcode breakpoints do not work unless
  2928. // they are in a file named relative to the source tree.
  2929. return this->CurrentLocalGenerator->
  2930. ConvertToRelativePath(this->ProjectSourceDirectoryComponents, p, true);
  2931. }
  2932. //----------------------------------------------------------------------------
  2933. std::string cmGlobalXCodeGenerator::RelativeToBinary(const char* p)
  2934. {
  2935. return this->CurrentLocalGenerator->
  2936. ConvertToRelativePath(this->ProjectOutputDirectoryComponents, p);
  2937. }
  2938. //----------------------------------------------------------------------------
  2939. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const char* p)
  2940. {
  2941. std::string ret = p;
  2942. if(ret.find(' ') != ret.npos)
  2943. {
  2944. std::string t = ret;
  2945. ret = "\"";
  2946. ret += t;
  2947. ret += "\"";
  2948. }
  2949. return ret;
  2950. }
  2951. //----------------------------------------------------------------------------
  2952. void cmGlobalXCodeGenerator::
  2953. GetTargetObjectFileDirectories(cmTarget* target,
  2954. std::vector<std::string>&
  2955. dirs)
  2956. {
  2957. std::string dir = this->CurrentMakefile->GetCurrentOutputDirectory();
  2958. dir += "/";
  2959. dir += this->CurrentMakefile->GetProjectName();
  2960. dir += ".build/";
  2961. dir += this->GetCMakeCFGInitDirectory();
  2962. dir += "/";
  2963. dir += target->GetName();
  2964. dir += ".build/Objects-normal/";
  2965. std::string dirsave = dir;
  2966. if(this->Architectures.size())
  2967. {
  2968. for(std::vector<std::string>::iterator i = this->Architectures.begin();
  2969. i != this->Architectures.end(); ++i)
  2970. {
  2971. dir += *i;
  2972. dirs.push_back(dir);
  2973. dir = dirsave;
  2974. }
  2975. }
  2976. else
  2977. {
  2978. dirs.push_back(dir);
  2979. }
  2980. }
  2981. //----------------------------------------------------------------------------
  2982. void
  2983. cmGlobalXCodeGenerator
  2984. ::AppendDirectoryForConfig(const char* prefix,
  2985. const char* config,
  2986. const char* suffix,
  2987. std::string& dir)
  2988. {
  2989. if(this->XcodeVersion > 20)
  2990. {
  2991. if(config)
  2992. {
  2993. dir += prefix;
  2994. dir += config;
  2995. dir += suffix;
  2996. }
  2997. }
  2998. }
  2999. //----------------------------------------------------------------------------
  3000. std::string cmGlobalXCodeGenerator::LookupFlags(const char* varNamePrefix,
  3001. const char* varNameLang,
  3002. const char* varNameSuffix,
  3003. const char* default_flags)
  3004. {
  3005. if(varNameLang)
  3006. {
  3007. std::string varName = varNamePrefix;
  3008. varName += varNameLang;
  3009. varName += varNameSuffix;
  3010. if(const char* varValue =
  3011. this->CurrentMakefile->GetDefinition(varName.c_str()))
  3012. {
  3013. if(*varValue)
  3014. {
  3015. return varValue;
  3016. }
  3017. }
  3018. }
  3019. return default_flags;
  3020. }
  3021. //----------------------------------------------------------------------------
  3022. void cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  3023. const char* defines_list,
  3024. bool dflag)
  3025. {
  3026. // Skip this if there are no definitions.
  3027. if(!defines_list)
  3028. {
  3029. return;
  3030. }
  3031. // Expand the list of definitions.
  3032. std::vector<std::string> defines;
  3033. cmSystemTools::ExpandListArgument(defines_list, defines);
  3034. // Store the definitions in the string.
  3035. this->AppendDefines(defs, defines, dflag);
  3036. }
  3037. //----------------------------------------------------------------------------
  3038. void
  3039. cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  3040. std::vector<std::string> const& defines,
  3041. bool dflag)
  3042. {
  3043. // GCC_PREPROCESSOR_DEFINITIONS is a space-separated list of definitions.
  3044. std::string def;
  3045. for(std::vector<std::string>::const_iterator di = defines.begin();
  3046. di != defines.end(); ++di)
  3047. {
  3048. // Start with -D if requested.
  3049. def = dflag? "-D": "";
  3050. def += *di;
  3051. // Append the flag with needed escapes.
  3052. std::string tmp;
  3053. this->AppendFlag(tmp, def);
  3054. defs.Add(tmp.c_str());
  3055. }
  3056. }
  3057. //----------------------------------------------------------------------------
  3058. void cmGlobalXCodeGenerator::AppendFlag(std::string& flags,
  3059. std::string const& flag)
  3060. {
  3061. // Short-circuit for an empty flag.
  3062. if(flag.empty())
  3063. {
  3064. return;
  3065. }
  3066. // Separate from previous flags.
  3067. if(!flags.empty())
  3068. {
  3069. flags += " ";
  3070. }
  3071. // Check if the flag needs quoting.
  3072. bool quoteFlag =
  3073. flag.find_first_of("`~!@#$%^&*()+={}[]|:;\"'<>,.? ") != flag.npos;
  3074. // We escape a flag as follows:
  3075. // - Place each flag in single quotes ''
  3076. // - Escape a single quote as \\'
  3077. // - Escape a backslash as \\\\ since it itself is an escape
  3078. // Note that in the code below we need one more level of escapes for
  3079. // C string syntax in this source file.
  3080. //
  3081. // The final level of escaping is done when the string is stored
  3082. // into the project file by cmXCodeObject::PrintString.
  3083. if(quoteFlag)
  3084. {
  3085. // Open single quote.
  3086. flags += "'";
  3087. }
  3088. // Flag value with escaped quotes and backslashes.
  3089. for(const char* c = flag.c_str(); *c; ++c)
  3090. {
  3091. if(*c == '\'')
  3092. {
  3093. flags += "\\\\'";
  3094. }
  3095. else if(*c == '\\')
  3096. {
  3097. flags += "\\\\\\\\";
  3098. }
  3099. else
  3100. {
  3101. flags += *c;
  3102. }
  3103. }
  3104. if(quoteFlag)
  3105. {
  3106. // Close single quote.
  3107. flags += "'";
  3108. }
  3109. }
  3110. //----------------------------------------------------------------------------
  3111. std::string
  3112. cmGlobalXCodeGenerator::ComputeInfoPListLocation(cmTarget& target)
  3113. {
  3114. std::string plist = target.GetMakefile()->GetCurrentOutputDirectory();
  3115. plist += cmake::GetCMakeFilesDirectory();
  3116. plist += "/";
  3117. plist += target.GetName();
  3118. plist += ".dir/Info.plist";
  3119. return plist;
  3120. }
  3121. //----------------------------------------------------------------------------
  3122. // Return true if the generated build tree may contain multiple builds.
  3123. // i.e. "Can I build Debug and Release in the same tree?"
  3124. bool cmGlobalXCodeGenerator::IsMultiConfig()
  3125. {
  3126. // Old Xcode 1.5 is single config:
  3127. if(this->XcodeVersion == 15)
  3128. {
  3129. return false;
  3130. }
  3131. // Newer Xcode versions are multi config:
  3132. return true;
  3133. }