summaryrefslogtreecommitdiffstats
path: root/Source/cmCreateTestSourceList.cxx
blob: 4a1825fe87a38601703ca2a841a4ae86c8bdcf85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmCreateTestSourceList.h"

#include <algorithm>

#include "cmMakefile.h"
#include "cmSourceFile.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"

class cmExecutionStatus;

// cmCreateTestSourceList
bool cmCreateTestSourceList::InitialPass(std::vector<std::string> const& args,
                                         cmExecutionStatus&)
{
  if (args.size() < 3) {
    this->SetError("called with wrong number of arguments.");
    return false;
  }

  std::vector<std::string>::const_iterator i = args.begin();
  std::string extraInclude;
  std::string function;
  std::vector<std::string> tests;
  // extract extra include and function ot
  for (; i != args.end(); i++) {
    if (*i == "EXTRA_INCLUDE") {
      ++i;
      if (i == args.end()) {
        this->SetError("incorrect arguments to EXTRA_INCLUDE");
        return false;
      }
      extraInclude = cmStrCat("#include \"", *i, "\"\n");
    } else if (*i == "FUNCTION") {
      ++i;
      if (i == args.end()) {
        this->SetError("incorrect arguments to FUNCTION");
        return false;
      }
      function = cmStrCat(*i, "(&ac, &av);\n");
    } else {
      tests.push_back(*i);
    }
  }
  i = tests.begin();

  // Name of the source list

  const char* sourceList = i->c_str();
  ++i;

  // Name of the test driver
  // make sure they specified an extension
  if (cmSystemTools::GetFilenameExtension(*i).size() < 2) {
    this->SetError(
      "You must specify a file extension for the test driver file.");
    return false;
  }
  std::string driver =
    cmStrCat(this->Makefile->GetCurrentBinaryDirectory(), '/', *i);
  ++i;

  std::string configFile = cmSystemTools::GetCMakeRoot();

  configFile += "/Templates/TestDriver.cxx.in";
  // Create the test driver file

  std::vector<std::string>::const_iterator testsBegin = i;
  std::vector<std::string> tests_func_name;

  // The rest of the arguments consist of a list of test source files.
  // Sadly, they can be in directories. Let's find a unique function
  // name for the corresponding test, and push it to the tests_func_name
  // list.
  // For the moment:
  //   - replace spaces ' ', ':' and '/' with underscores '_'
  std::string forwardDeclareCode;
  for (i = testsBegin; i != tests.end(); ++i) {
    if (*i == "EXTRA_INCLUDE") {
      break;
    }
    std::string func_name;
    if (!cmSystemTools::GetFilenamePath(*i).empty()) {
      func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
        cmSystemTools::GetFilenameWithoutLastExtension(*i);
    } else {
      func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
    }
    cmSystemTools::ConvertToUnixSlashes(func_name);
    std::replace(func_name.begin(), func_name.end(), ' ', '_');
    std::replace(func_name.begin(), func_name.end(), '/', '_');
    std::replace(func_name.begin(), func_name.end(), ':', '_');
    tests_func_name.push_back(func_name);
    forwardDeclareCode += "int ";
    forwardDeclareCode += func_name;
    forwardDeclareCode += "(int, char*[]);\n";
  }

  std::string functionMapCode;
  int numTests = 0;
  std::vector<std::string>::iterator j;
  for (i = testsBegin, j = tests_func_name.begin(); i != tests.end();
       ++i, ++j) {
    std::string func_name;
    if (!cmSystemTools::GetFilenamePath(*i).empty()) {
      func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
        cmSystemTools::GetFilenameWithoutLastExtension(*i);
    } else {
      func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
    }
    functionMapCode += "  {\n"
                       "    \"";
    functionMapCode += func_name;
    functionMapCode += "\",\n"
                       "    ";
    functionMapCode += *j;
    functionMapCode += "\n"
                       "  },\n";
    numTests++;
  }
  if (!extraInclude.empty()) {
    this->Makefile->AddDefinition("CMAKE_TESTDRIVER_EXTRA_INCLUDES",
                                  extraInclude);
  }
  if (!function.empty()) {
    this->Makefile->AddDefinition("CMAKE_TESTDRIVER_ARGVC_FUNCTION", function);
  }
  this->Makefile->AddDefinition("CMAKE_FORWARD_DECLARE_TESTS",
                                forwardDeclareCode);
  this->Makefile->AddDefinition("CMAKE_FUNCTION_TABLE_ENTIRES",
                                functionMapCode);
  bool res = true;
  if (!this->Makefile->ConfigureFile(configFile, driver, false, true, false)) {
    res = false;
  }

  // Construct the source list.
  std::string sourceListValue;
  {
    cmSourceFile* sf = this->Makefile->GetOrCreateSource(driver);
    sf->SetProperty("ABSTRACT", "0");
    sourceListValue = args[1];
  }
  for (i = testsBegin; i != tests.end(); ++i) {
    cmSourceFile* sf = this->Makefile->GetOrCreateSource(*i);
    sf->SetProperty("ABSTRACT", "0");
    sourceListValue += ";";
    sourceListValue += *i;
  }

  this->Makefile->AddDefinition(sourceList, sourceListValue);
  return res;
}