cmGlobalXCodeGenerator.cxx 124 KB

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