cmGlobalXCodeGenerator.cxx 111 KB

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