cmLocalGenerator.cxx 94 KB

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