cmLocalGenerator.cxx 97 KB

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