cmGlobalXCodeGenerator.cxx 125 KB

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