cmLocalGenerator.cxx 98 KB

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