cmIncludeCommand.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "cmIncludeCommand.h"
  11. // cmIncludeCommand
  12. bool cmIncludeCommand
  13. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  14. {
  15. if (args.size()< 1 || args.size() > 4)
  16. {
  17. this->SetError("called with wrong number of arguments. "
  18. "Include only takes one file.");
  19. return false;
  20. }
  21. bool optional = false;
  22. bool noPolicyScope = false;
  23. std::string fname = args[0];
  24. std::string resultVarName;
  25. for (unsigned int i=1; i<args.size(); i++)
  26. {
  27. if (args[i] == "OPTIONAL")
  28. {
  29. if (optional)
  30. {
  31. this->SetError("called with invalid arguments: OPTIONAL used twice");
  32. return false;
  33. }
  34. optional = true;
  35. }
  36. else if(args[i] == "RESULT_VARIABLE")
  37. {
  38. if (resultVarName.size() > 0)
  39. {
  40. this->SetError("called with invalid arguments: "
  41. "only one result variable allowed");
  42. return false;
  43. }
  44. if(++i < args.size())
  45. {
  46. resultVarName = args[i];
  47. }
  48. else
  49. {
  50. this->SetError("called with no value for RESULT_VARIABLE.");
  51. return false;
  52. }
  53. }
  54. else if(args[i] == "NO_POLICY_SCOPE")
  55. {
  56. noPolicyScope = true;
  57. }
  58. else if(i > 1) // compat.: in previous cmake versions the second
  59. // parameter was ignored if it wasn't "OPTIONAL"
  60. {
  61. std::string errorText = "called with invalid argument: ";
  62. errorText += args[i];
  63. this->SetError(errorText.c_str());
  64. return false;
  65. }
  66. }
  67. if(fname.empty())
  68. {
  69. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING,
  70. "include() given empty file name (ignored).");
  71. return true;
  72. }
  73. if(!cmSystemTools::FileIsFullPath(fname.c_str()))
  74. {
  75. // Not a path. Maybe module.
  76. std::string module = fname;
  77. module += ".cmake";
  78. std::string mfile = this->Makefile->GetModulesFile(module.c_str());
  79. if ( mfile.size() )
  80. {
  81. fname = mfile.c_str();
  82. }
  83. }
  84. std::string fname_abs =
  85. cmSystemTools::CollapseFullPath(fname.c_str(),
  86. this->Makefile->GetStartDirectory());
  87. if (this->Makefile->IsExportedTargetsFile(fname_abs))
  88. {
  89. const char *modal = 0;
  90. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  91. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0024))
  92. {
  93. case cmPolicies::WARN:
  94. modal = "should";
  95. case cmPolicies::OLD:
  96. break;
  97. case cmPolicies::REQUIRED_IF_USED:
  98. case cmPolicies::REQUIRED_ALWAYS:
  99. case cmPolicies::NEW:
  100. modal = "may";
  101. messageType = cmake::FATAL_ERROR;
  102. }
  103. if (modal)
  104. {
  105. cmOStringStream e;
  106. e << (this->Makefile->GetPolicies()
  107. ->GetPolicyWarning(cmPolicies::CMP0024)) << "\n";
  108. e << "The file\n " << fname_abs << "\nwas generated by the export() "
  109. "command. It " << modal << " not be used as the argument to the "
  110. "include() command. Use ALIAS targets instead to refer to targets "
  111. "by alternative names.\n";
  112. this->Makefile->IssueMessage(messageType, e.str().c_str());
  113. if (messageType == cmake::FATAL_ERROR)
  114. {
  115. return false;
  116. }
  117. }
  118. }
  119. std::string fullFilePath;
  120. bool readit =
  121. this->Makefile->ReadListFile( this->Makefile->GetCurrentListFile(),
  122. fname.c_str(), &fullFilePath,
  123. noPolicyScope);
  124. // add the location of the included file if a result variable was given
  125. if (resultVarName.size())
  126. {
  127. this->Makefile->AddDefinition(resultVarName.c_str(),
  128. readit?fullFilePath.c_str():"NOTFOUND");
  129. }
  130. if(!optional && !readit && !cmSystemTools::GetFatalErrorOccured())
  131. {
  132. std::string m =
  133. "could not find load file:\n"
  134. " ";
  135. m += fname;
  136. this->SetError(m.c_str());
  137. return false;
  138. }
  139. return true;
  140. }