cmGlobalXCodeGenerator.cxx 125 KB

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