cmLocalGenerator.cxx 92 KB

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