cmLocalGenerator.cxx 88 KB

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