cmGlobalXCodeGenerator.cxx 126 KB

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