cmGlobalXCodeGenerator.cxx 124 KB

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