summaryrefslogtreecommitdiffstats
path: root/Source/cmaketest.cxx
blob: 152d6010c4e56ddc4b7ef4ffe8ef5ca0ecc7e05e (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "cmaketest.h"
#include "cmSystemTools.h"
#include "cmake.h"
#include "cmListFileCache.h"
#include "cmCacheManager.h"
#include "cmDynamicLoader.h"

#if defined(_WIN32) && !defined(__CYGWIN__) 
#include "windows.h"
#endif

int do_cmaketest(int ac, char** av);

int main(int ac, char** av)
{
  cmSystemTools::EnableMSVCDebugHook();
  int ret = do_cmaketest(ac, av);
#ifdef CMAKE_BUILD_WITH_CMAKE
  cmDynamicLoader::FlushCache();
#endif
  cmListFileCache::GetInstance()->ClearCache(); 
  return ret;
}

// this is a test driver program for cmake.
int do_cmaketest (int argc, char **argv)
{
  if (argc < 4)
    {
    std::cerr << "Usage: " << argv[0] 
              << " test-src-dir test-bin-dir test-executable" << std::endl;
    std::cerr << "\tOptional arguments:  executable-directory project-name " << std::endl
              << "\t                     CMAKE_ARGS argument ...\n";
    return 1;
    }
  bool onlyOneConfig = false;
  if(argc > 3)
    {
    if(strcmp(argv[argc-1], "ONLY_ONE_CONFIG") == 0)
      {
      onlyOneConfig = true;
      argc--;
      }
    }
    
  // does the directory exist ?
  if (!cmSystemTools::FileIsDirectory(argv[2]))
    {
    cmSystemTools::MakeDirectory(argv[2]);
    }

  const char* sourceDirectory = argv[1];
  const char* binaryDirectory = argv[2];
  const char* executableName = argv[3];
  const char* executableDirectory = "";

  if(argc > 4)
    {
    executableDirectory = argv[4];
    }

  const char* projectName = executableName;
  if(argc > 5)
    {
    projectName = argv[5];
    }

  // WARNING: the rest of the args is passed to cmake

  /**
   * Run an executable command and put the stdout in output.
   */
  std::string output;
  
  // change to the tests directory and run cmake
  // use the cmake object instead of calling cmake
  std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  std::cout << "Changing into directory: " << binaryDirectory << "\n";
  cmSystemTools::ChangeDirectory(binaryDirectory);

  std::vector<std::string> args;
  std::string intdir = ".";
#ifdef  CMAKE_INTDIR
  intdir = CMAKE_INTDIR;
#endif

  // make sure the same generator is used
  // use this program as the cmake to be run, it should not
  // be run that way but the cmake object requires a vailid path
  std::string cmakeCommand = EXECUTABLE_OUTPUT_PATH;
  cmakeCommand += "/";
  cmakeCommand += intdir;
  cmakeCommand += "/cmake";
  cmakeCommand += cmSystemTools::GetExecutableExtension();

  std::cout << "*** " << cmakeCommand << "\n";
  args.push_back(cmakeCommand.c_str());
  args.push_back(sourceDirectory);
  std::string generator = "-G";
  generator += CMAKE_GENERATOR;
  args.push_back(generator);
  
  std::vector<std::string> progArgs;
  if(argc > 6)
    {
    if(strcmp(argv[6] , "CMAKE_ARGS") == 0)
      {
      for (int j = 7; j < argc ; j++) 
        {
        args.push_back(argv[j]);
        }
      }
    else
      {
      for(int j = 6; j < argc; j++)
        {
        progArgs.push_back(argv[j]);
        }
      }
    }
  
  std::cout << "Generating build files...\n";

  cmake cm;
  if (cm.Run(args) != 0)
    {
    std::cerr << "Error: cmake execution failed\n";
    // return to the original directory
    cmSystemTools::ChangeDirectory(cwd.c_str());
    return 1;
    }
  std::cout << "Done Generating build files.\n";
  // if the option ONLY_ONE_CONFIG is passed to the program
  // only run the config step once
  if(!onlyOneConfig)
    {
    std::cout << "Generating build files (again)...\n";
    if (cm.Run(args) != 0)
      {
      std::cerr << "Error: cmake execution failed\n";
      // return to the original directory
      cmSystemTools::ChangeDirectory(cwd.c_str());
      return 1;
      }
    std::cout << "Done Generating build files (again).\n";
    }
  

  cmListFileCache::GetInstance()->ClearCache();

  // now build the test
  std::string makeCommand = MAKEPROGRAM;
  if(makeCommand.size() == 0)
    {
    std::cerr << "Error: cmaketest does not have a valid MAKEPROGRAM\n";
    }

  // Return value for run command;
  int retVal = 0;
  int ret = 0;

  makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  std::string lowerCaseCommand = cmSystemTools::LowerCase(makeCommand);
  // if msdev is the make program then do the following
  // MSDEV 6.0
  if(lowerCaseCommand.find("msdev") != std::string::npos)
    {
    // if there are spaces in the makeCommand, assume a full path
    // and convert it to a path with no spaces in it as the
    // RunCommand does not like spaces
#if defined(_WIN32) && !defined(__CYGWIN__)      
    if(makeCommand.find(' ') != std::string::npos)
      {
      cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
      }
#endif
    makeCommand += " ";
    makeCommand += projectName;
    makeCommand += ".dsw /MAKE \"ALL_BUILD - ";
    makeCommand += intdir + "\" /REBUILD";
    }
  // MSDEV 7.0 .NET
  else if (lowerCaseCommand.find("devenv") != std::string::npos)
    {
#if defined(_WIN32) && !defined(__CYGWIN__)      
    if(makeCommand.find(' ') != std::string::npos)
      {
      cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
      }
#endif
    makeCommand += " ";
    makeCommand += projectName;
    makeCommand += ".sln /rebuild ";
    makeCommand += intdir + " /project ALL_BUILD";
    }
  // command line make program
  else
    {
    // assume a make sytle program
    // clean first
    std::string cleanCommand = makeCommand;
    cleanCommand += " clean";
    std::cout << "Running make clean command: " << cleanCommand.c_str() << " ...\n";
    retVal = 0;
    if (!cmSystemTools::RunCommand(cleanCommand.c_str(), output, retVal) || retVal)
      {
      std::cerr << "Error: " << cleanCommand.c_str() << "  execution failed\n";
      std::cerr << output.c_str() << "\n";
      // return to the original directory
      cmSystemTools::ChangeDirectory(cwd.c_str());
      std::cerr << "Return value: " << retVal << std::endl;
      return 1;
      }
    
    // now build
    }

  std::cout << "Running make command: " << makeCommand.c_str() << " ...\n";
  retVal = 0;
  if (!cmSystemTools::RunCommand(makeCommand.c_str(), output, retVal))
    {
    std::cerr << "Error: " << makeCommand.c_str() <<  "  execution failed\n";
    std::cerr << output.c_str() << "\n";
    // return to the original directory
    cmSystemTools::ChangeDirectory(cwd.c_str());
    return 1;
    }
  if ( retVal )
    {
    cmSystemTools::Error("Building of project failed\n");
    std::cerr << output.c_str() << "\n";
    // return to the original directory
    cmSystemTools::ChangeDirectory(cwd.c_str());
    ret = 1;
    }

  // now run the compiled test if we can find it
  // See if the executable exists as written.
  std::string fullPath;
  if(cmSystemTools::FileExists(executableName))
    {
    fullPath = cmSystemTools::CollapseFullPath(executableName);
    }
  std::string tryPath = executableName;
  tryPath += cmSystemTools::GetExecutableExtension();
  if(cmSystemTools::FileExists(tryPath.c_str()))
    {
    fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
    }
  // try the Debug extension
  tryPath = intdir + "/";
  tryPath += cmSystemTools::GetFilenameName(executableName);
  if(cmSystemTools::FileExists(tryPath.c_str()))
    {
    fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
    }
  tryPath += cmSystemTools::GetExecutableExtension();
  if(cmSystemTools::FileExists(tryPath.c_str()))
    {
    fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
    }
  tryPath = executableDirectory;
  tryPath += "/";
  tryPath += executableName;
  tryPath += cmSystemTools::GetExecutableExtension();
  if(cmSystemTools::FileExists(tryPath.c_str()))
    {
    fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
    }
  tryPath = executableDirectory;
  tryPath += "/";
  tryPath += intdir + "/";
  tryPath += executableName;
  tryPath += cmSystemTools::GetExecutableExtension();
  if(cmSystemTools::FileExists(tryPath.c_str()))
    {
    fullPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
    }
  if(!cmSystemTools::FileExists(fullPath.c_str()))
    {
    std::cerr << "Could not find path to executable, perhaps it was not built: " <<
      executableName << "\n";
    std::cerr << "Error: " << fullPath.c_str() << "  execution failed\n";
    // return to the original directory
    cmSystemTools::ChangeDirectory(cwd.c_str());
    return 1;
    }
  fullPath = cmSystemTools::ConvertToOutputPath(fullPath.c_str());
  for(std::vector<std::string>::iterator p = progArgs.begin();
      p != progArgs.end(); ++p)
    {
    fullPath += " ";
    fullPath += *p;
    }
  std::cout << "Running test executable: " << fullPath.c_str() << "\n";
  retVal = 0;
  if (!cmSystemTools::RunCommand(fullPath.c_str(), output, retVal, 0, true))
    {
    std::cerr << "Error: " << fullPath.c_str() << "  execution failed\n";
    // return to the original directory
    cmSystemTools::ChangeDirectory(cwd.c_str());
    return 1;
    }
  std::cout << output << "\n";
  // return to the original directory
  cmSystemTools::ChangeDirectory(cwd.c_str());
  if(retVal)
    {
    cmSystemTools::Error("test executable ", fullPath.c_str(), 
                         "returned a non-zero value");
    ret = 1;
    }
  if ( ret )
    {
    cmSystemTools::Error("Test failed");
    }
  return ret;
}