cmLocalGenerator.cxx 96 KB

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