cmGlobalXCodeGenerator.cxx 129 KB

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