cmLocalGenerator.cxx 84 KB

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