cmLocalGenerator.cxx 84 KB

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