cmGlobalXCodeGenerator.cxx 131 KB

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