cmIncludeDirectoryCommand.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "cmIncludeDirectoryCommand.h"
  4. #include <algorithm>
  5. #include <set>
  6. #include <utility>
  7. #include <cmext/algorithm>
  8. #include "cmExecutionStatus.h"
  9. #include "cmGeneratorExpression.h"
  10. #include "cmMakefile.h"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmSystemTools.h"
  13. static void GetIncludes(cmMakefile& mf, const std::string& arg,
  14. std::vector<std::string>& incs);
  15. static void NormalizeInclude(cmMakefile& mf, std::string& inc);
  16. bool cmIncludeDirectoryCommand(std::vector<std::string> const& args,
  17. cmExecutionStatus& status)
  18. {
  19. if (args.empty()) {
  20. return true;
  21. }
  22. cmMakefile& mf = status.GetMakefile();
  23. auto i = args.begin();
  24. bool before = mf.IsOn("CMAKE_INCLUDE_DIRECTORIES_BEFORE");
  25. bool system = false;
  26. if ((*i) == "BEFORE") {
  27. before = true;
  28. ++i;
  29. } else if ((*i) == "AFTER") {
  30. before = false;
  31. ++i;
  32. }
  33. std::vector<std::string> beforeIncludes;
  34. std::vector<std::string> afterIncludes;
  35. std::set<std::string> systemIncludes;
  36. for (; i != args.end(); ++i) {
  37. if (*i == "SYSTEM") {
  38. system = true;
  39. continue;
  40. }
  41. if (i->empty()) {
  42. status.SetError("given empty-string as include directory.");
  43. return false;
  44. }
  45. std::vector<std::string> includes;
  46. GetIncludes(mf, *i, includes);
  47. if (before) {
  48. cm::append(beforeIncludes, includes);
  49. } else {
  50. cm::append(afterIncludes, includes);
  51. }
  52. if (system) {
  53. systemIncludes.insert(includes.begin(), includes.end());
  54. }
  55. }
  56. std::reverse(beforeIncludes.begin(), beforeIncludes.end());
  57. mf.AddIncludeDirectories(afterIncludes);
  58. mf.AddIncludeDirectories(beforeIncludes, before);
  59. mf.AddSystemIncludeDirectories(systemIncludes);
  60. return true;
  61. }
  62. // do a lot of cleanup on the arguments because this is one place where folks
  63. // sometimes take the output of a program and pass it directly into this
  64. // command not thinking that a single argument could be filled with spaces
  65. // and newlines etc like below:
  66. //
  67. // " /foo/bar
  68. // /boo/hoo /dingle/berry "
  69. //
  70. // ideally that should be three separate arguments but when sucking the
  71. // output from a program and passing it into a command the cleanup doesn't
  72. // always happen
  73. //
  74. static void GetIncludes(cmMakefile& mf, const std::string& arg,
  75. std::vector<std::string>& incs)
  76. {
  77. // break apart any line feed arguments
  78. std::string::size_type pos = 0;
  79. std::string::size_type lastPos = 0;
  80. while ((pos = arg.find('\n', lastPos)) != std::string::npos) {
  81. if (pos) {
  82. std::string inc = arg.substr(lastPos, pos);
  83. NormalizeInclude(mf, inc);
  84. if (!inc.empty()) {
  85. incs.push_back(std::move(inc));
  86. }
  87. }
  88. lastPos = pos + 1;
  89. }
  90. std::string inc = arg.substr(lastPos);
  91. NormalizeInclude(mf, inc);
  92. if (!inc.empty()) {
  93. incs.push_back(std::move(inc));
  94. }
  95. }
  96. static void NormalizeInclude(cmMakefile& mf, std::string& inc)
  97. {
  98. std::string::size_type b = inc.find_first_not_of(" \r");
  99. std::string::size_type e = inc.find_last_not_of(" \r");
  100. if ((b != std::string::npos) && (e != std::string::npos)) {
  101. inc.assign(inc, b, 1 + e - b); // copy the remaining substring
  102. } else {
  103. inc.clear();
  104. return;
  105. }
  106. if (!cmIsOff(inc)) {
  107. cmSystemTools::ConvertToUnixSlashes(inc);
  108. if (!cmSystemTools::FileIsFullPath(inc) &&
  109. !cmGeneratorExpression::StartsWithGeneratorExpression(inc)) {
  110. inc = cmStrCat(mf.GetCurrentSourceDirectory(), '/', inc);
  111. }
  112. }
  113. }