cmIncludeCommand.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.empty())
  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);
  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.empty())
  80. {
  81. fname = mfile.c_str();
  82. }
  83. }
  84. std::string fname_abs =
  85. cmSystemTools::CollapseFullPath(fname,
  86. this->Makefile->GetCurrentSourceDirectory());
  87. cmGlobalGenerator *gg = this->Makefile->GetGlobalGenerator();
  88. if (gg->IsExportedTargetsFile(fname_abs))
  89. {
  90. const char *modal = 0;
  91. std::ostringstream e;
  92. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  93. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0024))
  94. {
  95. case cmPolicies::WARN:
  96. e << (this->Makefile->GetPolicies()
  97. ->GetPolicyWarning(cmPolicies::CMP0024)) << "\n";
  98. modal = "should";
  99. case cmPolicies::OLD:
  100. break;
  101. case cmPolicies::REQUIRED_IF_USED:
  102. case cmPolicies::REQUIRED_ALWAYS:
  103. case cmPolicies::NEW:
  104. modal = "may";
  105. messageType = cmake::FATAL_ERROR;
  106. }
  107. if (modal)
  108. {
  109. e << "The file\n " << fname_abs << "\nwas generated by the export() "
  110. "command. It " << modal << " not be used as the argument to the "
  111. "include() command. Use ALIAS targets instead to refer to targets "
  112. "by alternative names.\n";
  113. this->Makefile->IssueMessage(messageType, e.str());
  114. if (messageType == cmake::FATAL_ERROR)
  115. {
  116. return false;
  117. }
  118. }
  119. gg->GenerateImportFile(fname_abs);
  120. }
  121. std::string listFile =
  122. cmSystemTools::CollapseFullPath(fname.c_str(),
  123. this->Makefile->GetCurrentSourceDirectory());
  124. if(optional && !cmSystemTools::FileExists(listFile.c_str()))
  125. {
  126. if (!resultVarName.empty())
  127. {
  128. this->Makefile->AddDefinition(resultVarName, "NOTFOUND");
  129. }
  130. return true;
  131. }
  132. bool readit =
  133. this->Makefile->ReadDependentFile(listFile.c_str(), noPolicyScope);
  134. // add the location of the included file if a result variable was given
  135. if (!resultVarName.empty())
  136. {
  137. this->Makefile->AddDefinition(resultVarName,
  138. readit?fname_abs.c_str():"NOTFOUND");
  139. }
  140. if(!optional && !readit && !cmSystemTools::GetFatalErrorOccured())
  141. {
  142. std::string m =
  143. "could not find load file:\n"
  144. " ";
  145. m += fname;
  146. this->SetError(m);
  147. return false;
  148. }
  149. return true;
  150. }