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