cmGlobalXCodeGenerator.cxx 125 KB

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