cmLocalGenerator.cxx 87 KB

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