cmGlobalXCodeGenerator.cxx 124 KB

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