cmLocalGenerator.cxx 92 KB

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