cmGlobalXCodeGenerator.cxx 129 KB

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