1
0

cmLocalGenerator.cxx 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903
  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. << R"(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. if (target->IsIPOEnabled(lang, config)) {
  1145. this->AppendFeatureOptions(flags, lang, "IPO");
  1146. }
  1147. this->AddArchitectureFlags(flags, target, lang, config);
  1148. if (lang == "Fortran") {
  1149. this->AppendFlags(flags, this->GetTargetFortranFlags(target, config));
  1150. }
  1151. this->AddCMP0018Flags(flags, target, lang, config);
  1152. this->AddVisibilityPresetFlags(flags, target, lang);
  1153. this->AppendFlags(flags, mf->GetDefineFlags());
  1154. this->AppendFlags(flags, this->GetFrameworkFlags(lang, config, target));
  1155. this->AddCompileOptions(flags, target, lang, config);
  1156. }
  1157. static std::string GetFrameworkFlags(const std::string& lang,
  1158. const std::string& config,
  1159. cmGeneratorTarget* target)
  1160. {
  1161. cmLocalGenerator* lg = target->GetLocalGenerator();
  1162. cmMakefile* mf = lg->GetMakefile();
  1163. if (!mf->IsOn("APPLE")) {
  1164. return std::string();
  1165. }
  1166. std::string fwSearchFlagVar = "CMAKE_" + lang + "_FRAMEWORK_SEARCH_FLAG";
  1167. const char* fwSearchFlag = mf->GetDefinition(fwSearchFlagVar);
  1168. if (!(fwSearchFlag && *fwSearchFlag)) {
  1169. return std::string();
  1170. }
  1171. std::set<std::string> emitted;
  1172. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1173. emitted.insert("/System/Library/Frameworks");
  1174. #endif
  1175. std::vector<std::string> includes;
  1176. lg->GetIncludeDirectories(includes, target, "C", config);
  1177. // check all include directories for frameworks as this
  1178. // will already have added a -F for the framework
  1179. for (std::string const& include : includes) {
  1180. if (lg->GetGlobalGenerator()->NameResolvesToFramework(include)) {
  1181. std::string frameworkDir = include;
  1182. frameworkDir += "/../";
  1183. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1184. emitted.insert(frameworkDir);
  1185. }
  1186. }
  1187. std::string flags;
  1188. if (cmComputeLinkInformation* cli = target->GetLinkInformation(config)) {
  1189. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1190. for (std::string const& framework : frameworks) {
  1191. if (emitted.insert(framework).second) {
  1192. flags += fwSearchFlag;
  1193. flags +=
  1194. lg->ConvertToOutputFormat(framework, cmOutputConverter::SHELL);
  1195. flags += " ";
  1196. }
  1197. }
  1198. }
  1199. return flags;
  1200. }
  1201. std::string cmLocalGenerator::GetFrameworkFlags(std::string const& l,
  1202. std::string const& config,
  1203. cmGeneratorTarget* target)
  1204. {
  1205. return ::GetFrameworkFlags(l, config, target);
  1206. }
  1207. void cmLocalGenerator::GetTargetDefines(cmGeneratorTarget const* target,
  1208. std::string const& config,
  1209. std::string const& lang,
  1210. std::set<std::string>& defines) const
  1211. {
  1212. std::set<BT<std::string>> tmp = this->GetTargetDefines(target, config, lang);
  1213. for (BT<std::string> const& v : tmp) {
  1214. defines.emplace(v.Value);
  1215. }
  1216. }
  1217. std::set<BT<std::string>> cmLocalGenerator::GetTargetDefines(
  1218. cmGeneratorTarget const* target, std::string const& config,
  1219. std::string const& lang) const
  1220. {
  1221. std::set<BT<std::string>> defines;
  1222. // Add the export symbol definition for shared library objects.
  1223. if (const std::string* exportMacro = target->GetExportMacro()) {
  1224. this->AppendDefines(defines, *exportMacro);
  1225. }
  1226. // Add preprocessor definitions for this target and configuration.
  1227. std::vector<BT<std::string>> targetDefines =
  1228. target->GetCompileDefinitions(config, lang);
  1229. this->AppendDefines(defines, targetDefines);
  1230. return defines;
  1231. }
  1232. std::string cmLocalGenerator::GetTargetFortranFlags(
  1233. cmGeneratorTarget const* /*unused*/, std::string const& /*unused*/)
  1234. {
  1235. // Implemented by specific generators that override this.
  1236. return std::string();
  1237. }
  1238. /**
  1239. * Output the linking rules on a command line. For executables,
  1240. * targetLibrary should be a NULL pointer. For libraries, it should point
  1241. * to the name of the library. This will not link a library against itself.
  1242. */
  1243. void cmLocalGenerator::OutputLinkLibraries(
  1244. cmComputeLinkInformation* pcli, cmLinkLineComputer* linkLineComputer,
  1245. std::string& linkLibraries, std::string& frameworkPath,
  1246. std::string& linkPath)
  1247. {
  1248. cmComputeLinkInformation& cli = *pcli;
  1249. std::string linkLanguage = cli.GetLinkLanguage();
  1250. const std::string& libPathFlag =
  1251. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1252. const std::string& libPathTerminator =
  1253. this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
  1254. // Add standard libraries for this language.
  1255. std::string standardLibsVar = "CMAKE_";
  1256. standardLibsVar += cli.GetLinkLanguage();
  1257. standardLibsVar += "_STANDARD_LIBRARIES";
  1258. std::string stdLibString;
  1259. if (const char* stdLibs = this->Makefile->GetDefinition(standardLibsVar)) {
  1260. stdLibString = stdLibs;
  1261. }
  1262. // Append the framework search path flags.
  1263. std::string fwSearchFlagVar = "CMAKE_";
  1264. fwSearchFlagVar += linkLanguage;
  1265. fwSearchFlagVar += "_FRAMEWORK_SEARCH_FLAG";
  1266. std::string fwSearchFlag =
  1267. this->Makefile->GetSafeDefinition(fwSearchFlagVar);
  1268. frameworkPath = linkLineComputer->ComputeFrameworkPath(cli, fwSearchFlag);
  1269. linkPath =
  1270. linkLineComputer->ComputeLinkPath(cli, libPathFlag, libPathTerminator);
  1271. linkLibraries = linkLineComputer->ComputeLinkLibraries(cli, stdLibString);
  1272. }
  1273. std::string cmLocalGenerator::GetLinkLibsCMP0065(
  1274. std::string const& linkLanguage, cmGeneratorTarget& tgt) const
  1275. {
  1276. std::string linkFlags;
  1277. // Flags to link an executable to shared libraries.
  1278. if (tgt.GetType() == cmStateEnums::EXECUTABLE &&
  1279. this->StateSnapshot.GetState()->GetGlobalPropertyAsBool(
  1280. "TARGET_SUPPORTS_SHARED_LIBS")) {
  1281. bool add_shlib_flags = false;
  1282. switch (tgt.GetPolicyStatusCMP0065()) {
  1283. case cmPolicies::WARN:
  1284. if (!tgt.GetPropertyAsBool("ENABLE_EXPORTS") &&
  1285. this->Makefile->PolicyOptionalWarningEnabled(
  1286. "CMAKE_POLICY_WARNING_CMP0065")) {
  1287. std::ostringstream w;
  1288. /* clang-format off */
  1289. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0065) << "\n"
  1290. "For compatibility with older versions of CMake, "
  1291. "additional flags may be added to export symbols on all "
  1292. "executables regardless of their ENABLE_EXPORTS property.";
  1293. /* clang-format on */
  1294. this->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  1295. }
  1296. CM_FALLTHROUGH;
  1297. case cmPolicies::OLD:
  1298. // OLD behavior is to always add the flags
  1299. add_shlib_flags = true;
  1300. break;
  1301. case cmPolicies::REQUIRED_IF_USED:
  1302. case cmPolicies::REQUIRED_ALWAYS:
  1303. this->IssueMessage(
  1304. MessageType::FATAL_ERROR,
  1305. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0065));
  1306. CM_FALLTHROUGH;
  1307. case cmPolicies::NEW:
  1308. // NEW behavior is to only add the flags if ENABLE_EXPORTS is on
  1309. add_shlib_flags = tgt.GetPropertyAsBool("ENABLE_EXPORTS");
  1310. break;
  1311. }
  1312. if (add_shlib_flags) {
  1313. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1314. linkFlagsVar += linkLanguage;
  1315. linkFlagsVar += "_FLAGS";
  1316. linkFlags = this->Makefile->GetSafeDefinition(linkFlagsVar);
  1317. }
  1318. }
  1319. return linkFlags;
  1320. }
  1321. void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
  1322. cmGeneratorTarget const* target,
  1323. const std::string& lang,
  1324. const std::string& config)
  1325. {
  1326. // Only add macOS specific flags on Darwin platforms (macOS and iOS):
  1327. if (this->Makefile->IsOn("APPLE") && this->EmitUniversalBinaryFlags) {
  1328. std::vector<std::string> archs;
  1329. target->GetAppleArchs(config, archs);
  1330. const char* sysroot = this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1331. if (sysroot && sysroot[0] == '/' && !sysroot[1]) {
  1332. sysroot = nullptr;
  1333. }
  1334. std::string sysrootFlagVar =
  1335. std::string("CMAKE_") + lang + "_SYSROOT_FLAG";
  1336. const char* sysrootFlag = this->Makefile->GetDefinition(sysrootFlagVar);
  1337. const char* deploymentTarget =
  1338. this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  1339. std::string deploymentTargetFlagVar =
  1340. std::string("CMAKE_") + lang + "_OSX_DEPLOYMENT_TARGET_FLAG";
  1341. const char* deploymentTargetFlag =
  1342. this->Makefile->GetDefinition(deploymentTargetFlagVar);
  1343. if (!archs.empty() && !lang.empty() &&
  1344. (lang[0] == 'C' || lang[0] == 'F')) {
  1345. for (std::string const& arch : archs) {
  1346. flags += " -arch ";
  1347. flags += arch;
  1348. }
  1349. }
  1350. if (sysrootFlag && *sysrootFlag && sysroot && *sysroot) {
  1351. flags += " ";
  1352. flags += sysrootFlag;
  1353. flags += " ";
  1354. flags += this->ConvertToOutputFormat(sysroot, SHELL);
  1355. }
  1356. if (deploymentTargetFlag && *deploymentTargetFlag && deploymentTarget &&
  1357. *deploymentTarget) {
  1358. flags += " ";
  1359. flags += deploymentTargetFlag;
  1360. flags += deploymentTarget;
  1361. }
  1362. }
  1363. }
  1364. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1365. cmGeneratorTarget const* target,
  1366. const std::string& lang,
  1367. const std::string& config)
  1368. {
  1369. // Add language-specific flags.
  1370. std::string flagsVar = "CMAKE_";
  1371. flagsVar += lang;
  1372. flagsVar += "_FLAGS";
  1373. this->AddConfigVariableFlags(flags, flagsVar, config);
  1374. // Placeholder for possible future per-target flags.
  1375. static_cast<void>(target);
  1376. }
  1377. void cmLocalGenerator::AddLanguageFlagsForLinking(
  1378. std::string& flags, cmGeneratorTarget const* target, const std::string& lang,
  1379. const std::string& config)
  1380. {
  1381. if (this->Makefile->IsOn("CMAKE_" + lang +
  1382. "_LINK_WITH_STANDARD_COMPILE_OPTION")) {
  1383. // This toolchain requires use of the language standard flag
  1384. // when linking in order to use the matching standard library.
  1385. // FIXME: If CMake gains an abstraction for standard library
  1386. // selection, this will have to be reconciled with it.
  1387. this->AddCompilerRequirementFlag(flags, target, lang);
  1388. }
  1389. this->AddLanguageFlags(flags, target, lang, config);
  1390. if (target->IsIPOEnabled(lang, config)) {
  1391. this->AppendFeatureOptions(flags, lang, "IPO");
  1392. }
  1393. }
  1394. cmGeneratorTarget* cmLocalGenerator::FindGeneratorTargetToUse(
  1395. const std::string& name) const
  1396. {
  1397. GeneratorTargetMap::const_iterator imported =
  1398. this->ImportedGeneratorTargets.find(name);
  1399. if (imported != this->ImportedGeneratorTargets.end()) {
  1400. return imported->second;
  1401. }
  1402. if (cmGeneratorTarget* t = this->FindLocalNonAliasGeneratorTarget(name)) {
  1403. return t;
  1404. }
  1405. return this->GetGlobalGenerator()->FindGeneratorTarget(name);
  1406. }
  1407. bool cmLocalGenerator::GetRealDependency(const std::string& inName,
  1408. const std::string& config,
  1409. std::string& dep)
  1410. {
  1411. // Older CMake code may specify the dependency using the target
  1412. // output file rather than the target name. Such code would have
  1413. // been written before there was support for target properties that
  1414. // modify the name so stripping down to just the file name should
  1415. // produce the target name in this case.
  1416. std::string name = cmSystemTools::GetFilenameName(inName);
  1417. // If the input name is the empty string, there is no real
  1418. // dependency. Short-circuit the other checks:
  1419. if (name.empty()) {
  1420. return false;
  1421. }
  1422. if (cmSystemTools::GetFilenameLastExtension(name) == ".exe") {
  1423. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1424. }
  1425. // Look for a CMake target with the given name.
  1426. if (cmGeneratorTarget* target = this->FindGeneratorTargetToUse(name)) {
  1427. // make sure it is not just a coincidence that the target name
  1428. // found is part of the inName
  1429. if (cmSystemTools::FileIsFullPath(inName)) {
  1430. std::string tLocation;
  1431. if (target->GetType() >= cmStateEnums::EXECUTABLE &&
  1432. target->GetType() <= cmStateEnums::MODULE_LIBRARY) {
  1433. tLocation = target->GetLocation(config);
  1434. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1435. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  1436. }
  1437. std::string depLocation =
  1438. cmSystemTools::GetFilenamePath(std::string(inName));
  1439. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  1440. if (depLocation != tLocation) {
  1441. // it is a full path to a depend that has the same name
  1442. // as a target but is in a different location so do not use
  1443. // the target as the depend
  1444. dep = inName;
  1445. return true;
  1446. }
  1447. }
  1448. switch (target->GetType()) {
  1449. case cmStateEnums::EXECUTABLE:
  1450. case cmStateEnums::STATIC_LIBRARY:
  1451. case cmStateEnums::SHARED_LIBRARY:
  1452. case cmStateEnums::MODULE_LIBRARY:
  1453. case cmStateEnums::UNKNOWN_LIBRARY:
  1454. dep = target->GetLocation(config);
  1455. return true;
  1456. case cmStateEnums::OBJECT_LIBRARY:
  1457. // An object library has no single file on which to depend.
  1458. // This was listed to get the target-level dependency.
  1459. return false;
  1460. case cmStateEnums::INTERFACE_LIBRARY:
  1461. // An interface library has no file on which to depend.
  1462. // This was listed to get the target-level dependency.
  1463. return false;
  1464. case cmStateEnums::UTILITY:
  1465. case cmStateEnums::GLOBAL_TARGET:
  1466. // A utility target has no file on which to depend. This was listed
  1467. // only to get the target-level dependency.
  1468. return false;
  1469. }
  1470. }
  1471. // The name was not that of a CMake target. It must name a file.
  1472. if (cmSystemTools::FileIsFullPath(inName)) {
  1473. // This is a full path. Return it as given.
  1474. dep = inName;
  1475. return true;
  1476. }
  1477. // Check for a source file in this directory that matches the
  1478. // dependency.
  1479. if (cmSourceFile* sf = this->Makefile->GetSource(inName)) {
  1480. dep = sf->GetFullPath();
  1481. return true;
  1482. }
  1483. // Treat the name as relative to the source directory in which it
  1484. // was given.
  1485. dep = this->StateSnapshot.GetDirectory().GetCurrentSource();
  1486. dep += "/";
  1487. dep += inName;
  1488. return true;
  1489. }
  1490. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1491. const std::string& lang, bool shared)
  1492. {
  1493. std::string flagsVar;
  1494. // Add flags for dealing with shared libraries for this language.
  1495. if (shared) {
  1496. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1497. flagsVar += lang;
  1498. flagsVar += "_FLAGS";
  1499. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1500. }
  1501. }
  1502. void cmLocalGenerator::AddCompilerRequirementFlag(
  1503. std::string& flags, cmGeneratorTarget const* target, const std::string& lang)
  1504. {
  1505. if (lang.empty()) {
  1506. return;
  1507. }
  1508. const char* defaultStd =
  1509. this->Makefile->GetDefinition("CMAKE_" + lang + "_STANDARD_DEFAULT");
  1510. if (!defaultStd || !*defaultStd) {
  1511. // This compiler has no notion of language standard levels.
  1512. return;
  1513. }
  1514. std::string extProp = lang + "_EXTENSIONS";
  1515. bool ext = true;
  1516. if (const char* extPropValue = target->GetProperty(extProp)) {
  1517. if (cmSystemTools::IsOff(extPropValue)) {
  1518. ext = false;
  1519. }
  1520. }
  1521. std::string stdProp = lang + "_STANDARD";
  1522. const char* standardProp = target->GetProperty(stdProp);
  1523. if (!standardProp) {
  1524. if (ext) {
  1525. // No language standard is specified and extensions are not disabled.
  1526. // Check if this compiler needs a flag to enable extensions.
  1527. std::string const option_flag =
  1528. "CMAKE_" + lang + "_EXTENSION_COMPILE_OPTION";
  1529. if (const char* opt =
  1530. target->Target->GetMakefile()->GetDefinition(option_flag)) {
  1531. std::vector<std::string> optVec;
  1532. cmSystemTools::ExpandListArgument(opt, optVec);
  1533. for (std::string const& i : optVec) {
  1534. this->AppendFlagEscape(flags, i);
  1535. }
  1536. }
  1537. }
  1538. return;
  1539. }
  1540. std::string const type = ext ? "EXTENSION" : "STANDARD";
  1541. if (target->GetPropertyAsBool(lang + "_STANDARD_REQUIRED")) {
  1542. std::string option_flag =
  1543. "CMAKE_" + lang + standardProp + "_" + type + "_COMPILE_OPTION";
  1544. const char* opt =
  1545. target->Target->GetMakefile()->GetDefinition(option_flag);
  1546. if (!opt) {
  1547. std::ostringstream e;
  1548. e << "Target \"" << target->GetName()
  1549. << "\" requires the language "
  1550. "dialect \""
  1551. << lang << standardProp << "\" "
  1552. << (ext ? "(with compiler extensions)" : "")
  1553. << ", but CMake "
  1554. "does not know the compile flags to use to enable it.";
  1555. this->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1556. } else {
  1557. std::vector<std::string> optVec;
  1558. cmSystemTools::ExpandListArgument(opt, optVec);
  1559. for (std::string const& i : optVec) {
  1560. this->AppendFlagEscape(flags, i);
  1561. }
  1562. }
  1563. return;
  1564. }
  1565. static std::map<std::string, std::vector<std::string>> langStdMap;
  1566. if (langStdMap.empty()) {
  1567. // Maintain sorted order, most recent first.
  1568. langStdMap["CXX"].emplace_back("20");
  1569. langStdMap["CXX"].emplace_back("17");
  1570. langStdMap["CXX"].emplace_back("14");
  1571. langStdMap["CXX"].emplace_back("11");
  1572. langStdMap["CXX"].emplace_back("98");
  1573. langStdMap["C"].emplace_back("11");
  1574. langStdMap["C"].emplace_back("99");
  1575. langStdMap["C"].emplace_back("90");
  1576. langStdMap["CUDA"].emplace_back("14");
  1577. langStdMap["CUDA"].emplace_back("11");
  1578. langStdMap["CUDA"].emplace_back("98");
  1579. }
  1580. std::string standard(standardProp);
  1581. std::vector<std::string>& stds = langStdMap[lang];
  1582. std::vector<std::string>::const_iterator stdIt =
  1583. std::find(stds.begin(), stds.end(), standard);
  1584. if (stdIt == stds.end()) {
  1585. std::string e =
  1586. lang + "_STANDARD is set to invalid value '" + standard + "'";
  1587. this->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  1588. MessageType::FATAL_ERROR, e, target->GetBacktrace());
  1589. return;
  1590. }
  1591. std::vector<std::string>::const_iterator defaultStdIt =
  1592. std::find(stds.begin(), stds.end(), defaultStd);
  1593. if (defaultStdIt == stds.end()) {
  1594. std::string e = "CMAKE_" + lang +
  1595. "_STANDARD_DEFAULT is set to invalid value '" + std::string(defaultStd) +
  1596. "'";
  1597. this->IssueMessage(MessageType::INTERNAL_ERROR, e);
  1598. return;
  1599. }
  1600. // If the standard requested is older than the compiler's default
  1601. // then we need to use a flag to change it. The comparison is
  1602. // greater-or-equal because the standards are stored in backward
  1603. // chronological order.
  1604. if (stdIt >= defaultStdIt) {
  1605. std::string option_flag =
  1606. "CMAKE_" + lang + *stdIt + "_" + type + "_COMPILE_OPTION";
  1607. std::string const& opt =
  1608. target->Target->GetMakefile()->GetRequiredDefinition(option_flag);
  1609. std::vector<std::string> optVec;
  1610. cmSystemTools::ExpandListArgument(opt, optVec);
  1611. for (std::string const& i : optVec) {
  1612. this->AppendFlagEscape(flags, i);
  1613. }
  1614. return;
  1615. }
  1616. // The standard requested is at least as new as the compiler's default,
  1617. // and the standard request is not required. Decay to the newest standard
  1618. // for which a flag is defined.
  1619. for (; stdIt < defaultStdIt; ++stdIt) {
  1620. std::string option_flag =
  1621. "CMAKE_" + lang + *stdIt + "_" + type + "_COMPILE_OPTION";
  1622. if (const char* opt =
  1623. target->Target->GetMakefile()->GetDefinition(option_flag)) {
  1624. std::vector<std::string> optVec;
  1625. cmSystemTools::ExpandListArgument(opt, optVec);
  1626. for (std::string const& i : optVec) {
  1627. this->AppendFlagEscape(flags, i);
  1628. }
  1629. return;
  1630. }
  1631. }
  1632. }
  1633. static void AddVisibilityCompileOption(std::string& flags,
  1634. cmGeneratorTarget const* target,
  1635. cmLocalGenerator* lg,
  1636. const std::string& lang,
  1637. std::string* warnCMP0063)
  1638. {
  1639. std::string compileOption = "CMAKE_" + lang + "_COMPILE_OPTIONS_VISIBILITY";
  1640. const char* opt = lg->GetMakefile()->GetDefinition(compileOption);
  1641. if (!opt) {
  1642. return;
  1643. }
  1644. std::string flagDefine = lang + "_VISIBILITY_PRESET";
  1645. const char* prop = target->GetProperty(flagDefine);
  1646. if (!prop) {
  1647. return;
  1648. }
  1649. if (warnCMP0063) {
  1650. *warnCMP0063 += " " + flagDefine + "\n";
  1651. return;
  1652. }
  1653. if (strcmp(prop, "hidden") != 0 && strcmp(prop, "default") != 0 &&
  1654. strcmp(prop, "protected") != 0 && strcmp(prop, "internal") != 0) {
  1655. std::ostringstream e;
  1656. e << "Target " << target->GetName() << " uses unsupported value \"" << prop
  1657. << "\" for " << flagDefine << ".";
  1658. cmSystemTools::Error(e.str());
  1659. return;
  1660. }
  1661. std::string option = std::string(opt) + prop;
  1662. lg->AppendFlags(flags, option);
  1663. }
  1664. static void AddInlineVisibilityCompileOption(std::string& flags,
  1665. cmGeneratorTarget const* target,
  1666. cmLocalGenerator* lg,
  1667. std::string* warnCMP0063)
  1668. {
  1669. std::string compileOption =
  1670. "CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN";
  1671. const char* opt = lg->GetMakefile()->GetDefinition(compileOption);
  1672. if (!opt) {
  1673. return;
  1674. }
  1675. bool prop = target->GetPropertyAsBool("VISIBILITY_INLINES_HIDDEN");
  1676. if (!prop) {
  1677. return;
  1678. }
  1679. if (warnCMP0063) {
  1680. *warnCMP0063 += " VISIBILITY_INLINES_HIDDEN\n";
  1681. return;
  1682. }
  1683. lg->AppendFlags(flags, opt);
  1684. }
  1685. void cmLocalGenerator::AddVisibilityPresetFlags(
  1686. std::string& flags, cmGeneratorTarget const* target, const std::string& lang)
  1687. {
  1688. if (lang.empty()) {
  1689. return;
  1690. }
  1691. std::string warnCMP0063;
  1692. std::string* pWarnCMP0063 = nullptr;
  1693. if (target->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1694. target->GetType() != cmStateEnums::MODULE_LIBRARY &&
  1695. !target->IsExecutableWithExports()) {
  1696. switch (target->GetPolicyStatusCMP0063()) {
  1697. case cmPolicies::OLD:
  1698. return;
  1699. case cmPolicies::WARN:
  1700. pWarnCMP0063 = &warnCMP0063;
  1701. break;
  1702. default:
  1703. break;
  1704. }
  1705. }
  1706. AddVisibilityCompileOption(flags, target, this, lang, pWarnCMP0063);
  1707. if (lang == "CXX") {
  1708. AddInlineVisibilityCompileOption(flags, target, this, pWarnCMP0063);
  1709. }
  1710. if (!warnCMP0063.empty() && this->WarnCMP0063.insert(target).second) {
  1711. std::ostringstream w;
  1712. /* clang-format off */
  1713. w <<
  1714. cmPolicies::GetPolicyWarning(cmPolicies::CMP0063) << "\n"
  1715. "Target \"" << target->GetName() << "\" of "
  1716. "type \"" << cmState::GetTargetTypeName(target->GetType()) << "\" "
  1717. "has the following visibility properties set for " << lang << ":\n" <<
  1718. warnCMP0063 <<
  1719. "For compatibility CMake is not honoring them for this target.";
  1720. /* clang-format on */
  1721. target->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  1722. MessageType::AUTHOR_WARNING, w.str(), target->GetBacktrace());
  1723. }
  1724. }
  1725. void cmLocalGenerator::AddCMP0018Flags(std::string& flags,
  1726. cmGeneratorTarget const* target,
  1727. std::string const& lang,
  1728. const std::string& config)
  1729. {
  1730. int targetType = target->GetType();
  1731. bool shared = ((targetType == cmStateEnums::SHARED_LIBRARY) ||
  1732. (targetType == cmStateEnums::MODULE_LIBRARY));
  1733. if (this->GetShouldUseOldFlags(shared, lang)) {
  1734. this->AddSharedFlags(flags, lang, shared);
  1735. } else {
  1736. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1737. if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE")) {
  1738. this->AddPositionIndependentFlags(flags, lang, targetType);
  1739. }
  1740. return;
  1741. }
  1742. if (target->GetLinkInterfaceDependentBoolProperty(
  1743. "POSITION_INDEPENDENT_CODE", config)) {
  1744. this->AddPositionIndependentFlags(flags, lang, targetType);
  1745. }
  1746. if (shared) {
  1747. this->AppendFeatureOptions(flags, lang, "DLL");
  1748. }
  1749. }
  1750. }
  1751. bool cmLocalGenerator::GetShouldUseOldFlags(bool shared,
  1752. const std::string& lang) const
  1753. {
  1754. std::string originalFlags =
  1755. this->GlobalGenerator->GetSharedLibFlagsForLanguage(lang);
  1756. if (shared) {
  1757. std::string flagsVar = "CMAKE_SHARED_LIBRARY_";
  1758. flagsVar += lang;
  1759. flagsVar += "_FLAGS";
  1760. std::string const& flags = this->Makefile->GetSafeDefinition(flagsVar);
  1761. if (flags != originalFlags) {
  1762. switch (this->GetPolicyStatus(cmPolicies::CMP0018)) {
  1763. case cmPolicies::WARN: {
  1764. std::ostringstream e;
  1765. e << "Variable " << flagsVar
  1766. << " has been modified. CMake "
  1767. "will ignore the POSITION_INDEPENDENT_CODE target property for "
  1768. "shared libraries and will use the "
  1769. << flagsVar
  1770. << " variable "
  1771. "instead. This may cause errors if the original content of "
  1772. << flagsVar << " was removed.\n"
  1773. << cmPolicies::GetPolicyWarning(cmPolicies::CMP0018);
  1774. this->IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  1775. CM_FALLTHROUGH;
  1776. }
  1777. case cmPolicies::OLD:
  1778. return true;
  1779. case cmPolicies::REQUIRED_IF_USED:
  1780. case cmPolicies::REQUIRED_ALWAYS:
  1781. case cmPolicies::NEW:
  1782. return false;
  1783. }
  1784. }
  1785. }
  1786. return false;
  1787. }
  1788. void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags,
  1789. std::string const& lang,
  1790. int targetType)
  1791. {
  1792. std::string picFlags;
  1793. if (targetType == cmStateEnums::EXECUTABLE) {
  1794. std::string flagsVar = "CMAKE_";
  1795. flagsVar += lang;
  1796. flagsVar += "_COMPILE_OPTIONS_PIE";
  1797. picFlags = this->Makefile->GetSafeDefinition(flagsVar);
  1798. }
  1799. if (picFlags.empty()) {
  1800. std::string flagsVar = "CMAKE_";
  1801. flagsVar += lang;
  1802. flagsVar += "_COMPILE_OPTIONS_PIC";
  1803. picFlags = this->Makefile->GetSafeDefinition(flagsVar);
  1804. }
  1805. if (!picFlags.empty()) {
  1806. std::vector<std::string> options;
  1807. cmSystemTools::ExpandListArgument(picFlags, options);
  1808. for (std::string const& o : options) {
  1809. this->AppendFlagEscape(flags, o);
  1810. }
  1811. }
  1812. }
  1813. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1814. const std::string& var,
  1815. const std::string& config)
  1816. {
  1817. // Add the flags from the variable itself.
  1818. std::string flagsVar = var;
  1819. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1820. // Add the flags from the build-type specific variable.
  1821. if (!config.empty()) {
  1822. flagsVar += "_";
  1823. flagsVar += cmSystemTools::UpperCase(config);
  1824. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1825. }
  1826. }
  1827. void cmLocalGenerator::AppendFlags(std::string& flags,
  1828. const std::string& newFlags) const
  1829. {
  1830. if (!newFlags.empty()) {
  1831. if (!flags.empty()) {
  1832. flags += " ";
  1833. }
  1834. flags += newFlags;
  1835. }
  1836. }
  1837. void cmLocalGenerator::AppendFlags(std::string& flags,
  1838. const char* newFlags) const
  1839. {
  1840. if (newFlags && *newFlags) {
  1841. this->AppendFlags(flags, std::string(newFlags));
  1842. }
  1843. }
  1844. void cmLocalGenerator::AppendFlagEscape(std::string& flags,
  1845. const std::string& rawFlag) const
  1846. {
  1847. this->AppendFlags(flags, this->EscapeForShell(rawFlag));
  1848. }
  1849. void cmLocalGenerator::AppendIPOLinkerFlags(std::string& flags,
  1850. cmGeneratorTarget* target,
  1851. const std::string& config,
  1852. const std::string& lang)
  1853. {
  1854. if (!target->IsIPOEnabled(lang, config)) {
  1855. return;
  1856. }
  1857. switch (target->GetType()) {
  1858. case cmStateEnums::EXECUTABLE:
  1859. case cmStateEnums::SHARED_LIBRARY:
  1860. case cmStateEnums::MODULE_LIBRARY:
  1861. break;
  1862. default:
  1863. return;
  1864. }
  1865. const std::string name = "CMAKE_" + lang + "_LINK_OPTIONS_IPO";
  1866. const char* rawFlagsList = this->Makefile->GetDefinition(name);
  1867. if (rawFlagsList == nullptr) {
  1868. return;
  1869. }
  1870. std::vector<std::string> flagsList;
  1871. cmSystemTools::ExpandListArgument(rawFlagsList, flagsList);
  1872. for (std::string const& o : flagsList) {
  1873. this->AppendFlagEscape(flags, o);
  1874. }
  1875. }
  1876. void cmLocalGenerator::AppendPositionIndependentLinkerFlags(
  1877. std::string& flags, cmGeneratorTarget* target, const std::string& config,
  1878. const std::string& lang)
  1879. {
  1880. // For now, only EXECUTABLE is concerned
  1881. if (target->GetType() != cmStateEnums::EXECUTABLE) {
  1882. return;
  1883. }
  1884. const char* PICValue = target->GetLinkPIEProperty(config);
  1885. if (PICValue == nullptr) {
  1886. // POSITION_INDEPENDENT_CODE is not set
  1887. return;
  1888. }
  1889. const std::string mode = cmSystemTools::IsOn(PICValue) ? "PIE" : "NO_PIE";
  1890. std::string supported = "CMAKE_" + lang + "_LINK_" + mode + "_SUPPORTED";
  1891. if (cmSystemTools::IsOff(this->Makefile->GetDefinition(supported))) {
  1892. return;
  1893. }
  1894. std::string name = "CMAKE_" + lang + "_LINK_OPTIONS_" + mode;
  1895. auto pieFlags = this->Makefile->GetSafeDefinition(name);
  1896. if (pieFlags.empty()) {
  1897. return;
  1898. }
  1899. std::vector<std::string> flagsList;
  1900. cmSystemTools::ExpandListArgument(pieFlags, flagsList);
  1901. for (const auto& flag : flagsList) {
  1902. this->AppendFlagEscape(flags, flag);
  1903. }
  1904. }
  1905. void cmLocalGenerator::AppendCompileOptions(std::string& options,
  1906. const char* options_list,
  1907. const char* regex) const
  1908. {
  1909. // Short-circuit if there are no options.
  1910. if (!options_list) {
  1911. return;
  1912. }
  1913. // Expand the list of options.
  1914. std::vector<std::string> options_vec;
  1915. cmSystemTools::ExpandListArgument(options_list, options_vec);
  1916. this->AppendCompileOptions(options, options_vec, regex);
  1917. }
  1918. void cmLocalGenerator::AppendCompileOptions(
  1919. std::string& options, const std::vector<std::string>& options_vec,
  1920. const char* regex) const
  1921. {
  1922. if (regex != nullptr) {
  1923. // Filter flags upon specified reges.
  1924. cmsys::RegularExpression r(regex);
  1925. for (std::string const& opt : options_vec) {
  1926. if (r.find(opt)) {
  1927. this->AppendFlagEscape(options, opt);
  1928. }
  1929. }
  1930. } else {
  1931. for (std::string const& opt : options_vec) {
  1932. this->AppendFlagEscape(options, opt);
  1933. }
  1934. }
  1935. }
  1936. void cmLocalGenerator::AppendIncludeDirectories(
  1937. std::vector<std::string>& includes, const char* includes_list,
  1938. const cmSourceFile& sourceFile) const
  1939. {
  1940. // Short-circuit if there are no includes.
  1941. if (!includes_list) {
  1942. return;
  1943. }
  1944. // Expand the list of includes.
  1945. std::vector<std::string> includes_vec;
  1946. cmSystemTools::ExpandListArgument(includes_list, includes_vec);
  1947. this->AppendIncludeDirectories(includes, includes_vec, sourceFile);
  1948. }
  1949. void cmLocalGenerator::AppendIncludeDirectories(
  1950. std::vector<std::string>& includes,
  1951. const std::vector<std::string>& includes_vec,
  1952. const cmSourceFile& sourceFile) const
  1953. {
  1954. std::unordered_set<std::string> uniqueIncludes;
  1955. for (const std::string& include : includes_vec) {
  1956. if (!cmSystemTools::FileIsFullPath(include)) {
  1957. std::ostringstream e;
  1958. e << "Found relative path while evaluating include directories of "
  1959. "\""
  1960. << sourceFile.GetLocation().GetName() << "\":\n \"" << include
  1961. << "\"\n";
  1962. this->IssueMessage(MessageType::FATAL_ERROR, e.str());
  1963. return;
  1964. }
  1965. std::string inc = include;
  1966. if (!cmSystemTools::IsOff(inc)) {
  1967. cmSystemTools::ConvertToUnixSlashes(inc);
  1968. }
  1969. if (uniqueIncludes.insert(inc).second) {
  1970. includes.push_back(std::move(inc));
  1971. }
  1972. }
  1973. }
  1974. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  1975. std::string const& defines_list) const
  1976. {
  1977. std::set<BT<std::string>> tmp;
  1978. this->AppendDefines(tmp, ExpandListWithBacktrace(defines_list));
  1979. for (BT<std::string> const& i : tmp) {
  1980. defines.emplace(i.Value);
  1981. }
  1982. }
  1983. void cmLocalGenerator::AppendDefines(std::set<BT<std::string>>& defines,
  1984. std::string const& defines_list) const
  1985. {
  1986. // Short-circuit if there are no definitions.
  1987. if (defines_list.empty()) {
  1988. return;
  1989. }
  1990. // Expand the list of definitions.
  1991. this->AppendDefines(defines, ExpandListWithBacktrace(defines_list));
  1992. }
  1993. void cmLocalGenerator::AppendDefines(
  1994. std::set<BT<std::string>>& defines,
  1995. const std::vector<BT<std::string>>& defines_vec) const
  1996. {
  1997. for (BT<std::string> const& d : defines_vec) {
  1998. // Skip unsupported definitions.
  1999. if (!this->CheckDefinition(d.Value)) {
  2000. continue;
  2001. }
  2002. defines.insert(d);
  2003. }
  2004. }
  2005. void cmLocalGenerator::JoinDefines(const std::set<std::string>& defines,
  2006. std::string& definesString,
  2007. const std::string& lang)
  2008. {
  2009. // Lookup the define flag for the current language.
  2010. std::string dflag = "-D";
  2011. if (!lang.empty()) {
  2012. std::string defineFlagVar = "CMAKE_";
  2013. defineFlagVar += lang;
  2014. defineFlagVar += "_DEFINE_FLAG";
  2015. const char* df = this->Makefile->GetDefinition(defineFlagVar);
  2016. if (df && *df) {
  2017. dflag = df;
  2018. }
  2019. }
  2020. const char* itemSeparator = definesString.empty() ? "" : " ";
  2021. for (std::string const& define : defines) {
  2022. // Append the definition with proper escaping.
  2023. std::string def = dflag;
  2024. if (this->GetState()->UseWatcomWMake()) {
  2025. // The Watcom compiler does its own command line parsing instead
  2026. // of using the windows shell rules. Definitions are one of
  2027. // -DNAME
  2028. // -DNAME=<cpp-token>
  2029. // -DNAME="c-string with spaces and other characters(?@#$)"
  2030. //
  2031. // Watcom will properly parse each of these cases from the
  2032. // command line without any escapes. However we still have to
  2033. // get the '$' and '#' characters through WMake as '$$' and
  2034. // '$#'.
  2035. for (const char* c = define.c_str(); *c; ++c) {
  2036. if (*c == '$' || *c == '#') {
  2037. def += '$';
  2038. }
  2039. def += *c;
  2040. }
  2041. } else {
  2042. // Make the definition appear properly on the command line. Use
  2043. // -DNAME="value" instead of -D"NAME=value" for historical reasons.
  2044. std::string::size_type eq = define.find('=');
  2045. def += define.substr(0, eq);
  2046. if (eq != std::string::npos) {
  2047. def += "=";
  2048. def += this->EscapeForShell(define.substr(eq + 1), true);
  2049. }
  2050. }
  2051. definesString += itemSeparator;
  2052. itemSeparator = " ";
  2053. definesString += def;
  2054. }
  2055. }
  2056. void cmLocalGenerator::AppendFeatureOptions(std::string& flags,
  2057. const std::string& lang,
  2058. const char* feature)
  2059. {
  2060. std::string optVar = "CMAKE_";
  2061. optVar += lang;
  2062. optVar += "_COMPILE_OPTIONS_";
  2063. optVar += feature;
  2064. if (const char* optionList = this->Makefile->GetDefinition(optVar)) {
  2065. std::vector<std::string> options;
  2066. cmSystemTools::ExpandListArgument(optionList, options);
  2067. for (std::string const& o : options) {
  2068. this->AppendFlagEscape(flags, o);
  2069. }
  2070. }
  2071. }
  2072. const char* cmLocalGenerator::GetFeature(const std::string& feature,
  2073. const std::string& config)
  2074. {
  2075. std::string featureName = feature;
  2076. // TODO: Define accumulation policy for features (prepend, append, replace).
  2077. // Currently we always replace.
  2078. if (!config.empty()) {
  2079. featureName += "_";
  2080. featureName += cmSystemTools::UpperCase(config);
  2081. }
  2082. cmStateSnapshot snp = this->StateSnapshot;
  2083. while (snp.IsValid()) {
  2084. if (const char* value = snp.GetDirectory().GetProperty(featureName)) {
  2085. return value;
  2086. }
  2087. snp = snp.GetBuildsystemDirectoryParent();
  2088. }
  2089. return nullptr;
  2090. }
  2091. std::string cmLocalGenerator::GetProjectName() const
  2092. {
  2093. return this->StateSnapshot.GetProjectName();
  2094. }
  2095. std::string cmLocalGenerator::ConstructComment(
  2096. cmCustomCommandGenerator const& ccg, const char* default_comment)
  2097. {
  2098. // Check for a comment provided with the command.
  2099. if (ccg.GetComment()) {
  2100. return ccg.GetComment();
  2101. }
  2102. // Construct a reasonable default comment if possible.
  2103. if (!ccg.GetOutputs().empty()) {
  2104. std::string comment;
  2105. comment = "Generating ";
  2106. const char* sep = "";
  2107. std::string currentBinaryDir = this->GetCurrentBinaryDirectory();
  2108. for (std::string const& o : ccg.GetOutputs()) {
  2109. comment += sep;
  2110. comment += this->MaybeConvertToRelativePath(currentBinaryDir, o);
  2111. sep = ", ";
  2112. }
  2113. return comment;
  2114. }
  2115. // Otherwise use the provided default.
  2116. return default_comment;
  2117. }
  2118. class cmInstallTargetGeneratorLocal : public cmInstallTargetGenerator
  2119. {
  2120. public:
  2121. cmInstallTargetGeneratorLocal(cmLocalGenerator* lg, std::string const& t,
  2122. const char* dest, bool implib)
  2123. : cmInstallTargetGenerator(
  2124. t, dest, implib, "", std::vector<std::string>(), "Unspecified",
  2125. cmInstallGenerator::SelectMessageLevel(lg->GetMakefile()), false,
  2126. false)
  2127. {
  2128. this->Compute(lg);
  2129. }
  2130. };
  2131. void cmLocalGenerator::GenerateTargetInstallRules(
  2132. std::ostream& os, const std::string& config,
  2133. std::vector<std::string> const& configurationTypes)
  2134. {
  2135. // Convert the old-style install specification from each target to
  2136. // an install generator and run it.
  2137. const std::vector<cmGeneratorTarget*>& tgts = this->GetGeneratorTargets();
  2138. for (cmGeneratorTarget* l : tgts) {
  2139. if (l->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2140. continue;
  2141. }
  2142. // Include the user-specified pre-install script for this target.
  2143. if (const char* preinstall = l->GetProperty("PRE_INSTALL_SCRIPT")) {
  2144. cmInstallScriptGenerator g(preinstall, false, nullptr, false);
  2145. g.Generate(os, config, configurationTypes);
  2146. }
  2147. // Install this target if a destination is given.
  2148. if (!l->Target->GetInstallPath().empty()) {
  2149. // Compute the full install destination. Note that converting
  2150. // to unix slashes also removes any trailing slash.
  2151. // We also skip over the leading slash given by the user.
  2152. std::string destination = l->Target->GetInstallPath().substr(1);
  2153. cmSystemTools::ConvertToUnixSlashes(destination);
  2154. if (destination.empty()) {
  2155. destination = ".";
  2156. }
  2157. // Generate the proper install generator for this target type.
  2158. switch (l->GetType()) {
  2159. case cmStateEnums::EXECUTABLE:
  2160. case cmStateEnums::STATIC_LIBRARY:
  2161. case cmStateEnums::MODULE_LIBRARY: {
  2162. // Use a target install generator.
  2163. cmInstallTargetGeneratorLocal g(this, l->GetName(),
  2164. destination.c_str(), false);
  2165. g.Generate(os, config, configurationTypes);
  2166. } break;
  2167. case cmStateEnums::SHARED_LIBRARY: {
  2168. #if defined(_WIN32) || defined(__CYGWIN__)
  2169. // Special code to handle DLL. Install the import library
  2170. // to the normal destination and the DLL to the runtime
  2171. // destination.
  2172. cmInstallTargetGeneratorLocal g1(this, l->GetName(),
  2173. destination.c_str(), true);
  2174. g1.Generate(os, config, configurationTypes);
  2175. // We also skip over the leading slash given by the user.
  2176. destination = l->Target->GetRuntimeInstallPath().substr(1);
  2177. cmSystemTools::ConvertToUnixSlashes(destination);
  2178. cmInstallTargetGeneratorLocal g2(this, l->GetName(),
  2179. destination.c_str(), false);
  2180. g2.Generate(os, config, configurationTypes);
  2181. #else
  2182. // Use a target install generator.
  2183. cmInstallTargetGeneratorLocal g(this, l->GetName(),
  2184. destination.c_str(), false);
  2185. g.Generate(os, config, configurationTypes);
  2186. #endif
  2187. } break;
  2188. default:
  2189. break;
  2190. }
  2191. }
  2192. // Include the user-specified post-install script for this target.
  2193. if (const char* postinstall = l->GetProperty("POST_INSTALL_SCRIPT")) {
  2194. cmInstallScriptGenerator g(postinstall, false, nullptr, false);
  2195. g.Generate(os, config, configurationTypes);
  2196. }
  2197. }
  2198. }
  2199. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2200. static bool cmLocalGeneratorShortenObjectName(std::string& objName,
  2201. std::string::size_type max_len)
  2202. {
  2203. // Replace the beginning of the path portion of the object name with
  2204. // its own md5 sum.
  2205. std::string::size_type pos =
  2206. objName.find('/', objName.size() - max_len + 32);
  2207. if (pos != std::string::npos) {
  2208. cmCryptoHash md5(cmCryptoHash::AlgoMD5);
  2209. std::string md5name = md5.HashString(objName.substr(0, pos));
  2210. md5name += objName.substr(pos);
  2211. objName = md5name;
  2212. // The object name is now short enough.
  2213. return true;
  2214. }
  2215. // The object name could not be shortened enough.
  2216. return false;
  2217. }
  2218. bool cmLocalGeneratorCheckObjectName(std::string& objName,
  2219. std::string::size_type dir_len,
  2220. std::string::size_type max_total_len)
  2221. {
  2222. // Enforce the maximum file name length if possible.
  2223. std::string::size_type max_obj_len = max_total_len;
  2224. if (dir_len < max_total_len) {
  2225. max_obj_len = max_total_len - dir_len;
  2226. if (objName.size() > max_obj_len) {
  2227. // The current object file name is too long. Try to shorten it.
  2228. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2229. }
  2230. // The object file name is short enough.
  2231. return true;
  2232. }
  2233. // The build directory in which the object will be stored is
  2234. // already too deep.
  2235. return false;
  2236. }
  2237. #endif
  2238. std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName(
  2239. const std::string& sin, std::string const& dir_max)
  2240. {
  2241. // Look for an existing mapped name for this object file.
  2242. std::map<std::string, std::string>::iterator it =
  2243. this->UniqueObjectNamesMap.find(sin);
  2244. // If no entry exists create one.
  2245. if (it == this->UniqueObjectNamesMap.end()) {
  2246. // Start with the original name.
  2247. std::string ssin = sin;
  2248. // Avoid full paths by removing leading slashes.
  2249. ssin.erase(0, ssin.find_first_not_of('/'));
  2250. // Avoid full paths by removing colons.
  2251. std::replace(ssin.begin(), ssin.end(), ':', '_');
  2252. // Avoid relative paths that go up the tree.
  2253. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2254. // Avoid spaces.
  2255. std::replace(ssin.begin(), ssin.end(), ' ', '_');
  2256. // Mangle the name if necessary.
  2257. if (this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES")) {
  2258. bool done;
  2259. int cc = 0;
  2260. char rpstr[100];
  2261. sprintf(rpstr, "_p_");
  2262. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2263. std::string sssin = sin;
  2264. do {
  2265. done = true;
  2266. for (it = this->UniqueObjectNamesMap.begin();
  2267. it != this->UniqueObjectNamesMap.end(); ++it) {
  2268. if (it->second == ssin) {
  2269. done = false;
  2270. }
  2271. }
  2272. if (done) {
  2273. break;
  2274. }
  2275. sssin = ssin;
  2276. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2277. sprintf(rpstr, "_p%d_", cc++);
  2278. } while (!done);
  2279. }
  2280. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2281. if (!cmLocalGeneratorCheckObjectName(ssin, dir_max.size(),
  2282. this->ObjectPathMax)) {
  2283. // Warn if this is the first time the path has been seen.
  2284. if (this->ObjectMaxPathViolations.insert(dir_max).second) {
  2285. std::ostringstream m;
  2286. /* clang-format off */
  2287. m << "The object file directory\n"
  2288. << " " << dir_max << "\n"
  2289. << "has " << dir_max.size() << " characters. "
  2290. << "The maximum full path to an object file is "
  2291. << this->ObjectPathMax << " characters "
  2292. << "(see CMAKE_OBJECT_PATH_MAX). "
  2293. << "Object file\n"
  2294. << " " << ssin << "\n"
  2295. << "cannot be safely placed under this directory. "
  2296. << "The build may not work correctly.";
  2297. /* clang-format on */
  2298. this->IssueMessage(MessageType::WARNING, m.str());
  2299. }
  2300. }
  2301. #else
  2302. (void)dir_max;
  2303. #endif
  2304. // Insert the newly mapped object file name.
  2305. std::map<std::string, std::string>::value_type e(sin, ssin);
  2306. it = this->UniqueObjectNamesMap.insert(e).first;
  2307. }
  2308. // Return the map entry.
  2309. return it->second;
  2310. }
  2311. void cmLocalGenerator::ComputeObjectFilenames(
  2312. std::map<cmSourceFile const*, std::string>& /*unused*/,
  2313. cmGeneratorTarget const* /*unused*/)
  2314. {
  2315. }
  2316. bool cmLocalGenerator::IsWindowsShell() const
  2317. {
  2318. return this->GetState()->UseWindowsShell();
  2319. }
  2320. bool cmLocalGenerator::IsWatcomWMake() const
  2321. {
  2322. return this->GetState()->UseWatcomWMake();
  2323. }
  2324. bool cmLocalGenerator::IsMinGWMake() const
  2325. {
  2326. return this->GetState()->UseMinGWMake();
  2327. }
  2328. bool cmLocalGenerator::IsNMake() const
  2329. {
  2330. return this->GetState()->UseNMake();
  2331. }
  2332. std::string cmLocalGenerator::GetObjectFileNameWithoutTarget(
  2333. const cmSourceFile& source, std::string const& dir_max,
  2334. bool* hasSourceExtension, char const* customOutputExtension)
  2335. {
  2336. // Construct the object file name using the full path to the source
  2337. // file which is its only unique identification.
  2338. std::string const& fullPath = source.GetFullPath();
  2339. // Try referencing the source relative to the source tree.
  2340. std::string relFromSource = this->MaybeConvertToRelativePath(
  2341. this->GetCurrentSourceDirectory(), fullPath);
  2342. assert(!relFromSource.empty());
  2343. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource);
  2344. bool subSource = relSource && relFromSource[0] != '.';
  2345. // Try referencing the source relative to the binary tree.
  2346. std::string relFromBinary = this->MaybeConvertToRelativePath(
  2347. this->GetCurrentBinaryDirectory(), fullPath);
  2348. assert(!relFromBinary.empty());
  2349. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary);
  2350. bool subBinary = relBinary && relFromBinary[0] != '.';
  2351. // Select a nice-looking reference to the source file to construct
  2352. // the object file name.
  2353. std::string objectName;
  2354. if ((relSource && !relBinary) || (subSource && !subBinary)) {
  2355. objectName = relFromSource;
  2356. } else if ((relBinary && !relSource) || (subBinary && !subSource)) {
  2357. objectName = relFromBinary;
  2358. } else if (relFromBinary.length() < relFromSource.length()) {
  2359. objectName = relFromBinary;
  2360. } else {
  2361. objectName = relFromSource;
  2362. }
  2363. // if it is still a full path check for the try compile case
  2364. // try compile never have in source sources, and should not
  2365. // have conflicting source file names in the same target
  2366. if (cmSystemTools::FileIsFullPath(objectName)) {
  2367. if (this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile()) {
  2368. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2369. }
  2370. }
  2371. // Replace the original source file extension with the object file
  2372. // extension.
  2373. bool keptSourceExtension = true;
  2374. if (!source.GetPropertyAsBool("KEEP_EXTENSION")) {
  2375. // Decide whether this language wants to replace the source
  2376. // extension with the object extension. For CMake 2.4
  2377. // compatibility do this by default.
  2378. bool replaceExt = this->NeedBackwardsCompatibility_2_4();
  2379. if (!replaceExt) {
  2380. std::string lang = source.GetLanguage();
  2381. if (!lang.empty()) {
  2382. std::string repVar = "CMAKE_";
  2383. repVar += lang;
  2384. repVar += "_OUTPUT_EXTENSION_REPLACE";
  2385. replaceExt = this->Makefile->IsOn(repVar);
  2386. }
  2387. }
  2388. // Remove the source extension if it is to be replaced.
  2389. if (replaceExt || customOutputExtension) {
  2390. keptSourceExtension = false;
  2391. std::string::size_type dot_pos = objectName.rfind('.');
  2392. if (dot_pos != std::string::npos) {
  2393. objectName = objectName.substr(0, dot_pos);
  2394. }
  2395. }
  2396. // Store the new extension.
  2397. if (customOutputExtension) {
  2398. objectName += customOutputExtension;
  2399. } else {
  2400. objectName += this->GlobalGenerator->GetLanguageOutputExtension(source);
  2401. }
  2402. }
  2403. if (hasSourceExtension) {
  2404. *hasSourceExtension = keptSourceExtension;
  2405. }
  2406. // Convert to a safe name.
  2407. return this->CreateSafeUniqueObjectFileName(objectName, dir_max);
  2408. }
  2409. std::string cmLocalGenerator::GetSourceFileLanguage(const cmSourceFile& source)
  2410. {
  2411. return source.GetLanguage();
  2412. }
  2413. cmake* cmLocalGenerator::GetCMakeInstance() const
  2414. {
  2415. return this->GlobalGenerator->GetCMakeInstance();
  2416. }
  2417. std::string const& cmLocalGenerator::GetSourceDirectory() const
  2418. {
  2419. return this->GetCMakeInstance()->GetHomeDirectory();
  2420. }
  2421. std::string const& cmLocalGenerator::GetBinaryDirectory() const
  2422. {
  2423. return this->GetCMakeInstance()->GetHomeOutputDirectory();
  2424. }
  2425. std::string const& cmLocalGenerator::GetCurrentBinaryDirectory() const
  2426. {
  2427. return this->StateSnapshot.GetDirectory().GetCurrentBinary();
  2428. }
  2429. std::string const& cmLocalGenerator::GetCurrentSourceDirectory() const
  2430. {
  2431. return this->StateSnapshot.GetDirectory().GetCurrentSource();
  2432. }
  2433. std::string cmLocalGenerator::MaybeConvertToRelativePath(
  2434. std::string const& local_path, std::string const& remote_path) const
  2435. {
  2436. return this->StateSnapshot.GetDirectory().ConvertToRelPathIfNotContained(
  2437. local_path, remote_path);
  2438. }
  2439. std::string cmLocalGenerator::GetTargetDirectory(
  2440. const cmGeneratorTarget* /*unused*/) const
  2441. {
  2442. cmSystemTools::Error("GetTargetDirectory"
  2443. " called on cmLocalGenerator");
  2444. return "";
  2445. }
  2446. KWIML_INT_uint64_t cmLocalGenerator::GetBackwardsCompatibility()
  2447. {
  2448. // The computed version may change until the project is fully
  2449. // configured.
  2450. if (!this->BackwardsCompatibilityFinal) {
  2451. unsigned int major = 0;
  2452. unsigned int minor = 0;
  2453. unsigned int patch = 0;
  2454. if (const char* value =
  2455. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY")) {
  2456. switch (sscanf(value, "%u.%u.%u", &major, &minor, &patch)) {
  2457. case 2:
  2458. patch = 0;
  2459. break;
  2460. case 1:
  2461. minor = 0;
  2462. patch = 0;
  2463. break;
  2464. default:
  2465. break;
  2466. }
  2467. }
  2468. this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
  2469. this->BackwardsCompatibilityFinal = true;
  2470. }
  2471. return this->BackwardsCompatibility;
  2472. }
  2473. bool cmLocalGenerator::NeedBackwardsCompatibility_2_4()
  2474. {
  2475. // Check the policy to decide whether to pay attention to this
  2476. // variable.
  2477. switch (this->GetPolicyStatus(cmPolicies::CMP0001)) {
  2478. case cmPolicies::WARN:
  2479. // WARN is just OLD without warning because user code does not
  2480. // always affect whether this check is done.
  2481. case cmPolicies::OLD:
  2482. // Old behavior is to check the variable.
  2483. break;
  2484. case cmPolicies::NEW:
  2485. // New behavior is to ignore the variable.
  2486. return false;
  2487. case cmPolicies::REQUIRED_IF_USED:
  2488. case cmPolicies::REQUIRED_ALWAYS:
  2489. // This will never be the case because the only way to require
  2490. // the setting is to require the user to specify version policy
  2491. // 2.6 or higher. Once we add that requirement then this whole
  2492. // method can be removed anyway.
  2493. return false;
  2494. }
  2495. // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
  2496. // equal to or lower than the given version.
  2497. KWIML_INT_uint64_t actual_compat = this->GetBackwardsCompatibility();
  2498. return (actual_compat && actual_compat <= CMake_VERSION_ENCODE(2, 4, 255));
  2499. }
  2500. cmPolicies::PolicyStatus cmLocalGenerator::GetPolicyStatus(
  2501. cmPolicies::PolicyID id) const
  2502. {
  2503. return this->Makefile->GetPolicyStatus(id);
  2504. }
  2505. bool cmLocalGenerator::CheckDefinition(std::string const& define) const
  2506. {
  2507. // Many compilers do not support -DNAME(arg)=sdf so we disable it.
  2508. std::string::size_type pos = define.find_first_of("(=");
  2509. if (pos != std::string::npos) {
  2510. if (define[pos] == '(') {
  2511. std::ostringstream e;
  2512. /* clang-format off */
  2513. e << "WARNING: Function-style preprocessor definitions may not be "
  2514. << "passed on the compiler command line because many compilers "
  2515. << "do not support it.\n"
  2516. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2517. << "Consider defining the macro in a (configured) header file.\n";
  2518. /* clang-format on */
  2519. cmSystemTools::Message(e.str());
  2520. return false;
  2521. }
  2522. }
  2523. // Many compilers do not support # in the value so we disable it.
  2524. if (define.find_first_of('#') != std::string::npos) {
  2525. std::ostringstream e;
  2526. /* clang-format off */
  2527. e << "WARNING: Preprocessor definitions containing '#' may not be "
  2528. << "passed on the compiler command line because many compilers "
  2529. << "do not support it.\n"
  2530. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2531. << "Consider defining the macro in a (configured) header file.\n";
  2532. /* clang-format on */
  2533. cmSystemTools::Message(e.str());
  2534. return false;
  2535. }
  2536. // Assume it is supported.
  2537. return true;
  2538. }
  2539. static void cmLGInfoProp(cmMakefile* mf, cmGeneratorTarget* target,
  2540. const std::string& prop)
  2541. {
  2542. if (const char* val = target->GetProperty(prop)) {
  2543. mf->AddDefinition(prop, val);
  2544. }
  2545. }
  2546. void cmLocalGenerator::GenerateAppleInfoPList(cmGeneratorTarget* target,
  2547. const std::string& targetName,
  2548. const std::string& fname)
  2549. {
  2550. // Find the Info.plist template.
  2551. const char* in = target->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
  2552. std::string inFile = (in && *in) ? in : "MacOSXBundleInfo.plist.in";
  2553. if (!cmSystemTools::FileIsFullPath(inFile)) {
  2554. std::string inMod = this->Makefile->GetModulesFile(inFile);
  2555. if (!inMod.empty()) {
  2556. inFile = inMod;
  2557. }
  2558. }
  2559. if (!cmSystemTools::FileExists(inFile, true)) {
  2560. std::ostringstream e;
  2561. e << "Target " << target->GetName() << " Info.plist template \"" << inFile
  2562. << "\" could not be found.";
  2563. cmSystemTools::Error(e.str());
  2564. return;
  2565. }
  2566. // Convert target properties to variables in an isolated makefile
  2567. // scope to configure the file. If properties are set they will
  2568. // override user make variables. If not the configuration will fall
  2569. // back to the directory-level values set by the user.
  2570. cmMakefile* mf = this->Makefile;
  2571. cmMakefile::ScopePushPop varScope(mf);
  2572. mf->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName.c_str());
  2573. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_INFO_STRING");
  2574. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_ICON_FILE");
  2575. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_GUI_IDENTIFIER");
  2576. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_LONG_VERSION_STRING");
  2577. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_NAME");
  2578. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
  2579. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_VERSION");
  2580. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_COPYRIGHT");
  2581. mf->ConfigureFile(inFile, fname, false, false, false);
  2582. }
  2583. void cmLocalGenerator::GenerateFrameworkInfoPList(
  2584. cmGeneratorTarget* target, const std::string& targetName,
  2585. const std::string& fname)
  2586. {
  2587. // Find the Info.plist template.
  2588. const char* in = target->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
  2589. std::string inFile = (in && *in) ? in : "MacOSXFrameworkInfo.plist.in";
  2590. if (!cmSystemTools::FileIsFullPath(inFile)) {
  2591. std::string inMod = this->Makefile->GetModulesFile(inFile);
  2592. if (!inMod.empty()) {
  2593. inFile = inMod;
  2594. }
  2595. }
  2596. if (!cmSystemTools::FileExists(inFile, true)) {
  2597. std::ostringstream e;
  2598. e << "Target " << target->GetName() << " Info.plist template \"" << inFile
  2599. << "\" could not be found.";
  2600. cmSystemTools::Error(e.str());
  2601. return;
  2602. }
  2603. // Convert target properties to variables in an isolated makefile
  2604. // scope to configure the file. If properties are set they will
  2605. // override user make variables. If not the configuration will fall
  2606. // back to the directory-level values set by the user.
  2607. cmMakefile* mf = this->Makefile;
  2608. cmMakefile::ScopePushPop varScope(mf);
  2609. mf->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName.c_str());
  2610. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_ICON_FILE");
  2611. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_IDENTIFIER");
  2612. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
  2613. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
  2614. mf->ConfigureFile(inFile, fname, false, false, false);
  2615. }