cmGlobalXCodeGenerator.cxx 128 KB

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