cmLocalGenerator.cxx 86 KB

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