cmGlobalXCodeGenerator.cxx 124 KB

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