cmGlobalXCodeGenerator.cxx 129 KB

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