otbTestDriver.cxx 6.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
20
21
22
23
24
25
26
27
28
29
30
31
32
33


// define some itksys* things to make ShareForward.h happy
#define itksys_SHARED_FORWARD_DIR_BUILD ""
#define itksys_SHARED_FORWARD_PATH_BUILD ""
#define itksys_SHARED_FORWARD_PATH_INSTALL ""
#define itksys_SHARED_FORWARD_EXE_BUILD ""
#define itksys_SHARED_FORWARD_EXE_INSTALL ""

#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include "itksys/SystemTools.hxx"
34
#include "itkMacro.h"
35

36
37
// include SharedForward to avoid duplicating the code which find the library path variable
// name and the path separator
38
#if defined(__GNUC__) || defined(__clang__)
39
40
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
41
#include "itksys/SharedForward.h"
42
#pragma GCC diagnostic pop
43
44
45
46
#else
#include "itksys/SharedForward.h"
#endif

47
48
#include "itksys/Process.h"

49
50
// Temporary definition of otbTestMain
int otbTestMain(int arc, char * arv[]);
51

52
/** Display usage */
53
54
void usage()
{
55
  std::cerr << "usage: otbTestDriver [global_options] [non_regression_commands] Execute prg [args]" << std::endl;
56
  std::cerr << std::endl;
OTB Bot's avatar
STYLE    
OTB Bot committed
57
58
59
  std::cerr <<
  "otbTestDriver alter the environment, run a test program and does regression testing based on capabilities provided by otbTestMain.h"
            << std::endl;
60
  std::cerr << std::endl;
61
  std::cerr << "Global options:" << std::endl;
62
63
64
65
66
67
68
69
70
71
72
73
  std::cerr << "  --add-before-libpath PATH" << std::endl;
  std::cerr << "      Add a path to the library path environment. This option take care of" << std::endl;
  std::cerr << "      choosing the right environment variable for your system." << std::endl;
  std::cerr << "      This option can be used several times." << std::endl;
  std::cerr << std::endl;
  std::cerr << "  --add-before-env NAME VALUE" << std::endl;
  std::cerr << "      Add a VALUE to the variable name in the environment." << std::endl;
  std::cerr << "      This option can be used several times." << std::endl;
  std::cerr << std::endl;
  std::cerr << "  --help" << std::endl;
  std::cerr << "      Display this message and exit." << std::endl;
  std::cerr << std::endl;
OTB Bot's avatar
STYLE    
OTB Bot committed
74

75
76
}

77
78
79
/** This function parses the command line and process everything
 *  related to the --add-before-libpath, --add-before-env and --help.
 *  Every other args are added to the remainingArgs vector */
OTB Bot's avatar
STYLE    
OTB Bot committed
80
int parseCommandLine(int ac, char * av[], std::vector<char *>& remainingArgs)
81
82
{
  // parse the command line
OTB Bot's avatar
STYLE    
OTB Bot committed
83
  int  i = 1;
84
  bool skip = false;
OTB Bot's avatar
STYLE    
OTB Bot committed
85
  while (i < ac)
86
    {
OTB Bot's avatar
STYLE    
OTB Bot committed
87
    if (!skip && strcmp(av[i], "--add-before-libpath") == 0)
88
      {
OTB Bot's avatar
STYLE    
OTB Bot committed
89
      if (i + 1 >= ac)
90
91
92
93
94
95
        {
        usage();
        return 1;
        }
      std::string libpath = KWSYS_SHARED_FORWARD_LDPATH;
      libpath += "=";
OTB Bot's avatar
STYLE    
OTB Bot committed
96
      libpath += av[i + 1];
97
      char * oldenv = getenv(KWSYS_SHARED_FORWARD_LDPATH);
OTB Bot's avatar
STYLE    
OTB Bot committed
98
      if (oldenv)
99
100
101
102
        {
        libpath += KWSYS_SHARED_FORWARD_PATH_SEP;
        libpath += oldenv;
        }
OTB Bot's avatar
STYLE    
OTB Bot committed
103
      itksys::SystemTools::PutEnv(libpath.c_str());
104
105
106
107
      // on some 64 bit systems, LD_LIBRARY_PATH_64 is used before
      // LD_LIBRARY_PATH if it is set. It can lead the test to load
      // the system library instead of the expected one, so this
      // var must also be set
OTB Bot's avatar
STYLE    
OTB Bot committed
108
      if (std::string(KWSYS_SHARED_FORWARD_LDPATH) == "LD_LIBRARY_PATH")
109
        {
110
111
112
113
114
        std::string libpath64 = "LD_LIBRARY_PATH_64";
        libpath64 += "=";
        libpath64 += av[i + 1];
        char * oldenv2 = getenv("LD_LIBRARY_PATH_64");
        if (oldenv2)
115
          {
116
          libpath64 += KWSYS_SHARED_FORWARD_PATH_SEP;
Victor Poughon's avatar
Victor Poughon committed
117
          libpath64 += oldenv2;
118
          }
119
        itksys::SystemTools::PutEnv(libpath64.c_str());
120
121
122
        }
      i += 2;
      }
OTB Bot's avatar
STYLE    
OTB Bot committed
123
    else if (!skip && strcmp(av[i], "--add-before-env") == 0)
124
      {
OTB Bot's avatar
STYLE    
OTB Bot committed
125
      if (i + 2 >= ac)
126
127
128
129
        {
        usage();
        return 1;
        }
OTB Bot's avatar
STYLE    
OTB Bot committed
130
      std::string env = av[i + 1];
131
      env += "=";
OTB Bot's avatar
STYLE    
OTB Bot committed
132
133
134
      env += av[i + 2];
      char * oldenv = getenv(av[i + 1]);
      if (oldenv)
135
136
137
138
        {
        env += KWSYS_SHARED_FORWARD_PATH_SEP;
        env += oldenv;
        }
OTB Bot's avatar
STYLE    
OTB Bot committed
139
      itksys::SystemTools::PutEnv(env.c_str());
140
141
      i += 3;
      }
OTB Bot's avatar
STYLE    
OTB Bot committed
142
    else if (!skip && strcmp(av[i], "--help") == 0)
143
144
145
146
      {
      usage();
      return 0;
      }
OTB Bot's avatar
STYLE    
OTB Bot committed
147
    else
148
      {
OTB Bot's avatar
STYLE    
OTB Bot committed
149
      remainingArgs.push_back(av[i]);
150
151
152
      i += 1;
      }
    }
153
154
  return 0;
}
155

OTB Bot's avatar
STYLE    
OTB Bot committed
156
int main(int ac, char* av[])
157
158
{
  // A vector to store remaining args
OTB Bot's avatar
STYLE    
OTB Bot committed
159
160
  std::vector<char*> remainingArgs;

161
  // First parse the command line for system wide options
OTB Bot's avatar
STYLE    
OTB Bot committed
162
163
  int ret = parseCommandLine(ac, av, remainingArgs);

164
  // Check for the return code
OTB Bot's avatar
STYLE    
OTB Bot committed
165
  if (ret)
166
    {
OTB Bot's avatar
STYLE    
OTB Bot committed
167
    std::cerr << "Error while parsing arguments, exiting ..." << std::endl;
168
169
170
    return 1;
    }

171
  // Check if there are remaining args
OTB Bot's avatar
STYLE    
OTB Bot committed
172
  if (remainingArgs.empty())
173
    {
174
175
    usage();
    return 1;
OTB Bot's avatar
STYLE    
OTB Bot committed
176
177
178
    }
  // a NULL is required at the end of the table
  char** argv = new char*[remainingArgs.size() + 2];
179
  for (int i = 0; i < static_cast<int>(remainingArgs.size()); ++i)
OTB Bot's avatar
STYLE    
OTB Bot committed
180
181
182
    {
    argv[i + 1] = remainingArgs[i];
    }
183
  argv[remainingArgs.size() + 1] = ITK_NULLPTR;
OTB Bot's avatar
STYLE    
OTB Bot committed
184
185

  /** Call to the otbTestMain */
186
  return otbTestMain(static_cast<int>(remainingArgs.size()), argv);
187
188

}
189

190
191
192
// This is a dummy main to be registered as a test for the otbTestMain
int Execute(int argc, char * argv[])
{
OTB Bot's avatar
STYLE    
OTB Bot committed
193
194
  argc -= 1;
  argv += 1;
195
  // Create the appropriate itk process
196
  itksysProcess * process = itksysProcess_New();
OTB Bot's avatar
STYLE    
OTB Bot committed
197
198
199
200
  itksysProcess_SetCommand(process, argv);
  itksysProcess_SetPipeShared(process, itksysProcess_Pipe_STDOUT, true);
  itksysProcess_SetPipeShared(process, itksysProcess_Pipe_STDERR, true);
  itksysProcess_Execute(process);
201
  itksysProcess_WaitForExit(process, ITK_NULLPTR);
OTB Bot's avatar
STYLE    
OTB Bot committed
202
  int retCode = itksysProcess_GetExitValue(process);
203
204
  return retCode;
}
205

206
207
208
209
210
// Include otbTestMain and switch otbTestMain with main definition in otbTestMain.h
#undef otbTestMain
#undef main
#define main otbTestMain
#include "otbTestMain.h"
211

212
213
214
215
void RegisterTests()
{
  REGISTER_TEST(Execute);
}