cmGlobalXCodeGenerator.cxx 121 KB

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