cmGlobalXCodeGenerator.cxx 124 KB

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