cmGlobalXCodeGenerator.cxx 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548
  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 "cmStateTypes.h"
  24. #include "cmSystemTools.h"
  25. #include "cmTarget.h"
  26. #include "cmXCode21Object.h"
  27. #include "cmXCodeObject.h"
  28. #include "cm_auto_ptr.hxx"
  29. #include "cmake.h"
  30. struct cmLinkImplementation;
  31. #if defined(CMAKE_BUILD_WITH_CMAKE)
  32. #include "cmXMLParser.h"
  33. // parse the xml file storing the installed version of Xcode on
  34. // the machine
  35. class cmXcodeVersionParser : public cmXMLParser
  36. {
  37. public:
  38. cmXcodeVersionParser()
  39. : Version("1.5")
  40. {
  41. }
  42. void StartElement(const std::string&, const char**) { this->Data = ""; }
  43. void EndElement(const std::string& name)
  44. {
  45. if (name == "key") {
  46. this->Key = this->Data;
  47. } else if (name == "string") {
  48. if (this->Key == "CFBundleShortVersionString") {
  49. this->Version = this->Data;
  50. }
  51. }
  52. }
  53. void CharacterDataHandler(const char* data, int length)
  54. {
  55. this->Data.append(data, length);
  56. }
  57. std::string Version;
  58. std::string Key;
  59. std::string Data;
  60. };
  61. #endif
  62. // Builds either an object list or a space-separated string from the
  63. // given inputs.
  64. class cmGlobalXCodeGenerator::BuildObjectListOrString
  65. {
  66. cmGlobalXCodeGenerator* Generator;
  67. cmXCodeObject* Group;
  68. bool Empty;
  69. std::string String;
  70. public:
  71. BuildObjectListOrString(cmGlobalXCodeGenerator* gen, bool buildObjectList)
  72. : Generator(gen)
  73. , Group(0)
  74. , Empty(true)
  75. {
  76. if (buildObjectList) {
  77. this->Group = this->Generator->CreateObject(cmXCodeObject::OBJECT_LIST);
  78. }
  79. }
  80. bool IsEmpty() const { return this->Empty; }
  81. void Add(const std::string& newString)
  82. {
  83. this->Empty = false;
  84. if (this->Group) {
  85. this->Group->AddObject(this->Generator->CreateString(newString));
  86. } else {
  87. this->String += newString;
  88. this->String += ' ';
  89. }
  90. }
  91. const std::string& GetString() const { return this->String; }
  92. cmXCodeObject* CreateList()
  93. {
  94. if (this->Group) {
  95. return this->Group;
  96. } else {
  97. return this->Generator->CreateString(this->String);
  98. }
  99. }
  100. };
  101. class cmGlobalXCodeGenerator::Factory : public cmGlobalGeneratorFactory
  102. {
  103. public:
  104. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  105. cmake* cm) const CM_OVERRIDE;
  106. void GetDocumentation(cmDocumentationEntry& entry) const CM_OVERRIDE
  107. {
  108. cmGlobalXCodeGenerator::GetDocumentation(entry);
  109. }
  110. void GetGenerators(std::vector<std::string>& names) const CM_OVERRIDE
  111. {
  112. names.push_back(cmGlobalXCodeGenerator::GetActualName());
  113. }
  114. bool SupportsToolset() const CM_OVERRIDE { return true; }
  115. bool SupportsPlatform() const CM_OVERRIDE { return false; }
  116. };
  117. cmGlobalXCodeGenerator::cmGlobalXCodeGenerator(cmake* cm,
  118. std::string const& version)
  119. : cmGlobalGenerator(cm)
  120. {
  121. this->VersionString = version;
  122. // Compute an integer form of the version number.
  123. unsigned int v[2] = { 0, 0 };
  124. sscanf(this->VersionString.c_str(), "%u.%u", &v[0], &v[1]);
  125. this->XcodeVersion = 10 * v[0] + v[1];
  126. this->RootObject = 0;
  127. this->MainGroupChildren = 0;
  128. this->SourcesGroupChildren = 0;
  129. this->ResourcesGroupChildren = 0;
  130. this->CurrentMakefile = 0;
  131. this->CurrentLocalGenerator = 0;
  132. this->XcodeBuildCommandInitialized = false;
  133. }
  134. cmGlobalGeneratorFactory* cmGlobalXCodeGenerator::NewFactory()
  135. {
  136. return new Factory;
  137. }
  138. cmGlobalGenerator* cmGlobalXCodeGenerator::Factory::CreateGlobalGenerator(
  139. const std::string& name, cmake* cm) const
  140. {
  141. if (name != GetActualName())
  142. return 0;
  143. #if defined(CMAKE_BUILD_WITH_CMAKE)
  144. cmXcodeVersionParser parser;
  145. std::string versionFile;
  146. {
  147. std::string out;
  148. std::string::size_type pos;
  149. if (cmSystemTools::RunSingleCommand("xcode-select --print-path", &out, 0,
  150. 0, 0, cmSystemTools::OUTPUT_NONE) &&
  151. (pos = out.find(".app/"), pos != out.npos)) {
  152. versionFile = out.substr(0, pos + 5) + "Contents/version.plist";
  153. }
  154. }
  155. if (!versionFile.empty() && cmSystemTools::FileExists(versionFile.c_str())) {
  156. parser.ParseFile(versionFile.c_str());
  157. } else if (cmSystemTools::FileExists(
  158. "/Applications/Xcode.app/Contents/version.plist")) {
  159. parser.ParseFile("/Applications/Xcode.app/Contents/version.plist");
  160. } else {
  161. parser.ParseFile(
  162. "/Developer/Applications/Xcode.app/Contents/version.plist");
  163. }
  164. CM_AUTO_PTR<cmGlobalXCodeGenerator> gg(
  165. new cmGlobalXCodeGenerator(cm, parser.Version));
  166. if (gg->XcodeVersion == 20) {
  167. cmSystemTools::Message("Xcode 2.0 not really supported by cmake, "
  168. "using Xcode 15 generator\n");
  169. gg->XcodeVersion = 15;
  170. }
  171. return gg.release();
  172. #else
  173. std::cerr << "CMake should be built with cmake to use Xcode, "
  174. "default to Xcode 1.5\n";
  175. return new cmGlobalXCodeGenerator(cm);
  176. #endif
  177. }
  178. bool cmGlobalXCodeGenerator::FindMakeProgram(cmMakefile* mf)
  179. {
  180. // The Xcode generator knows how to lookup its build tool
  181. // directly instead of needing a helper module to do it, so we
  182. // do not actually need to put CMAKE_MAKE_PROGRAM into the cache.
  183. if (cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM"))) {
  184. mf->AddDefinition("CMAKE_MAKE_PROGRAM",
  185. this->GetXcodeBuildCommand().c_str());
  186. }
  187. return true;
  188. }
  189. std::string const& cmGlobalXCodeGenerator::GetXcodeBuildCommand()
  190. {
  191. if (!this->XcodeBuildCommandInitialized) {
  192. this->XcodeBuildCommandInitialized = true;
  193. this->XcodeBuildCommand = this->FindXcodeBuildCommand();
  194. }
  195. return this->XcodeBuildCommand;
  196. }
  197. std::string cmGlobalXCodeGenerator::FindXcodeBuildCommand()
  198. {
  199. if (this->XcodeVersion >= 40) {
  200. std::string makeProgram = cmSystemTools::FindProgram("xcodebuild");
  201. if (makeProgram.empty()) {
  202. makeProgram = "xcodebuild";
  203. }
  204. return makeProgram;
  205. } else {
  206. // Use cmakexbuild wrapper to suppress environment dump from output.
  207. return cmSystemTools::GetCMakeCommand() + "xbuild";
  208. }
  209. }
  210. bool cmGlobalXCodeGenerator::SetGeneratorToolset(std::string const& ts,
  211. cmMakefile* mf)
  212. {
  213. if (this->XcodeVersion >= 30) {
  214. if (ts.find_first_of(",=") != ts.npos) {
  215. std::ostringstream e;
  216. /* clang-format off */
  217. e <<
  218. "Generator\n"
  219. " " << this->GetName() << "\n"
  220. "does not recognize the toolset\n"
  221. " " << ts << "\n"
  222. "that was specified.";
  223. /* clang-format on */
  224. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  225. return false;
  226. }
  227. this->GeneratorToolset = ts;
  228. if (!this->GeneratorToolset.empty()) {
  229. mf->AddDefinition("CMAKE_XCODE_PLATFORM_TOOLSET",
  230. this->GeneratorToolset.c_str());
  231. }
  232. return true;
  233. } else {
  234. return cmGlobalGenerator::SetGeneratorToolset(ts, mf);
  235. }
  236. }
  237. void cmGlobalXCodeGenerator::EnableLanguage(
  238. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  239. {
  240. mf->AddDefinition("XCODE", "1");
  241. mf->AddDefinition("XCODE_VERSION", this->VersionString.c_str());
  242. if (this->XcodeVersion == 15) {
  243. } else {
  244. if (!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES")) {
  245. mf->AddCacheDefinition(
  246. "CMAKE_CONFIGURATION_TYPES", "Debug;Release;MinSizeRel;RelWithDebInfo",
  247. "Semicolon separated list of supported configuration types, "
  248. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  249. "anything else will be ignored.",
  250. cmStateEnums::STRING);
  251. }
  252. }
  253. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  254. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  255. const char* osxArch = mf->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  256. const char* sysroot = mf->GetDefinition("CMAKE_OSX_SYSROOT");
  257. if (osxArch && sysroot) {
  258. this->Architectures.clear();
  259. cmSystemTools::ExpandListArgument(std::string(osxArch),
  260. this->Architectures);
  261. }
  262. }
  263. void cmGlobalXCodeGenerator::GenerateBuildCommand(
  264. std::vector<std::string>& makeCommand, const std::string& makeProgram,
  265. const std::string& projectName, const std::string& /*projectDir*/,
  266. const std::string& targetName, const std::string& config, bool /*fast*/,
  267. bool /*verbose*/, std::vector<std::string> const& makeOptions)
  268. {
  269. // now build the test
  270. makeCommand.push_back(
  271. this->SelectMakeProgram(makeProgram, this->GetXcodeBuildCommand()));
  272. makeCommand.push_back("-project");
  273. std::string projectArg = projectName;
  274. projectArg += ".xcode";
  275. if (this->XcodeVersion > 20) {
  276. projectArg += "proj";
  277. }
  278. makeCommand.push_back(projectArg);
  279. bool clean = false;
  280. std::string realTarget = targetName;
  281. if (realTarget == "clean") {
  282. clean = true;
  283. realTarget = "ALL_BUILD";
  284. }
  285. if (clean) {
  286. makeCommand.push_back("clean");
  287. } else {
  288. makeCommand.push_back("build");
  289. }
  290. makeCommand.push_back("-target");
  291. if (!realTarget.empty()) {
  292. makeCommand.push_back(realTarget);
  293. } else {
  294. makeCommand.push_back("ALL_BUILD");
  295. }
  296. if (this->XcodeVersion == 15) {
  297. makeCommand.push_back("-buildstyle");
  298. makeCommand.push_back("Development");
  299. } else {
  300. makeCommand.push_back("-configuration");
  301. makeCommand.push_back(!config.empty() ? config : "Debug");
  302. }
  303. makeCommand.insert(makeCommand.end(), makeOptions.begin(),
  304. makeOptions.end());
  305. }
  306. ///! Create a local generator appropriate to this Global Generator
  307. cmLocalGenerator* cmGlobalXCodeGenerator::CreateLocalGenerator(cmMakefile* mf)
  308. {
  309. return new cmLocalXCodeGenerator(this, mf);
  310. }
  311. void cmGlobalXCodeGenerator::AddExtraIDETargets()
  312. {
  313. std::map<std::string, std::vector<cmLocalGenerator*> >::iterator it;
  314. // make sure extra targets are added before calling
  315. // the parent generate which will call trace depends
  316. for (it = this->ProjectMap.begin(); it != this->ProjectMap.end(); ++it) {
  317. cmLocalGenerator* root = it->second[0];
  318. this->SetGenerationRoot(root);
  319. // add ALL_BUILD, INSTALL, etc
  320. this->AddExtraTargets(root, it->second);
  321. }
  322. }
  323. void cmGlobalXCodeGenerator::Generate()
  324. {
  325. this->cmGlobalGenerator::Generate();
  326. if (cmSystemTools::GetErrorOccuredFlag()) {
  327. return;
  328. }
  329. std::map<std::string, std::vector<cmLocalGenerator*> >::iterator it;
  330. for (it = this->ProjectMap.begin(); it != this->ProjectMap.end(); ++it) {
  331. cmLocalGenerator* root = it->second[0];
  332. this->SetGenerationRoot(root);
  333. // now create the project
  334. this->OutputXCodeProject(root, it->second);
  335. }
  336. }
  337. void cmGlobalXCodeGenerator::SetGenerationRoot(cmLocalGenerator* root)
  338. {
  339. this->CurrentProject = root->GetProjectName();
  340. this->SetCurrentLocalGenerator(root);
  341. cmSystemTools::SplitPath(
  342. this->CurrentLocalGenerator->GetCurrentSourceDirectory(),
  343. this->ProjectSourceDirectoryComponents);
  344. cmSystemTools::SplitPath(
  345. this->CurrentLocalGenerator->GetCurrentBinaryDirectory(),
  346. this->ProjectOutputDirectoryComponents);
  347. this->CurrentXCodeHackMakefile = root->GetCurrentBinaryDirectory();
  348. this->CurrentXCodeHackMakefile += "/CMakeScripts";
  349. cmSystemTools::MakeDirectory(this->CurrentXCodeHackMakefile.c_str());
  350. this->CurrentXCodeHackMakefile += "/XCODE_DEPEND_HELPER.make";
  351. }
  352. std::string cmGlobalXCodeGenerator::PostBuildMakeTarget(
  353. std::string const& tName, std::string const& configName)
  354. {
  355. std::string target = tName;
  356. std::replace(target.begin(), target.end(), ' ', '_');
  357. std::string out = "PostBuild." + target;
  358. if (this->XcodeVersion > 20) {
  359. out += "." + configName;
  360. }
  361. return out;
  362. }
  363. #define CMAKE_CHECK_BUILD_SYSTEM_TARGET "ZERO_CHECK"
  364. void cmGlobalXCodeGenerator::AddExtraTargets(
  365. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& gens)
  366. {
  367. cmMakefile* mf = root->GetMakefile();
  368. // Add ALL_BUILD
  369. const char* no_working_directory = 0;
  370. std::vector<std::string> no_depends;
  371. cmTarget* allbuild =
  372. mf->AddUtilityCommand("ALL_BUILD", true, no_depends, no_working_directory,
  373. "echo", "Build all projects");
  374. cmGeneratorTarget* allBuildGt = new cmGeneratorTarget(allbuild, root);
  375. root->AddGeneratorTarget(allBuildGt);
  376. // Refer to the main build configuration file for easy editing.
  377. std::string listfile = root->GetCurrentSourceDirectory();
  378. listfile += "/";
  379. listfile += "CMakeLists.txt";
  380. allBuildGt->AddSource(listfile);
  381. // Add XCODE depend helper
  382. std::string dir = root->GetCurrentBinaryDirectory();
  383. cmCustomCommandLine makeHelper;
  384. if (this->XcodeVersion < 50) {
  385. makeHelper.push_back("make");
  386. makeHelper.push_back("-C");
  387. makeHelper.push_back(dir);
  388. makeHelper.push_back("-f");
  389. makeHelper.push_back(this->CurrentXCodeHackMakefile);
  390. makeHelper.push_back(""); // placeholder, see below
  391. }
  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 (!makeHelper.empty() &&
  429. (target->GetType() == cmStateEnums::EXECUTABLE ||
  430. // Nope - no post-build for OBJECT_LIRBRARY
  431. // target->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  432. target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  433. target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  434. target->GetType() == cmStateEnums::MODULE_LIBRARY)) {
  435. makeHelper[makeHelper.size() - 1] = // fill placeholder
  436. this->PostBuildMakeTarget(target->GetName(), "$(CONFIGURATION)");
  437. cmCustomCommandLines commandLines;
  438. commandLines.push_back(makeHelper);
  439. std::vector<std::string> no_byproducts;
  440. lg->GetMakefile()->AddCustomCommandToTarget(
  441. target->GetName(), no_byproducts, no_depends, commandLines,
  442. cmTarget::POST_BUILD, "Depend check for xcode", dir.c_str());
  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. // Add define flags
  1503. this->CurrentLocalGenerator->AppendFlags(
  1504. defFlags, this->CurrentMakefile->GetDefineFlags());
  1505. // Add preprocessor definitions for this target and configuration.
  1506. BuildObjectListOrString ppDefs(this, this->XcodeVersion >= 30);
  1507. if (this->XcodeVersion > 15) {
  1508. this->AppendDefines(
  1509. ppDefs, "CMAKE_INTDIR=\"$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)\"");
  1510. }
  1511. if (const char* exportMacro = gtgt->GetExportMacro()) {
  1512. // Add the export symbol definition for shared library objects.
  1513. this->AppendDefines(ppDefs, exportMacro);
  1514. }
  1515. std::vector<std::string> targetDefines;
  1516. gtgt->GetCompileDefinitions(targetDefines, configName, "C");
  1517. this->AppendDefines(ppDefs, targetDefines);
  1518. buildSettings->AddAttribute("GCC_PREPROCESSOR_DEFINITIONS",
  1519. ppDefs.CreateList());
  1520. std::string extraLinkOptionsVar;
  1521. std::string extraLinkOptions;
  1522. if (gtgt->GetType() == cmStateEnums::EXECUTABLE) {
  1523. extraLinkOptionsVar = "CMAKE_EXE_LINKER_FLAGS";
  1524. } else if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  1525. extraLinkOptionsVar = "CMAKE_SHARED_LINKER_FLAGS";
  1526. } else if (gtgt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  1527. extraLinkOptionsVar = "CMAKE_MODULE_LINKER_FLAGS";
  1528. }
  1529. if (!extraLinkOptionsVar.empty()) {
  1530. this->CurrentLocalGenerator->AddConfigVariableFlags(
  1531. extraLinkOptions, extraLinkOptionsVar, configName);
  1532. }
  1533. if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  1534. gtgt->GetType() == cmStateEnums::STATIC_LIBRARY) {
  1535. this->CurrentLocalGenerator->GetStaticLibraryFlags(
  1536. extraLinkOptions, cmSystemTools::UpperCase(configName), gtgt);
  1537. } else {
  1538. const char* targetLinkFlags = gtgt->GetProperty("LINK_FLAGS");
  1539. if (targetLinkFlags) {
  1540. this->CurrentLocalGenerator->AppendFlags(extraLinkOptions,
  1541. targetLinkFlags);
  1542. }
  1543. if (!configName.empty()) {
  1544. std::string linkFlagsVar = "LINK_FLAGS_";
  1545. linkFlagsVar += cmSystemTools::UpperCase(configName);
  1546. if (const char* linkFlags = gtgt->GetProperty(linkFlagsVar)) {
  1547. this->CurrentLocalGenerator->AppendFlags(extraLinkOptions, linkFlags);
  1548. }
  1549. }
  1550. }
  1551. // Set target-specific architectures.
  1552. std::vector<std::string> archs;
  1553. gtgt->GetAppleArchs(configName, archs);
  1554. if (!archs.empty()) {
  1555. // Enable ARCHS attribute.
  1556. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH", this->CreateString("NO"));
  1557. // Store ARCHS value.
  1558. if (archs.size() == 1) {
  1559. buildSettings->AddAttribute("ARCHS", this->CreateString(archs[0]));
  1560. } else {
  1561. cmXCodeObject* archObjects =
  1562. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1563. for (std::vector<std::string>::iterator i = archs.begin();
  1564. i != archs.end(); i++) {
  1565. archObjects->AddObject(this->CreateString(*i));
  1566. }
  1567. buildSettings->AddAttribute("ARCHS", archObjects);
  1568. }
  1569. }
  1570. // Get the product name components.
  1571. std::string pnprefix;
  1572. std::string pnbase;
  1573. std::string pnsuffix;
  1574. gtgt->GetFullNameComponents(pnprefix, pnbase, pnsuffix, configName);
  1575. const char* version = gtgt->GetProperty("VERSION");
  1576. const char* soversion = gtgt->GetProperty("SOVERSION");
  1577. if (!gtgt->HasSOName(configName) || gtgt->IsFrameworkOnApple()) {
  1578. version = 0;
  1579. soversion = 0;
  1580. }
  1581. if (version && !soversion) {
  1582. soversion = version;
  1583. }
  1584. if (!version && soversion) {
  1585. version = soversion;
  1586. }
  1587. std::string realName = pnbase;
  1588. std::string soName = pnbase;
  1589. if (version && soversion) {
  1590. realName += ".";
  1591. realName += version;
  1592. soName += ".";
  1593. soName += soversion;
  1594. }
  1595. // Set attributes to specify the proper name for the target.
  1596. std::string pndir = this->CurrentLocalGenerator->GetCurrentBinaryDirectory();
  1597. if (gtgt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  1598. gtgt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1599. gtgt->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1600. gtgt->GetType() == cmStateEnums::EXECUTABLE) {
  1601. if (this->XcodeVersion >= 21) {
  1602. if (!gtgt->UsesDefaultOutputDir(configName, false)) {
  1603. std::string pncdir = gtgt->GetDirectory(configName);
  1604. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  1605. this->CreateString(pncdir));
  1606. }
  1607. } else {
  1608. buildSettings->AddAttribute("OBJROOT", this->CreateString(pndir));
  1609. pndir = gtgt->GetDirectory(configName);
  1610. }
  1611. if (gtgt->IsFrameworkOnApple() || gtgt->IsCFBundleOnApple()) {
  1612. pnprefix = "";
  1613. }
  1614. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  1615. this->CreateString(pnprefix));
  1616. buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
  1617. this->CreateString(pnsuffix));
  1618. } else if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1619. pnprefix = "lib";
  1620. pnbase = gtgt->GetName();
  1621. pnsuffix = ".a";
  1622. if (this->XcodeVersion >= 21) {
  1623. std::string pncdir = this->GetObjectsNormalDirectory(
  1624. this->CurrentProject, configName, gtgt);
  1625. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  1626. this->CreateString(pncdir));
  1627. } else {
  1628. buildSettings->AddAttribute("OBJROOT", this->CreateString(pndir));
  1629. pndir = this->GetObjectsNormalDirectory(this->CurrentProject, configName,
  1630. gtgt);
  1631. }
  1632. }
  1633. // Store the product name for all target types.
  1634. buildSettings->AddAttribute("PRODUCT_NAME", this->CreateString(realName));
  1635. buildSettings->AddAttribute("SYMROOT", this->CreateString(pndir));
  1636. // Handle settings for each target type.
  1637. switch (gtgt->GetType()) {
  1638. case cmStateEnums::OBJECT_LIBRARY:
  1639. case cmStateEnums::STATIC_LIBRARY: {
  1640. buildSettings->AddAttribute("LIBRARY_STYLE",
  1641. this->CreateString("STATIC"));
  1642. break;
  1643. }
  1644. case cmStateEnums::MODULE_LIBRARY: {
  1645. buildSettings->AddAttribute("LIBRARY_STYLE",
  1646. this->CreateString("BUNDLE"));
  1647. if (gtgt->IsCFBundleOnApple()) {
  1648. // It turns out that a BUNDLE is basically the same
  1649. // in many ways as an application bundle, as far as
  1650. // link flags go
  1651. std::string createFlags = this->LookupFlags(
  1652. "CMAKE_SHARED_MODULE_CREATE_", llang, "_FLAGS", "-bundle");
  1653. if (!createFlags.empty()) {
  1654. extraLinkOptions += " ";
  1655. extraLinkOptions += createFlags;
  1656. }
  1657. const char* ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  1658. if (ext) {
  1659. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  1660. this->CreateString(ext));
  1661. }
  1662. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1663. // Xcode will create the final version of Info.plist at build time,
  1664. // so let it replace the cfbundle name. This avoids creating
  1665. // a per-configuration Info.plist file. The cfbundle plist
  1666. // is very similar to the application bundle plist
  1667. this->CurrentLocalGenerator->GenerateAppleInfoPList(
  1668. gtgt, "$(EXECUTABLE_NAME)", plist.c_str());
  1669. buildSettings->AddAttribute("INFOPLIST_FILE",
  1670. this->CreateString(plist));
  1671. } else if (this->XcodeVersion >= 22) {
  1672. buildSettings->AddAttribute("MACH_O_TYPE",
  1673. this->CreateString("mh_bundle"));
  1674. buildSettings->AddAttribute("GCC_DYNAMIC_NO_PIC",
  1675. this->CreateString("NO"));
  1676. // Add the flags to create an executable.
  1677. std::string createFlags =
  1678. this->LookupFlags("CMAKE_", llang, "_LINK_FLAGS", "");
  1679. if (!createFlags.empty()) {
  1680. extraLinkOptions += " ";
  1681. extraLinkOptions += createFlags;
  1682. }
  1683. } else {
  1684. // Add the flags to create a module.
  1685. std::string createFlags = this->LookupFlags(
  1686. "CMAKE_SHARED_MODULE_CREATE_", llang, "_FLAGS", "-bundle");
  1687. if (!createFlags.empty()) {
  1688. extraLinkOptions += " ";
  1689. extraLinkOptions += createFlags;
  1690. }
  1691. }
  1692. break;
  1693. }
  1694. case cmStateEnums::SHARED_LIBRARY: {
  1695. if (gtgt->GetPropertyAsBool("FRAMEWORK")) {
  1696. std::string fw_version = gtgt->GetFrameworkVersion();
  1697. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  1698. this->CreateString(fw_version));
  1699. const char* ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  1700. if (ext) {
  1701. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  1702. this->CreateString(ext));
  1703. }
  1704. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1705. // Xcode will create the final version of Info.plist at build time,
  1706. // so let it replace the framework name. This avoids creating
  1707. // a per-configuration Info.plist file.
  1708. this->CurrentLocalGenerator->GenerateFrameworkInfoPList(
  1709. gtgt, "$(EXECUTABLE_NAME)", plist.c_str());
  1710. buildSettings->AddAttribute("INFOPLIST_FILE",
  1711. this->CreateString(plist));
  1712. } else {
  1713. // Add the flags to create a shared library.
  1714. std::string createFlags = this->LookupFlags(
  1715. "CMAKE_SHARED_LIBRARY_CREATE_", llang, "_FLAGS", "-dynamiclib");
  1716. if (!createFlags.empty()) {
  1717. extraLinkOptions += " ";
  1718. extraLinkOptions += createFlags;
  1719. }
  1720. }
  1721. buildSettings->AddAttribute("LIBRARY_STYLE",
  1722. this->CreateString("DYNAMIC"));
  1723. break;
  1724. }
  1725. case cmStateEnums::EXECUTABLE: {
  1726. // Add the flags to create an executable.
  1727. std::string createFlags =
  1728. this->LookupFlags("CMAKE_", llang, "_LINK_FLAGS", "");
  1729. if (!createFlags.empty()) {
  1730. extraLinkOptions += " ";
  1731. extraLinkOptions += createFlags;
  1732. }
  1733. // Handle bundles and normal executables separately.
  1734. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  1735. const char* ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  1736. if (ext) {
  1737. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  1738. this->CreateString(ext));
  1739. }
  1740. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1741. // Xcode will create the final version of Info.plist at build time,
  1742. // so let it replace the executable name. This avoids creating
  1743. // a per-configuration Info.plist file.
  1744. this->CurrentLocalGenerator->GenerateAppleInfoPList(
  1745. gtgt, "$(EXECUTABLE_NAME)", plist.c_str());
  1746. buildSettings->AddAttribute("INFOPLIST_FILE",
  1747. this->CreateString(plist));
  1748. }
  1749. } break;
  1750. default:
  1751. break;
  1752. }
  1753. if (this->XcodeVersion >= 22 && this->XcodeVersion < 40) {
  1754. buildSettings->AddAttribute("PREBINDING", this->CreateString("NO"));
  1755. }
  1756. BuildObjectListOrString dirs(this, this->XcodeVersion >= 30);
  1757. BuildObjectListOrString fdirs(this, this->XcodeVersion >= 30);
  1758. std::vector<std::string> includes;
  1759. this->CurrentLocalGenerator->GetIncludeDirectories(includes, gtgt, "C",
  1760. configName);
  1761. std::set<std::string> emitted;
  1762. emitted.insert("/System/Library/Frameworks");
  1763. for (std::vector<std::string>::iterator i = includes.begin();
  1764. i != includes.end(); ++i) {
  1765. if (this->NameResolvesToFramework(*i)) {
  1766. std::string frameworkDir = *i;
  1767. frameworkDir += "/../";
  1768. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1769. if (emitted.insert(frameworkDir).second) {
  1770. fdirs.Add(this->XCodeEscapePath(frameworkDir));
  1771. }
  1772. } else {
  1773. std::string incpath = this->XCodeEscapePath(*i);
  1774. dirs.Add(incpath);
  1775. }
  1776. }
  1777. // Add framework search paths needed for linking.
  1778. if (cmComputeLinkInformation* cli = gtgt->GetLinkInformation(configName)) {
  1779. std::vector<std::string> const& fwDirs = cli->GetFrameworkPaths();
  1780. for (std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  1781. fdi != fwDirs.end(); ++fdi) {
  1782. if (emitted.insert(*fdi).second) {
  1783. fdirs.Add(this->XCodeEscapePath(*fdi));
  1784. }
  1785. }
  1786. }
  1787. if (!fdirs.IsEmpty()) {
  1788. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS", fdirs.CreateList());
  1789. }
  1790. if (!dirs.IsEmpty()) {
  1791. buildSettings->AddAttribute("HEADER_SEARCH_PATHS", dirs.CreateList());
  1792. }
  1793. bool same_gflags = true;
  1794. std::map<std::string, std::string> gflags;
  1795. std::string const* last_gflag = 0;
  1796. std::string optLevel = "0";
  1797. // Minimal map of flags to build settings.
  1798. for (std::set<std::string>::iterator li = languages.begin();
  1799. li != languages.end(); ++li) {
  1800. std::string& flags = cflags[*li];
  1801. std::string& gflag = gflags[*li];
  1802. std::string oflag =
  1803. this->ExtractFlagRegex("(^| )(-Ofast|-Os|-O[0-9]*)( |$)", 2, flags);
  1804. if (oflag.size() == 2) {
  1805. optLevel = "1";
  1806. } else if (oflag.size() > 2) {
  1807. optLevel = oflag.substr(2);
  1808. }
  1809. gflag = this->ExtractFlag("-g", flags);
  1810. // put back gdwarf-2 if used since there is no way
  1811. // to represent it in the gui, but we still want debug yes
  1812. if (gflag == "-gdwarf-2") {
  1813. flags += " ";
  1814. flags += gflag;
  1815. }
  1816. if (last_gflag && *last_gflag != gflag) {
  1817. same_gflags = false;
  1818. }
  1819. last_gflag = &gflag;
  1820. }
  1821. const char* debugStr = "YES";
  1822. if (!same_gflags) {
  1823. // We can't set the Xcode flag differently depending on the language,
  1824. // so put them back in this case.
  1825. for (std::set<std::string>::iterator li = languages.begin();
  1826. li != languages.end(); ++li) {
  1827. cflags[*li] += " ";
  1828. cflags[*li] += gflags[*li];
  1829. }
  1830. debugStr = "NO";
  1831. } else if (last_gflag && (last_gflag->empty() || *last_gflag == "-g0")) {
  1832. debugStr = "NO";
  1833. }
  1834. buildSettings->AddAttribute("COMBINE_HIDPI_IMAGES",
  1835. this->CreateString("YES"));
  1836. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  1837. this->CreateString(debugStr));
  1838. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  1839. this->CreateString(optLevel));
  1840. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  1841. this->CreateString("NO"));
  1842. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  1843. this->CreateString("NO"));
  1844. for (std::set<std::string>::iterator li = languages.begin();
  1845. li != languages.end(); ++li) {
  1846. std::string flags = cflags[*li] + " " + defFlags;
  1847. if (*li == "CXX") {
  1848. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  1849. this->CreateString(flags));
  1850. } else if (*li == "Fortran") {
  1851. buildSettings->AddAttribute("IFORT_OTHER_FLAGS",
  1852. this->CreateString(flags));
  1853. } else if (*li == "C") {
  1854. buildSettings->AddAttribute("OTHER_CFLAGS", this->CreateString(flags));
  1855. } else if (*li == "Swift") {
  1856. buildSettings->AddAttribute("OTHER_SWIFT_FLAGS",
  1857. this->CreateString(flags));
  1858. }
  1859. }
  1860. // Add Fortran source format attribute if property is set.
  1861. const char* format = 0;
  1862. const char* tgtfmt = gtgt->GetProperty("Fortran_FORMAT");
  1863. switch (cmOutputConverter::GetFortranFormat(tgtfmt)) {
  1864. case cmOutputConverter::FortranFormatFixed:
  1865. format = "fixed";
  1866. break;
  1867. case cmOutputConverter::FortranFormatFree:
  1868. format = "free";
  1869. break;
  1870. default:
  1871. break;
  1872. }
  1873. if (format) {
  1874. buildSettings->AddAttribute("IFORT_LANG_SRCFMT",
  1875. this->CreateString(format));
  1876. }
  1877. // Create the INSTALL_PATH attribute.
  1878. std::string install_name_dir;
  1879. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  1880. // Get the install_name directory for the build tree.
  1881. install_name_dir = gtgt->GetInstallNameDirForBuildTree(configName);
  1882. // Xcode doesn't create the correct install_name in some cases.
  1883. // That is, if the INSTALL_PATH is empty, or if we have versioning
  1884. // of dylib libraries, we want to specify the install_name.
  1885. // This is done by adding a link flag to create an install_name
  1886. // with just the library soname.
  1887. std::string install_name;
  1888. if (!install_name_dir.empty()) {
  1889. // Convert to a path for the native build tool.
  1890. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  1891. install_name += install_name_dir;
  1892. install_name += "/";
  1893. }
  1894. install_name += gtgt->GetSOName(configName);
  1895. if ((realName != soName) || install_name_dir.empty()) {
  1896. install_name_dir = "";
  1897. extraLinkOptions += " -install_name ";
  1898. extraLinkOptions += XCodeEscapePath(install_name);
  1899. }
  1900. }
  1901. buildSettings->AddAttribute("INSTALL_PATH",
  1902. this->CreateString(install_name_dir));
  1903. // Create the LD_RUNPATH_SEARCH_PATHS
  1904. cmComputeLinkInformation* pcli = gtgt->GetLinkInformation(configName);
  1905. if (pcli) {
  1906. std::string search_paths;
  1907. std::vector<std::string> runtimeDirs;
  1908. pcli->GetRPath(runtimeDirs, false);
  1909. // runpath dirs needs to be unique to prevent corruption
  1910. std::set<std::string> unique_dirs;
  1911. for (std::vector<std::string>::const_iterator i = runtimeDirs.begin();
  1912. i != runtimeDirs.end(); ++i) {
  1913. std::string runpath = *i;
  1914. runpath = this->ExpandCFGIntDir(runpath, configName);
  1915. if (unique_dirs.find(runpath) == unique_dirs.end()) {
  1916. unique_dirs.insert(runpath);
  1917. if (!search_paths.empty()) {
  1918. search_paths += " ";
  1919. }
  1920. search_paths += this->XCodeEscapePath(runpath);
  1921. }
  1922. }
  1923. if (!search_paths.empty()) {
  1924. buildSettings->AddAttribute("LD_RUNPATH_SEARCH_PATHS",
  1925. this->CreateString(search_paths));
  1926. }
  1927. }
  1928. buildSettings->AddAttribute(this->GetTargetLinkFlagsVar(gtgt),
  1929. this->CreateString(extraLinkOptions));
  1930. buildSettings->AddAttribute("OTHER_REZFLAGS", this->CreateString(""));
  1931. buildSettings->AddAttribute("SECTORDER_FLAGS", this->CreateString(""));
  1932. buildSettings->AddAttribute("USE_HEADERMAP", this->CreateString("NO"));
  1933. if (this->XcodeVersion >= 30) {
  1934. cmXCodeObject* group = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1935. group->AddObject(this->CreateString("-Wmost"));
  1936. group->AddObject(this->CreateString("-Wno-four-char-constants"));
  1937. group->AddObject(this->CreateString("-Wno-unknown-pragmas"));
  1938. group->AddObject(this->CreateString("$(inherited)"));
  1939. buildSettings->AddAttribute("WARNING_CFLAGS", group);
  1940. } else {
  1941. buildSettings->AddAttribute(
  1942. "WARNING_CFLAGS", this->CreateString("-Wmost -Wno-four-char-constants"
  1943. " -Wno-unknown-pragmas"));
  1944. }
  1945. // Runtime version information.
  1946. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  1947. int major;
  1948. int minor;
  1949. int patch;
  1950. // VERSION -> current_version
  1951. gtgt->GetTargetVersion(false, major, minor, patch);
  1952. std::ostringstream v;
  1953. // Xcode always wants at least 1.0.0 or nothing
  1954. if (!(major == 0 && minor == 0 && patch == 0)) {
  1955. v << major << "." << minor << "." << patch;
  1956. }
  1957. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  1958. this->CreateString(v.str()));
  1959. // SOVERSION -> compatibility_version
  1960. gtgt->GetTargetVersion(true, major, minor, patch);
  1961. std::ostringstream vso;
  1962. // Xcode always wants at least 1.0.0 or nothing
  1963. if (!(major == 0 && minor == 0 && patch == 0)) {
  1964. vso << major << "." << minor << "." << patch;
  1965. }
  1966. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  1967. this->CreateString(vso.str()));
  1968. }
  1969. // put this last so it can override existing settings
  1970. // Convert "XCODE_ATTRIBUTE_*" properties directly.
  1971. {
  1972. std::vector<std::string> const& props = gtgt->GetPropertyKeys();
  1973. for (std::vector<std::string>::const_iterator i = props.begin();
  1974. i != props.end(); ++i) {
  1975. if (i->find("XCODE_ATTRIBUTE_") == 0) {
  1976. std::string attribute = i->substr(16);
  1977. this->FilterConfigurationAttribute(configName, attribute);
  1978. if (!attribute.empty()) {
  1979. cmGeneratorExpression ge;
  1980. std::string processed =
  1981. ge.Parse(gtgt->GetProperty(*i))
  1982. ->Evaluate(this->CurrentLocalGenerator, configName);
  1983. buildSettings->AddAttribute(attribute,
  1984. this->CreateString(processed));
  1985. }
  1986. }
  1987. }
  1988. }
  1989. }
  1990. cmXCodeObject* cmGlobalXCodeGenerator::CreateUtilityTarget(
  1991. cmGeneratorTarget* gtgt)
  1992. {
  1993. cmXCodeObject* shellBuildPhase =
  1994. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1995. shellBuildPhase->AddAttribute("buildActionMask",
  1996. this->CreateString("2147483647"));
  1997. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1998. shellBuildPhase->AddAttribute("files", buildFiles);
  1999. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2000. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  2001. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2002. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  2003. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  2004. this->CreateString("0"));
  2005. shellBuildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  2006. shellBuildPhase->AddAttribute(
  2007. "shellScript", this->CreateString("# shell script goes here\nexit 0"));
  2008. shellBuildPhase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  2009. cmXCodeObject* target =
  2010. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  2011. target->SetComment(gtgt->GetName());
  2012. cmXCodeObject* buildPhases = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2013. std::vector<cmXCodeObject*> emptyContentVector;
  2014. this->CreateCustomCommands(buildPhases, 0, 0, 0, emptyContentVector, 0,
  2015. gtgt);
  2016. target->AddAttribute("buildPhases", buildPhases);
  2017. if (this->XcodeVersion > 20) {
  2018. this->AddConfigurations(target, gtgt);
  2019. } else {
  2020. std::string theConfig =
  2021. this->CurrentMakefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2022. cmXCodeObject* buildSettings =
  2023. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2024. this->CreateBuildSettings(gtgt, buildSettings, theConfig);
  2025. target->AddAttribute("buildSettings", buildSettings);
  2026. }
  2027. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2028. target->AddAttribute("dependencies", dependencies);
  2029. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2030. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2031. target->SetTarget(gtgt);
  2032. this->XCodeObjectMap[gtgt] = target;
  2033. // Add source files without build rules for editing convenience.
  2034. if (gtgt->GetType() == cmStateEnums::UTILITY) {
  2035. std::vector<cmSourceFile*> sources;
  2036. if (!gtgt->GetConfigCommonSourceFiles(sources)) {
  2037. return 0;
  2038. }
  2039. for (std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  2040. i != sources.end(); ++i) {
  2041. if (!(*i)->GetPropertyAsBool("GENERATED")) {
  2042. this->CreateXCodeFileReference(*i, gtgt);
  2043. }
  2044. }
  2045. }
  2046. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2047. return target;
  2048. }
  2049. std::string cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  2050. cmGeneratorTarget* gtgt)
  2051. {
  2052. std::string configTypes =
  2053. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES");
  2054. std::vector<std::string> configVectorIn;
  2055. std::vector<std::string> configVector;
  2056. configVectorIn.push_back(configTypes);
  2057. cmSystemTools::ExpandList(configVectorIn, configVector);
  2058. cmXCodeObject* configlist =
  2059. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2060. cmXCodeObject* buildConfigurations =
  2061. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2062. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2063. std::string comment = "Build configuration list for ";
  2064. comment += cmXCodeObject::PBXTypeNames[target->GetIsA()];
  2065. comment += " \"";
  2066. comment += gtgt->GetName();
  2067. comment += "\"";
  2068. configlist->SetComment(comment);
  2069. target->AddAttribute("buildConfigurationList",
  2070. this->CreateObjectReference(configlist));
  2071. for (unsigned int i = 0; i < configVector.size(); ++i) {
  2072. cmXCodeObject* config =
  2073. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2074. buildConfigurations->AddObject(config);
  2075. cmXCodeObject* buildSettings =
  2076. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2077. this->CreateBuildSettings(gtgt, buildSettings, configVector[i]);
  2078. config->AddAttribute("name", this->CreateString(configVector[i]));
  2079. config->SetComment(configVector[i]);
  2080. config->AddAttribute("buildSettings", buildSettings);
  2081. }
  2082. if (!configVector.empty()) {
  2083. configlist->AddAttribute("defaultConfigurationName",
  2084. this->CreateString(configVector[0]));
  2085. configlist->AddAttribute("defaultConfigurationIsVisible",
  2086. this->CreateString("0"));
  2087. return configVector[0];
  2088. }
  2089. return "";
  2090. }
  2091. const char* cmGlobalXCodeGenerator::GetTargetLinkFlagsVar(
  2092. cmGeneratorTarget const* target) const
  2093. {
  2094. if (this->XcodeVersion >= 60 &&
  2095. (target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2096. target->GetType() == cmStateEnums::OBJECT_LIBRARY)) {
  2097. return "OTHER_LIBTOOLFLAGS";
  2098. } else {
  2099. return "OTHER_LDFLAGS";
  2100. }
  2101. }
  2102. const char* cmGlobalXCodeGenerator::GetTargetFileType(
  2103. cmGeneratorTarget* target)
  2104. {
  2105. if (const char* e = target->GetProperty("XCODE_EXPLICIT_FILE_TYPE")) {
  2106. return e;
  2107. }
  2108. switch (target->GetType()) {
  2109. case cmStateEnums::OBJECT_LIBRARY:
  2110. case cmStateEnums::STATIC_LIBRARY:
  2111. return "archive.ar";
  2112. case cmStateEnums::MODULE_LIBRARY:
  2113. if (target->IsXCTestOnApple())
  2114. return "wrapper.cfbundle";
  2115. else if (target->IsCFBundleOnApple())
  2116. return "wrapper.plug-in";
  2117. else
  2118. return ((this->XcodeVersion >= 22) ? "compiled.mach-o.executable"
  2119. : "compiled.mach-o.dylib");
  2120. case cmStateEnums::SHARED_LIBRARY:
  2121. return (target->GetPropertyAsBool("FRAMEWORK")
  2122. ? "wrapper.framework"
  2123. : "compiled.mach-o.dylib");
  2124. case cmStateEnums::EXECUTABLE:
  2125. return "compiled.mach-o.executable";
  2126. default:
  2127. break;
  2128. }
  2129. return 0;
  2130. }
  2131. const char* cmGlobalXCodeGenerator::GetTargetProductType(
  2132. cmGeneratorTarget* target)
  2133. {
  2134. if (const char* e = target->GetProperty("XCODE_PRODUCT_TYPE")) {
  2135. return e;
  2136. }
  2137. switch (target->GetType()) {
  2138. case cmStateEnums::OBJECT_LIBRARY:
  2139. case cmStateEnums::STATIC_LIBRARY:
  2140. return "com.apple.product-type.library.static";
  2141. case cmStateEnums::MODULE_LIBRARY:
  2142. if (target->IsXCTestOnApple())
  2143. return "com.apple.product-type.bundle.unit-test";
  2144. else if (target->IsCFBundleOnApple())
  2145. return "com.apple.product-type.bundle";
  2146. else
  2147. return ((this->XcodeVersion >= 22)
  2148. ? "com.apple.product-type.tool"
  2149. : "com.apple.product-type.library.dynamic");
  2150. case cmStateEnums::SHARED_LIBRARY:
  2151. return (target->GetPropertyAsBool("FRAMEWORK")
  2152. ? "com.apple.product-type.framework"
  2153. : "com.apple.product-type.library.dynamic");
  2154. case cmStateEnums::EXECUTABLE:
  2155. return (target->GetPropertyAsBool("MACOSX_BUNDLE")
  2156. ? "com.apple.product-type.application"
  2157. : "com.apple.product-type.tool");
  2158. default:
  2159. break;
  2160. }
  2161. return 0;
  2162. }
  2163. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeTarget(
  2164. cmGeneratorTarget* gtgt, cmXCodeObject* buildPhases)
  2165. {
  2166. if (gtgt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2167. return 0;
  2168. }
  2169. cmXCodeObject* target = this->CreateObject(cmXCodeObject::PBXNativeTarget);
  2170. target->AddAttribute("buildPhases", buildPhases);
  2171. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2172. target->AddAttribute("buildRules", buildRules);
  2173. std::string defConfig;
  2174. if (this->XcodeVersion > 20) {
  2175. defConfig = this->AddConfigurations(target, gtgt);
  2176. } else {
  2177. cmXCodeObject* buildSettings =
  2178. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2179. defConfig = this->CurrentMakefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2180. this->CreateBuildSettings(gtgt, buildSettings, defConfig);
  2181. target->AddAttribute("buildSettings", buildSettings);
  2182. }
  2183. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2184. target->AddAttribute("dependencies", dependencies);
  2185. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2186. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2187. cmXCodeObject* fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  2188. if (const char* fileType = this->GetTargetFileType(gtgt)) {
  2189. fileRef->AddAttribute("explicitFileType", this->CreateString(fileType));
  2190. }
  2191. std::string fullName;
  2192. if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  2193. fullName = "lib";
  2194. fullName += gtgt->GetName();
  2195. fullName += ".a";
  2196. } else {
  2197. fullName = gtgt->GetFullName(defConfig);
  2198. }
  2199. fileRef->AddAttribute("path", this->CreateString(fullName));
  2200. if (this->XcodeVersion == 15) {
  2201. fileRef->AddAttribute("refType", this->CreateString("0"));
  2202. }
  2203. fileRef->AddAttribute("sourceTree",
  2204. this->CreateString("BUILT_PRODUCTS_DIR"));
  2205. fileRef->SetComment(gtgt->GetName());
  2206. target->AddAttribute("productReference",
  2207. this->CreateObjectReference(fileRef));
  2208. if (const char* productType = this->GetTargetProductType(gtgt)) {
  2209. target->AddAttribute("productType", this->CreateString(productType));
  2210. }
  2211. target->SetTarget(gtgt);
  2212. this->XCodeObjectMap[gtgt] = target;
  2213. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2214. return target;
  2215. }
  2216. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(
  2217. cmGeneratorTarget const* t)
  2218. {
  2219. if (!t) {
  2220. return 0;
  2221. }
  2222. std::map<cmGeneratorTarget const*, cmXCodeObject*>::const_iterator const i =
  2223. this->XCodeObjectMap.find(t);
  2224. if (i == this->XCodeObjectMap.end()) {
  2225. return 0;
  2226. }
  2227. return i->second;
  2228. }
  2229. std::string cmGlobalXCodeGenerator::GetOrCreateId(const std::string& name,
  2230. const std::string& id)
  2231. {
  2232. std::string guidStoreName = name;
  2233. guidStoreName += "_GUID_CMAKE";
  2234. const char* storedGUID =
  2235. this->CMakeInstance->GetCacheDefinition(guidStoreName);
  2236. if (storedGUID) {
  2237. return storedGUID;
  2238. }
  2239. this->CMakeInstance->AddCacheEntry(guidStoreName, id.c_str(),
  2240. "Stored Xcode object GUID",
  2241. cmStateEnums::INTERNAL);
  2242. return id;
  2243. }
  2244. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  2245. cmXCodeObject* dependTarget)
  2246. {
  2247. // This is called once for every edge in the target dependency graph.
  2248. cmXCodeObject* container =
  2249. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  2250. container->SetComment("PBXContainerItemProxy");
  2251. container->AddAttribute("containerPortal",
  2252. this->CreateObjectReference(this->RootObject));
  2253. container->AddAttribute("proxyType", this->CreateString("1"));
  2254. container->AddAttribute("remoteGlobalIDString",
  2255. this->CreateObjectReference(dependTarget));
  2256. container->AddAttribute(
  2257. "remoteInfo", this->CreateString(dependTarget->GetTarget()->GetName()));
  2258. cmXCodeObject* targetdep =
  2259. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  2260. targetdep->SetComment("PBXTargetDependency");
  2261. targetdep->AddAttribute("target", this->CreateObjectReference(dependTarget));
  2262. targetdep->AddAttribute("targetProxy",
  2263. this->CreateObjectReference(container));
  2264. cmXCodeObject* depends = target->GetObject("dependencies");
  2265. if (!depends) {
  2266. cmSystemTools::Error(
  2267. "target does not have dependencies attribute error..");
  2268. } else {
  2269. depends->AddUniqueObject(targetdep);
  2270. }
  2271. }
  2272. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  2273. const char* attribute,
  2274. const char* value)
  2275. {
  2276. if (settings) {
  2277. cmXCodeObject* attr = settings->GetObject(attribute);
  2278. if (!attr) {
  2279. settings->AddAttribute(attribute, this->CreateString(value));
  2280. } else {
  2281. std::string oldValue = attr->GetString();
  2282. oldValue += " ";
  2283. oldValue += value;
  2284. attr->SetString(oldValue);
  2285. }
  2286. }
  2287. }
  2288. void cmGlobalXCodeGenerator::AppendBuildSettingAttribute(
  2289. cmXCodeObject* target, const char* attribute, const char* value,
  2290. const std::string& configName)
  2291. {
  2292. if (this->XcodeVersion < 21) {
  2293. // There is only one configuration. Add the setting to the buildSettings
  2294. // of the target.
  2295. this->AppendOrAddBuildSetting(target->GetObject("buildSettings"),
  2296. attribute, value);
  2297. } else {
  2298. // There are multiple configurations. Add the setting to the
  2299. // buildSettings of the configuration name given.
  2300. cmXCodeObject* configurationList =
  2301. target->GetObject("buildConfigurationList")->GetObject();
  2302. cmXCodeObject* buildConfigs =
  2303. configurationList->GetObject("buildConfigurations");
  2304. std::vector<cmXCodeObject*> list = buildConfigs->GetObjectList();
  2305. // each configuration and the target itself has a buildSettings in it
  2306. // list.push_back(target);
  2307. for (std::vector<cmXCodeObject*>::iterator i = list.begin();
  2308. i != list.end(); ++i) {
  2309. if (!configName.empty()) {
  2310. if ((*i)->GetObject("name")->GetString() == configName) {
  2311. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2312. this->AppendOrAddBuildSetting(settings, attribute, value);
  2313. }
  2314. } else {
  2315. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2316. this->AppendOrAddBuildSetting(settings, attribute, value);
  2317. }
  2318. }
  2319. }
  2320. }
  2321. void cmGlobalXCodeGenerator::AddDependAndLinkInformation(cmXCodeObject* target)
  2322. {
  2323. cmGeneratorTarget* gt = target->GetTarget();
  2324. if (!gt) {
  2325. cmSystemTools::Error("Error no target on xobject\n");
  2326. return;
  2327. }
  2328. if (gt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2329. return;
  2330. }
  2331. // Add dependencies on other CMake targets.
  2332. TargetDependSet const& deps = this->GetTargetDirectDepends(gt);
  2333. for (TargetDependSet::const_iterator i = deps.begin(); i != deps.end();
  2334. ++i) {
  2335. if (cmXCodeObject* dptarget = this->FindXCodeTarget(*i)) {
  2336. this->AddDependTarget(target, dptarget);
  2337. }
  2338. }
  2339. // Loop over configuration types and set per-configuration info.
  2340. for (std::vector<std::string>::iterator i =
  2341. this->CurrentConfigurationTypes.begin();
  2342. i != this->CurrentConfigurationTypes.end(); ++i) {
  2343. // Get the current configuration name.
  2344. std::string configName = *i;
  2345. if (this->XcodeVersion >= 50) {
  2346. // Add object library contents as link flags.
  2347. std::string linkObjs;
  2348. const char* sep = "";
  2349. std::vector<std::string> objs;
  2350. gt->UseObjectLibraries(objs, "");
  2351. for (std::vector<std::string>::const_iterator oi = objs.begin();
  2352. oi != objs.end(); ++oi) {
  2353. linkObjs += sep;
  2354. sep = " ";
  2355. linkObjs += this->XCodeEscapePath(*oi);
  2356. }
  2357. this->AppendBuildSettingAttribute(
  2358. target, this->GetTargetLinkFlagsVar(gt), linkObjs.c_str(), configName);
  2359. }
  2360. // Skip link information for object libraries.
  2361. if (gt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  2362. gt->GetType() == cmStateEnums::STATIC_LIBRARY) {
  2363. continue;
  2364. }
  2365. // Compute the link library and directory information.
  2366. cmComputeLinkInformation* pcli = gt->GetLinkInformation(configName);
  2367. if (!pcli) {
  2368. continue;
  2369. }
  2370. cmComputeLinkInformation& cli = *pcli;
  2371. // Add dependencies directly on library files.
  2372. {
  2373. std::vector<std::string> const& libDeps = cli.GetDepends();
  2374. for (std::vector<std::string>::const_iterator j = libDeps.begin();
  2375. j != libDeps.end(); ++j) {
  2376. target->AddDependLibrary(configName, *j);
  2377. }
  2378. }
  2379. // add the library search paths
  2380. {
  2381. std::vector<std::string> const& libDirs = cli.GetDirectories();
  2382. std::string linkDirs;
  2383. for (std::vector<std::string>::const_iterator libDir = libDirs.begin();
  2384. libDir != libDirs.end(); ++libDir) {
  2385. if (libDir->size() && *libDir != "/usr/lib") {
  2386. if (this->XcodeVersion > 15) {
  2387. // Now add the same one but append
  2388. // $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) to it:
  2389. linkDirs += " ";
  2390. linkDirs += this->XCodeEscapePath(
  2391. *libDir + "/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)");
  2392. }
  2393. linkDirs += " ";
  2394. linkDirs += this->XCodeEscapePath(*libDir);
  2395. }
  2396. }
  2397. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  2398. linkDirs.c_str(), configName);
  2399. }
  2400. // now add the link libraries
  2401. {
  2402. std::string linkLibs;
  2403. const char* sep = "";
  2404. typedef cmComputeLinkInformation::ItemVector ItemVector;
  2405. ItemVector const& libNames = cli.GetItems();
  2406. for (ItemVector::const_iterator li = libNames.begin();
  2407. li != libNames.end(); ++li) {
  2408. linkLibs += sep;
  2409. sep = " ";
  2410. if (li->IsPath) {
  2411. linkLibs += this->XCodeEscapePath(li->Value);
  2412. } else if (!li->Target ||
  2413. li->Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  2414. linkLibs += li->Value;
  2415. }
  2416. if (li->Target && !li->Target->IsImported()) {
  2417. target->AddDependTarget(configName, li->Target->GetName());
  2418. }
  2419. }
  2420. this->AppendBuildSettingAttribute(
  2421. target, this->GetTargetLinkFlagsVar(gt), linkLibs.c_str(), configName);
  2422. }
  2423. }
  2424. }
  2425. bool cmGlobalXCodeGenerator::CreateGroups(
  2426. std::vector<cmLocalGenerator*>& generators)
  2427. {
  2428. for (std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2429. i != generators.end(); ++i) {
  2430. cmMakefile* mf = (*i)->GetMakefile();
  2431. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  2432. std::vector<cmGeneratorTarget*> tgts = (*i)->GetGeneratorTargets();
  2433. for (std::vector<cmGeneratorTarget*>::iterator l = tgts.begin();
  2434. l != tgts.end(); l++) {
  2435. cmGeneratorTarget* gtgt = *l;
  2436. // Same skipping logic here as in CreateXCodeTargets so that we do not
  2437. // end up with (empty anyhow) ALL_BUILD and XCODE_DEPEND_HELPER source
  2438. // groups:
  2439. //
  2440. if (gtgt->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2441. continue;
  2442. }
  2443. if (gtgt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2444. continue;
  2445. }
  2446. // add the soon to be generated Info.plist file as a source for a
  2447. // MACOSX_BUNDLE file
  2448. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  2449. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2450. mf->GetOrCreateSource(plist, true);
  2451. gtgt->AddSource(plist);
  2452. }
  2453. std::vector<cmSourceFile*> classes;
  2454. if (!gtgt->GetConfigCommonSourceFiles(classes)) {
  2455. return false;
  2456. }
  2457. // Put cmSourceFile instances in proper groups:
  2458. for (std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  2459. s != classes.end(); s++) {
  2460. cmSourceFile* sf = *s;
  2461. // Add the file to the list of sources.
  2462. std::string const& source = sf->GetFullPath();
  2463. cmSourceGroup* sourceGroup =
  2464. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2465. cmXCodeObject* pbxgroup = this->CreateOrGetPBXGroup(gtgt, sourceGroup);
  2466. std::string key = GetGroupMapKey(gtgt, sf);
  2467. this->GroupMap[key] = pbxgroup;
  2468. }
  2469. // Put OBJECT_LIBRARY objects in proper groups:
  2470. std::vector<std::string> objs;
  2471. gtgt->UseObjectLibraries(objs, "");
  2472. for (std::vector<std::string>::const_iterator oi = objs.begin();
  2473. oi != objs.end(); ++oi) {
  2474. std::string const& source = *oi;
  2475. cmSourceGroup* sourceGroup =
  2476. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2477. cmXCodeObject* pbxgroup = this->CreateOrGetPBXGroup(gtgt, sourceGroup);
  2478. std::string key = GetGroupMapKeyFromPath(gtgt, source);
  2479. this->GroupMap[key] = pbxgroup;
  2480. }
  2481. }
  2482. }
  2483. return true;
  2484. }
  2485. cmXCodeObject* cmGlobalXCodeGenerator::CreatePBXGroup(cmXCodeObject* parent,
  2486. std::string name)
  2487. {
  2488. cmXCodeObject* parentChildren = NULL;
  2489. if (parent)
  2490. parentChildren = parent->GetObject("children");
  2491. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  2492. cmXCodeObject* groupChildren =
  2493. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2494. group->AddAttribute("name", this->CreateString(name));
  2495. group->AddAttribute("children", groupChildren);
  2496. if (this->XcodeVersion == 15) {
  2497. group->AddAttribute("refType", this->CreateString("4"));
  2498. }
  2499. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  2500. if (parentChildren)
  2501. parentChildren->AddObject(group);
  2502. return group;
  2503. }
  2504. cmXCodeObject* cmGlobalXCodeGenerator::CreateOrGetPBXGroup(
  2505. cmGeneratorTarget* gtgt, cmSourceGroup* sg)
  2506. {
  2507. std::string s;
  2508. std::string target;
  2509. const std::string targetFolder = gtgt->GetEffectiveFolderName();
  2510. if (!targetFolder.empty()) {
  2511. target = targetFolder;
  2512. target += "/";
  2513. }
  2514. target += gtgt->GetName();
  2515. s = target + "/";
  2516. s += sg->GetFullName();
  2517. std::map<std::string, cmXCodeObject*>::iterator it =
  2518. this->GroupNameMap.find(s);
  2519. if (it != this->GroupNameMap.end()) {
  2520. return it->second;
  2521. }
  2522. it = this->TargetGroup.find(target);
  2523. cmXCodeObject* tgroup = 0;
  2524. if (it != this->TargetGroup.end()) {
  2525. tgroup = it->second;
  2526. } else {
  2527. std::vector<std::string> tgt_folders =
  2528. cmSystemTools::tokenize(target, "/");
  2529. std::string curr_tgt_folder;
  2530. for (std::vector<std::string>::size_type i = 0; i < tgt_folders.size();
  2531. i++) {
  2532. if (i != 0) {
  2533. curr_tgt_folder += "/";
  2534. }
  2535. curr_tgt_folder += tgt_folders[i];
  2536. it = this->TargetGroup.find(curr_tgt_folder);
  2537. if (it != this->TargetGroup.end()) {
  2538. tgroup = it->second;
  2539. continue;
  2540. }
  2541. tgroup = this->CreatePBXGroup(tgroup, tgt_folders[i]);
  2542. this->TargetGroup[curr_tgt_folder] = tgroup;
  2543. if (i == 0) {
  2544. this->SourcesGroupChildren->AddObject(tgroup);
  2545. }
  2546. }
  2547. }
  2548. this->TargetGroup[target] = tgroup;
  2549. // If it's the default source group (empty name) then put the source file
  2550. // directly in the tgroup...
  2551. //
  2552. if (std::string(sg->GetFullName()) == "") {
  2553. this->GroupNameMap[s] = tgroup;
  2554. return tgroup;
  2555. }
  2556. // It's a recursive folder structure, let's find the real parent group
  2557. if (std::string(sg->GetFullName()) != std::string(sg->GetName())) {
  2558. std::vector<std::string> folders =
  2559. cmSystemTools::tokenize(sg->GetFullName(), "\\");
  2560. std::string curr_folder = target;
  2561. curr_folder += "/";
  2562. for (std::vector<std::string>::size_type i = 0; i < folders.size(); i++) {
  2563. curr_folder += folders[i];
  2564. std::map<std::string, cmXCodeObject*>::iterator i_folder =
  2565. this->GroupNameMap.find(curr_folder);
  2566. // Create new folder
  2567. if (i_folder == this->GroupNameMap.end()) {
  2568. cmXCodeObject* group = this->CreatePBXGroup(tgroup, folders[i]);
  2569. this->GroupNameMap[curr_folder] = group;
  2570. tgroup = group;
  2571. } else {
  2572. tgroup = i_folder->second;
  2573. }
  2574. curr_folder = curr_folder + "\\";
  2575. }
  2576. return tgroup;
  2577. }
  2578. cmXCodeObject* group = this->CreatePBXGroup(tgroup, sg->GetName());
  2579. this->GroupNameMap[s] = group;
  2580. return group;
  2581. }
  2582. bool cmGlobalXCodeGenerator::CreateXCodeObjects(
  2583. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  2584. {
  2585. this->ClearXCodeObjects();
  2586. this->RootObject = 0;
  2587. this->SourcesGroupChildren = 0;
  2588. this->ResourcesGroupChildren = 0;
  2589. this->MainGroupChildren = 0;
  2590. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2591. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2592. cmXCodeObject* developBuildStyle =
  2593. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2594. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2595. if (this->XcodeVersion == 15) {
  2596. developBuildStyle->AddAttribute("name", this->CreateString("Development"));
  2597. developBuildStyle->AddAttribute("buildSettings", group);
  2598. listObjs->AddObject(developBuildStyle);
  2599. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2600. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("YES"));
  2601. cmXCodeObject* deployBuildStyle =
  2602. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2603. deployBuildStyle->AddAttribute("name", this->CreateString("Deployment"));
  2604. deployBuildStyle->AddAttribute("buildSettings", group);
  2605. listObjs->AddObject(deployBuildStyle);
  2606. } else {
  2607. for (unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i) {
  2608. cmXCodeObject* buildStyle =
  2609. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2610. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2611. buildStyle->AddAttribute("name", this->CreateString(name));
  2612. buildStyle->SetComment(name);
  2613. cmXCodeObject* sgroup =
  2614. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2615. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2616. buildStyle->AddAttribute("buildSettings", sgroup);
  2617. listObjs->AddObject(buildStyle);
  2618. }
  2619. }
  2620. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2621. this->MainGroupChildren = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2622. mainGroup->AddAttribute("children", this->MainGroupChildren);
  2623. if (this->XcodeVersion == 15) {
  2624. mainGroup->AddAttribute("refType", this->CreateString("4"));
  2625. }
  2626. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2627. cmXCodeObject* sourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2628. this->SourcesGroupChildren = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2629. sourcesGroup->AddAttribute("name", this->CreateString("Sources"));
  2630. sourcesGroup->AddAttribute("children", this->SourcesGroupChildren);
  2631. if (this->XcodeVersion == 15) {
  2632. sourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2633. }
  2634. sourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2635. this->MainGroupChildren->AddObject(sourcesGroup);
  2636. cmXCodeObject* resourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2637. this->ResourcesGroupChildren =
  2638. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2639. resourcesGroup->AddAttribute("name", this->CreateString("Resources"));
  2640. resourcesGroup->AddAttribute("children", this->ResourcesGroupChildren);
  2641. if (this->XcodeVersion == 15) {
  2642. resourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2643. }
  2644. resourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2645. this->MainGroupChildren->AddObject(resourcesGroup);
  2646. // now create the cmake groups
  2647. if (!this->CreateGroups(generators)) {
  2648. return false;
  2649. }
  2650. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2651. productGroup->AddAttribute("name", this->CreateString("Products"));
  2652. if (this->XcodeVersion == 15) {
  2653. productGroup->AddAttribute("refType", this->CreateString("4"));
  2654. }
  2655. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2656. cmXCodeObject* productGroupChildren =
  2657. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2658. productGroup->AddAttribute("children", productGroupChildren);
  2659. this->MainGroupChildren->AddObject(productGroup);
  2660. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  2661. this->RootObject->SetComment("Project object");
  2662. std::string project_id = "PROJECT_";
  2663. project_id += root->GetProjectName();
  2664. this->RootObject->SetId(
  2665. this->GetOrCreateId(project_id.c_str(), this->RootObject->GetId()));
  2666. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2667. this->RootObject->AddAttribute("mainGroup",
  2668. this->CreateObjectReference(mainGroup));
  2669. this->RootObject->AddAttribute("buildSettings", group);
  2670. this->RootObject->AddAttribute("buildStyles", listObjs);
  2671. this->RootObject->AddAttribute("hasScannedForEncodings",
  2672. this->CreateString("0"));
  2673. if (this->XcodeVersion >= 30) {
  2674. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2675. group->AddAttribute("BuildIndependentTargetsInParallel",
  2676. this->CreateString("YES"));
  2677. std::ostringstream v;
  2678. v << std::setfill('0') << std::setw(4) << XcodeVersion * 10;
  2679. group->AddAttribute("LastUpgradeCheck", this->CreateString(v.str()));
  2680. this->RootObject->AddAttribute("attributes", group);
  2681. if (this->XcodeVersion >= 32)
  2682. this->RootObject->AddAttribute("compatibilityVersion",
  2683. this->CreateString("Xcode 3.2"));
  2684. else if (this->XcodeVersion >= 31)
  2685. this->RootObject->AddAttribute("compatibilityVersion",
  2686. this->CreateString("Xcode 3.1"));
  2687. else
  2688. this->RootObject->AddAttribute("compatibilityVersion",
  2689. this->CreateString("Xcode 3.0"));
  2690. }
  2691. // Point Xcode at the top of the source tree.
  2692. {
  2693. std::string pdir =
  2694. this->RelativeToBinary(root->GetCurrentSourceDirectory());
  2695. this->RootObject->AddAttribute("projectDirPath", this->CreateString(pdir));
  2696. this->RootObject->AddAttribute("projectRoot", this->CreateString(""));
  2697. }
  2698. cmXCodeObject* configlist =
  2699. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2700. cmXCodeObject* buildConfigurations =
  2701. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2702. typedef std::vector<std::pair<std::string, cmXCodeObject*> > Configs;
  2703. Configs configs;
  2704. const char* defaultConfigName = "Debug";
  2705. if (this->XcodeVersion == 15) {
  2706. cmXCodeObject* configDebug =
  2707. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2708. configDebug->AddAttribute("name", this->CreateString("Debug"));
  2709. configs.push_back(std::make_pair("Debug", configDebug));
  2710. cmXCodeObject* configRelease =
  2711. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2712. configRelease->AddAttribute("name", this->CreateString("Release"));
  2713. configs.push_back(std::make_pair("Release", configRelease));
  2714. } else {
  2715. for (unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i) {
  2716. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2717. if (0 == i) {
  2718. defaultConfigName = name;
  2719. }
  2720. cmXCodeObject* config =
  2721. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2722. config->AddAttribute("name", this->CreateString(name));
  2723. configs.push_back(std::make_pair(name, config));
  2724. }
  2725. }
  2726. for (Configs::iterator c = configs.begin(); c != configs.end(); ++c) {
  2727. buildConfigurations->AddObject(c->second);
  2728. }
  2729. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2730. std::string comment = "Build configuration list for PBXProject";
  2731. comment += " \"";
  2732. comment += this->CurrentProject;
  2733. comment += "\"";
  2734. configlist->SetComment(comment);
  2735. configlist->AddAttribute("defaultConfigurationIsVisible",
  2736. this->CreateString("0"));
  2737. configlist->AddAttribute("defaultConfigurationName",
  2738. this->CreateString(defaultConfigName));
  2739. cmXCodeObject* buildSettings =
  2740. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2741. const char* osxArch =
  2742. this->CurrentMakefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  2743. const char* sysroot =
  2744. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  2745. const char* deploymentTarget =
  2746. this->CurrentMakefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  2747. std::string archs;
  2748. if (sysroot) {
  2749. if (osxArch) {
  2750. // recompute this as it may have been changed since enable language
  2751. this->Architectures.clear();
  2752. cmSystemTools::ExpandListArgument(std::string(osxArch),
  2753. this->Architectures);
  2754. archs = cmJoin(this->Architectures, " ");
  2755. }
  2756. buildSettings->AddAttribute("SDKROOT", this->CreateString(sysroot));
  2757. }
  2758. if (archs.empty()) {
  2759. // Tell Xcode to use NATIVE_ARCH instead of ARCHS.
  2760. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH", this->CreateString("YES"));
  2761. } else {
  2762. // Tell Xcode to use ARCHS (ONLY_ACTIVE_ARCH defaults to NO).
  2763. buildSettings->AddAttribute("ARCHS", this->CreateString(archs));
  2764. }
  2765. if (deploymentTarget && *deploymentTarget) {
  2766. buildSettings->AddAttribute("MACOSX_DEPLOYMENT_TARGET",
  2767. this->CreateString(deploymentTarget));
  2768. }
  2769. if (!this->GeneratorToolset.empty()) {
  2770. buildSettings->AddAttribute("GCC_VERSION",
  2771. this->CreateString(this->GeneratorToolset));
  2772. }
  2773. if (this->GetLanguageEnabled("Swift")) {
  2774. std::string swiftVersion = "2.3";
  2775. if (const char* vers = this->CurrentMakefile->GetDefinition(
  2776. "CMAKE_Swift_LANGUAGE_VERSION")) {
  2777. swiftVersion = vers;
  2778. }
  2779. buildSettings->AddAttribute("SWIFT_VERSION",
  2780. this->CreateString(swiftVersion));
  2781. }
  2782. std::string symroot = root->GetCurrentBinaryDirectory();
  2783. symroot += "/build";
  2784. buildSettings->AddAttribute("SYMROOT", this->CreateString(symroot));
  2785. for (Configs::iterator i = configs.begin(); i != configs.end(); ++i) {
  2786. cmXCodeObject* buildSettingsForCfg = this->CreateFlatClone(buildSettings);
  2787. // Put this last so it can override existing settings
  2788. // Convert "CMAKE_XCODE_ATTRIBUTE_*" variables directly.
  2789. std::vector<std::string> vars = this->CurrentMakefile->GetDefinitions();
  2790. for (std::vector<std::string>::const_iterator d = vars.begin();
  2791. d != vars.end(); ++d) {
  2792. if (d->find("CMAKE_XCODE_ATTRIBUTE_") == 0) {
  2793. std::string attribute = d->substr(22);
  2794. this->FilterConfigurationAttribute(i->first, attribute);
  2795. if (!attribute.empty()) {
  2796. cmGeneratorExpression ge;
  2797. std::string processed =
  2798. ge.Parse(this->CurrentMakefile->GetDefinition(*d))
  2799. ->Evaluate(this->CurrentLocalGenerator, i->first);
  2800. buildSettingsForCfg->AddAttribute(attribute,
  2801. this->CreateString(processed));
  2802. }
  2803. }
  2804. }
  2805. // store per-config buildSettings into configuration object
  2806. i->second->AddAttribute("buildSettings", buildSettingsForCfg);
  2807. }
  2808. this->RootObject->AddAttribute("buildConfigurationList",
  2809. this->CreateObjectReference(configlist));
  2810. std::vector<cmXCodeObject*> targets;
  2811. for (std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2812. i != generators.end(); ++i) {
  2813. if (!this->CreateXCodeTargets(*i, targets)) {
  2814. return false;
  2815. }
  2816. }
  2817. // loop over all targets and add link and depend info
  2818. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2819. i != targets.end(); ++i) {
  2820. cmXCodeObject* t = *i;
  2821. this->AddDependAndLinkInformation(t);
  2822. }
  2823. if (this->XcodeVersion < 50) {
  2824. // now create xcode depend hack makefile
  2825. this->CreateXCodeDependHackTarget(targets);
  2826. }
  2827. // now add all targets to the root object
  2828. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2829. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2830. i != targets.end(); ++i) {
  2831. cmXCodeObject* t = *i;
  2832. allTargets->AddObject(t);
  2833. cmXCodeObject* productRef = t->GetObject("productReference");
  2834. if (productRef) {
  2835. productGroupChildren->AddObject(productRef->GetObject());
  2836. }
  2837. }
  2838. this->RootObject->AddAttribute("targets", allTargets);
  2839. return true;
  2840. }
  2841. std::string cmGlobalXCodeGenerator::GetObjectsNormalDirectory(
  2842. const std::string& projName, const std::string& configName,
  2843. const cmGeneratorTarget* t) const
  2844. {
  2845. std::string dir = t->GetLocalGenerator()->GetCurrentBinaryDirectory();
  2846. dir += "/";
  2847. dir += projName;
  2848. dir += ".build/";
  2849. dir += configName;
  2850. dir += "/";
  2851. dir += t->GetName();
  2852. dir += ".build/Objects-normal/";
  2853. return dir;
  2854. }
  2855. void cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  2856. std::vector<cmXCodeObject*>& targets)
  2857. {
  2858. cmGeneratedFileStream makefileStream(this->CurrentXCodeHackMakefile.c_str());
  2859. if (!makefileStream) {
  2860. cmSystemTools::Error("Could not create",
  2861. this->CurrentXCodeHackMakefile.c_str());
  2862. return;
  2863. }
  2864. makefileStream.SetCopyIfDifferent(true);
  2865. // one more pass for external depend information not handled
  2866. // correctly by xcode
  2867. /* clang-format off */
  2868. makefileStream << "# DO NOT EDIT\n";
  2869. makefileStream << "# This makefile makes sure all linkable targets are\n";
  2870. makefileStream << "# up-to-date with anything they link to\n"
  2871. "default:\n"
  2872. "\techo \"Do not invoke directly\"\n"
  2873. "\n";
  2874. makefileStream
  2875. << "# For each target create a dummy rule "
  2876. "so the target does not have to exist\n";
  2877. /* clang-format on */
  2878. std::set<std::string> emitted;
  2879. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2880. i != targets.end(); ++i) {
  2881. cmXCodeObject* target = *i;
  2882. std::map<std::string, cmXCodeObject::StringVec> const& deplibs =
  2883. target->GetDependLibraries();
  2884. for (std::map<std::string, cmXCodeObject::StringVec>::const_iterator ci =
  2885. deplibs.begin();
  2886. ci != deplibs.end(); ++ci) {
  2887. for (cmXCodeObject::StringVec::const_iterator d = ci->second.begin();
  2888. d != ci->second.end(); ++d) {
  2889. if (emitted.insert(*d).second) {
  2890. makefileStream << this->ConvertToRelativeForMake(d->c_str())
  2891. << ":\n";
  2892. }
  2893. }
  2894. }
  2895. }
  2896. makefileStream << "\n\n";
  2897. // Write rules to help Xcode relink things at the right time.
  2898. /* clang-format off */
  2899. makefileStream <<
  2900. "# Rules to remove targets that are older than anything to which they\n"
  2901. "# link. This forces Xcode to relink the targets from scratch. It\n"
  2902. "# does not seem to check these dependencies itself.\n";
  2903. /* clang-format on */
  2904. for (std::vector<std::string>::const_iterator ct =
  2905. this->CurrentConfigurationTypes.begin();
  2906. ct != this->CurrentConfigurationTypes.end(); ++ct) {
  2907. std::string configName = *ct;
  2908. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2909. i != targets.end(); ++i) {
  2910. cmXCodeObject* target = *i;
  2911. cmGeneratorTarget* gt = target->GetTarget();
  2912. if (gt->GetType() == cmStateEnums::EXECUTABLE ||
  2913. // Nope - no post-build for OBJECT_LIRBRARY
  2914. // gt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  2915. gt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2916. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2917. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2918. // Declare an entry point for the target post-build phase.
  2919. makefileStream << this->PostBuildMakeTarget(gt->GetName(), *ct)
  2920. << ":\n";
  2921. }
  2922. if (gt->GetType() == cmStateEnums::EXECUTABLE ||
  2923. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2924. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2925. std::string tfull = gt->GetFullPath(configName);
  2926. std::string trel = this->ConvertToRelativeForMake(tfull.c_str());
  2927. // Add this target to the post-build phases of its dependencies.
  2928. std::map<std::string, cmXCodeObject::StringVec>::const_iterator y =
  2929. target->GetDependTargets().find(*ct);
  2930. if (y != target->GetDependTargets().end()) {
  2931. std::vector<std::string> const& deptgts = y->second;
  2932. for (std::vector<std::string>::const_iterator d = deptgts.begin();
  2933. d != deptgts.end(); ++d) {
  2934. makefileStream << this->PostBuildMakeTarget(*d, *ct) << ": "
  2935. << trel << "\n";
  2936. }
  2937. }
  2938. // Create a rule for this target.
  2939. makefileStream << trel << ":";
  2940. // List dependencies if any exist.
  2941. std::map<std::string, cmXCodeObject::StringVec>::const_iterator x =
  2942. target->GetDependLibraries().find(*ct);
  2943. if (x != target->GetDependLibraries().end()) {
  2944. std::vector<std::string> const& deplibs = x->second;
  2945. for (std::vector<std::string>::const_iterator d = deplibs.begin();
  2946. d != deplibs.end(); ++d) {
  2947. makefileStream << "\\\n\t"
  2948. << this->ConvertToRelativeForMake(d->c_str());
  2949. }
  2950. }
  2951. // Write the action to remove the target if it is out of date.
  2952. makefileStream << "\n";
  2953. makefileStream << "\t/bin/rm -f "
  2954. << this->ConvertToRelativeForMake(tfull.c_str())
  2955. << "\n";
  2956. // if building for more than one architecture
  2957. // then remove those exectuables as well
  2958. if (this->Architectures.size() > 1) {
  2959. std::string universal = this->GetObjectsNormalDirectory(
  2960. this->CurrentProject, configName, gt);
  2961. for (std::vector<std::string>::iterator arch =
  2962. this->Architectures.begin();
  2963. arch != this->Architectures.end(); ++arch) {
  2964. std::string universalFile = universal;
  2965. universalFile += *arch;
  2966. universalFile += "/";
  2967. universalFile += gt->GetFullName(configName);
  2968. makefileStream << "\t/bin/rm -f "
  2969. << this->ConvertToRelativeForMake(
  2970. universalFile.c_str())
  2971. << "\n";
  2972. }
  2973. }
  2974. makefileStream << "\n\n";
  2975. }
  2976. }
  2977. }
  2978. }
  2979. void cmGlobalXCodeGenerator::OutputXCodeProject(
  2980. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  2981. {
  2982. if (generators.size() == 0) {
  2983. return;
  2984. }
  2985. // Skip local generators that are excluded from this project.
  2986. for (std::vector<cmLocalGenerator*>::iterator g = generators.begin();
  2987. g != generators.end(); ++g) {
  2988. if (this->IsExcluded(root, *g)) {
  2989. continue;
  2990. }
  2991. }
  2992. if (!this->CreateXCodeObjects(root, generators)) {
  2993. return;
  2994. }
  2995. std::string xcodeDir = root->GetCurrentBinaryDirectory();
  2996. xcodeDir += "/";
  2997. xcodeDir += root->GetProjectName();
  2998. xcodeDir += ".xcode";
  2999. if (this->XcodeVersion > 20) {
  3000. xcodeDir += "proj";
  3001. }
  3002. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  3003. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  3004. cmGeneratedFileStream fout(xcodeProjFile.c_str());
  3005. fout.SetCopyIfDifferent(true);
  3006. if (!fout) {
  3007. return;
  3008. }
  3009. this->WriteXCodePBXProj(fout, root, generators);
  3010. this->ClearXCodeObjects();
  3011. // Since this call may have created new cache entries, save the cache:
  3012. //
  3013. root->GetMakefile()->GetCMakeInstance()->SaveCache(
  3014. root->GetBinaryDirectory());
  3015. }
  3016. void cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  3017. cmLocalGenerator*,
  3018. std::vector<cmLocalGenerator*>&)
  3019. {
  3020. SortXCodeObjects();
  3021. fout << "// !$*UTF8*$!\n";
  3022. fout << "{\n";
  3023. cmXCodeObject::Indent(1, fout);
  3024. fout << "archiveVersion = 1;\n";
  3025. cmXCodeObject::Indent(1, fout);
  3026. fout << "classes = {\n";
  3027. cmXCodeObject::Indent(1, fout);
  3028. fout << "};\n";
  3029. cmXCodeObject::Indent(1, fout);
  3030. if (this->XcodeVersion >= 21) {
  3031. if (this->XcodeVersion >= 32)
  3032. fout << "objectVersion = 46;\n";
  3033. else if (this->XcodeVersion >= 31)
  3034. fout << "objectVersion = 45;\n";
  3035. else if (this->XcodeVersion >= 30)
  3036. fout << "objectVersion = 44;\n";
  3037. else
  3038. fout << "objectVersion = 42;\n";
  3039. cmXCode21Object::PrintList(this->XCodeObjects, fout);
  3040. } else {
  3041. fout << "objectVersion = 39;\n";
  3042. cmXCodeObject::PrintList(this->XCodeObjects, fout);
  3043. }
  3044. cmXCodeObject::Indent(1, fout);
  3045. fout << "rootObject = " << this->RootObject->GetId()
  3046. << " /* Project object */;\n";
  3047. fout << "}\n";
  3048. }
  3049. const char* cmGlobalXCodeGenerator::GetCMakeCFGIntDir() const
  3050. {
  3051. return this->XcodeVersion >= 21
  3052. ? "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)"
  3053. : ".";
  3054. }
  3055. std::string cmGlobalXCodeGenerator::ExpandCFGIntDir(
  3056. const std::string& str, const std::string& config) const
  3057. {
  3058. std::string replace1 = "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)";
  3059. std::string replace2 = "$(CONFIGURATION)";
  3060. std::string tmp = str;
  3061. for (std::string::size_type i = tmp.find(replace1); i != std::string::npos;
  3062. i = tmp.find(replace1, i)) {
  3063. tmp.replace(i, replace1.size(), config);
  3064. i += config.size();
  3065. }
  3066. for (std::string::size_type i = tmp.find(replace2); i != std::string::npos;
  3067. i = tmp.find(replace2, i)) {
  3068. tmp.replace(i, replace2.size(), config);
  3069. i += config.size();
  3070. }
  3071. return tmp;
  3072. }
  3073. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  3074. {
  3075. entry.Name = cmGlobalXCodeGenerator::GetActualName();
  3076. entry.Brief = "Generate Xcode project files.";
  3077. }
  3078. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(const char* p)
  3079. {
  3080. return cmSystemTools::ConvertToOutputPath(p);
  3081. }
  3082. std::string cmGlobalXCodeGenerator::RelativeToSource(const char* p)
  3083. {
  3084. // We force conversion because Xcode breakpoints do not work unless
  3085. // they are in a file named relative to the source tree.
  3086. return cmOutputConverter::ForceToRelativePath(
  3087. cmSystemTools::JoinPath(this->ProjectSourceDirectoryComponents), p);
  3088. }
  3089. std::string cmGlobalXCodeGenerator::RelativeToBinary(const char* p)
  3090. {
  3091. return this->CurrentLocalGenerator->ConvertToRelativePath(
  3092. cmSystemTools::JoinPath(this->ProjectOutputDirectoryComponents), p);
  3093. }
  3094. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const std::string& p)
  3095. {
  3096. if (p.find(' ') != p.npos) {
  3097. std::string t = "\"";
  3098. t += p;
  3099. t += "\"";
  3100. return t;
  3101. }
  3102. return p;
  3103. }
  3104. void cmGlobalXCodeGenerator::AppendDirectoryForConfig(
  3105. const std::string& prefix, const std::string& config,
  3106. const std::string& suffix, std::string& dir)
  3107. {
  3108. if (this->XcodeVersion > 20) {
  3109. if (!config.empty()) {
  3110. dir += prefix;
  3111. dir += config;
  3112. dir += suffix;
  3113. }
  3114. }
  3115. }
  3116. std::string cmGlobalXCodeGenerator::LookupFlags(
  3117. const std::string& varNamePrefix, const std::string& varNameLang,
  3118. const std::string& varNameSuffix, const std::string& default_flags)
  3119. {
  3120. if (!varNameLang.empty()) {
  3121. std::string varName = varNamePrefix;
  3122. varName += varNameLang;
  3123. varName += varNameSuffix;
  3124. if (const char* varValue = this->CurrentMakefile->GetDefinition(varName)) {
  3125. if (*varValue) {
  3126. return varValue;
  3127. }
  3128. }
  3129. }
  3130. return default_flags;
  3131. }
  3132. void cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  3133. const char* defines_list,
  3134. bool dflag)
  3135. {
  3136. // Skip this if there are no definitions.
  3137. if (!defines_list) {
  3138. return;
  3139. }
  3140. // Expand the list of definitions.
  3141. std::vector<std::string> defines;
  3142. cmSystemTools::ExpandListArgument(defines_list, defines);
  3143. // Store the definitions in the string.
  3144. this->AppendDefines(defs, defines, dflag);
  3145. }
  3146. void cmGlobalXCodeGenerator::AppendDefines(
  3147. BuildObjectListOrString& defs, std::vector<std::string> const& defines,
  3148. bool dflag)
  3149. {
  3150. // GCC_PREPROCESSOR_DEFINITIONS is a space-separated list of definitions.
  3151. std::string def;
  3152. for (std::vector<std::string>::const_iterator di = defines.begin();
  3153. di != defines.end(); ++di) {
  3154. // Start with -D if requested.
  3155. def = dflag ? "-D" : "";
  3156. def += *di;
  3157. // Append the flag with needed escapes.
  3158. std::string tmp;
  3159. this->AppendFlag(tmp, def);
  3160. defs.Add(tmp);
  3161. }
  3162. }
  3163. void cmGlobalXCodeGenerator::AppendFlag(std::string& flags,
  3164. std::string const& flag)
  3165. {
  3166. // Short-circuit for an empty flag.
  3167. if (flag.empty()) {
  3168. return;
  3169. }
  3170. // Separate from previous flags.
  3171. if (!flags.empty()) {
  3172. flags += " ";
  3173. }
  3174. // Check if the flag needs quoting.
  3175. bool quoteFlag =
  3176. flag.find_first_of("`~!@#$%^&*()+={}[]|:;\"'<>,.? ") != flag.npos;
  3177. // We escape a flag as follows:
  3178. // - Place each flag in single quotes ''
  3179. // - Escape a single quote as \'
  3180. // - Escape a backslash as \\ since it itself is an escape
  3181. // Note that in the code below we need one more level of escapes for
  3182. // C string syntax in this source file.
  3183. //
  3184. // The final level of escaping is done when the string is stored
  3185. // into the project file by cmXCodeObject::PrintString.
  3186. if (quoteFlag) {
  3187. // Open single quote.
  3188. flags += "'";
  3189. }
  3190. // Flag value with escaped quotes and backslashes.
  3191. for (const char* c = flag.c_str(); *c; ++c) {
  3192. if (*c == '\'') {
  3193. if (this->XcodeVersion >= 40) {
  3194. flags += "'\\''";
  3195. } else {
  3196. flags += "\\'";
  3197. }
  3198. } else if (*c == '\\') {
  3199. flags += "\\\\";
  3200. } else {
  3201. flags += *c;
  3202. }
  3203. }
  3204. if (quoteFlag) {
  3205. // Close single quote.
  3206. flags += "'";
  3207. }
  3208. }
  3209. std::string cmGlobalXCodeGenerator::ComputeInfoPListLocation(
  3210. cmGeneratorTarget* target)
  3211. {
  3212. std::string plist = target->GetLocalGenerator()->GetCurrentBinaryDirectory();
  3213. plist += cmake::GetCMakeFilesDirectory();
  3214. plist += "/";
  3215. plist += target->GetName();
  3216. plist += ".dir/Info.plist";
  3217. return plist;
  3218. }
  3219. // Return true if the generated build tree may contain multiple builds.
  3220. // i.e. "Can I build Debug and Release in the same tree?"
  3221. bool cmGlobalXCodeGenerator::IsMultiConfig() const
  3222. {
  3223. // Old Xcode 1.5 is single config:
  3224. if (this->XcodeVersion == 15) {
  3225. return false;
  3226. }
  3227. // Newer Xcode versions are multi config:
  3228. return true;
  3229. }
  3230. void cmGlobalXCodeGenerator::ComputeTargetObjectDirectory(
  3231. cmGeneratorTarget* gt) const
  3232. {
  3233. std::string configName = this->GetCMakeCFGIntDir();
  3234. std::string dir =
  3235. this->GetObjectsNormalDirectory("$(PROJECT_NAME)", configName, gt);
  3236. if (this->XcodeVersion >= 21) {
  3237. dir += "$(CURRENT_ARCH)/";
  3238. } else {
  3239. #ifdef __ppc__
  3240. dir += "ppc/";
  3241. #endif
  3242. #ifdef __i386
  3243. dir += "i386/";
  3244. #endif
  3245. }
  3246. gt->ObjectDirectory = dir;
  3247. }