1
0

cmLocalGenerator.cxx 87 KB

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