cmLocalGenerator.cxx 86 KB

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