cmGlobalXCodeGenerator.cxx 124 KB

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