cmGlobalXCodeGenerator.cxx 124 KB

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