cmGlobalXCodeGenerator.cxx 127 KB

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