cmGlobalXCodeGenerator.cxx 124 KB

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