cmIncludeDirectoryCommand.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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<cmStdString> 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. 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. for (std::vector<std::string>::const_iterator li = includes.begin();
  64. li != includes.end(); ++li)
  65. {
  66. systemIncludes.insert(*li);
  67. }
  68. }
  69. }
  70. std::reverse(beforeIncludes.begin(), beforeIncludes.end());
  71. this->Makefile->AddIncludeDirectories(afterIncludes);
  72. this->Makefile->AddIncludeDirectories(beforeIncludes, before);
  73. this->Makefile->AddSystemIncludeDirectories(systemIncludes);
  74. return true;
  75. }
  76. static bool StartsWithGeneratorExpression(const std::string &input)
  77. {
  78. return input[0] == '$' && input[1] == '<';
  79. }
  80. // do a lot of cleanup on the arguments because this is one place where folks
  81. // sometimes take the output of a program and pass it directly into this
  82. // command not thinking that a single argument could be filled with spaces
  83. // and newlines etc liek below:
  84. //
  85. // " /foo/bar
  86. // /boo/hoo /dingle/berry "
  87. //
  88. // ideally that should be three separate arguments but when sucking the
  89. // output from a program and passing it into a command the cleanup doesn't
  90. // always happen
  91. //
  92. void cmIncludeDirectoryCommand::GetIncludes(const std::string &arg,
  93. std::vector<std::string> &incs)
  94. {
  95. // break apart any line feed arguments
  96. std::string::size_type pos = 0;
  97. std::string::size_type lastPos = 0;
  98. while((pos = arg.find('\n', lastPos)) != std::string::npos)
  99. {
  100. if (pos)
  101. {
  102. std::string inc = arg.substr(lastPos,pos);
  103. NormalizeInclude(inc);
  104. incs.push_back(inc);
  105. }
  106. lastPos = pos + 1;
  107. }
  108. std::string inc = arg.substr(lastPos);
  109. NormalizeInclude(inc);
  110. incs.push_back(inc);
  111. }
  112. void cmIncludeDirectoryCommand::NormalizeInclude(std::string &inc)
  113. {
  114. std::string::size_type b = inc.find_first_not_of(" \r");
  115. std::string::size_type e = inc.find_last_not_of(" \r");
  116. if ((b!=inc.npos) && (e!=inc.npos))
  117. {
  118. inc.assign(inc, b, 1+e-b); // copy the remaining substring
  119. }
  120. if (!cmSystemTools::IsOff(inc.c_str()))
  121. {
  122. cmSystemTools::ConvertToUnixSlashes(inc);
  123. if(!cmSystemTools::FileIsFullPath(inc.c_str()))
  124. {
  125. if(!StartsWithGeneratorExpression(inc))
  126. {
  127. std::string tmp = this->Makefile->GetStartDirectory();
  128. tmp += "/";
  129. tmp += inc;
  130. inc = tmp;
  131. }
  132. }
  133. }
  134. }