cmLocalGenerator.cxx 96 KB

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