cmGlobalXCodeGenerator.cxx 125 KB

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