cmGlobalXCodeGenerator.cxx 126 KB

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