cmInstallScriptGenerator.cxx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "cmInstallScriptGenerator.h"
  4. #include <ostream>
  5. #include <vector>
  6. #include "cmGeneratorExpression.h"
  7. #include "cmLocalGenerator.h"
  8. #include "cmMessageType.h"
  9. #include "cmPolicies.h"
  10. #include "cmScriptGenerator.h"
  11. cmInstallScriptGenerator::cmInstallScriptGenerator(const char* script,
  12. bool code,
  13. const char* component,
  14. bool exclude_from_all)
  15. : cmInstallGenerator(nullptr, std::vector<std::string>(), component,
  16. MessageDefault, exclude_from_all)
  17. , Script(script)
  18. , Code(code)
  19. , AllowGenex(false)
  20. {
  21. // We need per-config actions if the script has generator expressions.
  22. if (cmGeneratorExpression::Find(Script) != std::string::npos) {
  23. this->ActionsPerConfig = true;
  24. }
  25. }
  26. cmInstallScriptGenerator::~cmInstallScriptGenerator()
  27. {
  28. }
  29. void cmInstallScriptGenerator::Compute(cmLocalGenerator* lg)
  30. {
  31. this->LocalGenerator = lg;
  32. if (this->ActionsPerConfig) {
  33. switch (this->LocalGenerator->GetPolicyStatus(cmPolicies::CMP0087)) {
  34. case cmPolicies::WARN:
  35. this->LocalGenerator->IssueMessage(
  36. MessageType::AUTHOR_WARNING,
  37. cmPolicies::GetPolicyWarning(cmPolicies::CMP0087));
  38. CM_FALLTHROUGH;
  39. case cmPolicies::OLD:
  40. break;
  41. case cmPolicies::NEW:
  42. case cmPolicies::REQUIRED_ALWAYS:
  43. case cmPolicies::REQUIRED_IF_USED:
  44. this->AllowGenex = true;
  45. break;
  46. }
  47. }
  48. }
  49. void cmInstallScriptGenerator::AddScriptInstallRule(std::ostream& os,
  50. Indent indent,
  51. std::string const& script)
  52. {
  53. if (this->Code) {
  54. os << indent << script << "\n";
  55. } else {
  56. os << indent << "include(\"" << script << "\")\n";
  57. }
  58. }
  59. void cmInstallScriptGenerator::GenerateScriptActions(std::ostream& os,
  60. Indent indent)
  61. {
  62. if (this->AllowGenex && this->ActionsPerConfig) {
  63. this->cmInstallGenerator::GenerateScriptActions(os, indent);
  64. } else {
  65. this->AddScriptInstallRule(os, indent, this->Script);
  66. }
  67. }
  68. void cmInstallScriptGenerator::GenerateScriptForConfig(
  69. std::ostream& os, const std::string& config, Indent indent)
  70. {
  71. if (this->AllowGenex) {
  72. cmGeneratorExpression ge;
  73. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  74. ge.Parse(this->Script);
  75. this->AddScriptInstallRule(os, indent,
  76. cge->Evaluate(this->LocalGenerator, config));
  77. } else {
  78. this->AddScriptInstallRule(os, indent, this->Script);
  79. }
  80. }