cmGlobalXCodeGenerator.cxx 125 KB

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