cmIncludeDirectoryCommand.cxx 3.5 KB

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