cmTestGenerator.cxx 6.95 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3 4
#include "cmTestGenerator.h"

5
#include <memory>
6 7
#include <ostream>
#include <utility>
8
#include <vector>
9

10
#include "cmGeneratorExpression.h"
11
#include "cmGeneratorTarget.h"
12
#include "cmListFileCache.h"
13
#include "cmLocalGenerator.h"
14
#include "cmOutputConverter.h"
15
#include "cmPropertyMap.h"
16
#include "cmRange.h"
17
#include "cmStateTypes.h"
18
#include "cmStringAlgorithms.h"
19
#include "cmSystemTools.h"
20
#include "cmTest.h"
21

22 23 24 25
cmTestGenerator::cmTestGenerator(
  cmTest* test, std::vector<std::string> const& configurations)
  : cmScriptGenerator("CTEST_CONFIGURATION_TYPE", configurations)
  , Test(test)
26
{
27
  this->ActionsPerConfig = !test->GetOldStyle();
28
  this->TestGenerated = false;
Daniel Pfeifer's avatar
Daniel Pfeifer committed
29
  this->LG = nullptr;
30 31
}

wahikihiki's avatar
wahikihiki committed
32
cmTestGenerator::~cmTestGenerator() = default;
33

34 35 36 37 38
void cmTestGenerator::Compute(cmLocalGenerator* lg)
{
  this->LG = lg;
}

39 40 41 42 43 44 45 46 47 48
bool cmTestGenerator::TestsForConfig(const std::string& config)
{
  return this->GeneratesForConfig(config);
}

cmTest* cmTestGenerator::GetTest() const
{
  return this->Test;
}

49
void cmTestGenerator::GenerateScriptConfigs(std::ostream& os, Indent indent)
50
{
51
  // Create the tests.
52 53 54
  this->cmScriptGenerator::GenerateScriptConfigs(os, indent);
}

55
void cmTestGenerator::GenerateScriptActions(std::ostream& os, Indent indent)
56
{
57
  if (this->ActionsPerConfig) {
58 59 60 61
    // This is the per-config generation in a single-configuration
    // build generator case.  The superclass will call our per-config
    // method.
    this->cmScriptGenerator::GenerateScriptActions(os, indent);
62
  } else {
63
    // This is an old-style test, so there is only one config.
64
    // assert(this->Test->GetOldStyle());
65
    this->GenerateOldStyle(os, indent);
66
  }
67 68 69
}

void cmTestGenerator::GenerateScriptForConfig(std::ostream& os,
70
                                              const std::string& config,
71
                                              Indent indent)
72 73 74
{
  this->TestGenerated = true;

75
  // Set up generator expression evaluation context.
76
  cmGeneratorExpression ge(this->Test->GetBacktrace());
77

78
  // Start the test command.
79
  os << indent << "add_test(" << this->Test->GetName() << " ";
80

81 82 83 84 85 86
  // Evaluate command line arguments
  std::vector<std::string> argv =
    EvaluateCommandLineArguments(this->Test->GetCommand(), ge, config);

  // Expand arguments if COMMAND_EXPAND_LISTS is set
  if (this->Test->GetCommandExpandLists()) {
87
    argv = cmExpandedLists(argv.begin(), argv.end());
88 89 90 91 92
    // Expanding lists on an empty command may have left it empty
    if (argv.empty()) {
      argv.emplace_back();
    }
  }
93 94 95

  // Check whether the command executable is a target whose name is to
  // be translated.
96
  std::string exe = argv[0];
97
  cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(exe);
98
  if (target && target->GetType() == cmStateEnums::EXECUTABLE) {
99 100
    // Use the target file on disk.
    exe = target->GetFullPath(config);
101 102

    // Prepend with the emulator when cross compiling if required.
103
    const char* emulator = target->GetProperty("CROSSCOMPILING_EMULATOR");
104
    if (emulator != nullptr && *emulator) {
105
      std::vector<std::string> emulatorWithArgs;
106
      cmExpandList(emulator, emulatorWithArgs);
107 108
      std::string emulatorExe(emulatorWithArgs[0]);
      cmSystemTools::ConvertToUnixSlashes(emulatorExe);
109
      os << cmOutputConverter::EscapeForCMake(emulatorExe) << " ";
110 111
      for (std::string const& arg : cmMakeRange(emulatorWithArgs).advance(1)) {
        os << cmOutputConverter::EscapeForCMake(arg) << " ";
112
      }
113
    }
114
  } else {
115 116
    // Use the command name given.
    cmSystemTools::ConvertToUnixSlashes(exe);
117
  }
118 119

  // Generate the command line with full escapes.
120
  os << cmOutputConverter::EscapeForCMake(exe);
121 122 123

  for (auto const& arg : cmMakeRange(argv).advance(1)) {
    os << " " << cmOutputConverter::EscapeForCMake(arg);
124
  }
125 126 127

  // Finish the test command.
  os << ")\n";
128 129

  // Output properties for the test.
130 131
  os << indent << "set_tests_properties(" << this->Test->GetName()
     << " PROPERTIES ";
132
  for (auto const& i : this->Test->GetProperties().GetList()) {
133 134
    os << " " << i.first << " "
       << cmOutputConverter::EscapeForCMake(
135
            ge.Parse(i.second)->Evaluate(this->LG, config));
136
  }
137 138
  this->GenerateInternalProperties(os);
  os << ")" << std::endl;
139 140
}

141
void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os, Indent indent)
142
{
143
  os << indent << "add_test(" << this->Test->GetName() << " NOT_AVAILABLE)\n";
144 145 146 147
}

bool cmTestGenerator::NeedsScriptNoConfig() const
{
148
  return (this->TestGenerated &&    // test generated for at least one config
149
          this->ActionsPerConfig && // test is config-aware
150
          this->Configurations.empty() &&      // test runs in all configs
151 152 153
          !this->ConfigurationTypes->empty()); // config-dependent command
}

154
void cmTestGenerator::GenerateOldStyle(std::ostream& fout, Indent indent)
155 156 157
{
  this->TestGenerated = true;

158 159 160 161 162
  // Get the test command line to be executed.
  std::vector<std::string> const& command = this->Test->GetCommand();

  std::string exe = command[0];
  cmSystemTools::ConvertToUnixSlashes(exe);
163
  fout << indent;
164
  fout << "add_test(";
165
  fout << this->Test->GetName() << " \"" << exe << "\"";
166

167
  for (std::string const& arg : cmMakeRange(command).advance(1)) {
168 169 170
    // Just double-quote all arguments so they are re-parsed
    // correctly by the test system.
    fout << " \"";
171
    for (char c : arg) {
172 173 174
      // Escape quotes within arguments.  We should escape
      // backslashes too but we cannot because it makes the result
      // inconsistent with previous behavior of this command.
175
      if (c == '"') {
176 177
        fout << '\\';
      }
178
      fout << c;
179
    }
180 181
    fout << "\"";
  }
182
  fout << ")" << std::endl;
183 184

  // Output properties for the test.
185 186
  fout << indent << "set_tests_properties(" << this->Test->GetName()
       << " PROPERTIES ";
187
  for (auto const& i : this->Test->GetProperties().GetList()) {
188
    fout << " " << i.first << " "
189
         << cmOutputConverter::EscapeForCMake(i.second);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  }
  this->GenerateInternalProperties(fout);
  fout << ")" << std::endl;
}

void cmTestGenerator::GenerateInternalProperties(std::ostream& os)
{
  cmListFileBacktrace bt = this->Test->GetBacktrace();
  if (bt.Empty()) {
    return;
  }

  os << " "
     << "_BACKTRACE_TRIPLES"
     << " \"";

  bool prependTripleSeparator = false;
  while (!bt.Empty()) {
    const auto& entry = bt.Top();
    if (prependTripleSeparator) {
      os << ";";
211
    }
212 213 214
    os << entry.FilePath << ";" << entry.Line << ";" << entry.Name;
    bt = bt.Pop();
    prependTripleSeparator = true;
215
  }
216 217

  os << "\"";
218
}
219 220 221 222 223 224 225 226 227 228 229 230 231

std::vector<std::string> cmTestGenerator::EvaluateCommandLineArguments(
  const std::vector<std::string>& argv, cmGeneratorExpression& ge,
  const std::string& config) const
{
  // Evaluate executable name and arguments
  auto evaluatedRange =
    cmMakeRange(argv).transform([&](const std::string& arg) {
      return ge.Parse(arg)->Evaluate(this->LG, config);
    });

  return { evaluatedRange.begin(), evaluatedRange.end() };
}