cmLocalGenerator.cxx 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802
  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 "cmLocalGenerator.h"
  11. #include "cmAlgorithms.h"
  12. #include "cmComputeLinkInformation.h"
  13. #include "cmCustomCommandGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGeneratorExpressionEvaluationFile.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmInstallFilesGenerator.h"
  18. #include "cmInstallGenerator.h"
  19. #include "cmInstallScriptGenerator.h"
  20. #include "cmInstallTargetGenerator.h"
  21. #include "cmMakefile.h"
  22. #include "cmSourceFile.h"
  23. #include "cmTest.h"
  24. #include "cmTestGenerator.h"
  25. #include "cmVersion.h"
  26. #include "cmake.h"
  27. #if defined(CMAKE_BUILD_WITH_CMAKE)
  28. #define CM_LG_ENCODE_OBJECT_NAMES
  29. #include <cmsys/MD5.h>
  30. #endif
  31. #include <ctype.h> // for isalpha
  32. #include <assert.h>
  33. #if defined(__HAIKU__)
  34. #include <FindDirectory.h>
  35. #include <StorageDefs.h>
  36. #endif
  37. cmLocalGenerator::cmLocalGenerator(cmGlobalGenerator* gg, cmMakefile* makefile)
  38. : cmOutputConverter(makefile->GetStateSnapshot())
  39. , StateSnapshot(makefile->GetStateSnapshot())
  40. , DirectoryBacktrace(makefile->GetBacktrace())
  41. {
  42. this->GlobalGenerator = gg;
  43. this->Makefile = makefile;
  44. this->AliasTargets = makefile->GetAliasTargets();
  45. this->EmitUniversalBinaryFlags = true;
  46. this->BackwardsCompatibility = 0;
  47. this->BackwardsCompatibilityFinal = false;
  48. this->ComputeObjectMaxPath();
  49. }
  50. cmLocalGenerator::~cmLocalGenerator()
  51. {
  52. cmDeleteAll(this->GeneratorTargets);
  53. cmDeleteAll(this->OwnedImportedGeneratorTargets);
  54. }
  55. void cmLocalGenerator::IssueMessage(cmake::MessageType t,
  56. std::string const& text) const
  57. {
  58. this->GetCMakeInstance()->IssueMessage(t, text, this->DirectoryBacktrace);
  59. }
  60. void cmLocalGenerator::ComputeObjectMaxPath()
  61. {
  62. // Choose a maximum object file name length.
  63. #if defined(_WIN32) || defined(__CYGWIN__)
  64. this->ObjectPathMax = 250;
  65. #else
  66. this->ObjectPathMax = 1000;
  67. #endif
  68. const char* plen = this->Makefile->GetDefinition("CMAKE_OBJECT_PATH_MAX");
  69. if (plen && *plen) {
  70. unsigned int pmax;
  71. if (sscanf(plen, "%u", &pmax) == 1) {
  72. if (pmax >= 128) {
  73. this->ObjectPathMax = pmax;
  74. } else {
  75. std::ostringstream w;
  76. w << "CMAKE_OBJECT_PATH_MAX is set to " << pmax
  77. << ", which is less than the minimum of 128. "
  78. << "The value will be ignored.";
  79. this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  80. }
  81. } else {
  82. std::ostringstream w;
  83. w << "CMAKE_OBJECT_PATH_MAX is set to \"" << plen
  84. << "\", which fails to parse as a positive integer. "
  85. << "The value will be ignored.";
  86. this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  87. }
  88. }
  89. this->ObjectMaxPathViolations.clear();
  90. }
  91. void cmLocalGenerator::TraceDependencies()
  92. {
  93. std::vector<std::string> configs;
  94. this->Makefile->GetConfigurations(configs);
  95. if (configs.empty()) {
  96. configs.push_back("");
  97. }
  98. for (std::vector<std::string>::const_iterator ci = configs.begin();
  99. ci != configs.end(); ++ci) {
  100. this->GlobalGenerator->CreateEvaluationSourceFiles(*ci);
  101. }
  102. // Generate the rule files for each target.
  103. std::vector<cmGeneratorTarget*> targets = this->GetGeneratorTargets();
  104. for (std::vector<cmGeneratorTarget*>::iterator t = targets.begin();
  105. t != targets.end(); ++t) {
  106. if ((*t)->GetType() == cmState::INTERFACE_LIBRARY) {
  107. continue;
  108. }
  109. (*t)->TraceDependencies();
  110. }
  111. }
  112. void cmLocalGenerator::GenerateTestFiles()
  113. {
  114. if (!this->Makefile->IsOn("CMAKE_TESTING_ENABLED")) {
  115. return;
  116. }
  117. // Compute the set of configurations.
  118. std::vector<std::string> configurationTypes;
  119. const std::string& config =
  120. this->Makefile->GetConfigurations(configurationTypes, false);
  121. std::string file = this->StateSnapshot.GetDirectory().GetCurrentBinary();
  122. file += "/";
  123. file += "CTestTestfile.cmake";
  124. cmGeneratedFileStream fout(file.c_str());
  125. fout.SetCopyIfDifferent(true);
  126. fout << "# CMake generated Testfile for " << std::endl
  127. << "# Source directory: "
  128. << this->StateSnapshot.GetDirectory().GetCurrentSource() << std::endl
  129. << "# Build directory: "
  130. << this->StateSnapshot.GetDirectory().GetCurrentBinary() << std::endl
  131. << "# " << std::endl
  132. << "# This file includes the relevant testing commands "
  133. << "required for " << std::endl
  134. << "# testing this directory and lists subdirectories to "
  135. << "be tested as well." << std::endl;
  136. const char* testIncludeFile =
  137. this->Makefile->GetProperty("TEST_INCLUDE_FILE");
  138. if (testIncludeFile) {
  139. fout << "include(\"" << testIncludeFile << "\")" << std::endl;
  140. }
  141. // Ask each test generator to write its code.
  142. std::vector<cmTestGenerator*> const& testers =
  143. this->Makefile->GetTestGenerators();
  144. for (std::vector<cmTestGenerator*>::const_iterator gi = testers.begin();
  145. gi != testers.end(); ++gi) {
  146. (*gi)->Compute(this);
  147. (*gi)->Generate(fout, config, configurationTypes);
  148. }
  149. size_t i;
  150. std::vector<cmState::Snapshot> children =
  151. this->Makefile->GetStateSnapshot().GetChildren();
  152. for (i = 0; i < children.size(); ++i) {
  153. // TODO: Use add_subdirectory instead?
  154. fout << "subdirs(";
  155. std::string outP = children[i].GetDirectory().GetCurrentBinary();
  156. fout << this->ConvertToRelativePath(outP, START_OUTPUT);
  157. fout << ")" << std::endl;
  158. }
  159. }
  160. void cmLocalGenerator::CreateEvaluationFileOutputs(std::string const& config)
  161. {
  162. std::vector<cmGeneratorExpressionEvaluationFile*> ef =
  163. this->Makefile->GetEvaluationFiles();
  164. for (std::vector<cmGeneratorExpressionEvaluationFile*>::const_iterator li =
  165. ef.begin();
  166. li != ef.end(); ++li) {
  167. (*li)->CreateOutputFile(this, config);
  168. }
  169. }
  170. void cmLocalGenerator::ProcessEvaluationFiles(
  171. std::vector<std::string>& generatedFiles)
  172. {
  173. std::vector<cmGeneratorExpressionEvaluationFile*> ef =
  174. this->Makefile->GetEvaluationFiles();
  175. for (std::vector<cmGeneratorExpressionEvaluationFile*>::const_iterator li =
  176. ef.begin();
  177. li != ef.end(); ++li) {
  178. (*li)->Generate(this);
  179. if (cmSystemTools::GetFatalErrorOccured()) {
  180. return;
  181. }
  182. std::vector<std::string> files = (*li)->GetFiles();
  183. std::sort(files.begin(), files.end());
  184. std::vector<std::string> intersection;
  185. std::set_intersection(files.begin(), files.end(), generatedFiles.begin(),
  186. generatedFiles.end(),
  187. std::back_inserter(intersection));
  188. if (!intersection.empty()) {
  189. cmSystemTools::Error("Files to be generated by multiple different "
  190. "commands: ",
  191. cmWrap('"', intersection, '"', " ").c_str());
  192. return;
  193. }
  194. generatedFiles.insert(generatedFiles.end(), files.begin(), files.end());
  195. std::vector<std::string>::iterator newIt =
  196. generatedFiles.end() - files.size();
  197. std::inplace_merge(generatedFiles.begin(), newIt, generatedFiles.end());
  198. }
  199. }
  200. void cmLocalGenerator::GenerateInstallRules()
  201. {
  202. // Compute the install prefix.
  203. const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  204. #if defined(_WIN32) && !defined(__CYGWIN__)
  205. std::string prefix_win32;
  206. if (!prefix) {
  207. if (!cmSystemTools::GetEnv("SystemDrive", prefix_win32)) {
  208. prefix_win32 = "C:";
  209. }
  210. const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
  211. if (project_name && project_name[0]) {
  212. prefix_win32 += "/Program Files/";
  213. prefix_win32 += project_name;
  214. } else {
  215. prefix_win32 += "/InstalledCMakeProject";
  216. }
  217. prefix = prefix_win32.c_str();
  218. }
  219. #elif defined(__HAIKU__)
  220. char dir[B_PATH_NAME_LENGTH];
  221. if (!prefix) {
  222. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) ==
  223. B_OK) {
  224. prefix = dir;
  225. } else {
  226. prefix = "/boot/system";
  227. }
  228. }
  229. #else
  230. if (!prefix) {
  231. prefix = "/usr/local";
  232. }
  233. #endif
  234. if (const char* stagingPrefix =
  235. this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX")) {
  236. prefix = stagingPrefix;
  237. }
  238. // Compute the set of configurations.
  239. std::vector<std::string> configurationTypes;
  240. const std::string& config =
  241. this->Makefile->GetConfigurations(configurationTypes, false);
  242. // Choose a default install configuration.
  243. std::string default_config = config;
  244. const char* default_order[] = { "RELEASE", "MINSIZEREL", "RELWITHDEBINFO",
  245. "DEBUG", CM_NULLPTR };
  246. for (const char** c = default_order; *c && default_config.empty(); ++c) {
  247. for (std::vector<std::string>::iterator i = configurationTypes.begin();
  248. i != configurationTypes.end(); ++i) {
  249. if (cmSystemTools::UpperCase(*i) == *c) {
  250. default_config = *i;
  251. }
  252. }
  253. }
  254. if (default_config.empty() && !configurationTypes.empty()) {
  255. default_config = configurationTypes[0];
  256. }
  257. // Create the install script file.
  258. std::string file = this->StateSnapshot.GetDirectory().GetCurrentBinary();
  259. std::string homedir = this->GetState()->GetBinaryDirectory();
  260. int toplevel_install = 0;
  261. if (file == homedir) {
  262. toplevel_install = 1;
  263. }
  264. file += "/cmake_install.cmake";
  265. cmGeneratedFileStream fout(file.c_str());
  266. fout.SetCopyIfDifferent(true);
  267. // Write the header.
  268. fout << "# Install script for directory: "
  269. << this->StateSnapshot.GetDirectory().GetCurrentSource() << std::endl
  270. << std::endl;
  271. fout << "# Set the install prefix" << std::endl
  272. << "if(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  273. << " set(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
  274. << "endif()" << std::endl
  275. << "string(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
  276. << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
  277. << std::endl;
  278. // Write support code for generating per-configuration install rules.
  279. /* clang-format off */
  280. fout <<
  281. "# Set the install configuration name.\n"
  282. "if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  283. " if(BUILD_TYPE)\n"
  284. " string(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
  285. " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
  286. " else()\n"
  287. " set(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
  288. " endif()\n"
  289. " message(STATUS \"Install configuration: "
  290. "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
  291. "endif()\n"
  292. "\n";
  293. /* clang-format on */
  294. // Write support code for dealing with component-specific installs.
  295. /* clang-format off */
  296. fout <<
  297. "# Set the component getting installed.\n"
  298. "if(NOT CMAKE_INSTALL_COMPONENT)\n"
  299. " if(COMPONENT)\n"
  300. " message(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
  301. " set(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
  302. " else()\n"
  303. " set(CMAKE_INSTALL_COMPONENT)\n"
  304. " endif()\n"
  305. "endif()\n"
  306. "\n";
  307. /* clang-format on */
  308. // Copy user-specified install options to the install code.
  309. if (const char* so_no_exe =
  310. this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE")) {
  311. /* clang-format off */
  312. fout <<
  313. "# Install shared libraries without execute permission?\n"
  314. "if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  315. " set(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe << "\")\n"
  316. "endif()\n"
  317. "\n";
  318. /* clang-format on */
  319. }
  320. // Ask each install generator to write its code.
  321. std::vector<cmInstallGenerator*> const& installers =
  322. this->Makefile->GetInstallGenerators();
  323. for (std::vector<cmInstallGenerator*>::const_iterator gi =
  324. installers.begin();
  325. gi != installers.end(); ++gi) {
  326. (*gi)->Generate(fout, config, configurationTypes);
  327. }
  328. // Write rules from old-style specification stored in targets.
  329. this->GenerateTargetInstallRules(fout, config, configurationTypes);
  330. // Include install scripts from subdirectories.
  331. std::vector<cmState::Snapshot> children =
  332. this->Makefile->GetStateSnapshot().GetChildren();
  333. if (!children.empty()) {
  334. fout << "if(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
  335. fout << " # Include the install script for each subdirectory.\n";
  336. for (std::vector<cmState::Snapshot>::const_iterator ci = children.begin();
  337. ci != children.end(); ++ci) {
  338. if (!ci->GetDirectory().GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  339. std::string odir = ci->GetDirectory().GetCurrentBinary();
  340. cmSystemTools::ConvertToUnixSlashes(odir);
  341. fout << " include(\"" << odir << "/cmake_install.cmake\")"
  342. << std::endl;
  343. }
  344. }
  345. fout << "\n";
  346. fout << "endif()\n\n";
  347. }
  348. // Record the install manifest.
  349. if (toplevel_install) {
  350. /* clang-format off */
  351. fout <<
  352. "if(CMAKE_INSTALL_COMPONENT)\n"
  353. " set(CMAKE_INSTALL_MANIFEST \"install_manifest_"
  354. "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
  355. "else()\n"
  356. " set(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
  357. "endif()\n"
  358. "\n"
  359. "string(REPLACE \";\" \"\\n\" CMAKE_INSTALL_MANIFEST_CONTENT\n"
  360. " \"${CMAKE_INSTALL_MANIFEST_FILES}\")\n"
  361. "file(WRITE \"" << homedir << "/${CMAKE_INSTALL_MANIFEST}\"\n"
  362. " \"${CMAKE_INSTALL_MANIFEST_CONTENT}\")\n";
  363. /* clang-format on */
  364. }
  365. }
  366. void cmLocalGenerator::AddGeneratorTarget(cmGeneratorTarget* gt)
  367. {
  368. this->GeneratorTargets.push_back(gt);
  369. this->GlobalGenerator->IndexGeneratorTarget(gt);
  370. }
  371. void cmLocalGenerator::AddImportedGeneratorTarget(cmGeneratorTarget* gt)
  372. {
  373. this->ImportedGeneratorTargets.push_back(gt);
  374. this->GlobalGenerator->IndexGeneratorTarget(gt);
  375. }
  376. void cmLocalGenerator::AddOwnedImportedGeneratorTarget(cmGeneratorTarget* gt)
  377. {
  378. this->OwnedImportedGeneratorTargets.push_back(gt);
  379. }
  380. struct NamedGeneratorTargetFinder
  381. {
  382. NamedGeneratorTargetFinder(std::string const& name)
  383. : Name(name)
  384. {
  385. }
  386. bool operator()(cmGeneratorTarget* tgt)
  387. {
  388. return tgt->GetName() == this->Name;
  389. }
  390. private:
  391. std::string Name;
  392. };
  393. cmGeneratorTarget* cmLocalGenerator::FindLocalNonAliasGeneratorTarget(
  394. const std::string& name) const
  395. {
  396. std::vector<cmGeneratorTarget*>::const_iterator ti =
  397. std::find_if(this->GeneratorTargets.begin(), this->GeneratorTargets.end(),
  398. NamedGeneratorTargetFinder(name));
  399. if (ti != this->GeneratorTargets.end()) {
  400. return *ti;
  401. }
  402. return CM_NULLPTR;
  403. }
  404. void cmLocalGenerator::ComputeTargetManifest()
  405. {
  406. // Collect the set of configuration types.
  407. std::vector<std::string> configNames;
  408. this->Makefile->GetConfigurations(configNames);
  409. if (configNames.empty()) {
  410. configNames.push_back("");
  411. }
  412. // Add our targets to the manifest for each configuration.
  413. std::vector<cmGeneratorTarget*> targets = this->GetGeneratorTargets();
  414. for (std::vector<cmGeneratorTarget*>::iterator t = targets.begin();
  415. t != targets.end(); ++t) {
  416. cmGeneratorTarget* target = *t;
  417. if (target->GetType() == cmState::INTERFACE_LIBRARY) {
  418. continue;
  419. }
  420. for (std::vector<std::string>::iterator ci = configNames.begin();
  421. ci != configNames.end(); ++ci) {
  422. const char* config = ci->c_str();
  423. target->ComputeTargetManifest(config);
  424. }
  425. }
  426. }
  427. bool cmLocalGenerator::IsRootMakefile() const
  428. {
  429. return !this->StateSnapshot.GetBuildsystemDirectoryParent().IsValid();
  430. }
  431. cmState* cmLocalGenerator::GetState() const
  432. {
  433. return this->GlobalGenerator->GetCMakeInstance()->GetState();
  434. }
  435. cmState::Snapshot cmLocalGenerator::GetStateSnapshot() const
  436. {
  437. return this->Makefile->GetStateSnapshot();
  438. }
  439. // List of variables that are replaced when
  440. // rules are expanced. These variables are
  441. // replaced in the form <var> with GetSafeDefinition(var).
  442. // ${LANG} is replaced in the variable first with all enabled
  443. // languages.
  444. static const char* ruleReplaceVars[] = {
  445. "CMAKE_${LANG}_COMPILER",
  446. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  447. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  448. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  449. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  450. "CMAKE_${LANG}_LINK_FLAGS",
  451. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  452. "CMAKE_${LANG}_ARCHIVE",
  453. "CMAKE_AR",
  454. "CMAKE_CURRENT_SOURCE_DIR",
  455. "CMAKE_CURRENT_BINARY_DIR",
  456. "CMAKE_RANLIB",
  457. "CMAKE_LINKER",
  458. "CMAKE_CL_SHOWINCLUDES_PREFIX",
  459. CM_NULLPTR
  460. };
  461. std::string cmLocalGenerator::ExpandRuleVariable(
  462. std::string const& variable, const RuleVariables& replaceValues)
  463. {
  464. if (replaceValues.LinkFlags) {
  465. if (variable == "LINK_FLAGS") {
  466. return replaceValues.LinkFlags;
  467. }
  468. }
  469. if (replaceValues.Manifests) {
  470. if (variable == "MANIFESTS") {
  471. return replaceValues.Manifests;
  472. }
  473. }
  474. if (replaceValues.Flags) {
  475. if (variable == "FLAGS") {
  476. return replaceValues.Flags;
  477. }
  478. }
  479. if (replaceValues.Source) {
  480. if (variable == "SOURCE") {
  481. return replaceValues.Source;
  482. }
  483. }
  484. if (replaceValues.PreprocessedSource) {
  485. if (variable == "PREPROCESSED_SOURCE") {
  486. return replaceValues.PreprocessedSource;
  487. }
  488. }
  489. if (replaceValues.AssemblySource) {
  490. if (variable == "ASSEMBLY_SOURCE") {
  491. return replaceValues.AssemblySource;
  492. }
  493. }
  494. if (replaceValues.Object) {
  495. if (variable == "OBJECT") {
  496. return replaceValues.Object;
  497. }
  498. }
  499. if (replaceValues.ObjectDir) {
  500. if (variable == "OBJECT_DIR") {
  501. return replaceValues.ObjectDir;
  502. }
  503. }
  504. if (replaceValues.ObjectFileDir) {
  505. if (variable == "OBJECT_FILE_DIR") {
  506. return replaceValues.ObjectFileDir;
  507. }
  508. }
  509. if (replaceValues.Objects) {
  510. if (variable == "OBJECTS") {
  511. return replaceValues.Objects;
  512. }
  513. }
  514. if (replaceValues.ObjectsQuoted) {
  515. if (variable == "OBJECTS_QUOTED") {
  516. return replaceValues.ObjectsQuoted;
  517. }
  518. }
  519. if (replaceValues.Defines && variable == "DEFINES") {
  520. return replaceValues.Defines;
  521. }
  522. if (replaceValues.Includes && variable == "INCLUDES") {
  523. return replaceValues.Includes;
  524. }
  525. if (replaceValues.TargetPDB) {
  526. if (variable == "TARGET_PDB") {
  527. return replaceValues.TargetPDB;
  528. }
  529. }
  530. if (replaceValues.TargetCompilePDB) {
  531. if (variable == "TARGET_COMPILE_PDB") {
  532. return replaceValues.TargetCompilePDB;
  533. }
  534. }
  535. if (replaceValues.DependencyFile) {
  536. if (variable == "DEP_FILE") {
  537. return replaceValues.DependencyFile;
  538. }
  539. }
  540. if (replaceValues.Target) {
  541. if (variable == "TARGET_QUOTED") {
  542. std::string targetQuoted = replaceValues.Target;
  543. if (!targetQuoted.empty() && targetQuoted[0] != '\"') {
  544. targetQuoted = '\"';
  545. targetQuoted += replaceValues.Target;
  546. targetQuoted += '\"';
  547. }
  548. return targetQuoted;
  549. }
  550. if (variable == "TARGET_UNQUOTED") {
  551. std::string unquoted = replaceValues.Target;
  552. std::string::size_type sz = unquoted.size();
  553. if (sz > 2 && unquoted[0] == '\"' && unquoted[sz - 1] == '\"') {
  554. unquoted = unquoted.substr(1, sz - 2);
  555. }
  556. return unquoted;
  557. }
  558. if (replaceValues.LanguageCompileFlags) {
  559. if (variable == "LANGUAGE_COMPILE_FLAGS") {
  560. return replaceValues.LanguageCompileFlags;
  561. }
  562. }
  563. if (replaceValues.Target) {
  564. if (variable == "TARGET") {
  565. return replaceValues.Target;
  566. }
  567. }
  568. if (variable == "TARGET_IMPLIB") {
  569. return this->TargetImplib;
  570. }
  571. if (variable == "TARGET_VERSION_MAJOR") {
  572. if (replaceValues.TargetVersionMajor) {
  573. return replaceValues.TargetVersionMajor;
  574. }
  575. return "0";
  576. }
  577. if (variable == "TARGET_VERSION_MINOR") {
  578. if (replaceValues.TargetVersionMinor) {
  579. return replaceValues.TargetVersionMinor;
  580. }
  581. return "0";
  582. }
  583. if (replaceValues.Target) {
  584. if (variable == "TARGET_BASE") {
  585. // Strip the last extension off the target name.
  586. std::string targetBase = replaceValues.Target;
  587. std::string::size_type pos = targetBase.rfind('.');
  588. if (pos != targetBase.npos) {
  589. return targetBase.substr(0, pos);
  590. }
  591. return targetBase;
  592. }
  593. }
  594. }
  595. if (variable == "TARGET_SONAME" || variable == "SONAME_FLAG" ||
  596. variable == "TARGET_INSTALLNAME_DIR") {
  597. // All these variables depend on TargetSOName
  598. if (replaceValues.TargetSOName) {
  599. if (variable == "TARGET_SONAME") {
  600. return replaceValues.TargetSOName;
  601. }
  602. if (variable == "SONAME_FLAG" && replaceValues.SONameFlag) {
  603. return replaceValues.SONameFlag;
  604. }
  605. if (replaceValues.TargetInstallNameDir &&
  606. variable == "TARGET_INSTALLNAME_DIR") {
  607. return replaceValues.TargetInstallNameDir;
  608. }
  609. }
  610. return "";
  611. }
  612. if (replaceValues.LinkLibraries) {
  613. if (variable == "LINK_LIBRARIES") {
  614. return replaceValues.LinkLibraries;
  615. }
  616. }
  617. if (replaceValues.Language) {
  618. if (variable == "LANGUAGE") {
  619. return replaceValues.Language;
  620. }
  621. }
  622. if (replaceValues.CMTarget) {
  623. if (variable == "TARGET_NAME") {
  624. return replaceValues.CMTarget->GetName();
  625. }
  626. if (variable == "TARGET_TYPE") {
  627. return cmState::GetTargetTypeName(replaceValues.CMTarget->GetType());
  628. }
  629. }
  630. if (replaceValues.Output) {
  631. if (variable == "OUTPUT") {
  632. return replaceValues.Output;
  633. }
  634. }
  635. if (variable == "CMAKE_COMMAND") {
  636. return this->ConvertToOutputFormat(
  637. cmSystemTools::CollapseFullPath(cmSystemTools::GetCMakeCommand()),
  638. SHELL);
  639. }
  640. std::vector<std::string> enabledLanguages =
  641. this->GetState()->GetEnabledLanguages();
  642. // loop over language specific replace variables
  643. int pos = 0;
  644. while (ruleReplaceVars[pos]) {
  645. for (std::vector<std::string>::iterator i = enabledLanguages.begin();
  646. i != enabledLanguages.end(); ++i) {
  647. const char* lang = i->c_str();
  648. std::string actualReplace = ruleReplaceVars[pos];
  649. // If this is the compiler then look for the extra variable
  650. // _COMPILER_ARG1 which must be the first argument to the compiler
  651. const char* compilerArg1 = CM_NULLPTR;
  652. const char* compilerTarget = CM_NULLPTR;
  653. const char* compilerOptionTarget = CM_NULLPTR;
  654. const char* compilerExternalToolchain = CM_NULLPTR;
  655. const char* compilerOptionExternalToolchain = CM_NULLPTR;
  656. const char* compilerSysroot = CM_NULLPTR;
  657. const char* compilerOptionSysroot = CM_NULLPTR;
  658. if (actualReplace == "CMAKE_${LANG}_COMPILER") {
  659. std::string arg1 = actualReplace + "_ARG1";
  660. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  661. compilerArg1 = this->Makefile->GetDefinition(arg1);
  662. compilerTarget = this->Makefile->GetDefinition(
  663. std::string("CMAKE_") + lang + "_COMPILER_TARGET");
  664. compilerOptionTarget = this->Makefile->GetDefinition(
  665. std::string("CMAKE_") + lang + "_COMPILE_OPTIONS_TARGET");
  666. compilerExternalToolchain = this->Makefile->GetDefinition(
  667. std::string("CMAKE_") + lang + "_COMPILER_EXTERNAL_TOOLCHAIN");
  668. compilerOptionExternalToolchain =
  669. this->Makefile->GetDefinition(std::string("CMAKE_") + lang +
  670. "_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN");
  671. compilerSysroot = this->Makefile->GetDefinition("CMAKE_SYSROOT");
  672. compilerOptionSysroot = this->Makefile->GetDefinition(
  673. std::string("CMAKE_") + lang + "_COMPILE_OPTIONS_SYSROOT");
  674. }
  675. if (actualReplace.find("${LANG}") != actualReplace.npos) {
  676. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  677. }
  678. if (actualReplace == variable) {
  679. std::string replace = this->Makefile->GetSafeDefinition(variable);
  680. // if the variable is not a FLAG then treat it like a path
  681. if (variable.find("_FLAG") == variable.npos) {
  682. std::string ret = this->ConvertToOutputForExisting(replace);
  683. // if there is a required first argument to the compiler add it
  684. // to the compiler string
  685. if (compilerArg1) {
  686. ret += " ";
  687. ret += compilerArg1;
  688. }
  689. if (compilerTarget && compilerOptionTarget) {
  690. ret += " ";
  691. ret += compilerOptionTarget;
  692. ret += compilerTarget;
  693. }
  694. if (compilerExternalToolchain && compilerOptionExternalToolchain) {
  695. ret += " ";
  696. ret += compilerOptionExternalToolchain;
  697. ret += this->EscapeForShell(compilerExternalToolchain, true);
  698. }
  699. if (compilerSysroot && compilerOptionSysroot) {
  700. ret += " ";
  701. ret += compilerOptionSysroot;
  702. ret += this->EscapeForShell(compilerSysroot, true);
  703. }
  704. return ret;
  705. }
  706. return replace;
  707. }
  708. }
  709. pos++;
  710. }
  711. return variable;
  712. }
  713. void cmLocalGenerator::ExpandRuleVariables(std::string& s,
  714. const RuleVariables& replaceValues)
  715. {
  716. if (replaceValues.RuleLauncher) {
  717. this->InsertRuleLauncher(s, replaceValues.CMTarget,
  718. replaceValues.RuleLauncher);
  719. }
  720. std::string::size_type start = s.find('<');
  721. // no variables to expand
  722. if (start == s.npos) {
  723. return;
  724. }
  725. std::string::size_type pos = 0;
  726. std::string expandedInput;
  727. while (start != s.npos && start < s.size() - 2) {
  728. std::string::size_type end = s.find('>', start);
  729. // if we find a < with no > we are done
  730. if (end == s.npos) {
  731. return;
  732. }
  733. char c = s[start + 1];
  734. // if the next char after the < is not A-Za-z then
  735. // skip it and try to find the next < in the string
  736. if (!isalpha(c)) {
  737. start = s.find('<', start + 1);
  738. } else {
  739. // extract the var
  740. std::string var = s.substr(start + 1, end - start - 1);
  741. std::string replace = this->ExpandRuleVariable(var, replaceValues);
  742. expandedInput += s.substr(pos, start - pos);
  743. expandedInput += replace;
  744. // move to next one
  745. start = s.find('<', start + var.size() + 2);
  746. pos = end + 1;
  747. }
  748. }
  749. // add the rest of the input
  750. expandedInput += s.substr(pos, s.size() - pos);
  751. s = expandedInput;
  752. }
  753. const char* cmLocalGenerator::GetRuleLauncher(cmGeneratorTarget* target,
  754. const std::string& prop)
  755. {
  756. if (target) {
  757. return target->GetProperty(prop);
  758. }
  759. return this->Makefile->GetProperty(prop);
  760. }
  761. void cmLocalGenerator::InsertRuleLauncher(std::string& s,
  762. cmGeneratorTarget* target,
  763. const std::string& prop)
  764. {
  765. if (const char* val = this->GetRuleLauncher(target, prop)) {
  766. std::ostringstream wrapped;
  767. wrapped << val << " " << s;
  768. s = wrapped.str();
  769. }
  770. }
  771. std::string cmLocalGenerator::ConvertToIncludeReference(
  772. std::string const& path, OutputFormat format, bool forceFullPaths)
  773. {
  774. static_cast<void>(forceFullPaths);
  775. return this->ConvertToOutputForExisting(path, format);
  776. }
  777. std::string cmLocalGenerator::GetIncludeFlags(
  778. const std::vector<std::string>& includes, cmGeneratorTarget* target,
  779. const std::string& lang, bool forceFullPaths, bool forResponseFile,
  780. const std::string& config)
  781. {
  782. if (lang.empty()) {
  783. return "";
  784. }
  785. OutputFormat shellFormat = forResponseFile ? RESPONSE : SHELL;
  786. std::ostringstream includeFlags;
  787. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  788. flagVar += lang;
  789. const char* includeFlag = this->Makefile->GetSafeDefinition(flagVar);
  790. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  791. flagVar += lang;
  792. const char* sep = this->Makefile->GetDefinition(flagVar);
  793. bool quotePaths = false;
  794. if (this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS")) {
  795. quotePaths = true;
  796. }
  797. bool repeatFlag = true;
  798. // should the include flag be repeated like ie. -IA -IB
  799. if (!sep) {
  800. sep = " ";
  801. } else {
  802. // if there is a separator then the flag is not repeated but is only
  803. // given once i.e. -classpath a:b:c
  804. repeatFlag = false;
  805. }
  806. // Support special system include flag if it is available and the
  807. // normal flag is repeated for each directory.
  808. std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
  809. sysFlagVar += lang;
  810. const char* sysIncludeFlag = CM_NULLPTR;
  811. if (repeatFlag) {
  812. sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar);
  813. }
  814. std::string fwSearchFlagVar = "CMAKE_";
  815. fwSearchFlagVar += lang;
  816. fwSearchFlagVar += "_FRAMEWORK_SEARCH_FLAG";
  817. const char* fwSearchFlag = this->Makefile->GetDefinition(fwSearchFlagVar);
  818. std::string sysFwSearchFlagVar = "CMAKE_";
  819. sysFwSearchFlagVar += lang;
  820. sysFwSearchFlagVar += "_SYSTEM_FRAMEWORK_SEARCH_FLAG";
  821. const char* sysFwSearchFlag =
  822. this->Makefile->GetDefinition(sysFwSearchFlagVar);
  823. bool flagUsed = false;
  824. std::set<std::string> emitted;
  825. #ifdef __APPLE__
  826. emitted.insert("/System/Library/Frameworks");
  827. #endif
  828. std::vector<std::string>::const_iterator i;
  829. for (i = includes.begin(); i != includes.end(); ++i) {
  830. if (fwSearchFlag && *fwSearchFlag && this->Makefile->IsOn("APPLE") &&
  831. cmSystemTools::IsPathToFramework(i->c_str())) {
  832. std::string frameworkDir = *i;
  833. frameworkDir += "/../";
  834. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  835. if (emitted.insert(frameworkDir).second) {
  836. if (sysFwSearchFlag && target &&
  837. target->IsSystemIncludeDirectory(*i, config)) {
  838. includeFlags << sysFwSearchFlag;
  839. } else {
  840. includeFlags << fwSearchFlag;
  841. }
  842. includeFlags << this->ConvertToOutputFormat(frameworkDir, shellFormat)
  843. << " ";
  844. }
  845. continue;
  846. }
  847. if (!flagUsed || repeatFlag) {
  848. if (sysIncludeFlag && target &&
  849. target->IsSystemIncludeDirectory(*i, config)) {
  850. includeFlags << sysIncludeFlag;
  851. } else {
  852. includeFlags << includeFlag;
  853. }
  854. flagUsed = true;
  855. }
  856. std::string includePath =
  857. this->ConvertToIncludeReference(*i, shellFormat, forceFullPaths);
  858. if (quotePaths && !includePath.empty() && includePath[0] != '\"') {
  859. includeFlags << "\"";
  860. }
  861. includeFlags << includePath;
  862. if (quotePaths && !includePath.empty() && includePath[0] != '\"') {
  863. includeFlags << "\"";
  864. }
  865. includeFlags << sep;
  866. }
  867. std::string flags = includeFlags.str();
  868. // remove trailing separators
  869. if ((sep[0] != ' ') && !flags.empty() && flags[flags.size() - 1] == sep[0]) {
  870. flags[flags.size() - 1] = ' ';
  871. }
  872. return flags;
  873. }
  874. void cmLocalGenerator::AddCompileDefinitions(std::set<std::string>& defines,
  875. cmGeneratorTarget const* target,
  876. const std::string& config,
  877. const std::string& lang) const
  878. {
  879. std::vector<std::string> targetDefines;
  880. target->GetCompileDefinitions(targetDefines, config, lang);
  881. this->AppendDefines(defines, targetDefines);
  882. }
  883. void cmLocalGenerator::AddCompileOptions(std::string& flags,
  884. cmGeneratorTarget* target,
  885. const std::string& lang,
  886. const std::string& config)
  887. {
  888. std::string langFlagRegexVar = std::string("CMAKE_") + lang + "_FLAG_REGEX";
  889. if (const char* langFlagRegexStr =
  890. this->Makefile->GetDefinition(langFlagRegexVar)) {
  891. // Filter flags acceptable to this language.
  892. cmsys::RegularExpression r(langFlagRegexStr);
  893. std::vector<std::string> opts;
  894. if (const char* targetFlags = target->GetProperty("COMPILE_FLAGS")) {
  895. cmSystemTools::ParseWindowsCommandLine(targetFlags, opts);
  896. }
  897. target->GetCompileOptions(opts, config, lang);
  898. for (std::vector<std::string>::const_iterator i = opts.begin();
  899. i != opts.end(); ++i) {
  900. if (r.find(i->c_str())) {
  901. // (Re-)Escape this flag. COMPILE_FLAGS were already parsed
  902. // as a command line above, and COMPILE_OPTIONS are escaped.
  903. this->AppendFlagEscape(flags, *i);
  904. }
  905. }
  906. } else {
  907. // Use all flags.
  908. if (const char* targetFlags = target->GetProperty("COMPILE_FLAGS")) {
  909. // COMPILE_FLAGS are not escaped for historical reasons.
  910. this->AppendFlags(flags, targetFlags);
  911. }
  912. std::vector<std::string> opts;
  913. target->GetCompileOptions(opts, config, lang);
  914. for (std::vector<std::string>::const_iterator i = opts.begin();
  915. i != opts.end(); ++i) {
  916. // COMPILE_OPTIONS are escaped.
  917. this->AppendFlagEscape(flags, *i);
  918. }
  919. }
  920. std::vector<std::string> features;
  921. target->GetCompileFeatures(features, config);
  922. for (std::vector<std::string>::const_iterator it = features.begin();
  923. it != features.end(); ++it) {
  924. if (!this->Makefile->AddRequiredTargetFeature(target->Target, *it)) {
  925. return;
  926. }
  927. }
  928. for (std::map<std::string, std::string>::const_iterator it =
  929. target->GetMaxLanguageStandards().begin();
  930. it != target->GetMaxLanguageStandards().end(); ++it) {
  931. const char* standard = target->GetProperty(it->first + "_STANDARD");
  932. if (!standard) {
  933. continue;
  934. }
  935. if (this->Makefile->IsLaterStandard(it->first, standard, it->second)) {
  936. std::ostringstream e;
  937. e << "The COMPILE_FEATURES property of target \"" << target->GetName()
  938. << "\" was evaluated when computing the link "
  939. "implementation, and the \""
  940. << it->first << "_STANDARD\" was \"" << it->second
  941. << "\" for that computation. Computing the "
  942. "COMPILE_FEATURES based on the link implementation resulted in a "
  943. "higher \""
  944. << it->first << "_STANDARD\" \"" << standard
  945. << "\". "
  946. "This is not permitted. The COMPILE_FEATURES may not both depend "
  947. "on "
  948. "and be depended on by the link implementation."
  949. << std::endl;
  950. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  951. return;
  952. }
  953. }
  954. this->AddCompilerRequirementFlag(flags, target, lang);
  955. }
  956. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
  957. cmGeneratorTarget const* target,
  958. const std::string& lang,
  959. const std::string& config,
  960. bool stripImplicitInclDirs) const
  961. {
  962. // Need to decide whether to automatically include the source and
  963. // binary directories at the beginning of the include path.
  964. bool includeSourceDir = false;
  965. bool includeBinaryDir = false;
  966. // When automatic include directories are requested for a build then
  967. // include the source and binary directories at the beginning of the
  968. // include path to approximate include file behavior for an
  969. // in-source build. This does not account for the case of a source
  970. // file in a subdirectory of the current source directory but we
  971. // cannot fix this because not all native build tools support
  972. // per-source-file include paths.
  973. if (this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR")) {
  974. includeSourceDir = true;
  975. includeBinaryDir = true;
  976. }
  977. // Do not repeat an include path.
  978. std::set<std::string> emitted;
  979. // Store the automatic include paths.
  980. if (includeBinaryDir) {
  981. std::string binDir = this->StateSnapshot.GetDirectory().GetCurrentBinary();
  982. if (emitted.find(binDir) == emitted.end()) {
  983. dirs.push_back(binDir);
  984. emitted.insert(binDir);
  985. }
  986. }
  987. if (includeSourceDir) {
  988. std::string srcDir = this->StateSnapshot.GetDirectory().GetCurrentSource();
  989. if (emitted.find(srcDir) == emitted.end()) {
  990. dirs.push_back(srcDir);
  991. emitted.insert(srcDir);
  992. }
  993. }
  994. if (!target) {
  995. return;
  996. }
  997. std::string rootPath = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  998. std::vector<std::string> implicitDirs;
  999. // Load implicit include directories for this language.
  1000. std::string impDirVar = "CMAKE_";
  1001. impDirVar += lang;
  1002. impDirVar += "_IMPLICIT_INCLUDE_DIRECTORIES";
  1003. if (const char* value = this->Makefile->GetDefinition(impDirVar)) {
  1004. std::vector<std::string> impDirVec;
  1005. cmSystemTools::ExpandListArgument(value, impDirVec);
  1006. for (std::vector<std::string>::const_iterator i = impDirVec.begin();
  1007. i != impDirVec.end(); ++i) {
  1008. std::string d = rootPath + *i;
  1009. cmSystemTools::ConvertToUnixSlashes(d);
  1010. emitted.insert(d);
  1011. if (!stripImplicitInclDirs) {
  1012. implicitDirs.push_back(*i);
  1013. }
  1014. }
  1015. }
  1016. // Get the target-specific include directories.
  1017. std::vector<std::string> includes;
  1018. includes = target->GetIncludeDirectories(config, lang);
  1019. // Support putting all the in-project include directories first if
  1020. // it is requested by the project.
  1021. if (this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE")) {
  1022. const char* topSourceDir = this->GetState()->GetSourceDirectory();
  1023. const char* topBinaryDir = this->GetState()->GetBinaryDirectory();
  1024. for (std::vector<std::string>::const_iterator i = includes.begin();
  1025. i != includes.end(); ++i) {
  1026. // Emit this directory only if it is a subdirectory of the
  1027. // top-level source or binary tree.
  1028. if (cmSystemTools::ComparePath(*i, topSourceDir) ||
  1029. cmSystemTools::ComparePath(*i, topBinaryDir) ||
  1030. cmSystemTools::IsSubDirectory(*i, topSourceDir) ||
  1031. cmSystemTools::IsSubDirectory(*i, topBinaryDir)) {
  1032. if (emitted.insert(*i).second) {
  1033. dirs.push_back(*i);
  1034. }
  1035. }
  1036. }
  1037. }
  1038. // Construct the final ordered include directory list.
  1039. for (std::vector<std::string>::const_iterator i = includes.begin();
  1040. i != includes.end(); ++i) {
  1041. if (emitted.insert(*i).second) {
  1042. dirs.push_back(*i);
  1043. }
  1044. }
  1045. for (std::vector<std::string>::const_iterator i = implicitDirs.begin();
  1046. i != implicitDirs.end(); ++i) {
  1047. if (std::find(includes.begin(), includes.end(), *i) != includes.end()) {
  1048. dirs.push_back(*i);
  1049. }
  1050. }
  1051. }
  1052. void cmLocalGenerator::GetStaticLibraryFlags(std::string& flags,
  1053. std::string const& config,
  1054. cmGeneratorTarget* target)
  1055. {
  1056. this->AppendFlags(
  1057. flags, this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS"));
  1058. if (!config.empty()) {
  1059. std::string name = "CMAKE_STATIC_LINKER_FLAGS_" + config;
  1060. this->AppendFlags(flags, this->Makefile->GetSafeDefinition(name));
  1061. }
  1062. this->AppendFlags(flags, target->GetProperty("STATIC_LIBRARY_FLAGS"));
  1063. if (!config.empty()) {
  1064. std::string name = "STATIC_LIBRARY_FLAGS_" + config;
  1065. this->AppendFlags(flags, target->GetProperty(name));
  1066. }
  1067. }
  1068. void cmLocalGenerator::GetTargetFlags(
  1069. const std::string& config, std::string& linkLibs, std::string& flags,
  1070. std::string& linkFlags, std::string& frameworkPath, std::string& linkPath,
  1071. cmGeneratorTarget* target, bool useWatcomQuote)
  1072. {
  1073. const std::string buildType = cmSystemTools::UpperCase(config);
  1074. const char* libraryLinkVariable =
  1075. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1076. switch (target->GetType()) {
  1077. case cmState::STATIC_LIBRARY:
  1078. this->GetStaticLibraryFlags(linkFlags, buildType, target);
  1079. break;
  1080. case cmState::MODULE_LIBRARY:
  1081. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1082. case cmState::SHARED_LIBRARY: {
  1083. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  1084. linkFlags += " ";
  1085. if (!buildType.empty()) {
  1086. std::string build = libraryLinkVariable;
  1087. build += "_";
  1088. build += buildType;
  1089. linkFlags += this->Makefile->GetSafeDefinition(build);
  1090. linkFlags += " ";
  1091. }
  1092. if (this->Makefile->IsOn("WIN32") &&
  1093. !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW"))) {
  1094. std::vector<cmSourceFile*> sources;
  1095. target->GetSourceFiles(sources, buildType);
  1096. for (std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1097. i != sources.end(); ++i) {
  1098. cmSourceFile* sf = *i;
  1099. if (sf->GetExtension() == "def") {
  1100. linkFlags +=
  1101. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1102. linkFlags += this->ConvertToOutputFormat(
  1103. cmSystemTools::CollapseFullPath(sf->GetFullPath()), SHELL);
  1104. linkFlags += " ";
  1105. }
  1106. }
  1107. }
  1108. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1109. if (targetLinkFlags) {
  1110. linkFlags += targetLinkFlags;
  1111. linkFlags += " ";
  1112. }
  1113. if (!buildType.empty()) {
  1114. std::string configLinkFlags = "LINK_FLAGS_";
  1115. configLinkFlags += buildType;
  1116. targetLinkFlags = target->GetProperty(configLinkFlags);
  1117. if (targetLinkFlags) {
  1118. linkFlags += targetLinkFlags;
  1119. linkFlags += " ";
  1120. }
  1121. }
  1122. this->OutputLinkLibraries(linkLibs, frameworkPath, linkPath, *target,
  1123. false, false, useWatcomQuote);
  1124. } break;
  1125. case cmState::EXECUTABLE: {
  1126. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1127. linkFlags += " ";
  1128. if (!buildType.empty()) {
  1129. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1130. build += buildType;
  1131. linkFlags += this->Makefile->GetSafeDefinition(build);
  1132. linkFlags += " ";
  1133. }
  1134. std::string linkLanguage = target->GetLinkerLanguage(buildType);
  1135. if (linkLanguage.empty()) {
  1136. cmSystemTools::Error(
  1137. "CMake can not determine linker language for target: ",
  1138. target->GetName().c_str());
  1139. return;
  1140. }
  1141. this->AddLanguageFlags(flags, linkLanguage, buildType);
  1142. this->OutputLinkLibraries(linkLibs, frameworkPath, linkPath, *target,
  1143. false, false, useWatcomQuote);
  1144. if (cmSystemTools::IsOn(
  1145. this->Makefile->GetDefinition("BUILD_SHARED_LIBS"))) {
  1146. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") +
  1147. linkLanguage + std::string("_FLAGS");
  1148. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar);
  1149. linkFlags += " ";
  1150. }
  1151. if (target->GetPropertyAsBool("WIN32_EXECUTABLE")) {
  1152. linkFlags +=
  1153. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1154. linkFlags += " ";
  1155. } else {
  1156. linkFlags +=
  1157. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1158. linkFlags += " ";
  1159. }
  1160. if (target->IsExecutableWithExports()) {
  1161. std::string exportFlagVar = "CMAKE_EXE_EXPORTS_";
  1162. exportFlagVar += linkLanguage;
  1163. exportFlagVar += "_FLAG";
  1164. linkFlags += this->Makefile->GetSafeDefinition(exportFlagVar);
  1165. linkFlags += " ";
  1166. }
  1167. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1168. if (targetLinkFlags) {
  1169. linkFlags += targetLinkFlags;
  1170. linkFlags += " ";
  1171. }
  1172. if (!buildType.empty()) {
  1173. std::string configLinkFlags = "LINK_FLAGS_";
  1174. configLinkFlags += buildType;
  1175. targetLinkFlags = target->GetProperty(configLinkFlags);
  1176. if (targetLinkFlags) {
  1177. linkFlags += targetLinkFlags;
  1178. linkFlags += " ";
  1179. }
  1180. }
  1181. } break;
  1182. default:
  1183. break;
  1184. }
  1185. }
  1186. void cmLocalGenerator::GetTargetCompileFlags(cmGeneratorTarget* target,
  1187. std::string const& config,
  1188. std::string const& lang,
  1189. std::string& flags)
  1190. {
  1191. cmMakefile* mf = this->GetMakefile();
  1192. // Add language-specific flags.
  1193. this->AddLanguageFlags(flags, lang, config);
  1194. if (target->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION", config)) {
  1195. this->AppendFeatureOptions(flags, lang, "IPO");
  1196. }
  1197. this->AddArchitectureFlags(flags, target, lang, config);
  1198. if (lang == "Fortran") {
  1199. this->AppendFlags(flags, this->GetTargetFortranFlags(target, config));
  1200. }
  1201. this->AddCMP0018Flags(flags, target, lang, config);
  1202. this->AddVisibilityPresetFlags(flags, target, lang);
  1203. this->AppendFlags(flags, mf->GetDefineFlags());
  1204. this->AppendFlags(flags, this->GetFrameworkFlags(lang, config, target));
  1205. this->AddCompileOptions(flags, target, lang, config);
  1206. }
  1207. static std::string GetFrameworkFlags(const std::string& lang,
  1208. const std::string& config,
  1209. cmGeneratorTarget* target)
  1210. {
  1211. cmLocalGenerator* lg = target->GetLocalGenerator();
  1212. cmMakefile* mf = lg->GetMakefile();
  1213. if (!mf->IsOn("APPLE")) {
  1214. return std::string();
  1215. }
  1216. std::string fwSearchFlagVar = "CMAKE_" + lang + "_FRAMEWORK_SEARCH_FLAG";
  1217. const char* fwSearchFlag = mf->GetDefinition(fwSearchFlagVar);
  1218. if (!(fwSearchFlag && *fwSearchFlag)) {
  1219. return std::string();
  1220. }
  1221. std::set<std::string> emitted;
  1222. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1223. emitted.insert("/System/Library/Frameworks");
  1224. #endif
  1225. std::vector<std::string> includes;
  1226. lg->GetIncludeDirectories(includes, target, "C", config);
  1227. // check all include directories for frameworks as this
  1228. // will already have added a -F for the framework
  1229. for (std::vector<std::string>::iterator i = includes.begin();
  1230. i != includes.end(); ++i) {
  1231. if (lg->GetGlobalGenerator()->NameResolvesToFramework(*i)) {
  1232. std::string frameworkDir = *i;
  1233. frameworkDir += "/../";
  1234. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1235. emitted.insert(frameworkDir);
  1236. }
  1237. }
  1238. std::string flags;
  1239. if (cmComputeLinkInformation* cli = target->GetLinkInformation(config)) {
  1240. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1241. for (std::vector<std::string>::const_iterator i = frameworks.begin();
  1242. i != frameworks.end(); ++i) {
  1243. if (emitted.insert(*i).second) {
  1244. flags += fwSearchFlag;
  1245. flags += lg->ConvertToOutputFormat(*i, cmOutputConverter::SHELL);
  1246. flags += " ";
  1247. }
  1248. }
  1249. }
  1250. return flags;
  1251. }
  1252. std::string cmLocalGenerator::GetFrameworkFlags(std::string const& l,
  1253. std::string const& config,
  1254. cmGeneratorTarget* target)
  1255. {
  1256. return ::GetFrameworkFlags(l, config, target);
  1257. }
  1258. void cmLocalGenerator::GetTargetDefines(cmGeneratorTarget const* target,
  1259. std::string const& config,
  1260. std::string const& lang,
  1261. std::set<std::string>& defines) const
  1262. {
  1263. // Add the export symbol definition for shared library objects.
  1264. if (const char* exportMacro = target->GetExportMacro()) {
  1265. this->AppendDefines(defines, exportMacro);
  1266. }
  1267. // Add preprocessor definitions for this target and configuration.
  1268. this->AddCompileDefinitions(defines, target, config, lang.c_str());
  1269. }
  1270. std::string cmLocalGenerator::GetTargetFortranFlags(
  1271. cmGeneratorTarget const* /*unused*/, std::string const& /*unused*/)
  1272. {
  1273. // Implemented by specific generators that override this.
  1274. return std::string();
  1275. }
  1276. std::string cmLocalGenerator::ConvertToLinkReference(std::string const& lib,
  1277. OutputFormat format)
  1278. {
  1279. #if defined(_WIN32) && !defined(__CYGWIN__)
  1280. // Work-ardound command line parsing limitations in MSVC 6.0
  1281. if (this->Makefile->IsOn("MSVC60")) {
  1282. // Search for the last space.
  1283. std::string::size_type pos = lib.rfind(' ');
  1284. if (pos != lib.npos) {
  1285. // Find the slash after the last space, if any.
  1286. pos = lib.find('/', pos);
  1287. // Convert the portion of the path with a space to a short path.
  1288. std::string sp;
  1289. if (cmSystemTools::GetShortPath(lib.substr(0, pos).c_str(), sp)) {
  1290. // Append the rest of the path with no space.
  1291. sp += lib.substr(pos);
  1292. // Convert to an output path.
  1293. return this->ConvertToOutputFormat(sp.c_str(), format);
  1294. }
  1295. }
  1296. }
  1297. #endif
  1298. // Normal behavior.
  1299. return this->Convert(lib, START_OUTPUT, format);
  1300. }
  1301. /**
  1302. * Output the linking rules on a command line. For executables,
  1303. * targetLibrary should be a NULL pointer. For libraries, it should point
  1304. * to the name of the library. This will not link a library against itself.
  1305. */
  1306. void cmLocalGenerator::OutputLinkLibraries(std::string& linkLibraries,
  1307. std::string& frameworkPath,
  1308. std::string& linkPath,
  1309. cmGeneratorTarget& tgt, bool relink,
  1310. bool forResponseFile,
  1311. bool useWatcomQuote)
  1312. {
  1313. OutputFormat shellFormat =
  1314. (forResponseFile) ? RESPONSE : ((useWatcomQuote) ? WATCOMQUOTE : SHELL);
  1315. bool escapeAllowMakeVars = !forResponseFile;
  1316. std::ostringstream fout;
  1317. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1318. cmComputeLinkInformation* pcli = tgt.GetLinkInformation(config);
  1319. if (!pcli) {
  1320. return;
  1321. }
  1322. cmComputeLinkInformation& cli = *pcli;
  1323. // Collect library linking flags command line options.
  1324. std::string linkLibs;
  1325. std::string linkLanguage = cli.GetLinkLanguage();
  1326. std::string libPathFlag =
  1327. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1328. std::string libPathTerminator =
  1329. this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
  1330. // Flags to link an executable to shared libraries.
  1331. if (tgt.GetType() == cmState::EXECUTABLE &&
  1332. this->StateSnapshot.GetState()->GetGlobalPropertyAsBool(
  1333. "TARGET_SUPPORTS_SHARED_LIBS")) {
  1334. bool add_shlib_flags = false;
  1335. switch (tgt.GetPolicyStatusCMP0065()) {
  1336. case cmPolicies::WARN:
  1337. if (!tgt.GetPropertyAsBool("ENABLE_EXPORTS") &&
  1338. this->Makefile->PolicyOptionalWarningEnabled(
  1339. "CMAKE_POLICY_WARNING_CMP0065")) {
  1340. std::ostringstream w;
  1341. /* clang-format off */
  1342. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0065) << "\n"
  1343. "For compatibility with older versions of CMake, "
  1344. "additional flags may be added to export symbols on all "
  1345. "executables regardless of their ENABLE_EXPORTS property.";
  1346. /* clang-format on */
  1347. this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  1348. }
  1349. case cmPolicies::OLD:
  1350. // OLD behavior is to always add the flags
  1351. add_shlib_flags = true;
  1352. break;
  1353. case cmPolicies::REQUIRED_IF_USED:
  1354. case cmPolicies::REQUIRED_ALWAYS:
  1355. this->IssueMessage(
  1356. cmake::FATAL_ERROR,
  1357. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0065));
  1358. case cmPolicies::NEW:
  1359. // NEW behavior is to only add the flags if ENABLE_EXPORTS is on
  1360. add_shlib_flags = tgt.GetPropertyAsBool("ENABLE_EXPORTS");
  1361. break;
  1362. }
  1363. if (add_shlib_flags) {
  1364. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1365. linkFlagsVar += linkLanguage;
  1366. linkFlagsVar += "_FLAGS";
  1367. linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar);
  1368. linkLibs += " ";
  1369. }
  1370. }
  1371. // Append the framework search path flags.
  1372. std::string fwSearchFlagVar = "CMAKE_";
  1373. fwSearchFlagVar += linkLanguage;
  1374. fwSearchFlagVar += "_FRAMEWORK_SEARCH_FLAG";
  1375. const char* fwSearchFlag = this->Makefile->GetDefinition(fwSearchFlagVar);
  1376. if (fwSearchFlag && *fwSearchFlag) {
  1377. std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
  1378. for (std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  1379. fdi != fwDirs.end(); ++fdi) {
  1380. frameworkPath += fwSearchFlag;
  1381. frameworkPath += this->ConvertToOutputFormat(*fdi, shellFormat);
  1382. frameworkPath += " ";
  1383. }
  1384. }
  1385. // Append the library search path flags.
  1386. std::vector<std::string> const& libDirs = cli.GetDirectories();
  1387. for (std::vector<std::string>::const_iterator libDir = libDirs.begin();
  1388. libDir != libDirs.end(); ++libDir) {
  1389. std::string libpath =
  1390. this->ConvertToOutputForExisting(*libDir, shellFormat);
  1391. linkPath += " " + libPathFlag;
  1392. linkPath += libpath;
  1393. linkPath += libPathTerminator;
  1394. linkPath += " ";
  1395. }
  1396. // Append the link items.
  1397. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1398. ItemVector const& items = cli.GetItems();
  1399. for (ItemVector::const_iterator li = items.begin(); li != items.end();
  1400. ++li) {
  1401. if (li->Target && li->Target->GetType() == cmState::INTERFACE_LIBRARY) {
  1402. continue;
  1403. }
  1404. if (li->IsPath) {
  1405. linkLibs += this->ConvertToLinkReference(li->Value, shellFormat);
  1406. } else {
  1407. linkLibs += li->Value;
  1408. }
  1409. linkLibs += " ";
  1410. }
  1411. // Write the library flags to the build rule.
  1412. fout << linkLibs;
  1413. // Check what kind of rpath flags to use.
  1414. if (cli.GetRuntimeSep().empty()) {
  1415. // Each rpath entry gets its own option ("-R a -R b -R c")
  1416. std::vector<std::string> runtimeDirs;
  1417. cli.GetRPath(runtimeDirs, relink);
  1418. std::string rpath;
  1419. for (std::vector<std::string>::iterator ri = runtimeDirs.begin();
  1420. ri != runtimeDirs.end(); ++ri) {
  1421. rpath += cli.GetRuntimeFlag();
  1422. rpath += this->ConvertToOutputFormat(*ri, shellFormat);
  1423. rpath += " ";
  1424. }
  1425. fout << rpath;
  1426. } else {
  1427. // All rpath entries are combined ("-Wl,-rpath,a:b:c").
  1428. std::string rpath = cli.GetRPathString(relink);
  1429. // Store the rpath option in the stream.
  1430. if (!rpath.empty()) {
  1431. fout << cli.GetRuntimeFlag();
  1432. fout << this->EscapeForShell(rpath, escapeAllowMakeVars);
  1433. fout << " ";
  1434. }
  1435. }
  1436. // Add the linker runtime search path if any.
  1437. std::string rpath_link = cli.GetRPathLinkString();
  1438. if (!cli.GetRPathLinkFlag().empty() && !rpath_link.empty()) {
  1439. fout << cli.GetRPathLinkFlag();
  1440. fout << this->EscapeForShell(rpath_link, escapeAllowMakeVars);
  1441. fout << " ";
  1442. }
  1443. // Add standard libraries for this language.
  1444. std::string standardLibsVar = "CMAKE_";
  1445. standardLibsVar += cli.GetLinkLanguage();
  1446. standardLibsVar += "_STANDARD_LIBRARIES";
  1447. if (const char* stdLibs = this->Makefile->GetDefinition(standardLibsVar)) {
  1448. fout << stdLibs << " ";
  1449. }
  1450. linkLibraries = fout.str();
  1451. }
  1452. void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
  1453. cmGeneratorTarget const* target,
  1454. const std::string& lang,
  1455. const std::string& config)
  1456. {
  1457. // Only add Mac OS X specific flags on Darwin platforms (OSX and iphone):
  1458. if (this->Makefile->IsOn("APPLE") && this->EmitUniversalBinaryFlags) {
  1459. std::vector<std::string> archs;
  1460. target->GetAppleArchs(config, archs);
  1461. const char* sysroot = this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1462. if (sysroot && sysroot[0] == '/' && !sysroot[1]) {
  1463. sysroot = CM_NULLPTR;
  1464. }
  1465. std::string sysrootFlagVar =
  1466. std::string("CMAKE_") + lang + "_SYSROOT_FLAG";
  1467. const char* sysrootFlag = this->Makefile->GetDefinition(sysrootFlagVar);
  1468. const char* deploymentTarget =
  1469. this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  1470. std::string deploymentTargetFlagVar =
  1471. std::string("CMAKE_") + lang + "_OSX_DEPLOYMENT_TARGET_FLAG";
  1472. const char* deploymentTargetFlag =
  1473. this->Makefile->GetDefinition(deploymentTargetFlagVar);
  1474. if (!archs.empty() && !lang.empty() &&
  1475. (lang[0] == 'C' || lang[0] == 'F')) {
  1476. for (std::vector<std::string>::iterator i = archs.begin();
  1477. i != archs.end(); ++i) {
  1478. flags += " -arch ";
  1479. flags += *i;
  1480. }
  1481. }
  1482. if (sysrootFlag && *sysrootFlag && sysroot && *sysroot) {
  1483. flags += " ";
  1484. flags += sysrootFlag;
  1485. flags += " ";
  1486. flags += this->ConvertToOutputFormat(sysroot, SHELL);
  1487. }
  1488. if (deploymentTargetFlag && *deploymentTargetFlag && deploymentTarget &&
  1489. *deploymentTarget) {
  1490. flags += " ";
  1491. flags += deploymentTargetFlag;
  1492. flags += deploymentTarget;
  1493. }
  1494. }
  1495. }
  1496. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1497. const std::string& lang,
  1498. const std::string& config)
  1499. {
  1500. // Add language-specific flags.
  1501. std::string flagsVar = "CMAKE_";
  1502. flagsVar += lang;
  1503. flagsVar += "_FLAGS";
  1504. this->AddConfigVariableFlags(flags, flagsVar, config);
  1505. }
  1506. cmGeneratorTarget* cmLocalGenerator::FindGeneratorTargetToUse(
  1507. const std::string& name) const
  1508. {
  1509. std::vector<cmGeneratorTarget*>::const_iterator imported = std::find_if(
  1510. this->ImportedGeneratorTargets.begin(),
  1511. this->ImportedGeneratorTargets.end(), NamedGeneratorTargetFinder(name));
  1512. if (imported != this->ImportedGeneratorTargets.end()) {
  1513. return *imported;
  1514. }
  1515. if (cmGeneratorTarget* t = this->FindLocalNonAliasGeneratorTarget(name)) {
  1516. return t;
  1517. }
  1518. return this->GetGlobalGenerator()->FindGeneratorTarget(name);
  1519. }
  1520. bool cmLocalGenerator::GetRealDependency(const std::string& inName,
  1521. const std::string& config,
  1522. std::string& dep)
  1523. {
  1524. // Older CMake code may specify the dependency using the target
  1525. // output file rather than the target name. Such code would have
  1526. // been written before there was support for target properties that
  1527. // modify the name so stripping down to just the file name should
  1528. // produce the target name in this case.
  1529. std::string name = cmSystemTools::GetFilenameName(inName);
  1530. // If the input name is the empty string, there is no real
  1531. // dependency. Short-circuit the other checks:
  1532. if (name == "") {
  1533. return false;
  1534. }
  1535. if (cmSystemTools::GetFilenameLastExtension(name) == ".exe") {
  1536. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1537. }
  1538. // Look for a CMake target with the given name.
  1539. if (cmGeneratorTarget* target = this->FindGeneratorTargetToUse(name)) {
  1540. // make sure it is not just a coincidence that the target name
  1541. // found is part of the inName
  1542. if (cmSystemTools::FileIsFullPath(inName.c_str())) {
  1543. std::string tLocation;
  1544. if (target->GetType() >= cmState::EXECUTABLE &&
  1545. target->GetType() <= cmState::MODULE_LIBRARY) {
  1546. tLocation = target->GetLocation(config);
  1547. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1548. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  1549. }
  1550. std::string depLocation =
  1551. cmSystemTools::GetFilenamePath(std::string(inName));
  1552. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  1553. if (depLocation != tLocation) {
  1554. // it is a full path to a depend that has the same name
  1555. // as a target but is in a different location so do not use
  1556. // the target as the depend
  1557. dep = inName;
  1558. return true;
  1559. }
  1560. }
  1561. switch (target->GetType()) {
  1562. case cmState::EXECUTABLE:
  1563. case cmState::STATIC_LIBRARY:
  1564. case cmState::SHARED_LIBRARY:
  1565. case cmState::MODULE_LIBRARY:
  1566. case cmState::UNKNOWN_LIBRARY:
  1567. dep = target->GetLocation(config);
  1568. return true;
  1569. case cmState::OBJECT_LIBRARY:
  1570. // An object library has no single file on which to depend.
  1571. // This was listed to get the target-level dependency.
  1572. return false;
  1573. case cmState::INTERFACE_LIBRARY:
  1574. // An interface library has no file on which to depend.
  1575. // This was listed to get the target-level dependency.
  1576. return false;
  1577. case cmState::UTILITY:
  1578. case cmState::GLOBAL_TARGET:
  1579. // A utility target has no file on which to depend. This was listed
  1580. // only to get the target-level dependency.
  1581. return false;
  1582. }
  1583. }
  1584. // The name was not that of a CMake target. It must name a file.
  1585. if (cmSystemTools::FileIsFullPath(inName.c_str())) {
  1586. // This is a full path. Return it as given.
  1587. dep = inName;
  1588. return true;
  1589. }
  1590. // Check for a source file in this directory that matches the
  1591. // dependency.
  1592. if (cmSourceFile* sf = this->Makefile->GetSource(inName)) {
  1593. dep = sf->GetFullPath();
  1594. return true;
  1595. }
  1596. // Treat the name as relative to the source directory in which it
  1597. // was given.
  1598. dep = this->StateSnapshot.GetDirectory().GetCurrentSource();
  1599. dep += "/";
  1600. dep += inName;
  1601. return true;
  1602. }
  1603. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1604. const std::string& lang, bool shared)
  1605. {
  1606. std::string flagsVar;
  1607. // Add flags for dealing with shared libraries for this language.
  1608. if (shared) {
  1609. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1610. flagsVar += lang;
  1611. flagsVar += "_FLAGS";
  1612. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1613. }
  1614. }
  1615. void cmLocalGenerator::AddCompilerRequirementFlag(
  1616. std::string& flags, cmGeneratorTarget const* target, const std::string& lang)
  1617. {
  1618. if (lang.empty()) {
  1619. return;
  1620. }
  1621. const char* defaultStd =
  1622. this->Makefile->GetDefinition("CMAKE_" + lang + "_STANDARD_DEFAULT");
  1623. if (!defaultStd || !*defaultStd) {
  1624. // This compiler has no notion of language standard levels.
  1625. return;
  1626. }
  1627. std::string stdProp = lang + "_STANDARD";
  1628. const char* standardProp = target->GetProperty(stdProp);
  1629. if (!standardProp) {
  1630. return;
  1631. }
  1632. std::string extProp = lang + "_EXTENSIONS";
  1633. std::string type = "EXTENSION";
  1634. bool ext = true;
  1635. if (const char* extPropValue = target->GetProperty(extProp)) {
  1636. if (cmSystemTools::IsOff(extPropValue)) {
  1637. ext = false;
  1638. type = "STANDARD";
  1639. }
  1640. }
  1641. if (target->GetPropertyAsBool(lang + "_STANDARD_REQUIRED")) {
  1642. std::string option_flag =
  1643. "CMAKE_" + lang + standardProp + "_" + type + "_COMPILE_OPTION";
  1644. const char* opt =
  1645. target->Target->GetMakefile()->GetDefinition(option_flag);
  1646. if (!opt) {
  1647. std::ostringstream e;
  1648. e << "Target \"" << target->GetName() << "\" requires the language "
  1649. "dialect \""
  1650. << lang << standardProp << "\" "
  1651. << (ext ? "(with compiler extensions)" : "")
  1652. << ", but CMake "
  1653. "does not know the compile flags to use to enable it.";
  1654. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  1655. } else {
  1656. this->AppendFlagEscape(flags, opt);
  1657. }
  1658. return;
  1659. }
  1660. static std::map<std::string, std::vector<std::string> > langStdMap;
  1661. if (langStdMap.empty()) {
  1662. // Maintain sorted order, most recent first.
  1663. langStdMap["CXX"].push_back("14");
  1664. langStdMap["CXX"].push_back("11");
  1665. langStdMap["CXX"].push_back("98");
  1666. langStdMap["C"].push_back("11");
  1667. langStdMap["C"].push_back("99");
  1668. langStdMap["C"].push_back("90");
  1669. }
  1670. std::string standard(standardProp);
  1671. std::vector<std::string>& stds = langStdMap[lang];
  1672. std::vector<std::string>::const_iterator stdIt =
  1673. std::find(stds.begin(), stds.end(), standard);
  1674. if (stdIt == stds.end()) {
  1675. std::string e =
  1676. lang + "_STANDARD is set to invalid value '" + standard + "'";
  1677. this->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  1678. cmake::FATAL_ERROR, e, target->GetBacktrace());
  1679. return;
  1680. }
  1681. std::vector<std::string>::const_iterator defaultStdIt =
  1682. std::find(stds.begin(), stds.end(), defaultStd);
  1683. if (defaultStdIt == stds.end()) {
  1684. std::string e = "CMAKE_" + lang +
  1685. "_STANDARD_DEFAULT is set to invalid value '" + std::string(defaultStd) +
  1686. "'";
  1687. this->IssueMessage(cmake::INTERNAL_ERROR, e);
  1688. return;
  1689. }
  1690. // Greater or equal because the standards are stored in
  1691. // backward chronological order.
  1692. if (stdIt >= defaultStdIt) {
  1693. std::string option_flag =
  1694. "CMAKE_" + lang + *stdIt + "_" + type + "_COMPILE_OPTION";
  1695. const char* opt =
  1696. target->Target->GetMakefile()->GetRequiredDefinition(option_flag);
  1697. this->AppendFlagEscape(flags, opt);
  1698. return;
  1699. }
  1700. for (; stdIt < defaultStdIt; ++stdIt) {
  1701. std::string option_flag =
  1702. "CMAKE_" + lang + *stdIt + "_" + type + "_COMPILE_OPTION";
  1703. if (const char* opt =
  1704. target->Target->GetMakefile()->GetDefinition(option_flag)) {
  1705. this->AppendFlagEscape(flags, opt);
  1706. return;
  1707. }
  1708. }
  1709. }
  1710. static void AddVisibilityCompileOption(std::string& flags,
  1711. cmGeneratorTarget const* target,
  1712. cmLocalGenerator* lg,
  1713. const std::string& lang,
  1714. std::string* warnCMP0063)
  1715. {
  1716. std::string compileOption = "CMAKE_" + lang + "_COMPILE_OPTIONS_VISIBILITY";
  1717. const char* opt = lg->GetMakefile()->GetDefinition(compileOption);
  1718. if (!opt) {
  1719. return;
  1720. }
  1721. std::string flagDefine = lang + "_VISIBILITY_PRESET";
  1722. const char* prop = target->GetProperty(flagDefine);
  1723. if (!prop) {
  1724. return;
  1725. }
  1726. if (warnCMP0063) {
  1727. *warnCMP0063 += " " + flagDefine + "\n";
  1728. return;
  1729. }
  1730. if (strcmp(prop, "hidden") != 0 && strcmp(prop, "default") != 0 &&
  1731. strcmp(prop, "protected") != 0 && strcmp(prop, "internal") != 0) {
  1732. std::ostringstream e;
  1733. e << "Target " << target->GetName() << " uses unsupported value \"" << prop
  1734. << "\" for " << flagDefine << ".";
  1735. cmSystemTools::Error(e.str().c_str());
  1736. return;
  1737. }
  1738. std::string option = std::string(opt) + prop;
  1739. lg->AppendFlags(flags, option);
  1740. }
  1741. static void AddInlineVisibilityCompileOption(std::string& flags,
  1742. cmGeneratorTarget const* target,
  1743. cmLocalGenerator* lg,
  1744. std::string* warnCMP0063)
  1745. {
  1746. std::string compileOption =
  1747. "CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN";
  1748. const char* opt = lg->GetMakefile()->GetDefinition(compileOption);
  1749. if (!opt) {
  1750. return;
  1751. }
  1752. bool prop = target->GetPropertyAsBool("VISIBILITY_INLINES_HIDDEN");
  1753. if (!prop) {
  1754. return;
  1755. }
  1756. if (warnCMP0063) {
  1757. *warnCMP0063 += " VISIBILITY_INLINES_HIDDEN\n";
  1758. return;
  1759. }
  1760. lg->AppendFlags(flags, opt);
  1761. }
  1762. void cmLocalGenerator::AddVisibilityPresetFlags(
  1763. std::string& flags, cmGeneratorTarget const* target, const std::string& lang)
  1764. {
  1765. if (lang.empty()) {
  1766. return;
  1767. }
  1768. std::string warnCMP0063;
  1769. std::string* pWarnCMP0063 = CM_NULLPTR;
  1770. if (target->GetType() != cmState::SHARED_LIBRARY &&
  1771. target->GetType() != cmState::MODULE_LIBRARY &&
  1772. !target->IsExecutableWithExports()) {
  1773. switch (target->GetPolicyStatusCMP0063()) {
  1774. case cmPolicies::OLD:
  1775. return;
  1776. case cmPolicies::WARN:
  1777. pWarnCMP0063 = &warnCMP0063;
  1778. break;
  1779. default:
  1780. break;
  1781. }
  1782. }
  1783. AddVisibilityCompileOption(flags, target, this, lang, pWarnCMP0063);
  1784. if (lang == "CXX") {
  1785. AddInlineVisibilityCompileOption(flags, target, this, pWarnCMP0063);
  1786. }
  1787. if (!warnCMP0063.empty() && this->WarnCMP0063.insert(target).second) {
  1788. std::ostringstream w;
  1789. /* clang-format off */
  1790. w <<
  1791. cmPolicies::GetPolicyWarning(cmPolicies::CMP0063) << "\n"
  1792. "Target \"" << target->GetName() << "\" of "
  1793. "type \"" << cmState::GetTargetTypeName(target->GetType()) << "\" "
  1794. "has the following visibility properties set for " << lang << ":\n" <<
  1795. warnCMP0063 <<
  1796. "For compatibility CMake is not honoring them for this target.";
  1797. /* clang-format on */
  1798. target->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  1799. cmake::AUTHOR_WARNING, w.str(), target->GetBacktrace());
  1800. }
  1801. }
  1802. void cmLocalGenerator::AddCMP0018Flags(std::string& flags,
  1803. cmGeneratorTarget const* target,
  1804. std::string const& lang,
  1805. const std::string& config)
  1806. {
  1807. int targetType = target->GetType();
  1808. bool shared = ((targetType == cmState::SHARED_LIBRARY) ||
  1809. (targetType == cmState::MODULE_LIBRARY));
  1810. if (this->GetShouldUseOldFlags(shared, lang)) {
  1811. this->AddSharedFlags(flags, lang, shared);
  1812. } else {
  1813. if (target->GetType() == cmState::OBJECT_LIBRARY) {
  1814. if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE")) {
  1815. this->AddPositionIndependentFlags(flags, lang, targetType);
  1816. }
  1817. return;
  1818. }
  1819. if (target->GetLinkInterfaceDependentBoolProperty(
  1820. "POSITION_INDEPENDENT_CODE", config)) {
  1821. this->AddPositionIndependentFlags(flags, lang, targetType);
  1822. }
  1823. if (shared) {
  1824. this->AppendFeatureOptions(flags, lang, "DLL");
  1825. }
  1826. }
  1827. }
  1828. bool cmLocalGenerator::GetShouldUseOldFlags(bool shared,
  1829. const std::string& lang) const
  1830. {
  1831. std::string originalFlags =
  1832. this->GlobalGenerator->GetSharedLibFlagsForLanguage(lang);
  1833. if (shared) {
  1834. std::string flagsVar = "CMAKE_SHARED_LIBRARY_";
  1835. flagsVar += lang;
  1836. flagsVar += "_FLAGS";
  1837. const char* flags = this->Makefile->GetSafeDefinition(flagsVar);
  1838. if (flags && flags != originalFlags) {
  1839. switch (this->GetPolicyStatus(cmPolicies::CMP0018)) {
  1840. case cmPolicies::WARN: {
  1841. std::ostringstream e;
  1842. e << "Variable " << flagsVar
  1843. << " has been modified. CMake "
  1844. "will ignore the POSITION_INDEPENDENT_CODE target property for "
  1845. "shared libraries and will use the "
  1846. << flagsVar
  1847. << " variable "
  1848. "instead. This may cause errors if the original content of "
  1849. << flagsVar << " was removed.\n"
  1850. << cmPolicies::GetPolicyWarning(cmPolicies::CMP0018);
  1851. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  1852. // fall through to OLD behaviour
  1853. }
  1854. case cmPolicies::OLD:
  1855. return true;
  1856. case cmPolicies::REQUIRED_IF_USED:
  1857. case cmPolicies::REQUIRED_ALWAYS:
  1858. case cmPolicies::NEW:
  1859. return false;
  1860. }
  1861. }
  1862. }
  1863. return false;
  1864. }
  1865. void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags,
  1866. std::string const& lang,
  1867. int targetType)
  1868. {
  1869. const char* picFlags = CM_NULLPTR;
  1870. if (targetType == cmState::EXECUTABLE) {
  1871. std::string flagsVar = "CMAKE_";
  1872. flagsVar += lang;
  1873. flagsVar += "_COMPILE_OPTIONS_PIE";
  1874. picFlags = this->Makefile->GetSafeDefinition(flagsVar);
  1875. }
  1876. if (!picFlags) {
  1877. std::string flagsVar = "CMAKE_";
  1878. flagsVar += lang;
  1879. flagsVar += "_COMPILE_OPTIONS_PIC";
  1880. picFlags = this->Makefile->GetSafeDefinition(flagsVar);
  1881. }
  1882. if (picFlags) {
  1883. std::vector<std::string> options;
  1884. cmSystemTools::ExpandListArgument(picFlags, options);
  1885. for (std::vector<std::string>::const_iterator oi = options.begin();
  1886. oi != options.end(); ++oi) {
  1887. this->AppendFlagEscape(flags, *oi);
  1888. }
  1889. }
  1890. }
  1891. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1892. const std::string& var,
  1893. const std::string& config)
  1894. {
  1895. // Add the flags from the variable itself.
  1896. std::string flagsVar = var;
  1897. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1898. // Add the flags from the build-type specific variable.
  1899. if (!config.empty()) {
  1900. flagsVar += "_";
  1901. flagsVar += cmSystemTools::UpperCase(config);
  1902. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1903. }
  1904. }
  1905. void cmLocalGenerator::AppendFlags(std::string& flags,
  1906. const std::string& newFlags)
  1907. {
  1908. if (!newFlags.empty()) {
  1909. if (!flags.empty()) {
  1910. flags += " ";
  1911. }
  1912. flags += newFlags;
  1913. }
  1914. }
  1915. void cmLocalGenerator::AppendFlags(std::string& flags, const char* newFlags)
  1916. {
  1917. if (newFlags && *newFlags) {
  1918. this->AppendFlags(flags, std::string(newFlags));
  1919. }
  1920. }
  1921. void cmLocalGenerator::AppendFlagEscape(std::string& flags,
  1922. const std::string& rawFlag)
  1923. {
  1924. this->AppendFlags(flags, this->EscapeForShell(rawFlag));
  1925. }
  1926. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  1927. const char* defines_list) const
  1928. {
  1929. // Short-circuit if there are no definitions.
  1930. if (!defines_list) {
  1931. return;
  1932. }
  1933. // Expand the list of definitions.
  1934. std::vector<std::string> defines_vec;
  1935. cmSystemTools::ExpandListArgument(defines_list, defines_vec);
  1936. this->AppendDefines(defines, defines_vec);
  1937. }
  1938. void cmLocalGenerator::AppendDefines(
  1939. std::set<std::string>& defines,
  1940. const std::vector<std::string>& defines_vec) const
  1941. {
  1942. for (std::vector<std::string>::const_iterator di = defines_vec.begin();
  1943. di != defines_vec.end(); ++di) {
  1944. // Skip unsupported definitions.
  1945. if (!this->CheckDefinition(*di)) {
  1946. continue;
  1947. }
  1948. defines.insert(*di);
  1949. }
  1950. }
  1951. void cmLocalGenerator::JoinDefines(const std::set<std::string>& defines,
  1952. std::string& definesString,
  1953. const std::string& lang)
  1954. {
  1955. // Lookup the define flag for the current language.
  1956. std::string dflag = "-D";
  1957. if (!lang.empty()) {
  1958. std::string defineFlagVar = "CMAKE_";
  1959. defineFlagVar += lang;
  1960. defineFlagVar += "_DEFINE_FLAG";
  1961. const char* df = this->Makefile->GetDefinition(defineFlagVar);
  1962. if (df && *df) {
  1963. dflag = df;
  1964. }
  1965. }
  1966. std::set<std::string>::const_iterator defineIt = defines.begin();
  1967. const std::set<std::string>::const_iterator defineEnd = defines.end();
  1968. const char* itemSeparator = definesString.empty() ? "" : " ";
  1969. for (; defineIt != defineEnd; ++defineIt) {
  1970. // Append the definition with proper escaping.
  1971. std::string def = dflag;
  1972. if (this->GetState()->UseWatcomWMake()) {
  1973. // The Watcom compiler does its own command line parsing instead
  1974. // of using the windows shell rules. Definitions are one of
  1975. // -DNAME
  1976. // -DNAME=<cpp-token>
  1977. // -DNAME="c-string with spaces and other characters(?@#$)"
  1978. //
  1979. // Watcom will properly parse each of these cases from the
  1980. // command line without any escapes. However we still have to
  1981. // get the '$' and '#' characters through WMake as '$$' and
  1982. // '$#'.
  1983. for (const char* c = defineIt->c_str(); *c; ++c) {
  1984. if (*c == '$' || *c == '#') {
  1985. def += '$';
  1986. }
  1987. def += *c;
  1988. }
  1989. } else {
  1990. // Make the definition appear properly on the command line. Use
  1991. // -DNAME="value" instead of -D"NAME=value" for historical reasons.
  1992. std::string::size_type eq = defineIt->find("=");
  1993. def += defineIt->substr(0, eq);
  1994. if (eq != defineIt->npos) {
  1995. def += "=";
  1996. def += this->EscapeForShell(defineIt->c_str() + eq + 1, true);
  1997. }
  1998. }
  1999. definesString += itemSeparator;
  2000. itemSeparator = " ";
  2001. definesString += def;
  2002. }
  2003. }
  2004. void cmLocalGenerator::AppendFeatureOptions(std::string& flags,
  2005. const std::string& lang,
  2006. const char* feature)
  2007. {
  2008. std::string optVar = "CMAKE_";
  2009. optVar += lang;
  2010. optVar += "_COMPILE_OPTIONS_";
  2011. optVar += feature;
  2012. if (const char* optionList = this->Makefile->GetDefinition(optVar)) {
  2013. std::vector<std::string> options;
  2014. cmSystemTools::ExpandListArgument(optionList, options);
  2015. for (std::vector<std::string>::const_iterator oi = options.begin();
  2016. oi != options.end(); ++oi) {
  2017. this->AppendFlagEscape(flags, *oi);
  2018. }
  2019. }
  2020. }
  2021. const char* cmLocalGenerator::GetFeature(const std::string& feature,
  2022. const std::string& config)
  2023. {
  2024. std::string featureName = feature;
  2025. // TODO: Define accumulation policy for features (prepend, append, replace).
  2026. // Currently we always replace.
  2027. if (!config.empty()) {
  2028. featureName += "_";
  2029. featureName += cmSystemTools::UpperCase(config);
  2030. }
  2031. cmState::Snapshot snp = this->StateSnapshot;
  2032. while (snp.IsValid()) {
  2033. if (const char* value = snp.GetDirectory().GetProperty(featureName)) {
  2034. return value;
  2035. }
  2036. snp = snp.GetBuildsystemDirectoryParent();
  2037. }
  2038. return CM_NULLPTR;
  2039. }
  2040. std::string cmLocalGenerator::GetProjectName() const
  2041. {
  2042. return this->StateSnapshot.GetProjectName();
  2043. }
  2044. std::string cmLocalGenerator::ConstructComment(
  2045. cmCustomCommandGenerator const& ccg, const char* default_comment)
  2046. {
  2047. // Check for a comment provided with the command.
  2048. if (ccg.GetComment()) {
  2049. return ccg.GetComment();
  2050. }
  2051. // Construct a reasonable default comment if possible.
  2052. if (!ccg.GetOutputs().empty()) {
  2053. std::string comment;
  2054. comment = "Generating ";
  2055. const char* sep = "";
  2056. for (std::vector<std::string>::const_iterator o = ccg.GetOutputs().begin();
  2057. o != ccg.GetOutputs().end(); ++o) {
  2058. comment += sep;
  2059. comment +=
  2060. this->ConvertToRelativePath(*o, cmOutputConverter::START_OUTPUT);
  2061. sep = ", ";
  2062. }
  2063. return comment;
  2064. }
  2065. // Otherwise use the provided default.
  2066. return default_comment;
  2067. }
  2068. class cmInstallTargetGeneratorLocal : public cmInstallTargetGenerator
  2069. {
  2070. public:
  2071. cmInstallTargetGeneratorLocal(cmLocalGenerator* lg, std::string const& t,
  2072. const char* dest, bool implib)
  2073. : cmInstallTargetGenerator(
  2074. t, dest, implib, "", std::vector<std::string>(), "Unspecified",
  2075. cmInstallGenerator::SelectMessageLevel(lg->GetMakefile()), false,
  2076. false)
  2077. {
  2078. this->Compute(lg);
  2079. }
  2080. };
  2081. void cmLocalGenerator::GenerateTargetInstallRules(
  2082. std::ostream& os, const std::string& config,
  2083. std::vector<std::string> const& configurationTypes)
  2084. {
  2085. // Convert the old-style install specification from each target to
  2086. // an install generator and run it.
  2087. std::vector<cmGeneratorTarget*> tgts = this->GetGeneratorTargets();
  2088. for (std::vector<cmGeneratorTarget*>::iterator l = tgts.begin();
  2089. l != tgts.end(); ++l) {
  2090. if ((*l)->GetType() == cmState::INTERFACE_LIBRARY) {
  2091. continue;
  2092. }
  2093. // Include the user-specified pre-install script for this target.
  2094. if (const char* preinstall = (*l)->GetProperty("PRE_INSTALL_SCRIPT")) {
  2095. cmInstallScriptGenerator g(preinstall, false, CM_NULLPTR, false);
  2096. g.Generate(os, config, configurationTypes);
  2097. }
  2098. // Install this target if a destination is given.
  2099. if ((*l)->Target->GetInstallPath() != "") {
  2100. // Compute the full install destination. Note that converting
  2101. // to unix slashes also removes any trailing slash.
  2102. // We also skip over the leading slash given by the user.
  2103. std::string destination = (*l)->Target->GetInstallPath().substr(1);
  2104. cmSystemTools::ConvertToUnixSlashes(destination);
  2105. if (destination.empty()) {
  2106. destination = ".";
  2107. }
  2108. // Generate the proper install generator for this target type.
  2109. switch ((*l)->GetType()) {
  2110. case cmState::EXECUTABLE:
  2111. case cmState::STATIC_LIBRARY:
  2112. case cmState::MODULE_LIBRARY: {
  2113. // Use a target install generator.
  2114. cmInstallTargetGeneratorLocal g(this, (*l)->GetName(),
  2115. destination.c_str(), false);
  2116. g.Generate(os, config, configurationTypes);
  2117. } break;
  2118. case cmState::SHARED_LIBRARY: {
  2119. #if defined(_WIN32) || defined(__CYGWIN__)
  2120. // Special code to handle DLL. Install the import library
  2121. // to the normal destination and the DLL to the runtime
  2122. // destination.
  2123. cmInstallTargetGeneratorLocal g1(this, (*l)->GetName(),
  2124. destination.c_str(), true);
  2125. g1.Generate(os, config, configurationTypes);
  2126. // We also skip over the leading slash given by the user.
  2127. destination = (*l)->Target->GetRuntimeInstallPath().substr(1);
  2128. cmSystemTools::ConvertToUnixSlashes(destination);
  2129. cmInstallTargetGeneratorLocal g2(this, (*l)->GetName(),
  2130. destination.c_str(), false);
  2131. g2.Generate(os, config, configurationTypes);
  2132. #else
  2133. // Use a target install generator.
  2134. cmInstallTargetGeneratorLocal g(this, (*l)->GetName(),
  2135. destination.c_str(), false);
  2136. g.Generate(os, config, configurationTypes);
  2137. #endif
  2138. } break;
  2139. default:
  2140. break;
  2141. }
  2142. }
  2143. // Include the user-specified post-install script for this target.
  2144. if (const char* postinstall = (*l)->GetProperty("POST_INSTALL_SCRIPT")) {
  2145. cmInstallScriptGenerator g(postinstall, false, CM_NULLPTR, false);
  2146. g.Generate(os, config, configurationTypes);
  2147. }
  2148. }
  2149. }
  2150. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2151. static std::string cmLocalGeneratorMD5(const char* input)
  2152. {
  2153. char md5out[32];
  2154. cmsysMD5* md5 = cmsysMD5_New();
  2155. cmsysMD5_Initialize(md5);
  2156. cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
  2157. cmsysMD5_FinalizeHex(md5, md5out);
  2158. cmsysMD5_Delete(md5);
  2159. return std::string(md5out, 32);
  2160. }
  2161. static bool cmLocalGeneratorShortenObjectName(std::string& objName,
  2162. std::string::size_type max_len)
  2163. {
  2164. // Replace the beginning of the path portion of the object name with
  2165. // its own md5 sum.
  2166. std::string::size_type pos =
  2167. objName.find('/', objName.size() - max_len + 32);
  2168. if (pos != objName.npos) {
  2169. std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str());
  2170. md5name += objName.substr(pos);
  2171. objName = md5name;
  2172. // The object name is now short enough.
  2173. return true;
  2174. }
  2175. // The object name could not be shortened enough.
  2176. return false;
  2177. }
  2178. bool cmLocalGeneratorCheckObjectName(std::string& objName,
  2179. std::string::size_type dir_len,
  2180. std::string::size_type max_total_len)
  2181. {
  2182. // Enforce the maximum file name length if possible.
  2183. std::string::size_type max_obj_len = max_total_len;
  2184. if (dir_len < max_total_len) {
  2185. max_obj_len = max_total_len - dir_len;
  2186. if (objName.size() > max_obj_len) {
  2187. // The current object file name is too long. Try to shorten it.
  2188. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2189. }
  2190. // The object file name is short enough.
  2191. return true;
  2192. }
  2193. // The build directory in which the object will be stored is
  2194. // already too deep.
  2195. return false;
  2196. }
  2197. #endif
  2198. std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName(
  2199. const std::string& sin, std::string const& dir_max)
  2200. {
  2201. // Look for an existing mapped name for this object file.
  2202. std::map<std::string, std::string>::iterator it =
  2203. this->UniqueObjectNamesMap.find(sin);
  2204. // If no entry exists create one.
  2205. if (it == this->UniqueObjectNamesMap.end()) {
  2206. // Start with the original name.
  2207. std::string ssin = sin;
  2208. // Avoid full paths by removing leading slashes.
  2209. ssin.erase(0, ssin.find_first_not_of('/'));
  2210. // Avoid full paths by removing colons.
  2211. std::replace(ssin.begin(), ssin.end(), ':', '_');
  2212. // Avoid relative paths that go up the tree.
  2213. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2214. // Avoid spaces.
  2215. std::replace(ssin.begin(), ssin.end(), ' ', '_');
  2216. // Mangle the name if necessary.
  2217. if (this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES")) {
  2218. bool done;
  2219. int cc = 0;
  2220. char rpstr[100];
  2221. sprintf(rpstr, "_p_");
  2222. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2223. std::string sssin = sin;
  2224. do {
  2225. done = true;
  2226. for (it = this->UniqueObjectNamesMap.begin();
  2227. it != this->UniqueObjectNamesMap.end(); ++it) {
  2228. if (it->second == ssin) {
  2229. done = false;
  2230. }
  2231. }
  2232. if (done) {
  2233. break;
  2234. }
  2235. sssin = ssin;
  2236. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2237. sprintf(rpstr, "_p%d_", cc++);
  2238. } while (!done);
  2239. }
  2240. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2241. if (!cmLocalGeneratorCheckObjectName(ssin, dir_max.size(),
  2242. this->ObjectPathMax)) {
  2243. // Warn if this is the first time the path has been seen.
  2244. if (this->ObjectMaxPathViolations.insert(dir_max).second) {
  2245. std::ostringstream m;
  2246. /* clang-format off */
  2247. m << "The object file directory\n"
  2248. << " " << dir_max << "\n"
  2249. << "has " << dir_max.size() << " characters. "
  2250. << "The maximum full path to an object file is "
  2251. << this->ObjectPathMax << " characters "
  2252. << "(see CMAKE_OBJECT_PATH_MAX). "
  2253. << "Object file\n"
  2254. << " " << ssin << "\n"
  2255. << "cannot be safely placed under this directory. "
  2256. << "The build may not work correctly.";
  2257. /* clang-format on */
  2258. this->IssueMessage(cmake::WARNING, m.str());
  2259. }
  2260. }
  2261. #else
  2262. (void)dir_max;
  2263. #endif
  2264. // Insert the newly mapped object file name.
  2265. std::map<std::string, std::string>::value_type e(sin, ssin);
  2266. it = this->UniqueObjectNamesMap.insert(e).first;
  2267. }
  2268. // Return the map entry.
  2269. return it->second;
  2270. }
  2271. void cmLocalGenerator::ComputeObjectFilenames(
  2272. std::map<cmSourceFile const*, std::string>& /*unused*/,
  2273. cmGeneratorTarget const* /*unused*/)
  2274. {
  2275. }
  2276. bool cmLocalGenerator::IsWindowsShell() const
  2277. {
  2278. return this->GetState()->UseWindowsShell();
  2279. }
  2280. bool cmLocalGenerator::IsWatcomWMake() const
  2281. {
  2282. return this->GetState()->UseWatcomWMake();
  2283. }
  2284. bool cmLocalGenerator::IsMinGWMake() const
  2285. {
  2286. return this->GetState()->UseMinGWMake();
  2287. }
  2288. bool cmLocalGenerator::IsNMake() const
  2289. {
  2290. return this->GetState()->UseNMake();
  2291. }
  2292. std::string cmLocalGenerator::GetObjectFileNameWithoutTarget(
  2293. const cmSourceFile& source, std::string const& dir_max,
  2294. bool* hasSourceExtension)
  2295. {
  2296. // Construct the object file name using the full path to the source
  2297. // file which is its only unique identification.
  2298. const char* fullPath = source.GetFullPath().c_str();
  2299. // Try referencing the source relative to the source tree.
  2300. std::string relFromSource = this->ConvertToRelativePath(fullPath, START);
  2301. assert(!relFromSource.empty());
  2302. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
  2303. bool subSource = relSource && relFromSource[0] != '.';
  2304. // Try referencing the source relative to the binary tree.
  2305. std::string relFromBinary =
  2306. this->ConvertToRelativePath(fullPath, START_OUTPUT);
  2307. assert(!relFromBinary.empty());
  2308. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
  2309. bool subBinary = relBinary && relFromBinary[0] != '.';
  2310. // Select a nice-looking reference to the source file to construct
  2311. // the object file name.
  2312. std::string objectName;
  2313. if ((relSource && !relBinary) || (subSource && !subBinary)) {
  2314. objectName = relFromSource;
  2315. } else if ((relBinary && !relSource) || (subBinary && !subSource)) {
  2316. objectName = relFromBinary;
  2317. } else if (relFromBinary.length() < relFromSource.length()) {
  2318. objectName = relFromBinary;
  2319. } else {
  2320. objectName = relFromSource;
  2321. }
  2322. // if it is still a full path check for the try compile case
  2323. // try compile never have in source sources, and should not
  2324. // have conflicting source file names in the same target
  2325. if (cmSystemTools::FileIsFullPath(objectName.c_str())) {
  2326. if (this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile()) {
  2327. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2328. }
  2329. }
  2330. // Replace the original source file extension with the object file
  2331. // extension.
  2332. bool keptSourceExtension = true;
  2333. if (!source.GetPropertyAsBool("KEEP_EXTENSION")) {
  2334. // Decide whether this language wants to replace the source
  2335. // extension with the object extension. For CMake 2.4
  2336. // compatibility do this by default.
  2337. bool replaceExt = this->NeedBackwardsCompatibility_2_4();
  2338. if (!replaceExt) {
  2339. std::string lang = source.GetLanguage();
  2340. if (!lang.empty()) {
  2341. std::string repVar = "CMAKE_";
  2342. repVar += lang;
  2343. repVar += "_OUTPUT_EXTENSION_REPLACE";
  2344. replaceExt = this->Makefile->IsOn(repVar);
  2345. }
  2346. }
  2347. // Remove the source extension if it is to be replaced.
  2348. if (replaceExt) {
  2349. keptSourceExtension = false;
  2350. std::string::size_type dot_pos = objectName.rfind('.');
  2351. if (dot_pos != std::string::npos) {
  2352. objectName = objectName.substr(0, dot_pos);
  2353. }
  2354. }
  2355. // Store the new extension.
  2356. objectName += this->GlobalGenerator->GetLanguageOutputExtension(source);
  2357. }
  2358. if (hasSourceExtension) {
  2359. *hasSourceExtension = keptSourceExtension;
  2360. }
  2361. // Convert to a safe name.
  2362. return this->CreateSafeUniqueObjectFileName(objectName, dir_max);
  2363. }
  2364. std::string cmLocalGenerator::GetSourceFileLanguage(const cmSourceFile& source)
  2365. {
  2366. return source.GetLanguage();
  2367. }
  2368. cmake* cmLocalGenerator::GetCMakeInstance() const
  2369. {
  2370. return this->GlobalGenerator->GetCMakeInstance();
  2371. }
  2372. const char* cmLocalGenerator::GetSourceDirectory() const
  2373. {
  2374. return this->GetCMakeInstance()->GetHomeDirectory();
  2375. }
  2376. const char* cmLocalGenerator::GetBinaryDirectory() const
  2377. {
  2378. return this->GetCMakeInstance()->GetHomeOutputDirectory();
  2379. }
  2380. const char* cmLocalGenerator::GetCurrentBinaryDirectory() const
  2381. {
  2382. return this->StateSnapshot.GetDirectory().GetCurrentBinary();
  2383. }
  2384. const char* cmLocalGenerator::GetCurrentSourceDirectory() const
  2385. {
  2386. return this->StateSnapshot.GetDirectory().GetCurrentSource();
  2387. }
  2388. std::string cmLocalGenerator::GetTargetDirectory(
  2389. const cmGeneratorTarget* /*unused*/) const
  2390. {
  2391. cmSystemTools::Error("GetTargetDirectory"
  2392. " called on cmLocalGenerator");
  2393. return "";
  2394. }
  2395. KWIML_INT_uint64_t cmLocalGenerator::GetBackwardsCompatibility()
  2396. {
  2397. // The computed version may change until the project is fully
  2398. // configured.
  2399. if (!this->BackwardsCompatibilityFinal) {
  2400. unsigned int major = 0;
  2401. unsigned int minor = 0;
  2402. unsigned int patch = 0;
  2403. if (const char* value =
  2404. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY")) {
  2405. switch (sscanf(value, "%u.%u.%u", &major, &minor, &patch)) {
  2406. case 2:
  2407. patch = 0;
  2408. break;
  2409. case 1:
  2410. minor = 0;
  2411. patch = 0;
  2412. break;
  2413. default:
  2414. break;
  2415. }
  2416. }
  2417. this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
  2418. this->BackwardsCompatibilityFinal = true;
  2419. }
  2420. return this->BackwardsCompatibility;
  2421. }
  2422. bool cmLocalGenerator::NeedBackwardsCompatibility_2_4()
  2423. {
  2424. // Check the policy to decide whether to pay attention to this
  2425. // variable.
  2426. switch (this->GetPolicyStatus(cmPolicies::CMP0001)) {
  2427. case cmPolicies::WARN:
  2428. // WARN is just OLD without warning because user code does not
  2429. // always affect whether this check is done.
  2430. case cmPolicies::OLD:
  2431. // Old behavior is to check the variable.
  2432. break;
  2433. case cmPolicies::NEW:
  2434. // New behavior is to ignore the variable.
  2435. return false;
  2436. case cmPolicies::REQUIRED_IF_USED:
  2437. case cmPolicies::REQUIRED_ALWAYS:
  2438. // This will never be the case because the only way to require
  2439. // the setting is to require the user to specify version policy
  2440. // 2.6 or higher. Once we add that requirement then this whole
  2441. // method can be removed anyway.
  2442. return false;
  2443. }
  2444. // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
  2445. // equal to or lower than the given version.
  2446. KWIML_INT_uint64_t actual_compat = this->GetBackwardsCompatibility();
  2447. return (actual_compat && actual_compat <= CMake_VERSION_ENCODE(2, 4, 255));
  2448. }
  2449. cmPolicies::PolicyStatus cmLocalGenerator::GetPolicyStatus(
  2450. cmPolicies::PolicyID id) const
  2451. {
  2452. return this->Makefile->GetPolicyStatus(id);
  2453. }
  2454. bool cmLocalGenerator::CheckDefinition(std::string const& define) const
  2455. {
  2456. // Many compilers do not support -DNAME(arg)=sdf so we disable it.
  2457. std::string::size_type pos = define.find_first_of("(=");
  2458. if (pos != std::string::npos) {
  2459. if (define[pos] == '(') {
  2460. std::ostringstream e;
  2461. /* clang-format off */
  2462. e << "WARNING: Function-style preprocessor definitions may not be "
  2463. << "passed on the compiler command line because many compilers "
  2464. << "do not support it.\n"
  2465. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2466. << "Consider defining the macro in a (configured) header file.\n";
  2467. /* clang-format on */
  2468. cmSystemTools::Message(e.str().c_str());
  2469. return false;
  2470. }
  2471. }
  2472. // Many compilers do not support # in the value so we disable it.
  2473. if (define.find_first_of('#') != define.npos) {
  2474. std::ostringstream e;
  2475. /* clang-format off */
  2476. e << "WARNING: Preprocessor definitions containing '#' may not be "
  2477. << "passed on the compiler command line because many compilers "
  2478. << "do not support it.\n"
  2479. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2480. << "Consider defining the macro in a (configured) header file.\n";
  2481. /* clang-format on */
  2482. cmSystemTools::Message(e.str().c_str());
  2483. return false;
  2484. }
  2485. // Assume it is supported.
  2486. return true;
  2487. }
  2488. static void cmLGInfoProp(cmMakefile* mf, cmGeneratorTarget* target,
  2489. const std::string& prop)
  2490. {
  2491. if (const char* val = target->GetProperty(prop)) {
  2492. mf->AddDefinition(prop, val);
  2493. }
  2494. }
  2495. void cmLocalGenerator::GenerateAppleInfoPList(cmGeneratorTarget* target,
  2496. const std::string& targetName,
  2497. const char* fname)
  2498. {
  2499. // Find the Info.plist template.
  2500. const char* in = target->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
  2501. std::string inFile = (in && *in) ? in : "MacOSXBundleInfo.plist.in";
  2502. if (!cmSystemTools::FileIsFullPath(inFile.c_str())) {
  2503. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  2504. if (!inMod.empty()) {
  2505. inFile = inMod;
  2506. }
  2507. }
  2508. if (!cmSystemTools::FileExists(inFile.c_str(), true)) {
  2509. std::ostringstream e;
  2510. e << "Target " << target->GetName() << " Info.plist template \"" << inFile
  2511. << "\" could not be found.";
  2512. cmSystemTools::Error(e.str().c_str());
  2513. return;
  2514. }
  2515. // Convert target properties to variables in an isolated makefile
  2516. // scope to configure the file. If properties are set they will
  2517. // override user make variables. If not the configuration will fall
  2518. // back to the directory-level values set by the user.
  2519. cmMakefile* mf = this->Makefile;
  2520. cmMakefile::ScopePushPop varScope(mf);
  2521. mf->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName.c_str());
  2522. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_INFO_STRING");
  2523. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_ICON_FILE");
  2524. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_GUI_IDENTIFIER");
  2525. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_LONG_VERSION_STRING");
  2526. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_NAME");
  2527. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
  2528. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_VERSION");
  2529. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_COPYRIGHT");
  2530. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  2531. }
  2532. void cmLocalGenerator::GenerateFrameworkInfoPList(
  2533. cmGeneratorTarget* target, const std::string& targetName, const char* fname)
  2534. {
  2535. // Find the Info.plist template.
  2536. const char* in = target->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
  2537. std::string inFile = (in && *in) ? in : "MacOSXFrameworkInfo.plist.in";
  2538. if (!cmSystemTools::FileIsFullPath(inFile.c_str())) {
  2539. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  2540. if (!inMod.empty()) {
  2541. inFile = inMod;
  2542. }
  2543. }
  2544. if (!cmSystemTools::FileExists(inFile.c_str(), true)) {
  2545. std::ostringstream e;
  2546. e << "Target " << target->GetName() << " Info.plist template \"" << inFile
  2547. << "\" could not be found.";
  2548. cmSystemTools::Error(e.str().c_str());
  2549. return;
  2550. }
  2551. // Convert target properties to variables in an isolated makefile
  2552. // scope to configure the file. If properties are set they will
  2553. // override user make variables. If not the configuration will fall
  2554. // back to the directory-level values set by the user.
  2555. cmMakefile* mf = this->Makefile;
  2556. cmMakefile::ScopePushPop varScope(mf);
  2557. mf->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName.c_str());
  2558. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_ICON_FILE");
  2559. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_IDENTIFIER");
  2560. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
  2561. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
  2562. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  2563. }