cmGlobalXCodeGenerator.cxx 126 KB

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