cmIncludeDirectoryCommand.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmIncludeDirectoryCommand.h"
  11. // cmIncludeDirectoryCommand
  12. bool cmIncludeDirectoryCommand
  13. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  14. {
  15. if(args.size() < 1 )
  16. {
  17. return true;
  18. }
  19. std::vector<std::string>::const_iterator i = args.begin();
  20. bool before = this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_BEFORE");
  21. bool system = false;
  22. if ((*i) == "BEFORE")
  23. {
  24. before = true;
  25. ++i;
  26. }
  27. else if ((*i) == "AFTER")
  28. {
  29. before = false;
  30. ++i;
  31. }
  32. std::vector<std::string> beforeIncludes;
  33. std::vector<std::string> afterIncludes;
  34. std::set<std::string> systemIncludes;
  35. for(; i != args.end(); ++i)
  36. {
  37. if(*i == "SYSTEM")
  38. {
  39. system = true;
  40. continue;
  41. }
  42. if(i->size() == 0)
  43. {
  44. this->SetError("given empty-string as include directory.");
  45. return false;
  46. }
  47. std::vector<std::string> includes;
  48. this->GetIncludes(*i, includes);
  49. if (before)
  50. {
  51. beforeIncludes.insert(beforeIncludes.end(),
  52. includes.begin(),
  53. includes.end());
  54. }
  55. else
  56. {
  57. afterIncludes.insert(afterIncludes.end(),
  58. includes.begin(),
  59. includes.end());
  60. }
  61. if (system)
  62. {
  63. systemIncludes.insert(includes.begin(), includes.end());
  64. }
  65. }
  66. std::reverse(beforeIncludes.begin(), beforeIncludes.end());
  67. this->Makefile->AddIncludeDirectories(afterIncludes);
  68. this->Makefile->AddIncludeDirectories(beforeIncludes, before);
  69. this->Makefile->AddSystemIncludeDirectories(systemIncludes);
  70. return true;
  71. }
  72. static bool StartsWithGeneratorExpression(const std::string &input)
  73. {
  74. return input[0] == '$' && input[1] == '<';
  75. }
  76. // do a lot of cleanup on the arguments because this is one place where folks
  77. // sometimes take the output of a program and pass it directly into this
  78. // command not thinking that a single argument could be filled with spaces
  79. // and newlines etc liek below:
  80. //
  81. // " /foo/bar
  82. // /boo/hoo /dingle/berry "
  83. //
  84. // ideally that should be three separate arguments but when sucking the
  85. // output from a program and passing it into a command the cleanup doesn't
  86. // always happen
  87. //
  88. void cmIncludeDirectoryCommand::GetIncludes(const std::string &arg,
  89. std::vector<std::string> &incs)
  90. {
  91. // break apart any line feed arguments
  92. std::string::size_type pos = 0;
  93. std::string::size_type lastPos = 0;
  94. while((pos = arg.find('\n', lastPos)) != std::string::npos)
  95. {
  96. if (pos)
  97. {
  98. std::string inc = arg.substr(lastPos,pos);
  99. this->NormalizeInclude(inc);
  100. if (!inc.empty())
  101. {
  102. incs.push_back(inc);
  103. }
  104. }
  105. lastPos = pos + 1;
  106. }
  107. std::string inc = arg.substr(lastPos);
  108. this->NormalizeInclude(inc);
  109. if (!inc.empty())
  110. {
  111. incs.push_back(inc);
  112. }
  113. }
  114. void cmIncludeDirectoryCommand::NormalizeInclude(std::string &inc)
  115. {
  116. std::string::size_type b = inc.find_first_not_of(" \r");
  117. std::string::size_type e = inc.find_last_not_of(" \r");
  118. if ((b!=inc.npos) && (e!=inc.npos))
  119. {
  120. inc.assign(inc, b, 1+e-b); // copy the remaining substring
  121. }
  122. else
  123. {
  124. inc = "";
  125. return;
  126. }
  127. if (!cmSystemTools::IsOff(inc.c_str()))
  128. {
  129. cmSystemTools::ConvertToUnixSlashes(inc);
  130. if(!cmSystemTools::FileIsFullPath(inc.c_str()))
  131. {
  132. if(!StartsWithGeneratorExpression(inc))
  133. {
  134. std::string tmp = this->Makefile->GetStartDirectory();
  135. tmp += "/";
  136. tmp += inc;
  137. inc = tmp;
  138. }
  139. }
  140. }
  141. }