cmLocalGenerator.cxx 88 KB

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