cmGlobalXCodeGenerator.cxx 123 KB

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