cmLocalGenerator.cxx 92 KB

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