cmLocalGenerator.cxx 84 KB

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