cmLocalGenerator.cxx 91 KB

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