cmLocalGenerator.cxx 90 KB

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