cmLocalGenerator.cxx 90 KB

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