cmGlobalXCodeGenerator.cxx 124 KB

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