cmGlobalXCodeGenerator.cxx 125 KB

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