cmGlobalXCodeGenerator.cxx 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512
  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);
  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);
  367. makeHelper.push_back("-f");
  368. makeHelper.push_back(this->CurrentXCodeHackMakefile);
  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);
  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)) {
  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);
  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. // Select the current set of configuration types.
  799. this->CurrentConfigurationTypes.clear();
  800. this->CurrentMakefile->GetConfigurations(this->CurrentConfigurationTypes);
  801. if (this->CurrentConfigurationTypes.empty()) {
  802. this->CurrentConfigurationTypes.push_back("");
  803. }
  804. }
  805. struct cmSourceFilePathCompare
  806. {
  807. bool operator()(cmSourceFile* l, cmSourceFile* r)
  808. {
  809. return l->GetFullPath() < r->GetFullPath();
  810. }
  811. };
  812. struct cmCompareTargets
  813. {
  814. bool operator()(std::string const& a, std::string const& b) const
  815. {
  816. if (a == "ALL_BUILD") {
  817. return true;
  818. }
  819. if (b == "ALL_BUILD") {
  820. return false;
  821. }
  822. return strcmp(a.c_str(), b.c_str()) < 0;
  823. }
  824. };
  825. bool cmGlobalXCodeGenerator::CreateXCodeTargets(
  826. cmLocalGenerator* gen, std::vector<cmXCodeObject*>& targets)
  827. {
  828. this->SetCurrentLocalGenerator(gen);
  829. std::vector<cmGeneratorTarget*> tgts =
  830. this->CurrentLocalGenerator->GetGeneratorTargets();
  831. typedef std::map<std::string, cmGeneratorTarget*, cmCompareTargets>
  832. cmSortedTargets;
  833. cmSortedTargets sortedTargets;
  834. for (std::vector<cmGeneratorTarget*>::iterator l = tgts.begin();
  835. l != tgts.end(); l++) {
  836. sortedTargets[(*l)->GetName()] = *l;
  837. }
  838. for (cmSortedTargets::iterator l = sortedTargets.begin();
  839. l != sortedTargets.end(); l++) {
  840. cmGeneratorTarget* gtgt = l->second;
  841. std::string targetName = gtgt->GetName();
  842. // make sure ALL_BUILD, INSTALL, etc are only done once
  843. if (this->SpecialTargetEmitted(targetName)) {
  844. continue;
  845. }
  846. if (gtgt->GetType() == cmState::INTERFACE_LIBRARY) {
  847. continue;
  848. }
  849. if (gtgt->GetType() == cmState::UTILITY ||
  850. gtgt->GetType() == cmState::GLOBAL_TARGET) {
  851. cmXCodeObject* t = this->CreateUtilityTarget(gtgt);
  852. if (!t) {
  853. return false;
  854. }
  855. targets.push_back(t);
  856. continue;
  857. }
  858. // organize the sources
  859. std::vector<cmSourceFile*> classes;
  860. if (!gtgt->GetConfigCommonSourceFiles(classes)) {
  861. return false;
  862. }
  863. std::sort(classes.begin(), classes.end(), cmSourceFilePathCompare());
  864. gtgt->ComputeObjectMapping();
  865. std::vector<cmXCodeObject*> externalObjFiles;
  866. std::vector<cmXCodeObject*> headerFiles;
  867. std::vector<cmXCodeObject*> resourceFiles;
  868. std::vector<cmXCodeObject*> sourceFiles;
  869. for (std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  870. i != classes.end(); ++i) {
  871. cmXCodeObject* xsf =
  872. this->CreateXCodeSourceFile(this->CurrentLocalGenerator, *i, gtgt);
  873. cmXCodeObject* fr = xsf->GetObject("fileRef");
  874. cmXCodeObject* filetype = fr->GetObject()->GetObject("explicitFileType");
  875. cmGeneratorTarget::SourceFileFlags tsFlags =
  876. gtgt->GetTargetSourceFileFlags(*i);
  877. if (filetype && filetype->GetString() == "compiled.mach-o.objfile") {
  878. if ((*i)->GetObjectLibrary().empty()) {
  879. externalObjFiles.push_back(xsf);
  880. }
  881. } else if (this->IsHeaderFile(*i) ||
  882. (tsFlags.Type ==
  883. cmGeneratorTarget::SourceFileTypePrivateHeader) ||
  884. (tsFlags.Type ==
  885. cmGeneratorTarget::SourceFileTypePublicHeader)) {
  886. headerFiles.push_back(xsf);
  887. } else if (tsFlags.Type == cmGeneratorTarget::SourceFileTypeResource) {
  888. resourceFiles.push_back(xsf);
  889. } else if (!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY")) {
  890. // Include this file in the build if it has a known language
  891. // and has not been listed as an ignored extension for this
  892. // generator.
  893. if (!this->CurrentLocalGenerator->GetSourceFileLanguage(**i).empty() &&
  894. !this->IgnoreFile((*i)->GetExtension().c_str())) {
  895. sourceFiles.push_back(xsf);
  896. }
  897. }
  898. }
  899. if (this->XcodeVersion < 50) {
  900. // Add object library contents as external objects. (Equivalent to
  901. // the externalObjFiles above, except each one is not a cmSourceFile
  902. // within the target.)
  903. std::vector<std::string> objs;
  904. gtgt->UseObjectLibraries(objs, "");
  905. for (std::vector<std::string>::const_iterator oi = objs.begin();
  906. oi != objs.end(); ++oi) {
  907. std::string obj = *oi;
  908. cmXCodeObject* xsf =
  909. this->CreateXCodeSourceFileFromPath(obj, gtgt, "", 0);
  910. externalObjFiles.push_back(xsf);
  911. }
  912. }
  913. // some build phases only apply to bundles and/or frameworks
  914. bool isFrameworkTarget = gtgt->IsFrameworkOnApple();
  915. bool isBundleTarget = gtgt->GetPropertyAsBool("MACOSX_BUNDLE");
  916. bool isCFBundleTarget = gtgt->IsCFBundleOnApple();
  917. cmXCodeObject* buildFiles = 0;
  918. // create source build phase
  919. cmXCodeObject* sourceBuildPhase = 0;
  920. if (!sourceFiles.empty()) {
  921. sourceBuildPhase =
  922. this->CreateObject(cmXCodeObject::PBXSourcesBuildPhase);
  923. sourceBuildPhase->SetComment("Sources");
  924. sourceBuildPhase->AddAttribute("buildActionMask",
  925. this->CreateString("2147483647"));
  926. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  927. for (std::vector<cmXCodeObject*>::iterator i = sourceFiles.begin();
  928. i != sourceFiles.end(); ++i) {
  929. buildFiles->AddObject(*i);
  930. }
  931. sourceBuildPhase->AddAttribute("files", buildFiles);
  932. sourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  933. this->CreateString("0"));
  934. }
  935. // create header build phase - only for framework targets
  936. cmXCodeObject* headerBuildPhase = 0;
  937. if (!headerFiles.empty() && isFrameworkTarget) {
  938. headerBuildPhase =
  939. this->CreateObject(cmXCodeObject::PBXHeadersBuildPhase);
  940. headerBuildPhase->SetComment("Headers");
  941. headerBuildPhase->AddAttribute("buildActionMask",
  942. this->CreateString("2147483647"));
  943. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  944. for (std::vector<cmXCodeObject*>::iterator i = headerFiles.begin();
  945. i != headerFiles.end(); ++i) {
  946. buildFiles->AddObject(*i);
  947. }
  948. headerBuildPhase->AddAttribute("files", buildFiles);
  949. headerBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  950. this->CreateString("0"));
  951. }
  952. // create resource build phase - only for framework or bundle targets
  953. cmXCodeObject* resourceBuildPhase = 0;
  954. if (!resourceFiles.empty() &&
  955. (isFrameworkTarget || isBundleTarget || isCFBundleTarget)) {
  956. resourceBuildPhase =
  957. this->CreateObject(cmXCodeObject::PBXResourcesBuildPhase);
  958. resourceBuildPhase->SetComment("Resources");
  959. resourceBuildPhase->AddAttribute("buildActionMask",
  960. this->CreateString("2147483647"));
  961. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  962. for (std::vector<cmXCodeObject*>::iterator i = resourceFiles.begin();
  963. i != resourceFiles.end(); ++i) {
  964. buildFiles->AddObject(*i);
  965. }
  966. resourceBuildPhase->AddAttribute("files", buildFiles);
  967. resourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  968. this->CreateString("0"));
  969. }
  970. // create vector of "non-resource content file" build phases - only for
  971. // framework or bundle targets
  972. std::vector<cmXCodeObject*> contentBuildPhases;
  973. if (isFrameworkTarget || isBundleTarget || isCFBundleTarget) {
  974. typedef std::map<std::string, std::vector<cmSourceFile*> >
  975. mapOfVectorOfSourceFiles;
  976. mapOfVectorOfSourceFiles bundleFiles;
  977. for (std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  978. i != classes.end(); ++i) {
  979. cmGeneratorTarget::SourceFileFlags tsFlags =
  980. gtgt->GetTargetSourceFileFlags(*i);
  981. if (tsFlags.Type == cmGeneratorTarget::SourceFileTypeMacContent) {
  982. bundleFiles[tsFlags.MacFolder].push_back(*i);
  983. }
  984. }
  985. mapOfVectorOfSourceFiles::iterator mit;
  986. for (mit = bundleFiles.begin(); mit != bundleFiles.end(); ++mit) {
  987. cmXCodeObject* copyFilesBuildPhase =
  988. this->CreateObject(cmXCodeObject::PBXCopyFilesBuildPhase);
  989. copyFilesBuildPhase->SetComment("Copy files");
  990. copyFilesBuildPhase->AddAttribute("buildActionMask",
  991. this->CreateString("2147483647"));
  992. copyFilesBuildPhase->AddAttribute("dstSubfolderSpec",
  993. this->CreateString("6"));
  994. std::ostringstream ostr;
  995. if (gtgt->IsFrameworkOnApple()) {
  996. // dstPath in frameworks is relative to Versions/<version>
  997. ostr << mit->first;
  998. } else if (mit->first != "MacOS") {
  999. // dstPath in bundles is relative to Contents/MacOS
  1000. ostr << "../" << mit->first.c_str();
  1001. }
  1002. copyFilesBuildPhase->AddAttribute("dstPath",
  1003. this->CreateString(ostr.str()));
  1004. copyFilesBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1005. this->CreateString("0"));
  1006. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1007. copyFilesBuildPhase->AddAttribute("files", buildFiles);
  1008. std::vector<cmSourceFile*>::iterator sfIt;
  1009. for (sfIt = mit->second.begin(); sfIt != mit->second.end(); ++sfIt) {
  1010. cmXCodeObject* xsf = this->CreateXCodeSourceFile(
  1011. this->CurrentLocalGenerator, *sfIt, gtgt);
  1012. buildFiles->AddObject(xsf);
  1013. }
  1014. contentBuildPhases.push_back(copyFilesBuildPhase);
  1015. }
  1016. }
  1017. // create framework build phase
  1018. cmXCodeObject* frameworkBuildPhase = 0;
  1019. if (!externalObjFiles.empty()) {
  1020. frameworkBuildPhase =
  1021. this->CreateObject(cmXCodeObject::PBXFrameworksBuildPhase);
  1022. frameworkBuildPhase->SetComment("Frameworks");
  1023. frameworkBuildPhase->AddAttribute("buildActionMask",
  1024. this->CreateString("2147483647"));
  1025. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1026. frameworkBuildPhase->AddAttribute("files", buildFiles);
  1027. for (std::vector<cmXCodeObject*>::iterator i = externalObjFiles.begin();
  1028. i != externalObjFiles.end(); ++i) {
  1029. buildFiles->AddObject(*i);
  1030. }
  1031. frameworkBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1032. this->CreateString("0"));
  1033. }
  1034. // create list of build phases and create the Xcode target
  1035. cmXCodeObject* buildPhases =
  1036. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1037. this->CreateCustomCommands(buildPhases, sourceBuildPhase, headerBuildPhase,
  1038. resourceBuildPhase, contentBuildPhases,
  1039. frameworkBuildPhase, gtgt);
  1040. targets.push_back(this->CreateXCodeTarget(gtgt, buildPhases));
  1041. }
  1042. return true;
  1043. }
  1044. void cmGlobalXCodeGenerator::ForceLinkerLanguages()
  1045. {
  1046. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i) {
  1047. std::vector<cmGeneratorTarget*> tgts =
  1048. this->LocalGenerators[i]->GetGeneratorTargets();
  1049. // All targets depend on the build-system check target.
  1050. for (std::vector<cmGeneratorTarget*>::const_iterator ti = tgts.begin();
  1051. ti != tgts.end(); ++ti) {
  1052. // This makes sure all targets link using the proper language.
  1053. this->ForceLinkerLanguage(*ti);
  1054. }
  1055. }
  1056. }
  1057. void cmGlobalXCodeGenerator::ForceLinkerLanguage(cmGeneratorTarget* gtgt)
  1058. {
  1059. // This matters only for targets that link.
  1060. if (gtgt->GetType() != cmState::EXECUTABLE &&
  1061. gtgt->GetType() != cmState::SHARED_LIBRARY &&
  1062. gtgt->GetType() != cmState::MODULE_LIBRARY) {
  1063. return;
  1064. }
  1065. std::string llang = gtgt->GetLinkerLanguage("NOCONFIG");
  1066. if (llang.empty()) {
  1067. return;
  1068. }
  1069. // If the language is compiled as a source trust Xcode to link with it.
  1070. cmLinkImplementation const* impl = gtgt->GetLinkImplementation("NOCONFIG");
  1071. for (std::vector<std::string>::const_iterator li = impl->Languages.begin();
  1072. li != impl->Languages.end(); ++li) {
  1073. if (*li == llang) {
  1074. return;
  1075. }
  1076. }
  1077. // Add an empty source file to the target that compiles with the
  1078. // linker language. This should convince Xcode to choose the proper
  1079. // language.
  1080. cmMakefile* mf = gtgt->Target->GetMakefile();
  1081. std::string fname = gtgt->GetLocalGenerator()->GetCurrentBinaryDirectory();
  1082. fname += cmake::GetCMakeFilesDirectory();
  1083. fname += "/";
  1084. fname += gtgt->GetName();
  1085. fname += "-CMakeForceLinker";
  1086. fname += ".";
  1087. fname += cmSystemTools::LowerCase(llang);
  1088. {
  1089. cmGeneratedFileStream fout(fname.c_str());
  1090. fout << "\n";
  1091. }
  1092. if (cmSourceFile* sf = mf->GetOrCreateSource(fname)) {
  1093. sf->SetProperty("LANGUAGE", llang.c_str());
  1094. gtgt->AddSource(fname);
  1095. }
  1096. }
  1097. bool cmGlobalXCodeGenerator::IsHeaderFile(cmSourceFile* sf)
  1098. {
  1099. const std::vector<std::string>& hdrExts =
  1100. this->CMakeInstance->GetHeaderExtensions();
  1101. return (std::find(hdrExts.begin(), hdrExts.end(), sf->GetExtension()) !=
  1102. hdrExts.end());
  1103. }
  1104. cmXCodeObject* cmGlobalXCodeGenerator::CreateBuildPhase(
  1105. const char* name, const char* name2, cmGeneratorTarget* target,
  1106. const std::vector<cmCustomCommand>& commands)
  1107. {
  1108. if (commands.size() == 0 && strcmp(name, "CMake ReRun") != 0) {
  1109. return 0;
  1110. }
  1111. cmXCodeObject* buildPhase =
  1112. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1113. buildPhase->AddAttribute("buildActionMask",
  1114. this->CreateString("2147483647"));
  1115. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1116. buildPhase->AddAttribute("files", buildFiles);
  1117. buildPhase->AddAttribute("name", this->CreateString(name));
  1118. buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1119. this->CreateString("0"));
  1120. buildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  1121. this->AddCommandsToBuildPhase(buildPhase, target, commands, name2);
  1122. return buildPhase;
  1123. }
  1124. void cmGlobalXCodeGenerator::CreateCustomCommands(
  1125. cmXCodeObject* buildPhases, cmXCodeObject* sourceBuildPhase,
  1126. cmXCodeObject* headerBuildPhase, cmXCodeObject* resourceBuildPhase,
  1127. std::vector<cmXCodeObject*> contentBuildPhases,
  1128. cmXCodeObject* frameworkBuildPhase, cmGeneratorTarget* gtgt)
  1129. {
  1130. std::vector<cmCustomCommand> const& prebuild = gtgt->GetPreBuildCommands();
  1131. std::vector<cmCustomCommand> const& prelink = gtgt->GetPreLinkCommands();
  1132. std::vector<cmCustomCommand> postbuild = gtgt->GetPostBuildCommands();
  1133. if (gtgt->GetType() == cmState::SHARED_LIBRARY &&
  1134. !gtgt->IsFrameworkOnApple()) {
  1135. cmCustomCommandLines cmd;
  1136. cmd.resize(1);
  1137. cmd[0].push_back(cmSystemTools::GetCMakeCommand());
  1138. cmd[0].push_back("-E");
  1139. cmd[0].push_back("cmake_symlink_library");
  1140. std::string str_file = "$<TARGET_FILE:";
  1141. str_file += gtgt->GetName();
  1142. str_file += ">";
  1143. std::string str_so_file = "$<TARGET_SONAME_FILE:";
  1144. str_so_file += gtgt->GetName();
  1145. str_so_file += ">";
  1146. std::string str_link_file = "$<TARGET_LINKER_FILE:";
  1147. str_link_file += gtgt->GetName();
  1148. str_link_file += ">";
  1149. cmd[0].push_back(str_file);
  1150. cmd[0].push_back(str_so_file);
  1151. cmd[0].push_back(str_link_file);
  1152. cmCustomCommand command(this->CurrentMakefile, std::vector<std::string>(),
  1153. std::vector<std::string>(),
  1154. std::vector<std::string>(), cmd,
  1155. "Creating symlinks", "");
  1156. postbuild.push_back(command);
  1157. }
  1158. std::vector<cmSourceFile*> classes;
  1159. if (!gtgt->GetConfigCommonSourceFiles(classes)) {
  1160. return;
  1161. }
  1162. // add all the sources
  1163. std::vector<cmCustomCommand> commands;
  1164. for (std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  1165. i != classes.end(); ++i) {
  1166. if ((*i)->GetCustomCommand()) {
  1167. commands.push_back(*(*i)->GetCustomCommand());
  1168. }
  1169. }
  1170. // create prebuild phase
  1171. cmXCodeObject* cmakeRulesBuildPhase = this->CreateBuildPhase(
  1172. "CMake Rules", "cmakeRulesBuildPhase", gtgt, commands);
  1173. // create prebuild phase
  1174. cmXCodeObject* preBuildPhase = this->CreateBuildPhase(
  1175. "CMake PreBuild Rules", "preBuildCommands", gtgt, prebuild);
  1176. // create prelink phase
  1177. cmXCodeObject* preLinkPhase = this->CreateBuildPhase(
  1178. "CMake PreLink Rules", "preLinkCommands", gtgt, prelink);
  1179. // create postbuild phase
  1180. cmXCodeObject* postBuildPhase = this->CreateBuildPhase(
  1181. "CMake PostBuild Rules", "postBuildPhase", gtgt, postbuild);
  1182. // The order here is the order they will be built in.
  1183. // The order "headers, resources, sources" mimics a native project generated
  1184. // from an xcode template...
  1185. //
  1186. if (preBuildPhase) {
  1187. buildPhases->AddObject(preBuildPhase);
  1188. }
  1189. if (cmakeRulesBuildPhase) {
  1190. buildPhases->AddObject(cmakeRulesBuildPhase);
  1191. }
  1192. if (headerBuildPhase) {
  1193. buildPhases->AddObject(headerBuildPhase);
  1194. }
  1195. if (resourceBuildPhase) {
  1196. buildPhases->AddObject(resourceBuildPhase);
  1197. }
  1198. std::vector<cmXCodeObject*>::iterator cit;
  1199. for (cit = contentBuildPhases.begin(); cit != contentBuildPhases.end();
  1200. ++cit) {
  1201. buildPhases->AddObject(*cit);
  1202. }
  1203. if (sourceBuildPhase) {
  1204. buildPhases->AddObject(sourceBuildPhase);
  1205. }
  1206. if (preLinkPhase) {
  1207. buildPhases->AddObject(preLinkPhase);
  1208. }
  1209. if (frameworkBuildPhase) {
  1210. buildPhases->AddObject(frameworkBuildPhase);
  1211. }
  1212. if (postBuildPhase) {
  1213. buildPhases->AddObject(postBuildPhase);
  1214. }
  1215. }
  1216. // This function removes each occurrence of the flag and returns the last one
  1217. // (i.e., the dominant flag in GCC)
  1218. std::string cmGlobalXCodeGenerator::ExtractFlag(const char* flag,
  1219. std::string& flags)
  1220. {
  1221. std::string retFlag;
  1222. std::string::size_type lastOccurancePos = flags.rfind(flag);
  1223. bool saved = false;
  1224. while (lastOccurancePos != flags.npos) {
  1225. // increment pos, we use lastOccurancePos to reduce search space on next
  1226. // inc
  1227. std::string::size_type pos = lastOccurancePos;
  1228. if (pos == 0 || flags[pos - 1] == ' ') {
  1229. while (pos < flags.size() && flags[pos] != ' ') {
  1230. if (!saved) {
  1231. retFlag += flags[pos];
  1232. }
  1233. flags[pos] = ' ';
  1234. pos++;
  1235. }
  1236. saved = true;
  1237. }
  1238. // decrement lastOccurancePos while making sure we don't loop around
  1239. // and become a very large positive number since size_type is unsigned
  1240. lastOccurancePos = lastOccurancePos == 0 ? 0 : lastOccurancePos - 1;
  1241. lastOccurancePos = flags.rfind(flag, lastOccurancePos);
  1242. }
  1243. return retFlag;
  1244. }
  1245. // This function removes each matching occurrence of the expression and
  1246. // returns the last one (i.e., the dominant flag in GCC)
  1247. std::string cmGlobalXCodeGenerator::ExtractFlagRegex(const char* exp,
  1248. int matchIndex,
  1249. std::string& flags)
  1250. {
  1251. std::string retFlag;
  1252. cmsys::RegularExpression regex(exp);
  1253. assert(regex.is_valid());
  1254. if (!regex.is_valid()) {
  1255. return retFlag;
  1256. }
  1257. std::string::size_type offset = 0;
  1258. while (regex.find(flags.c_str() + offset)) {
  1259. const std::string::size_type startPos = offset + regex.start(matchIndex);
  1260. const std::string::size_type endPos = offset + regex.end(matchIndex);
  1261. const std::string::size_type size = endPos - startPos;
  1262. offset = startPos + 1;
  1263. retFlag.assign(flags, startPos, size);
  1264. flags.replace(startPos, size, size, ' ');
  1265. }
  1266. return retFlag;
  1267. }
  1268. //----------------------------------------------------------------------------
  1269. // This function strips off Xcode attributes that do not target the current
  1270. // configuration
  1271. void cmGlobalXCodeGenerator::FilterConfigurationAttribute(
  1272. std::string const& configName, std::string& attribute)
  1273. {
  1274. // Handle [variant=<config>] condition explicitly here.
  1275. std::string::size_type beginVariant = attribute.find("[variant=");
  1276. if (beginVariant == std::string::npos) {
  1277. // There is no variant in this attribute.
  1278. return;
  1279. }
  1280. std::string::size_type endVariant = attribute.find("]", beginVariant + 9);
  1281. if (endVariant == std::string::npos) {
  1282. // There is no terminating bracket.
  1283. return;
  1284. }
  1285. // Compare the variant to the configuration.
  1286. std::string variant =
  1287. attribute.substr(beginVariant + 9, endVariant - beginVariant - 9);
  1288. if (variant == configName) {
  1289. // The variant matches the configuration so use this
  1290. // attribute but drop the [variant=<config>] condition.
  1291. attribute.erase(beginVariant, endVariant - beginVariant + 1);
  1292. } else {
  1293. // The variant does not match the configuration so
  1294. // do not use this attribute.
  1295. attribute.clear();
  1296. }
  1297. }
  1298. void cmGlobalXCodeGenerator::AddCommandsToBuildPhase(
  1299. cmXCodeObject* buildphase, cmGeneratorTarget* target,
  1300. std::vector<cmCustomCommand> const& commands, const char* name)
  1301. {
  1302. std::string dir = this->CurrentLocalGenerator->GetCurrentBinaryDirectory();
  1303. dir += "/CMakeScripts";
  1304. cmSystemTools::MakeDirectory(dir.c_str());
  1305. std::string makefile = dir;
  1306. makefile += "/";
  1307. makefile += target->GetName();
  1308. makefile += "_";
  1309. makefile += name;
  1310. makefile += ".make";
  1311. for (std::vector<std::string>::const_iterator currentConfig =
  1312. this->CurrentConfigurationTypes.begin();
  1313. currentConfig != this->CurrentConfigurationTypes.end();
  1314. currentConfig++) {
  1315. this->CreateCustomRulesMakefile(makefile.c_str(), target, commands,
  1316. *currentConfig);
  1317. }
  1318. std::string cdir = this->CurrentLocalGenerator->GetCurrentBinaryDirectory();
  1319. cdir = this->ConvertToRelativeForMake(cdir.c_str());
  1320. std::string makecmd = "make -C ";
  1321. makecmd += cdir;
  1322. makecmd += " -f ";
  1323. makecmd +=
  1324. this->ConvertToRelativeForMake((makefile + "$CONFIGURATION").c_str());
  1325. makecmd += " all";
  1326. buildphase->AddAttribute("shellScript", this->CreateString(makecmd));
  1327. buildphase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  1328. }
  1329. void cmGlobalXCodeGenerator::CreateCustomRulesMakefile(
  1330. const char* makefileBasename, cmGeneratorTarget* target,
  1331. std::vector<cmCustomCommand> const& commands, const std::string& configName)
  1332. {
  1333. std::string makefileName = makefileBasename;
  1334. if (this->XcodeVersion > 20) {
  1335. makefileName += configName;
  1336. }
  1337. cmGeneratedFileStream makefileStream(makefileName.c_str());
  1338. if (!makefileStream) {
  1339. return;
  1340. }
  1341. makefileStream.SetCopyIfDifferent(true);
  1342. makefileStream << "# Generated by CMake, DO NOT EDIT\n";
  1343. makefileStream << "# Custom rules for " << target->GetName() << "\n";
  1344. // disable the implicit rules
  1345. makefileStream << ".SUFFIXES: "
  1346. << "\n";
  1347. // have all depend on all outputs
  1348. makefileStream << "all: ";
  1349. std::map<const cmCustomCommand*, std::string> tname;
  1350. int count = 0;
  1351. for (std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1352. i != commands.end(); ++i) {
  1353. cmCustomCommandGenerator ccg(*i, configName, this->CurrentLocalGenerator);
  1354. if (ccg.GetNumberOfCommands() > 0) {
  1355. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1356. if (!outputs.empty()) {
  1357. for (std::vector<std::string>::const_iterator o = outputs.begin();
  1358. o != outputs.end(); ++o) {
  1359. makefileStream << "\\\n\t"
  1360. << this->ConvertToRelativeForMake(o->c_str());
  1361. }
  1362. } else {
  1363. std::ostringstream str;
  1364. str << "_buildpart_" << count++;
  1365. tname[&ccg.GetCC()] = std::string(target->GetName()) + str.str();
  1366. makefileStream << "\\\n\t" << tname[&ccg.GetCC()];
  1367. }
  1368. }
  1369. }
  1370. makefileStream << "\n\n";
  1371. for (std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1372. i != commands.end(); ++i) {
  1373. cmCustomCommandGenerator ccg(*i, configName, this->CurrentLocalGenerator);
  1374. if (ccg.GetNumberOfCommands() > 0) {
  1375. makefileStream << "\n";
  1376. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1377. if (!outputs.empty()) {
  1378. // There is at least one output, start the rule for it
  1379. const char* sep = "";
  1380. for (std::vector<std::string>::const_iterator oi = outputs.begin();
  1381. oi != outputs.end(); ++oi) {
  1382. makefileStream << sep << this->ConvertToRelativeForMake(oi->c_str());
  1383. sep = " ";
  1384. }
  1385. makefileStream << ": ";
  1386. } else {
  1387. // There are no outputs. Use the generated force rule name.
  1388. makefileStream << tname[&ccg.GetCC()] << ": ";
  1389. }
  1390. for (std::vector<std::string>::const_iterator d =
  1391. ccg.GetDepends().begin();
  1392. d != ccg.GetDepends().end(); ++d) {
  1393. std::string dep;
  1394. if (this->CurrentLocalGenerator->GetRealDependency(*d, configName,
  1395. dep)) {
  1396. makefileStream << "\\\n"
  1397. << this->ConvertToRelativeForMake(dep.c_str());
  1398. }
  1399. }
  1400. makefileStream << "\n";
  1401. if (const char* comment = ccg.GetComment()) {
  1402. std::string echo_cmd = "echo ";
  1403. echo_cmd += (this->CurrentLocalGenerator->EscapeForShell(
  1404. comment, ccg.GetCC().GetEscapeAllowMakeVars()));
  1405. makefileStream << "\t" << echo_cmd << "\n";
  1406. }
  1407. // Add each command line to the set of commands.
  1408. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  1409. // Build the command line in a single string.
  1410. std::string cmd2 = ccg.GetCommand(c);
  1411. cmSystemTools::ReplaceString(cmd2, "/./", "/");
  1412. cmd2 = this->ConvertToRelativeForMake(cmd2.c_str());
  1413. std::string cmd;
  1414. std::string wd = ccg.GetWorkingDirectory();
  1415. if (!wd.empty()) {
  1416. cmd += "cd ";
  1417. cmd += this->ConvertToRelativeForMake(wd.c_str());
  1418. cmd += " && ";
  1419. }
  1420. cmd += cmd2;
  1421. ccg.AppendArguments(c, cmd);
  1422. makefileStream << "\t" << cmd << "\n";
  1423. }
  1424. }
  1425. }
  1426. }
  1427. void cmGlobalXCodeGenerator::CreateBuildSettings(cmGeneratorTarget* gtgt,
  1428. cmXCodeObject* buildSettings,
  1429. const std::string& configName)
  1430. {
  1431. if (gtgt->GetType() == cmState::INTERFACE_LIBRARY) {
  1432. return;
  1433. }
  1434. std::string defFlags;
  1435. bool shared = ((gtgt->GetType() == cmState::SHARED_LIBRARY) ||
  1436. (gtgt->GetType() == cmState::MODULE_LIBRARY));
  1437. bool binary = ((gtgt->GetType() == cmState::OBJECT_LIBRARY) ||
  1438. (gtgt->GetType() == cmState::STATIC_LIBRARY) ||
  1439. (gtgt->GetType() == cmState::EXECUTABLE) || shared);
  1440. // Compute the compilation flags for each language.
  1441. std::set<std::string> languages;
  1442. gtgt->GetLanguages(languages, configName);
  1443. std::map<std::string, std::string> cflags;
  1444. for (std::set<std::string>::iterator li = languages.begin();
  1445. li != languages.end(); ++li) {
  1446. std::string const& lang = *li;
  1447. std::string& flags = cflags[lang];
  1448. // Add language-specific flags.
  1449. this->CurrentLocalGenerator->AddLanguageFlags(flags, lang, configName);
  1450. // Add shared-library flags if needed.
  1451. this->CurrentLocalGenerator->AddCMP0018Flags(flags, gtgt, lang,
  1452. configName);
  1453. this->CurrentLocalGenerator->AddVisibilityPresetFlags(flags, gtgt, lang);
  1454. this->CurrentLocalGenerator->AddCompileOptions(flags, gtgt, lang,
  1455. configName);
  1456. }
  1457. std::string llang = gtgt->GetLinkerLanguage(configName);
  1458. if (binary && llang.empty()) {
  1459. cmSystemTools::Error(
  1460. "CMake can not determine linker language for target: ",
  1461. gtgt->GetName().c_str());
  1462. return;
  1463. }
  1464. // Add define flags
  1465. this->CurrentLocalGenerator->AppendFlags(
  1466. defFlags, this->CurrentMakefile->GetDefineFlags());
  1467. // Add preprocessor definitions for this target and configuration.
  1468. BuildObjectListOrString ppDefs(this, this->XcodeVersion >= 30);
  1469. if (this->XcodeVersion > 15) {
  1470. this->AppendDefines(
  1471. ppDefs, "CMAKE_INTDIR=\"$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)\"");
  1472. }
  1473. if (const char* exportMacro = gtgt->GetExportMacro()) {
  1474. // Add the export symbol definition for shared library objects.
  1475. this->AppendDefines(ppDefs, exportMacro);
  1476. }
  1477. std::vector<std::string> targetDefines;
  1478. gtgt->GetCompileDefinitions(targetDefines, configName, "C");
  1479. this->AppendDefines(ppDefs, targetDefines);
  1480. buildSettings->AddAttribute("GCC_PREPROCESSOR_DEFINITIONS",
  1481. ppDefs.CreateList());
  1482. std::string extraLinkOptionsVar;
  1483. std::string extraLinkOptions;
  1484. if (gtgt->GetType() == cmState::EXECUTABLE) {
  1485. extraLinkOptionsVar = "CMAKE_EXE_LINKER_FLAGS";
  1486. } else if (gtgt->GetType() == cmState::SHARED_LIBRARY) {
  1487. extraLinkOptionsVar = "CMAKE_SHARED_LINKER_FLAGS";
  1488. } else if (gtgt->GetType() == cmState::MODULE_LIBRARY) {
  1489. extraLinkOptionsVar = "CMAKE_MODULE_LINKER_FLAGS";
  1490. }
  1491. if (!extraLinkOptionsVar.empty()) {
  1492. this->CurrentLocalGenerator->AddConfigVariableFlags(
  1493. extraLinkOptions, extraLinkOptionsVar, configName);
  1494. }
  1495. if (gtgt->GetType() == cmState::OBJECT_LIBRARY ||
  1496. gtgt->GetType() == cmState::STATIC_LIBRARY) {
  1497. this->CurrentLocalGenerator->GetStaticLibraryFlags(
  1498. extraLinkOptions, cmSystemTools::UpperCase(configName), gtgt);
  1499. } else {
  1500. const char* targetLinkFlags = gtgt->GetProperty("LINK_FLAGS");
  1501. if (targetLinkFlags) {
  1502. this->CurrentLocalGenerator->AppendFlags(extraLinkOptions,
  1503. targetLinkFlags);
  1504. }
  1505. if (!configName.empty()) {
  1506. std::string linkFlagsVar = "LINK_FLAGS_";
  1507. linkFlagsVar += cmSystemTools::UpperCase(configName);
  1508. if (const char* linkFlags = gtgt->GetProperty(linkFlagsVar)) {
  1509. this->CurrentLocalGenerator->AppendFlags(extraLinkOptions, linkFlags);
  1510. }
  1511. }
  1512. }
  1513. // Set target-specific architectures.
  1514. std::vector<std::string> archs;
  1515. gtgt->GetAppleArchs(configName, archs);
  1516. if (!archs.empty()) {
  1517. // Enable ARCHS attribute.
  1518. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH", this->CreateString("NO"));
  1519. // Store ARCHS value.
  1520. if (archs.size() == 1) {
  1521. buildSettings->AddAttribute("ARCHS", this->CreateString(archs[0]));
  1522. } else {
  1523. cmXCodeObject* archObjects =
  1524. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1525. for (std::vector<std::string>::iterator i = archs.begin();
  1526. i != archs.end(); i++) {
  1527. archObjects->AddObject(this->CreateString(*i));
  1528. }
  1529. buildSettings->AddAttribute("ARCHS", archObjects);
  1530. }
  1531. }
  1532. // Get the product name components.
  1533. std::string pnprefix;
  1534. std::string pnbase;
  1535. std::string pnsuffix;
  1536. gtgt->GetFullNameComponents(pnprefix, pnbase, pnsuffix, configName);
  1537. const char* version = gtgt->GetProperty("VERSION");
  1538. const char* soversion = gtgt->GetProperty("SOVERSION");
  1539. if (!gtgt->HasSOName(configName) || gtgt->IsFrameworkOnApple()) {
  1540. version = 0;
  1541. soversion = 0;
  1542. }
  1543. if (version && !soversion) {
  1544. soversion = version;
  1545. }
  1546. if (!version && soversion) {
  1547. version = soversion;
  1548. }
  1549. std::string realName = pnbase;
  1550. std::string soName = pnbase;
  1551. if (version && soversion) {
  1552. realName += ".";
  1553. realName += version;
  1554. soName += ".";
  1555. soName += soversion;
  1556. }
  1557. // Set attributes to specify the proper name for the target.
  1558. std::string pndir = this->CurrentLocalGenerator->GetCurrentBinaryDirectory();
  1559. if (gtgt->GetType() == cmState::STATIC_LIBRARY ||
  1560. gtgt->GetType() == cmState::SHARED_LIBRARY ||
  1561. gtgt->GetType() == cmState::MODULE_LIBRARY ||
  1562. gtgt->GetType() == cmState::EXECUTABLE) {
  1563. if (this->XcodeVersion >= 21) {
  1564. if (!gtgt->UsesDefaultOutputDir(configName, false)) {
  1565. std::string pncdir = gtgt->GetDirectory(configName);
  1566. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  1567. this->CreateString(pncdir));
  1568. }
  1569. } else {
  1570. buildSettings->AddAttribute("OBJROOT", this->CreateString(pndir));
  1571. pndir = gtgt->GetDirectory(configName);
  1572. }
  1573. if (gtgt->IsFrameworkOnApple() || gtgt->IsCFBundleOnApple()) {
  1574. pnprefix = "";
  1575. }
  1576. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  1577. this->CreateString(pnprefix));
  1578. buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
  1579. this->CreateString(pnsuffix));
  1580. } else if (gtgt->GetType() == cmState::OBJECT_LIBRARY) {
  1581. pnprefix = "lib";
  1582. pnbase = gtgt->GetName();
  1583. pnsuffix = ".a";
  1584. if (this->XcodeVersion >= 21) {
  1585. std::string pncdir = this->GetObjectsNormalDirectory(
  1586. this->CurrentProject, configName, gtgt);
  1587. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  1588. this->CreateString(pncdir));
  1589. } else {
  1590. buildSettings->AddAttribute("OBJROOT", this->CreateString(pndir));
  1591. pndir = this->GetObjectsNormalDirectory(this->CurrentProject, configName,
  1592. gtgt);
  1593. }
  1594. }
  1595. // Store the product name for all target types.
  1596. buildSettings->AddAttribute("PRODUCT_NAME", this->CreateString(realName));
  1597. buildSettings->AddAttribute("SYMROOT", this->CreateString(pndir));
  1598. // Handle settings for each target type.
  1599. switch (gtgt->GetType()) {
  1600. case cmState::OBJECT_LIBRARY:
  1601. case cmState::STATIC_LIBRARY: {
  1602. buildSettings->AddAttribute("LIBRARY_STYLE",
  1603. this->CreateString("STATIC"));
  1604. break;
  1605. }
  1606. case cmState::MODULE_LIBRARY: {
  1607. buildSettings->AddAttribute("LIBRARY_STYLE",
  1608. this->CreateString("BUNDLE"));
  1609. if (gtgt->IsCFBundleOnApple()) {
  1610. // It turns out that a BUNDLE is basically the same
  1611. // in many ways as an application bundle, as far as
  1612. // link flags go
  1613. std::string createFlags = this->LookupFlags(
  1614. "CMAKE_SHARED_MODULE_CREATE_", llang, "_FLAGS", "-bundle");
  1615. if (!createFlags.empty()) {
  1616. extraLinkOptions += " ";
  1617. extraLinkOptions += createFlags;
  1618. }
  1619. const char* ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  1620. if (ext) {
  1621. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  1622. this->CreateString(ext));
  1623. }
  1624. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1625. // Xcode will create the final version of Info.plist at build time,
  1626. // so let it replace the cfbundle name. This avoids creating
  1627. // a per-configuration Info.plist file. The cfbundle plist
  1628. // is very similar to the application bundle plist
  1629. this->CurrentLocalGenerator->GenerateAppleInfoPList(
  1630. gtgt, "$(EXECUTABLE_NAME)", plist.c_str());
  1631. buildSettings->AddAttribute("INFOPLIST_FILE",
  1632. this->CreateString(plist));
  1633. } else if (this->XcodeVersion >= 22) {
  1634. buildSettings->AddAttribute("MACH_O_TYPE",
  1635. this->CreateString("mh_bundle"));
  1636. buildSettings->AddAttribute("GCC_DYNAMIC_NO_PIC",
  1637. this->CreateString("NO"));
  1638. // Add the flags to create an executable.
  1639. std::string createFlags =
  1640. this->LookupFlags("CMAKE_", llang, "_LINK_FLAGS", "");
  1641. if (!createFlags.empty()) {
  1642. extraLinkOptions += " ";
  1643. extraLinkOptions += createFlags;
  1644. }
  1645. } else {
  1646. // Add the flags to create a module.
  1647. std::string createFlags = this->LookupFlags(
  1648. "CMAKE_SHARED_MODULE_CREATE_", llang, "_FLAGS", "-bundle");
  1649. if (!createFlags.empty()) {
  1650. extraLinkOptions += " ";
  1651. extraLinkOptions += createFlags;
  1652. }
  1653. }
  1654. break;
  1655. }
  1656. case cmState::SHARED_LIBRARY: {
  1657. if (gtgt->GetPropertyAsBool("FRAMEWORK")) {
  1658. std::string fw_version = gtgt->GetFrameworkVersion();
  1659. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  1660. this->CreateString(fw_version));
  1661. const char* ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  1662. if (ext) {
  1663. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  1664. this->CreateString(ext));
  1665. }
  1666. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1667. // Xcode will create the final version of Info.plist at build time,
  1668. // so let it replace the framework name. This avoids creating
  1669. // a per-configuration Info.plist file.
  1670. this->CurrentLocalGenerator->GenerateFrameworkInfoPList(
  1671. gtgt, "$(EXECUTABLE_NAME)", plist.c_str());
  1672. buildSettings->AddAttribute("INFOPLIST_FILE",
  1673. this->CreateString(plist));
  1674. } else {
  1675. // Add the flags to create a shared library.
  1676. std::string createFlags = this->LookupFlags(
  1677. "CMAKE_SHARED_LIBRARY_CREATE_", llang, "_FLAGS", "-dynamiclib");
  1678. if (!createFlags.empty()) {
  1679. extraLinkOptions += " ";
  1680. extraLinkOptions += createFlags;
  1681. }
  1682. }
  1683. buildSettings->AddAttribute("LIBRARY_STYLE",
  1684. this->CreateString("DYNAMIC"));
  1685. break;
  1686. }
  1687. case cmState::EXECUTABLE: {
  1688. // Add the flags to create an executable.
  1689. std::string createFlags =
  1690. this->LookupFlags("CMAKE_", llang, "_LINK_FLAGS", "");
  1691. if (!createFlags.empty()) {
  1692. extraLinkOptions += " ";
  1693. extraLinkOptions += createFlags;
  1694. }
  1695. // Handle bundles and normal executables separately.
  1696. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  1697. const char* ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  1698. if (ext) {
  1699. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  1700. this->CreateString(ext));
  1701. }
  1702. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1703. // Xcode will create the final version of Info.plist at build time,
  1704. // so let it replace the executable name. This avoids creating
  1705. // a per-configuration Info.plist file.
  1706. this->CurrentLocalGenerator->GenerateAppleInfoPList(
  1707. gtgt, "$(EXECUTABLE_NAME)", plist.c_str());
  1708. buildSettings->AddAttribute("INFOPLIST_FILE",
  1709. this->CreateString(plist));
  1710. }
  1711. } break;
  1712. default:
  1713. break;
  1714. }
  1715. if (this->XcodeVersion >= 22 && this->XcodeVersion < 40) {
  1716. buildSettings->AddAttribute("PREBINDING", this->CreateString("NO"));
  1717. }
  1718. BuildObjectListOrString dirs(this, this->XcodeVersion >= 30);
  1719. BuildObjectListOrString fdirs(this, this->XcodeVersion >= 30);
  1720. std::set<std::string> emitted;
  1721. emitted.insert("/System/Library/Frameworks");
  1722. if (this->XcodeVersion < 60) {
  1723. std::vector<std::string> includes;
  1724. this->CurrentLocalGenerator->GetIncludeDirectories(includes, gtgt, "C",
  1725. configName);
  1726. for (std::vector<std::string>::iterator i = includes.begin();
  1727. i != includes.end(); ++i) {
  1728. if (this->NameResolvesToFramework(*i)) {
  1729. std::string frameworkDir = *i;
  1730. frameworkDir += "/../";
  1731. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1732. if (emitted.insert(frameworkDir).second) {
  1733. fdirs.Add(this->XCodeEscapePath(frameworkDir));
  1734. }
  1735. } else {
  1736. std::string incpath = this->XCodeEscapePath(*i);
  1737. dirs.Add(incpath);
  1738. }
  1739. }
  1740. } else {
  1741. for (std::set<std::string>::iterator li = languages.begin();
  1742. li != languages.end(); ++li) {
  1743. std::vector<std::string> includes;
  1744. this->CurrentLocalGenerator->GetIncludeDirectories(includes, gtgt, *li,
  1745. configName);
  1746. std::string includeFlags = this->CurrentLocalGenerator->GetIncludeFlags(
  1747. includes, gtgt, *li, true, false, configName);
  1748. std::string& flags = cflags[*li];
  1749. if (!includeFlags.empty()) {
  1750. flags += " " + includeFlags;
  1751. }
  1752. }
  1753. }
  1754. // Add framework search paths needed for linking.
  1755. if (cmComputeLinkInformation* cli = gtgt->GetLinkInformation(configName)) {
  1756. std::vector<std::string> const& fwDirs = cli->GetFrameworkPaths();
  1757. for (std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  1758. fdi != fwDirs.end(); ++fdi) {
  1759. if (emitted.insert(*fdi).second) {
  1760. fdirs.Add(this->XCodeEscapePath(*fdi));
  1761. }
  1762. }
  1763. }
  1764. if (!fdirs.IsEmpty()) {
  1765. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS", fdirs.CreateList());
  1766. }
  1767. if (!dirs.IsEmpty()) {
  1768. buildSettings->AddAttribute("HEADER_SEARCH_PATHS", dirs.CreateList());
  1769. }
  1770. bool same_gflags = true;
  1771. std::map<std::string, std::string> gflags;
  1772. std::string const* last_gflag = 0;
  1773. std::string optLevel = "0";
  1774. // Minimal map of flags to build settings.
  1775. for (std::set<std::string>::iterator li = languages.begin();
  1776. li != languages.end(); ++li) {
  1777. std::string& flags = cflags[*li];
  1778. std::string& gflag = gflags[*li];
  1779. std::string oflag =
  1780. this->ExtractFlagRegex("(^| )(-Ofast|-Os|-O[0-9]*)( |$)", 2, flags);
  1781. if (oflag.size() == 2) {
  1782. optLevel = "1";
  1783. } else if (oflag.size() > 2) {
  1784. optLevel = oflag.substr(2);
  1785. }
  1786. gflag = this->ExtractFlag("-g", flags);
  1787. // put back gdwarf-2 if used since there is no way
  1788. // to represent it in the gui, but we still want debug yes
  1789. if (gflag == "-gdwarf-2") {
  1790. flags += " ";
  1791. flags += gflag;
  1792. }
  1793. if (last_gflag && *last_gflag != gflag) {
  1794. same_gflags = false;
  1795. }
  1796. last_gflag = &gflag;
  1797. }
  1798. const char* debugStr = "YES";
  1799. if (!same_gflags) {
  1800. // We can't set the Xcode flag differently depending on the language,
  1801. // so put them back in this case.
  1802. for (std::set<std::string>::iterator li = languages.begin();
  1803. li != languages.end(); ++li) {
  1804. cflags[*li] += " ";
  1805. cflags[*li] += gflags[*li];
  1806. }
  1807. debugStr = "NO";
  1808. } else if (last_gflag && (last_gflag->empty() || *last_gflag == "-g0")) {
  1809. debugStr = "NO";
  1810. }
  1811. buildSettings->AddAttribute("COMBINE_HIDPI_IMAGES",
  1812. this->CreateString("YES"));
  1813. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  1814. this->CreateString(debugStr));
  1815. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  1816. this->CreateString(optLevel));
  1817. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  1818. this->CreateString("NO"));
  1819. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  1820. this->CreateString("NO"));
  1821. for (std::set<std::string>::iterator li = languages.begin();
  1822. li != languages.end(); ++li) {
  1823. std::string flags = cflags[*li] + " " + defFlags;
  1824. if (*li == "CXX") {
  1825. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  1826. this->CreateString(flags));
  1827. } else if (*li == "Fortran") {
  1828. buildSettings->AddAttribute("IFORT_OTHER_FLAGS",
  1829. this->CreateString(flags));
  1830. } else if (*li == "C") {
  1831. buildSettings->AddAttribute("OTHER_CFLAGS", this->CreateString(flags));
  1832. } else if (*li == "Swift") {
  1833. buildSettings->AddAttribute("OTHER_SWIFT_FLAGS",
  1834. this->CreateString(flags));
  1835. }
  1836. }
  1837. // Add Fortran source format attribute if property is set.
  1838. const char* format = 0;
  1839. const char* tgtfmt = gtgt->GetProperty("Fortran_FORMAT");
  1840. switch (cmOutputConverter::GetFortranFormat(tgtfmt)) {
  1841. case cmOutputConverter::FortranFormatFixed:
  1842. format = "fixed";
  1843. break;
  1844. case cmOutputConverter::FortranFormatFree:
  1845. format = "free";
  1846. break;
  1847. default:
  1848. break;
  1849. }
  1850. if (format) {
  1851. buildSettings->AddAttribute("IFORT_LANG_SRCFMT",
  1852. this->CreateString(format));
  1853. }
  1854. // Create the INSTALL_PATH attribute.
  1855. std::string install_name_dir;
  1856. if (gtgt->GetType() == cmState::SHARED_LIBRARY) {
  1857. // Get the install_name directory for the build tree.
  1858. install_name_dir = gtgt->GetInstallNameDirForBuildTree(configName);
  1859. // Xcode doesn't create the correct install_name in some cases.
  1860. // That is, if the INSTALL_PATH is empty, or if we have versioning
  1861. // of dylib libraries, we want to specify the install_name.
  1862. // This is done by adding a link flag to create an install_name
  1863. // with just the library soname.
  1864. std::string install_name;
  1865. if (!install_name_dir.empty()) {
  1866. // Convert to a path for the native build tool.
  1867. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  1868. install_name += install_name_dir;
  1869. install_name += "/";
  1870. }
  1871. install_name += gtgt->GetSOName(configName);
  1872. if ((realName != soName) || install_name_dir.empty()) {
  1873. install_name_dir = "";
  1874. extraLinkOptions += " -install_name ";
  1875. extraLinkOptions += XCodeEscapePath(install_name);
  1876. }
  1877. }
  1878. buildSettings->AddAttribute("INSTALL_PATH",
  1879. this->CreateString(install_name_dir));
  1880. // Create the LD_RUNPATH_SEARCH_PATHS
  1881. cmComputeLinkInformation* pcli = gtgt->GetLinkInformation(configName);
  1882. if (pcli) {
  1883. std::string search_paths;
  1884. std::vector<std::string> runtimeDirs;
  1885. pcli->GetRPath(runtimeDirs, false);
  1886. // runpath dirs needs to be unique to prevent corruption
  1887. std::set<std::string> unique_dirs;
  1888. for (std::vector<std::string>::const_iterator i = runtimeDirs.begin();
  1889. i != runtimeDirs.end(); ++i) {
  1890. std::string runpath = *i;
  1891. runpath = this->ExpandCFGIntDir(runpath, configName);
  1892. if (unique_dirs.find(runpath) == unique_dirs.end()) {
  1893. unique_dirs.insert(runpath);
  1894. if (!search_paths.empty()) {
  1895. search_paths += " ";
  1896. }
  1897. search_paths += this->XCodeEscapePath(runpath);
  1898. }
  1899. }
  1900. if (!search_paths.empty()) {
  1901. buildSettings->AddAttribute("LD_RUNPATH_SEARCH_PATHS",
  1902. this->CreateString(search_paths));
  1903. }
  1904. }
  1905. buildSettings->AddAttribute(this->GetTargetLinkFlagsVar(gtgt),
  1906. this->CreateString(extraLinkOptions));
  1907. buildSettings->AddAttribute("OTHER_REZFLAGS", this->CreateString(""));
  1908. buildSettings->AddAttribute("SECTORDER_FLAGS", this->CreateString(""));
  1909. buildSettings->AddAttribute("USE_HEADERMAP", this->CreateString("NO"));
  1910. if (this->XcodeVersion >= 30) {
  1911. cmXCodeObject* group = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1912. group->AddObject(this->CreateString("-Wmost"));
  1913. group->AddObject(this->CreateString("-Wno-four-char-constants"));
  1914. group->AddObject(this->CreateString("-Wno-unknown-pragmas"));
  1915. group->AddObject(this->CreateString("$(inherited)"));
  1916. buildSettings->AddAttribute("WARNING_CFLAGS", group);
  1917. } else {
  1918. buildSettings->AddAttribute(
  1919. "WARNING_CFLAGS", this->CreateString("-Wmost -Wno-four-char-constants"
  1920. " -Wno-unknown-pragmas"));
  1921. }
  1922. // Runtime version information.
  1923. if (gtgt->GetType() == cmState::SHARED_LIBRARY) {
  1924. int major;
  1925. int minor;
  1926. int patch;
  1927. // VERSION -> current_version
  1928. gtgt->GetTargetVersion(false, major, minor, patch);
  1929. std::ostringstream v;
  1930. // Xcode always wants at least 1.0.0 or nothing
  1931. if (!(major == 0 && minor == 0 && patch == 0)) {
  1932. v << major << "." << minor << "." << patch;
  1933. }
  1934. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  1935. this->CreateString(v.str()));
  1936. // SOVERSION -> compatibility_version
  1937. gtgt->GetTargetVersion(true, major, minor, patch);
  1938. std::ostringstream vso;
  1939. // Xcode always wants at least 1.0.0 or nothing
  1940. if (!(major == 0 && minor == 0 && patch == 0)) {
  1941. vso << major << "." << minor << "." << patch;
  1942. }
  1943. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  1944. this->CreateString(vso.str()));
  1945. }
  1946. // put this last so it can override existing settings
  1947. // Convert "XCODE_ATTRIBUTE_*" properties directly.
  1948. {
  1949. std::vector<std::string> const& props = gtgt->GetPropertyKeys();
  1950. for (std::vector<std::string>::const_iterator i = props.begin();
  1951. i != props.end(); ++i) {
  1952. if (i->find("XCODE_ATTRIBUTE_") == 0) {
  1953. std::string attribute = i->substr(16);
  1954. this->FilterConfigurationAttribute(configName, attribute);
  1955. if (!attribute.empty()) {
  1956. cmGeneratorExpression ge;
  1957. std::string processed =
  1958. ge.Parse(gtgt->GetProperty(*i))
  1959. ->Evaluate(this->CurrentLocalGenerator, configName);
  1960. buildSettings->AddAttribute(attribute,
  1961. this->CreateString(processed));
  1962. }
  1963. }
  1964. }
  1965. }
  1966. }
  1967. cmXCodeObject* cmGlobalXCodeGenerator::CreateUtilityTarget(
  1968. cmGeneratorTarget* gtgt)
  1969. {
  1970. cmXCodeObject* shellBuildPhase =
  1971. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1972. shellBuildPhase->AddAttribute("buildActionMask",
  1973. this->CreateString("2147483647"));
  1974. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1975. shellBuildPhase->AddAttribute("files", buildFiles);
  1976. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1977. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  1978. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1979. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  1980. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1981. this->CreateString("0"));
  1982. shellBuildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  1983. shellBuildPhase->AddAttribute(
  1984. "shellScript", this->CreateString("# shell script goes here\nexit 0"));
  1985. shellBuildPhase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  1986. cmXCodeObject* target =
  1987. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  1988. target->SetComment(gtgt->GetName());
  1989. cmXCodeObject* buildPhases = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1990. std::vector<cmXCodeObject*> emptyContentVector;
  1991. this->CreateCustomCommands(buildPhases, 0, 0, 0, emptyContentVector, 0,
  1992. gtgt);
  1993. target->AddAttribute("buildPhases", buildPhases);
  1994. if (this->XcodeVersion > 20) {
  1995. this->AddConfigurations(target, gtgt);
  1996. } else {
  1997. std::string theConfig =
  1998. this->CurrentMakefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1999. cmXCodeObject* buildSettings =
  2000. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2001. this->CreateBuildSettings(gtgt, buildSettings, theConfig);
  2002. target->AddAttribute("buildSettings", buildSettings);
  2003. }
  2004. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2005. target->AddAttribute("dependencies", dependencies);
  2006. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2007. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2008. target->SetTarget(gtgt);
  2009. this->XCodeObjectMap[gtgt] = target;
  2010. // Add source files without build rules for editing convenience.
  2011. if (gtgt->GetType() == cmState::UTILITY) {
  2012. std::vector<cmSourceFile*> sources;
  2013. if (!gtgt->GetConfigCommonSourceFiles(sources)) {
  2014. return 0;
  2015. }
  2016. for (std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  2017. i != sources.end(); ++i) {
  2018. if (!(*i)->GetPropertyAsBool("GENERATED")) {
  2019. this->CreateXCodeFileReference(*i, gtgt);
  2020. }
  2021. }
  2022. }
  2023. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2024. return target;
  2025. }
  2026. std::string cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  2027. cmGeneratorTarget* gtgt)
  2028. {
  2029. std::string configTypes =
  2030. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES");
  2031. std::vector<std::string> configVectorIn;
  2032. std::vector<std::string> configVector;
  2033. configVectorIn.push_back(configTypes);
  2034. cmSystemTools::ExpandList(configVectorIn, configVector);
  2035. cmXCodeObject* configlist =
  2036. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2037. cmXCodeObject* buildConfigurations =
  2038. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2039. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2040. std::string comment = "Build configuration list for ";
  2041. comment += cmXCodeObject::PBXTypeNames[target->GetIsA()];
  2042. comment += " \"";
  2043. comment += gtgt->GetName();
  2044. comment += "\"";
  2045. configlist->SetComment(comment);
  2046. target->AddAttribute("buildConfigurationList",
  2047. this->CreateObjectReference(configlist));
  2048. for (unsigned int i = 0; i < configVector.size(); ++i) {
  2049. cmXCodeObject* config =
  2050. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2051. buildConfigurations->AddObject(config);
  2052. cmXCodeObject* buildSettings =
  2053. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2054. this->CreateBuildSettings(gtgt, buildSettings, configVector[i]);
  2055. config->AddAttribute("name", this->CreateString(configVector[i]));
  2056. config->SetComment(configVector[i]);
  2057. config->AddAttribute("buildSettings", buildSettings);
  2058. }
  2059. if (!configVector.empty()) {
  2060. configlist->AddAttribute("defaultConfigurationName",
  2061. this->CreateString(configVector[0]));
  2062. configlist->AddAttribute("defaultConfigurationIsVisible",
  2063. this->CreateString("0"));
  2064. return configVector[0];
  2065. }
  2066. return "";
  2067. }
  2068. const char* cmGlobalXCodeGenerator::GetTargetLinkFlagsVar(
  2069. cmGeneratorTarget const* target) const
  2070. {
  2071. if (this->XcodeVersion >= 60 &&
  2072. (target->GetType() == cmState::STATIC_LIBRARY ||
  2073. target->GetType() == cmState::OBJECT_LIBRARY)) {
  2074. return "OTHER_LIBTOOLFLAGS";
  2075. } else {
  2076. return "OTHER_LDFLAGS";
  2077. }
  2078. }
  2079. const char* cmGlobalXCodeGenerator::GetTargetFileType(
  2080. cmGeneratorTarget* target)
  2081. {
  2082. switch (target->GetType()) {
  2083. case cmState::OBJECT_LIBRARY:
  2084. case cmState::STATIC_LIBRARY:
  2085. return "archive.ar";
  2086. case cmState::MODULE_LIBRARY:
  2087. if (target->IsXCTestOnApple())
  2088. return "wrapper.cfbundle";
  2089. else if (target->IsCFBundleOnApple())
  2090. return "wrapper.plug-in";
  2091. else
  2092. return ((this->XcodeVersion >= 22) ? "compiled.mach-o.executable"
  2093. : "compiled.mach-o.dylib");
  2094. case cmState::SHARED_LIBRARY:
  2095. return (target->GetPropertyAsBool("FRAMEWORK")
  2096. ? "wrapper.framework"
  2097. : "compiled.mach-o.dylib");
  2098. case cmState::EXECUTABLE:
  2099. return "compiled.mach-o.executable";
  2100. default:
  2101. break;
  2102. }
  2103. return 0;
  2104. }
  2105. const char* cmGlobalXCodeGenerator::GetTargetProductType(
  2106. cmGeneratorTarget* target)
  2107. {
  2108. switch (target->GetType()) {
  2109. case cmState::OBJECT_LIBRARY:
  2110. case cmState::STATIC_LIBRARY:
  2111. return "com.apple.product-type.library.static";
  2112. case cmState::MODULE_LIBRARY:
  2113. if (target->IsXCTestOnApple())
  2114. return "com.apple.product-type.bundle.unit-test";
  2115. else if (target->IsCFBundleOnApple())
  2116. return "com.apple.product-type.bundle";
  2117. else
  2118. return ((this->XcodeVersion >= 22)
  2119. ? "com.apple.product-type.tool"
  2120. : "com.apple.product-type.library.dynamic");
  2121. case cmState::SHARED_LIBRARY:
  2122. return (target->GetPropertyAsBool("FRAMEWORK")
  2123. ? "com.apple.product-type.framework"
  2124. : "com.apple.product-type.library.dynamic");
  2125. case cmState::EXECUTABLE:
  2126. return (target->GetPropertyAsBool("MACOSX_BUNDLE")
  2127. ? "com.apple.product-type.application"
  2128. : "com.apple.product-type.tool");
  2129. default:
  2130. break;
  2131. }
  2132. return 0;
  2133. }
  2134. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeTarget(
  2135. cmGeneratorTarget* gtgt, cmXCodeObject* buildPhases)
  2136. {
  2137. if (gtgt->GetType() == cmState::INTERFACE_LIBRARY) {
  2138. return 0;
  2139. }
  2140. cmXCodeObject* target = this->CreateObject(cmXCodeObject::PBXNativeTarget);
  2141. target->AddAttribute("buildPhases", buildPhases);
  2142. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2143. target->AddAttribute("buildRules", buildRules);
  2144. std::string defConfig;
  2145. if (this->XcodeVersion > 20) {
  2146. defConfig = this->AddConfigurations(target, gtgt);
  2147. } else {
  2148. cmXCodeObject* buildSettings =
  2149. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2150. defConfig = this->CurrentMakefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2151. this->CreateBuildSettings(gtgt, buildSettings, defConfig);
  2152. target->AddAttribute("buildSettings", buildSettings);
  2153. }
  2154. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2155. target->AddAttribute("dependencies", dependencies);
  2156. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2157. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2158. cmXCodeObject* fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  2159. if (const char* fileType = this->GetTargetFileType(gtgt)) {
  2160. fileRef->AddAttribute("explicitFileType", this->CreateString(fileType));
  2161. }
  2162. std::string fullName;
  2163. if (gtgt->GetType() == cmState::OBJECT_LIBRARY) {
  2164. fullName = "lib";
  2165. fullName += gtgt->GetName();
  2166. fullName += ".a";
  2167. } else {
  2168. fullName = gtgt->GetFullName(defConfig);
  2169. }
  2170. fileRef->AddAttribute("path", this->CreateString(fullName));
  2171. if (this->XcodeVersion == 15) {
  2172. fileRef->AddAttribute("refType", this->CreateString("0"));
  2173. }
  2174. fileRef->AddAttribute("sourceTree",
  2175. this->CreateString("BUILT_PRODUCTS_DIR"));
  2176. fileRef->SetComment(gtgt->GetName());
  2177. target->AddAttribute("productReference",
  2178. this->CreateObjectReference(fileRef));
  2179. if (const char* productType = this->GetTargetProductType(gtgt)) {
  2180. target->AddAttribute("productType", this->CreateString(productType));
  2181. }
  2182. target->SetTarget(gtgt);
  2183. this->XCodeObjectMap[gtgt] = target;
  2184. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2185. return target;
  2186. }
  2187. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(
  2188. cmGeneratorTarget const* t)
  2189. {
  2190. if (!t) {
  2191. return 0;
  2192. }
  2193. std::map<cmGeneratorTarget const*, cmXCodeObject*>::const_iterator const i =
  2194. this->XCodeObjectMap.find(t);
  2195. if (i == this->XCodeObjectMap.end()) {
  2196. return 0;
  2197. }
  2198. return i->second;
  2199. }
  2200. std::string cmGlobalXCodeGenerator::GetOrCreateId(const std::string& name,
  2201. const std::string& id)
  2202. {
  2203. std::string guidStoreName = name;
  2204. guidStoreName += "_GUID_CMAKE";
  2205. const char* storedGUID =
  2206. this->CMakeInstance->GetCacheDefinition(guidStoreName);
  2207. if (storedGUID) {
  2208. return storedGUID;
  2209. }
  2210. this->CMakeInstance->AddCacheEntry(
  2211. guidStoreName, id.c_str(), "Stored Xcode object GUID", cmState::INTERNAL);
  2212. return id;
  2213. }
  2214. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  2215. cmXCodeObject* dependTarget)
  2216. {
  2217. // This is called once for every edge in the target dependency graph.
  2218. cmXCodeObject* container =
  2219. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  2220. container->SetComment("PBXContainerItemProxy");
  2221. container->AddAttribute("containerPortal",
  2222. this->CreateObjectReference(this->RootObject));
  2223. container->AddAttribute("proxyType", this->CreateString("1"));
  2224. container->AddAttribute("remoteGlobalIDString",
  2225. this->CreateObjectReference(dependTarget));
  2226. container->AddAttribute(
  2227. "remoteInfo", this->CreateString(dependTarget->GetTarget()->GetName()));
  2228. cmXCodeObject* targetdep =
  2229. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  2230. targetdep->SetComment("PBXTargetDependency");
  2231. targetdep->AddAttribute("target", this->CreateObjectReference(dependTarget));
  2232. targetdep->AddAttribute("targetProxy",
  2233. this->CreateObjectReference(container));
  2234. cmXCodeObject* depends = target->GetObject("dependencies");
  2235. if (!depends) {
  2236. cmSystemTools::Error(
  2237. "target does not have dependencies attribute error..");
  2238. } else {
  2239. depends->AddUniqueObject(targetdep);
  2240. }
  2241. }
  2242. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  2243. const char* attribute,
  2244. const char* value)
  2245. {
  2246. if (settings) {
  2247. cmXCodeObject* attr = settings->GetObject(attribute);
  2248. if (!attr) {
  2249. settings->AddAttribute(attribute, this->CreateString(value));
  2250. } else {
  2251. std::string oldValue = attr->GetString();
  2252. oldValue += " ";
  2253. oldValue += value;
  2254. attr->SetString(oldValue);
  2255. }
  2256. }
  2257. }
  2258. void cmGlobalXCodeGenerator::AppendBuildSettingAttribute(
  2259. cmXCodeObject* target, const char* attribute, const char* value,
  2260. const std::string& configName)
  2261. {
  2262. if (this->XcodeVersion < 21) {
  2263. // There is only one configuration. Add the setting to the buildSettings
  2264. // of the target.
  2265. this->AppendOrAddBuildSetting(target->GetObject("buildSettings"),
  2266. attribute, value);
  2267. } else {
  2268. // There are multiple configurations. Add the setting to the
  2269. // buildSettings of the configuration name given.
  2270. cmXCodeObject* configurationList =
  2271. target->GetObject("buildConfigurationList")->GetObject();
  2272. cmXCodeObject* buildConfigs =
  2273. configurationList->GetObject("buildConfigurations");
  2274. std::vector<cmXCodeObject*> list = buildConfigs->GetObjectList();
  2275. // each configuration and the target itself has a buildSettings in it
  2276. // list.push_back(target);
  2277. for (std::vector<cmXCodeObject*>::iterator i = list.begin();
  2278. i != list.end(); ++i) {
  2279. if (!configName.empty()) {
  2280. if ((*i)->GetObject("name")->GetString() == configName) {
  2281. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2282. this->AppendOrAddBuildSetting(settings, attribute, value);
  2283. }
  2284. } else {
  2285. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2286. this->AppendOrAddBuildSetting(settings, attribute, value);
  2287. }
  2288. }
  2289. }
  2290. }
  2291. void cmGlobalXCodeGenerator::AddDependAndLinkInformation(cmXCodeObject* target)
  2292. {
  2293. cmGeneratorTarget* gt = target->GetTarget();
  2294. if (!gt) {
  2295. cmSystemTools::Error("Error no target on xobject\n");
  2296. return;
  2297. }
  2298. if (gt->GetType() == cmState::INTERFACE_LIBRARY) {
  2299. return;
  2300. }
  2301. // Add dependencies on other CMake targets.
  2302. TargetDependSet const& deps = this->GetTargetDirectDepends(gt);
  2303. for (TargetDependSet::const_iterator i = deps.begin(); i != deps.end();
  2304. ++i) {
  2305. if (cmXCodeObject* dptarget = this->FindXCodeTarget(*i)) {
  2306. this->AddDependTarget(target, dptarget);
  2307. }
  2308. }
  2309. // Loop over configuration types and set per-configuration info.
  2310. for (std::vector<std::string>::iterator i =
  2311. this->CurrentConfigurationTypes.begin();
  2312. i != this->CurrentConfigurationTypes.end(); ++i) {
  2313. // Get the current configuration name.
  2314. std::string configName = *i;
  2315. if (this->XcodeVersion >= 50) {
  2316. // Add object library contents as link flags.
  2317. std::string linkObjs;
  2318. const char* sep = "";
  2319. std::vector<std::string> objs;
  2320. gt->UseObjectLibraries(objs, "");
  2321. for (std::vector<std::string>::const_iterator oi = objs.begin();
  2322. oi != objs.end(); ++oi) {
  2323. linkObjs += sep;
  2324. sep = " ";
  2325. linkObjs += this->XCodeEscapePath(*oi);
  2326. }
  2327. this->AppendBuildSettingAttribute(
  2328. target, this->GetTargetLinkFlagsVar(gt), linkObjs.c_str(), configName);
  2329. }
  2330. // Skip link information for object libraries.
  2331. if (gt->GetType() == cmState::OBJECT_LIBRARY ||
  2332. gt->GetType() == cmState::STATIC_LIBRARY) {
  2333. continue;
  2334. }
  2335. // Compute the link library and directory information.
  2336. cmComputeLinkInformation* pcli = gt->GetLinkInformation(configName);
  2337. if (!pcli) {
  2338. continue;
  2339. }
  2340. cmComputeLinkInformation& cli = *pcli;
  2341. // Add dependencies directly on library files.
  2342. {
  2343. std::vector<std::string> const& libDeps = cli.GetDepends();
  2344. for (std::vector<std::string>::const_iterator j = libDeps.begin();
  2345. j != libDeps.end(); ++j) {
  2346. target->AddDependLibrary(configName, *j);
  2347. }
  2348. }
  2349. // add the library search paths
  2350. {
  2351. std::vector<std::string> const& libDirs = cli.GetDirectories();
  2352. std::string linkDirs;
  2353. for (std::vector<std::string>::const_iterator libDir = libDirs.begin();
  2354. libDir != libDirs.end(); ++libDir) {
  2355. if (libDir->size() && *libDir != "/usr/lib") {
  2356. if (this->XcodeVersion > 15) {
  2357. // Now add the same one but append
  2358. // $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) to it:
  2359. linkDirs += " ";
  2360. linkDirs += this->XCodeEscapePath(
  2361. *libDir + "/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)");
  2362. }
  2363. linkDirs += " ";
  2364. linkDirs += this->XCodeEscapePath(*libDir);
  2365. }
  2366. }
  2367. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  2368. linkDirs.c_str(), configName);
  2369. }
  2370. // now add the link libraries
  2371. {
  2372. std::string linkLibs;
  2373. const char* sep = "";
  2374. typedef cmComputeLinkInformation::ItemVector ItemVector;
  2375. ItemVector const& libNames = cli.GetItems();
  2376. for (ItemVector::const_iterator li = libNames.begin();
  2377. li != libNames.end(); ++li) {
  2378. linkLibs += sep;
  2379. sep = " ";
  2380. if (li->IsPath) {
  2381. linkLibs += this->XCodeEscapePath(li->Value);
  2382. } else if (!li->Target ||
  2383. li->Target->GetType() != cmState::INTERFACE_LIBRARY) {
  2384. linkLibs += li->Value;
  2385. }
  2386. if (li->Target && !li->Target->IsImported()) {
  2387. target->AddDependTarget(configName, li->Target->GetName());
  2388. }
  2389. }
  2390. this->AppendBuildSettingAttribute(
  2391. target, this->GetTargetLinkFlagsVar(gt), linkLibs.c_str(), configName);
  2392. }
  2393. }
  2394. }
  2395. bool cmGlobalXCodeGenerator::CreateGroups(
  2396. std::vector<cmLocalGenerator*>& generators)
  2397. {
  2398. for (std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2399. i != generators.end(); ++i) {
  2400. cmMakefile* mf = (*i)->GetMakefile();
  2401. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  2402. std::vector<cmGeneratorTarget*> tgts = (*i)->GetGeneratorTargets();
  2403. for (std::vector<cmGeneratorTarget*>::iterator l = tgts.begin();
  2404. l != tgts.end(); l++) {
  2405. cmGeneratorTarget* gtgt = *l;
  2406. // Same skipping logic here as in CreateXCodeTargets so that we do not
  2407. // end up with (empty anyhow) ALL_BUILD and XCODE_DEPEND_HELPER source
  2408. // groups:
  2409. //
  2410. if (gtgt->GetType() == cmState::GLOBAL_TARGET) {
  2411. continue;
  2412. }
  2413. if (gtgt->GetType() == cmState::INTERFACE_LIBRARY) {
  2414. continue;
  2415. }
  2416. // add the soon to be generated Info.plist file as a source for a
  2417. // MACOSX_BUNDLE file
  2418. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  2419. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2420. mf->GetOrCreateSource(plist, true);
  2421. gtgt->AddSource(plist);
  2422. }
  2423. std::vector<cmSourceFile*> classes;
  2424. if (!gtgt->GetConfigCommonSourceFiles(classes)) {
  2425. return false;
  2426. }
  2427. // Put cmSourceFile instances in proper groups:
  2428. for (std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  2429. s != classes.end(); s++) {
  2430. cmSourceFile* sf = *s;
  2431. // Add the file to the list of sources.
  2432. std::string const& source = sf->GetFullPath();
  2433. cmSourceGroup* sourceGroup =
  2434. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2435. cmXCodeObject* pbxgroup = this->CreateOrGetPBXGroup(gtgt, sourceGroup);
  2436. std::string key = GetGroupMapKey(gtgt, sf);
  2437. this->GroupMap[key] = pbxgroup;
  2438. }
  2439. // Put OBJECT_LIBRARY objects in proper groups:
  2440. std::vector<std::string> objs;
  2441. gtgt->UseObjectLibraries(objs, "");
  2442. for (std::vector<std::string>::const_iterator oi = objs.begin();
  2443. oi != objs.end(); ++oi) {
  2444. std::string const& source = *oi;
  2445. cmSourceGroup* sourceGroup =
  2446. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2447. cmXCodeObject* pbxgroup = this->CreateOrGetPBXGroup(gtgt, sourceGroup);
  2448. std::string key = GetGroupMapKeyFromPath(gtgt, source);
  2449. this->GroupMap[key] = pbxgroup;
  2450. }
  2451. }
  2452. }
  2453. return true;
  2454. }
  2455. cmXCodeObject* cmGlobalXCodeGenerator::CreatePBXGroup(cmXCodeObject* parent,
  2456. std::string name)
  2457. {
  2458. cmXCodeObject* parentChildren = NULL;
  2459. if (parent)
  2460. parentChildren = parent->GetObject("children");
  2461. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  2462. cmXCodeObject* groupChildren =
  2463. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2464. group->AddAttribute("name", this->CreateString(name));
  2465. group->AddAttribute("children", groupChildren);
  2466. if (this->XcodeVersion == 15) {
  2467. group->AddAttribute("refType", this->CreateString("4"));
  2468. }
  2469. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  2470. if (parentChildren)
  2471. parentChildren->AddObject(group);
  2472. return group;
  2473. }
  2474. cmXCodeObject* cmGlobalXCodeGenerator::CreateOrGetPBXGroup(
  2475. cmGeneratorTarget* gtgt, cmSourceGroup* sg)
  2476. {
  2477. std::string s;
  2478. std::string target;
  2479. const char* targetFolder = gtgt->GetProperty("FOLDER");
  2480. if (targetFolder) {
  2481. target = targetFolder;
  2482. target += "/";
  2483. }
  2484. target += gtgt->GetName();
  2485. s = target + "/";
  2486. s += sg->GetFullName();
  2487. std::map<std::string, cmXCodeObject*>::iterator it =
  2488. this->GroupNameMap.find(s);
  2489. if (it != this->GroupNameMap.end()) {
  2490. return it->second;
  2491. }
  2492. it = this->TargetGroup.find(target);
  2493. cmXCodeObject* tgroup = 0;
  2494. if (it != this->TargetGroup.end()) {
  2495. tgroup = it->second;
  2496. } else {
  2497. std::vector<std::string> tgt_folders =
  2498. cmSystemTools::tokenize(target, "/");
  2499. std::string curr_tgt_folder;
  2500. for (std::vector<std::string>::size_type i = 0; i < tgt_folders.size();
  2501. i++) {
  2502. if (i != 0) {
  2503. curr_tgt_folder += "/";
  2504. }
  2505. curr_tgt_folder += tgt_folders[i];
  2506. it = this->TargetGroup.find(curr_tgt_folder);
  2507. if (it != this->TargetGroup.end()) {
  2508. tgroup = it->second;
  2509. continue;
  2510. }
  2511. tgroup = this->CreatePBXGroup(tgroup, tgt_folders[i]);
  2512. this->TargetGroup[curr_tgt_folder] = tgroup;
  2513. if (i == 0) {
  2514. this->SourcesGroupChildren->AddObject(tgroup);
  2515. }
  2516. }
  2517. }
  2518. this->TargetGroup[target] = tgroup;
  2519. // If it's the default source group (empty name) then put the source file
  2520. // directly in the tgroup...
  2521. //
  2522. if (std::string(sg->GetFullName()) == "") {
  2523. this->GroupNameMap[s] = tgroup;
  2524. return tgroup;
  2525. }
  2526. // It's a recursive folder structure, let's find the real parent group
  2527. if (std::string(sg->GetFullName()) != std::string(sg->GetName())) {
  2528. std::vector<std::string> folders =
  2529. cmSystemTools::tokenize(sg->GetFullName(), "\\");
  2530. std::string curr_folder = target;
  2531. curr_folder += "/";
  2532. for (std::vector<std::string>::size_type i = 0; i < folders.size(); i++) {
  2533. curr_folder += folders[i];
  2534. std::map<std::string, cmXCodeObject*>::iterator i_folder =
  2535. this->GroupNameMap.find(curr_folder);
  2536. // Create new folder
  2537. if (i_folder == this->GroupNameMap.end()) {
  2538. cmXCodeObject* group = this->CreatePBXGroup(tgroup, folders[i]);
  2539. this->GroupNameMap[curr_folder] = group;
  2540. tgroup = group;
  2541. } else {
  2542. tgroup = i_folder->second;
  2543. }
  2544. curr_folder = curr_folder + "\\";
  2545. }
  2546. return tgroup;
  2547. }
  2548. cmXCodeObject* group = this->CreatePBXGroup(tgroup, sg->GetName());
  2549. this->GroupNameMap[s] = group;
  2550. return group;
  2551. }
  2552. bool cmGlobalXCodeGenerator::CreateXCodeObjects(
  2553. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  2554. {
  2555. this->ClearXCodeObjects();
  2556. this->RootObject = 0;
  2557. this->SourcesGroupChildren = 0;
  2558. this->ResourcesGroupChildren = 0;
  2559. this->MainGroupChildren = 0;
  2560. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2561. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2562. cmXCodeObject* developBuildStyle =
  2563. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2564. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2565. if (this->XcodeVersion == 15) {
  2566. developBuildStyle->AddAttribute("name", this->CreateString("Development"));
  2567. developBuildStyle->AddAttribute("buildSettings", group);
  2568. listObjs->AddObject(developBuildStyle);
  2569. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2570. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("YES"));
  2571. cmXCodeObject* deployBuildStyle =
  2572. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2573. deployBuildStyle->AddAttribute("name", this->CreateString("Deployment"));
  2574. deployBuildStyle->AddAttribute("buildSettings", group);
  2575. listObjs->AddObject(deployBuildStyle);
  2576. } else {
  2577. for (unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i) {
  2578. cmXCodeObject* buildStyle =
  2579. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2580. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2581. buildStyle->AddAttribute("name", this->CreateString(name));
  2582. buildStyle->SetComment(name);
  2583. cmXCodeObject* sgroup =
  2584. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2585. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2586. buildStyle->AddAttribute("buildSettings", sgroup);
  2587. listObjs->AddObject(buildStyle);
  2588. }
  2589. }
  2590. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2591. this->MainGroupChildren = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2592. mainGroup->AddAttribute("children", this->MainGroupChildren);
  2593. if (this->XcodeVersion == 15) {
  2594. mainGroup->AddAttribute("refType", this->CreateString("4"));
  2595. }
  2596. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2597. cmXCodeObject* sourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2598. this->SourcesGroupChildren = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2599. sourcesGroup->AddAttribute("name", this->CreateString("Sources"));
  2600. sourcesGroup->AddAttribute("children", this->SourcesGroupChildren);
  2601. if (this->XcodeVersion == 15) {
  2602. sourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2603. }
  2604. sourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2605. this->MainGroupChildren->AddObject(sourcesGroup);
  2606. cmXCodeObject* resourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2607. this->ResourcesGroupChildren =
  2608. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2609. resourcesGroup->AddAttribute("name", this->CreateString("Resources"));
  2610. resourcesGroup->AddAttribute("children", this->ResourcesGroupChildren);
  2611. if (this->XcodeVersion == 15) {
  2612. resourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2613. }
  2614. resourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2615. this->MainGroupChildren->AddObject(resourcesGroup);
  2616. // now create the cmake groups
  2617. if (!this->CreateGroups(generators)) {
  2618. return false;
  2619. }
  2620. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2621. productGroup->AddAttribute("name", this->CreateString("Products"));
  2622. if (this->XcodeVersion == 15) {
  2623. productGroup->AddAttribute("refType", this->CreateString("4"));
  2624. }
  2625. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2626. cmXCodeObject* productGroupChildren =
  2627. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2628. productGroup->AddAttribute("children", productGroupChildren);
  2629. this->MainGroupChildren->AddObject(productGroup);
  2630. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  2631. this->RootObject->SetComment("Project object");
  2632. std::string project_id = "PROJECT_";
  2633. project_id += root->GetProjectName();
  2634. this->RootObject->SetId(
  2635. this->GetOrCreateId(project_id.c_str(), this->RootObject->GetId()));
  2636. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2637. this->RootObject->AddAttribute("mainGroup",
  2638. this->CreateObjectReference(mainGroup));
  2639. this->RootObject->AddAttribute("buildSettings", group);
  2640. this->RootObject->AddAttribute("buildStyles", listObjs);
  2641. this->RootObject->AddAttribute("hasScannedForEncodings",
  2642. this->CreateString("0"));
  2643. if (this->XcodeVersion >= 30) {
  2644. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2645. group->AddAttribute("BuildIndependentTargetsInParallel",
  2646. this->CreateString("YES"));
  2647. std::ostringstream v;
  2648. v << std::setfill('0') << std::setw(4) << XcodeVersion * 10;
  2649. group->AddAttribute("LastUpgradeCheck", this->CreateString(v.str()));
  2650. this->RootObject->AddAttribute("attributes", group);
  2651. if (this->XcodeVersion >= 32)
  2652. this->RootObject->AddAttribute("compatibilityVersion",
  2653. this->CreateString("Xcode 3.2"));
  2654. else if (this->XcodeVersion >= 31)
  2655. this->RootObject->AddAttribute("compatibilityVersion",
  2656. this->CreateString("Xcode 3.1"));
  2657. else
  2658. this->RootObject->AddAttribute("compatibilityVersion",
  2659. this->CreateString("Xcode 3.0"));
  2660. }
  2661. // Point Xcode at the top of the source tree.
  2662. {
  2663. std::string pdir =
  2664. this->RelativeToBinary(root->GetCurrentSourceDirectory());
  2665. this->RootObject->AddAttribute("projectDirPath", this->CreateString(pdir));
  2666. this->RootObject->AddAttribute("projectRoot", this->CreateString(""));
  2667. }
  2668. cmXCodeObject* configlist =
  2669. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2670. cmXCodeObject* buildConfigurations =
  2671. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2672. typedef std::vector<std::pair<std::string, cmXCodeObject*> > Configs;
  2673. Configs configs;
  2674. const char* defaultConfigName = "Debug";
  2675. if (this->XcodeVersion == 15) {
  2676. cmXCodeObject* configDebug =
  2677. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2678. configDebug->AddAttribute("name", this->CreateString("Debug"));
  2679. configs.push_back(std::make_pair("Debug", configDebug));
  2680. cmXCodeObject* configRelease =
  2681. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2682. configRelease->AddAttribute("name", this->CreateString("Release"));
  2683. configs.push_back(std::make_pair("Release", configRelease));
  2684. } else {
  2685. for (unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i) {
  2686. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2687. if (0 == i) {
  2688. defaultConfigName = name;
  2689. }
  2690. cmXCodeObject* config =
  2691. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2692. config->AddAttribute("name", this->CreateString(name));
  2693. configs.push_back(std::make_pair(name, config));
  2694. }
  2695. }
  2696. for (Configs::iterator c = configs.begin(); c != configs.end(); ++c) {
  2697. buildConfigurations->AddObject(c->second);
  2698. }
  2699. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2700. std::string comment = "Build configuration list for PBXProject";
  2701. comment += " \"";
  2702. comment += this->CurrentProject;
  2703. comment += "\"";
  2704. configlist->SetComment(comment);
  2705. configlist->AddAttribute("defaultConfigurationIsVisible",
  2706. this->CreateString("0"));
  2707. configlist->AddAttribute("defaultConfigurationName",
  2708. this->CreateString(defaultConfigName));
  2709. cmXCodeObject* buildSettings =
  2710. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2711. const char* osxArch =
  2712. this->CurrentMakefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  2713. const char* sysroot =
  2714. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  2715. const char* deploymentTarget =
  2716. this->CurrentMakefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  2717. std::string archs;
  2718. if (sysroot) {
  2719. if (osxArch) {
  2720. // recompute this as it may have been changed since enable language
  2721. this->Architectures.clear();
  2722. cmSystemTools::ExpandListArgument(std::string(osxArch),
  2723. this->Architectures);
  2724. archs = cmJoin(this->Architectures, " ");
  2725. }
  2726. buildSettings->AddAttribute("SDKROOT", this->CreateString(sysroot));
  2727. }
  2728. if (archs.empty()) {
  2729. // Tell Xcode to use NATIVE_ARCH instead of ARCHS.
  2730. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH", this->CreateString("YES"));
  2731. } else {
  2732. // Tell Xcode to use ARCHS (ONLY_ACTIVE_ARCH defaults to NO).
  2733. buildSettings->AddAttribute("ARCHS", this->CreateString(archs));
  2734. }
  2735. if (deploymentTarget && *deploymentTarget) {
  2736. buildSettings->AddAttribute("MACOSX_DEPLOYMENT_TARGET",
  2737. this->CreateString(deploymentTarget));
  2738. }
  2739. if (!this->GeneratorToolset.empty()) {
  2740. buildSettings->AddAttribute("GCC_VERSION",
  2741. this->CreateString(this->GeneratorToolset));
  2742. }
  2743. if (this->GetLanguageEnabled("Swift")) {
  2744. buildSettings->AddAttribute("SWIFT_VERSION", this->CreateString("2.3"));
  2745. }
  2746. std::string symroot = root->GetCurrentBinaryDirectory();
  2747. symroot += "/build";
  2748. buildSettings->AddAttribute("SYMROOT", this->CreateString(symroot));
  2749. for (Configs::iterator i = configs.begin(); i != configs.end(); ++i) {
  2750. cmXCodeObject* buildSettingsForCfg = this->CreateFlatClone(buildSettings);
  2751. // Put this last so it can override existing settings
  2752. // Convert "CMAKE_XCODE_ATTRIBUTE_*" variables directly.
  2753. std::vector<std::string> vars = this->CurrentMakefile->GetDefinitions();
  2754. for (std::vector<std::string>::const_iterator d = vars.begin();
  2755. d != vars.end(); ++d) {
  2756. if (d->find("CMAKE_XCODE_ATTRIBUTE_") == 0) {
  2757. std::string attribute = d->substr(22);
  2758. this->FilterConfigurationAttribute(i->first, attribute);
  2759. if (!attribute.empty()) {
  2760. cmGeneratorExpression ge;
  2761. std::string processed =
  2762. ge.Parse(this->CurrentMakefile->GetDefinition(*d))
  2763. ->Evaluate(this->CurrentLocalGenerator, i->first);
  2764. buildSettingsForCfg->AddAttribute(attribute,
  2765. this->CreateString(processed));
  2766. }
  2767. }
  2768. }
  2769. // store per-config buildSettings into configuration object
  2770. i->second->AddAttribute("buildSettings", buildSettingsForCfg);
  2771. }
  2772. this->RootObject->AddAttribute("buildConfigurationList",
  2773. this->CreateObjectReference(configlist));
  2774. std::vector<cmXCodeObject*> targets;
  2775. for (std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2776. i != generators.end(); ++i) {
  2777. if (!this->CreateXCodeTargets(*i, targets)) {
  2778. return false;
  2779. }
  2780. }
  2781. // loop over all targets and add link and depend info
  2782. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2783. i != targets.end(); ++i) {
  2784. cmXCodeObject* t = *i;
  2785. this->AddDependAndLinkInformation(t);
  2786. }
  2787. if (this->XcodeVersion < 50) {
  2788. // now create xcode depend hack makefile
  2789. this->CreateXCodeDependHackTarget(targets);
  2790. }
  2791. // now add all targets to the root object
  2792. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2793. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2794. i != targets.end(); ++i) {
  2795. cmXCodeObject* t = *i;
  2796. allTargets->AddObject(t);
  2797. cmXCodeObject* productRef = t->GetObject("productReference");
  2798. if (productRef) {
  2799. productGroupChildren->AddObject(productRef->GetObject());
  2800. }
  2801. }
  2802. this->RootObject->AddAttribute("targets", allTargets);
  2803. return true;
  2804. }
  2805. std::string cmGlobalXCodeGenerator::GetObjectsNormalDirectory(
  2806. const std::string& projName, const std::string& configName,
  2807. const cmGeneratorTarget* t) const
  2808. {
  2809. std::string dir = t->GetLocalGenerator()->GetCurrentBinaryDirectory();
  2810. dir += "/";
  2811. dir += projName;
  2812. dir += ".build/";
  2813. dir += configName;
  2814. dir += "/";
  2815. dir += t->GetName();
  2816. dir += ".build/Objects-normal/";
  2817. return dir;
  2818. }
  2819. void cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  2820. std::vector<cmXCodeObject*>& targets)
  2821. {
  2822. cmGeneratedFileStream makefileStream(this->CurrentXCodeHackMakefile.c_str());
  2823. if (!makefileStream) {
  2824. cmSystemTools::Error("Could not create",
  2825. this->CurrentXCodeHackMakefile.c_str());
  2826. return;
  2827. }
  2828. makefileStream.SetCopyIfDifferent(true);
  2829. // one more pass for external depend information not handled
  2830. // correctly by xcode
  2831. /* clang-format off */
  2832. makefileStream << "# DO NOT EDIT\n";
  2833. makefileStream << "# This makefile makes sure all linkable targets are\n";
  2834. makefileStream << "# up-to-date with anything they link to\n"
  2835. "default:\n"
  2836. "\techo \"Do not invoke directly\"\n"
  2837. "\n";
  2838. makefileStream
  2839. << "# For each target create a dummy rule "
  2840. "so the target does not have to exist\n";
  2841. /* clang-format on */
  2842. std::set<std::string> emitted;
  2843. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2844. i != targets.end(); ++i) {
  2845. cmXCodeObject* target = *i;
  2846. std::map<std::string, cmXCodeObject::StringVec> const& deplibs =
  2847. target->GetDependLibraries();
  2848. for (std::map<std::string, cmXCodeObject::StringVec>::const_iterator ci =
  2849. deplibs.begin();
  2850. ci != deplibs.end(); ++ci) {
  2851. for (cmXCodeObject::StringVec::const_iterator d = ci->second.begin();
  2852. d != ci->second.end(); ++d) {
  2853. if (emitted.insert(*d).second) {
  2854. makefileStream << this->ConvertToRelativeForMake(d->c_str())
  2855. << ":\n";
  2856. }
  2857. }
  2858. }
  2859. }
  2860. makefileStream << "\n\n";
  2861. // Write rules to help Xcode relink things at the right time.
  2862. /* clang-format off */
  2863. makefileStream <<
  2864. "# Rules to remove targets that are older than anything to which they\n"
  2865. "# link. This forces Xcode to relink the targets from scratch. It\n"
  2866. "# does not seem to check these dependencies itself.\n";
  2867. /* clang-format on */
  2868. for (std::vector<std::string>::const_iterator ct =
  2869. this->CurrentConfigurationTypes.begin();
  2870. ct != this->CurrentConfigurationTypes.end(); ++ct) {
  2871. std::string configName = *ct;
  2872. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2873. i != targets.end(); ++i) {
  2874. cmXCodeObject* target = *i;
  2875. cmGeneratorTarget* gt = target->GetTarget();
  2876. if (gt->GetType() == cmState::EXECUTABLE ||
  2877. // Nope - no post-build for OBJECT_LIRBRARY
  2878. // gt->GetType() == cmState::OBJECT_LIBRARY ||
  2879. gt->GetType() == cmState::STATIC_LIBRARY ||
  2880. gt->GetType() == cmState::SHARED_LIBRARY ||
  2881. gt->GetType() == cmState::MODULE_LIBRARY) {
  2882. // Declare an entry point for the target post-build phase.
  2883. makefileStream << this->PostBuildMakeTarget(gt->GetName(), *ct)
  2884. << ":\n";
  2885. }
  2886. if (gt->GetType() == cmState::EXECUTABLE ||
  2887. gt->GetType() == cmState::SHARED_LIBRARY ||
  2888. gt->GetType() == cmState::MODULE_LIBRARY) {
  2889. std::string tfull = gt->GetFullPath(configName);
  2890. std::string trel = this->ConvertToRelativeForMake(tfull.c_str());
  2891. // Add this target to the post-build phases of its dependencies.
  2892. std::map<std::string, cmXCodeObject::StringVec>::const_iterator y =
  2893. target->GetDependTargets().find(*ct);
  2894. if (y != target->GetDependTargets().end()) {
  2895. std::vector<std::string> const& deptgts = y->second;
  2896. for (std::vector<std::string>::const_iterator d = deptgts.begin();
  2897. d != deptgts.end(); ++d) {
  2898. makefileStream << this->PostBuildMakeTarget(*d, *ct) << ": "
  2899. << trel << "\n";
  2900. }
  2901. }
  2902. // Create a rule for this target.
  2903. makefileStream << trel << ":";
  2904. // List dependencies if any exist.
  2905. std::map<std::string, cmXCodeObject::StringVec>::const_iterator x =
  2906. target->GetDependLibraries().find(*ct);
  2907. if (x != target->GetDependLibraries().end()) {
  2908. std::vector<std::string> const& deplibs = x->second;
  2909. for (std::vector<std::string>::const_iterator d = deplibs.begin();
  2910. d != deplibs.end(); ++d) {
  2911. makefileStream << "\\\n\t"
  2912. << this->ConvertToRelativeForMake(d->c_str());
  2913. }
  2914. }
  2915. // Write the action to remove the target if it is out of date.
  2916. makefileStream << "\n";
  2917. makefileStream << "\t/bin/rm -f "
  2918. << this->ConvertToRelativeForMake(tfull.c_str())
  2919. << "\n";
  2920. // if building for more than one architecture
  2921. // then remove those exectuables as well
  2922. if (this->Architectures.size() > 1) {
  2923. std::string universal = this->GetObjectsNormalDirectory(
  2924. this->CurrentProject, configName, gt);
  2925. for (std::vector<std::string>::iterator arch =
  2926. this->Architectures.begin();
  2927. arch != this->Architectures.end(); ++arch) {
  2928. std::string universalFile = universal;
  2929. universalFile += *arch;
  2930. universalFile += "/";
  2931. universalFile += gt->GetFullName(configName);
  2932. makefileStream << "\t/bin/rm -f "
  2933. << this->ConvertToRelativeForMake(
  2934. universalFile.c_str())
  2935. << "\n";
  2936. }
  2937. }
  2938. makefileStream << "\n\n";
  2939. }
  2940. }
  2941. }
  2942. }
  2943. void cmGlobalXCodeGenerator::OutputXCodeProject(
  2944. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  2945. {
  2946. if (generators.size() == 0) {
  2947. return;
  2948. }
  2949. // Skip local generators that are excluded from this project.
  2950. for (std::vector<cmLocalGenerator*>::iterator g = generators.begin();
  2951. g != generators.end(); ++g) {
  2952. if (this->IsExcluded(root, *g)) {
  2953. continue;
  2954. }
  2955. }
  2956. if (!this->CreateXCodeObjects(root, generators)) {
  2957. return;
  2958. }
  2959. std::string xcodeDir = root->GetCurrentBinaryDirectory();
  2960. xcodeDir += "/";
  2961. xcodeDir += root->GetProjectName();
  2962. xcodeDir += ".xcode";
  2963. if (this->XcodeVersion > 20) {
  2964. xcodeDir += "proj";
  2965. }
  2966. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  2967. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  2968. cmGeneratedFileStream fout(xcodeProjFile.c_str());
  2969. fout.SetCopyIfDifferent(true);
  2970. if (!fout) {
  2971. return;
  2972. }
  2973. this->WriteXCodePBXProj(fout, root, generators);
  2974. this->ClearXCodeObjects();
  2975. // Since this call may have created new cache entries, save the cache:
  2976. //
  2977. root->GetMakefile()->GetCMakeInstance()->SaveCache(
  2978. root->GetBinaryDirectory());
  2979. }
  2980. void cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  2981. cmLocalGenerator*,
  2982. std::vector<cmLocalGenerator*>&)
  2983. {
  2984. SortXCodeObjects();
  2985. fout << "// !$*UTF8*$!\n";
  2986. fout << "{\n";
  2987. cmXCodeObject::Indent(1, fout);
  2988. fout << "archiveVersion = 1;\n";
  2989. cmXCodeObject::Indent(1, fout);
  2990. fout << "classes = {\n";
  2991. cmXCodeObject::Indent(1, fout);
  2992. fout << "};\n";
  2993. cmXCodeObject::Indent(1, fout);
  2994. if (this->XcodeVersion >= 21) {
  2995. if (this->XcodeVersion >= 32)
  2996. fout << "objectVersion = 46;\n";
  2997. else if (this->XcodeVersion >= 31)
  2998. fout << "objectVersion = 45;\n";
  2999. else if (this->XcodeVersion >= 30)
  3000. fout << "objectVersion = 44;\n";
  3001. else
  3002. fout << "objectVersion = 42;\n";
  3003. cmXCode21Object::PrintList(this->XCodeObjects, fout);
  3004. } else {
  3005. fout << "objectVersion = 39;\n";
  3006. cmXCodeObject::PrintList(this->XCodeObjects, fout);
  3007. }
  3008. cmXCodeObject::Indent(1, fout);
  3009. fout << "rootObject = " << this->RootObject->GetId()
  3010. << " /* Project object */;\n";
  3011. fout << "}\n";
  3012. }
  3013. const char* cmGlobalXCodeGenerator::GetCMakeCFGIntDir() const
  3014. {
  3015. return this->XcodeVersion >= 21
  3016. ? "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)"
  3017. : ".";
  3018. }
  3019. std::string cmGlobalXCodeGenerator::ExpandCFGIntDir(
  3020. const std::string& str, const std::string& config) const
  3021. {
  3022. std::string replace1 = "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)";
  3023. std::string replace2 = "$(CONFIGURATION)";
  3024. std::string tmp = str;
  3025. for (std::string::size_type i = tmp.find(replace1); i != std::string::npos;
  3026. i = tmp.find(replace1, i)) {
  3027. tmp.replace(i, replace1.size(), config);
  3028. i += config.size();
  3029. }
  3030. for (std::string::size_type i = tmp.find(replace2); i != std::string::npos;
  3031. i = tmp.find(replace2, i)) {
  3032. tmp.replace(i, replace2.size(), config);
  3033. i += config.size();
  3034. }
  3035. return tmp;
  3036. }
  3037. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  3038. {
  3039. entry.Name = cmGlobalXCodeGenerator::GetActualName();
  3040. entry.Brief = "Generate Xcode project files.";
  3041. }
  3042. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(const char* p)
  3043. {
  3044. return cmSystemTools::ConvertToOutputPath(p);
  3045. }
  3046. std::string cmGlobalXCodeGenerator::RelativeToSource(const char* p)
  3047. {
  3048. // We force conversion because Xcode breakpoints do not work unless
  3049. // they are in a file named relative to the source tree.
  3050. return this->CurrentLocalGenerator->ConvertToRelativePath(
  3051. this->ProjectSourceDirectoryComponents, p, true);
  3052. }
  3053. std::string cmGlobalXCodeGenerator::RelativeToBinary(const char* p)
  3054. {
  3055. return this->CurrentLocalGenerator->ConvertToRelativePath(
  3056. this->ProjectOutputDirectoryComponents, p);
  3057. }
  3058. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const std::string& p)
  3059. {
  3060. if (p.find(' ') != p.npos) {
  3061. std::string t = "\"";
  3062. t += p;
  3063. t += "\"";
  3064. return t;
  3065. }
  3066. return p;
  3067. }
  3068. void cmGlobalXCodeGenerator::AppendDirectoryForConfig(
  3069. const std::string& prefix, const std::string& config,
  3070. const std::string& suffix, std::string& dir)
  3071. {
  3072. if (this->XcodeVersion > 20) {
  3073. if (!config.empty()) {
  3074. dir += prefix;
  3075. dir += config;
  3076. dir += suffix;
  3077. }
  3078. }
  3079. }
  3080. std::string cmGlobalXCodeGenerator::LookupFlags(
  3081. const std::string& varNamePrefix, const std::string& varNameLang,
  3082. const std::string& varNameSuffix, const std::string& default_flags)
  3083. {
  3084. if (!varNameLang.empty()) {
  3085. std::string varName = varNamePrefix;
  3086. varName += varNameLang;
  3087. varName += varNameSuffix;
  3088. if (const char* varValue = this->CurrentMakefile->GetDefinition(varName)) {
  3089. if (*varValue) {
  3090. return varValue;
  3091. }
  3092. }
  3093. }
  3094. return default_flags;
  3095. }
  3096. void cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  3097. const char* defines_list,
  3098. bool dflag)
  3099. {
  3100. // Skip this if there are no definitions.
  3101. if (!defines_list) {
  3102. return;
  3103. }
  3104. // Expand the list of definitions.
  3105. std::vector<std::string> defines;
  3106. cmSystemTools::ExpandListArgument(defines_list, defines);
  3107. // Store the definitions in the string.
  3108. this->AppendDefines(defs, defines, dflag);
  3109. }
  3110. void cmGlobalXCodeGenerator::AppendDefines(
  3111. BuildObjectListOrString& defs, std::vector<std::string> const& defines,
  3112. bool dflag)
  3113. {
  3114. // GCC_PREPROCESSOR_DEFINITIONS is a space-separated list of definitions.
  3115. std::string def;
  3116. for (std::vector<std::string>::const_iterator di = defines.begin();
  3117. di != defines.end(); ++di) {
  3118. // Start with -D if requested.
  3119. def = dflag ? "-D" : "";
  3120. def += *di;
  3121. // Append the flag with needed escapes.
  3122. std::string tmp;
  3123. this->AppendFlag(tmp, def);
  3124. defs.Add(tmp);
  3125. }
  3126. }
  3127. void cmGlobalXCodeGenerator::AppendFlag(std::string& flags,
  3128. std::string const& flag)
  3129. {
  3130. // Short-circuit for an empty flag.
  3131. if (flag.empty()) {
  3132. return;
  3133. }
  3134. // Separate from previous flags.
  3135. if (!flags.empty()) {
  3136. flags += " ";
  3137. }
  3138. // Check if the flag needs quoting.
  3139. bool quoteFlag =
  3140. flag.find_first_of("`~!@#$%^&*()+={}[]|:;\"'<>,.? ") != flag.npos;
  3141. // We escape a flag as follows:
  3142. // - Place each flag in single quotes ''
  3143. // - Escape a single quote as \'
  3144. // - Escape a backslash as \\ since it itself is an escape
  3145. // Note that in the code below we need one more level of escapes for
  3146. // C string syntax in this source file.
  3147. //
  3148. // The final level of escaping is done when the string is stored
  3149. // into the project file by cmXCodeObject::PrintString.
  3150. if (quoteFlag) {
  3151. // Open single quote.
  3152. flags += "'";
  3153. }
  3154. // Flag value with escaped quotes and backslashes.
  3155. for (const char* c = flag.c_str(); *c; ++c) {
  3156. if (*c == '\'') {
  3157. if (this->XcodeVersion >= 40) {
  3158. flags += "'\\''";
  3159. } else {
  3160. flags += "\\'";
  3161. }
  3162. } else if (*c == '\\') {
  3163. flags += "\\\\";
  3164. } else {
  3165. flags += *c;
  3166. }
  3167. }
  3168. if (quoteFlag) {
  3169. // Close single quote.
  3170. flags += "'";
  3171. }
  3172. }
  3173. std::string cmGlobalXCodeGenerator::ComputeInfoPListLocation(
  3174. cmGeneratorTarget* target)
  3175. {
  3176. std::string plist = target->GetLocalGenerator()->GetCurrentBinaryDirectory();
  3177. plist += cmake::GetCMakeFilesDirectory();
  3178. plist += "/";
  3179. plist += target->GetName();
  3180. plist += ".dir/Info.plist";
  3181. return plist;
  3182. }
  3183. // Return true if the generated build tree may contain multiple builds.
  3184. // i.e. "Can I build Debug and Release in the same tree?"
  3185. bool cmGlobalXCodeGenerator::IsMultiConfig() const
  3186. {
  3187. // Old Xcode 1.5 is single config:
  3188. if (this->XcodeVersion == 15) {
  3189. return false;
  3190. }
  3191. // Newer Xcode versions are multi config:
  3192. return true;
  3193. }
  3194. void cmGlobalXCodeGenerator::ComputeTargetObjectDirectory(
  3195. cmGeneratorTarget* gt) const
  3196. {
  3197. std::string configName = this->GetCMakeCFGIntDir();
  3198. std::string dir =
  3199. this->GetObjectsNormalDirectory("$(PROJECT_NAME)", configName, gt);
  3200. if (this->XcodeVersion >= 21) {
  3201. dir += "$(CURRENT_ARCH)/";
  3202. } else {
  3203. #ifdef __ppc__
  3204. dir += "ppc/";
  3205. #endif
  3206. #ifdef __i386
  3207. dir += "i386/";
  3208. #endif
  3209. }
  3210. gt->ObjectDirectory = dir;
  3211. }