cmGlobalXCodeGenerator.cxx 123 KB

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