summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeLib/PseudoMemcheck/memtester.cxx.in
blob: f37ad59648091fd659ea4533fed1c6714af25f70 (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
#include <string>
#include <vector>

#include "cmsys/Encoding.hxx"

#include <cmSystemTools.h>

// clang-format off
#define RETVAL @_retval@
#define CMAKE_COMMAND "@CMAKE_COMMAND@"
// clang-format on

int main(int ac, char** av)
{
  cmsys::Encoding::CommandLineArguments args =
    cmsys::Encoding::CommandLineArguments::Main(ac, av);
  int argc = args.argc();
  const char* const* argv = args.argv();

  std::string exename = argv[0];
  std::string logarg;
  bool nextarg = false;
  // execute the part after the last argument?
  // the logfile path gets passed as environment variable PSEUDO_LOGFILE
  bool exec = false;

  if (exename.find("valgrind") != std::string::npos) {
    logarg = "--log-file=";
  } else if (exename.find("purify") != std::string::npos) {
#ifdef _WIN32
    logarg = "/SAVETEXTDATA=";
#else
    logarg = "-log-file=";
#endif
  } else if (exename.find("BC") != std::string::npos) {
    nextarg = true;
    logarg = "/X";
  } else if (exename.find("cuda-memcheck") != std::string::npos) {
    nextarg = true;
    exec = true;
    logarg = "--log-file";
  }

  if (!logarg.empty()) {
    std::string logfile;
    for (int i = 1; i < argc; i++) {
      std::string arg = argv[i];
      if (arg.find(logarg) == 0) {
        if (nextarg) {
          if (i == argc - 1) {
            return 1; // invalid command line
          }
          logfile = argv[i + 1];
        } else {
          logfile = arg.substr(logarg.length());
        }
        // keep searching, it may be overridden later to provoke an error
      }
    }

    // find the last argument position
    int lastarg_pos = 1;
    for (int i = 1; i < argc; ++i) {
      std::string arg = argv[i];
      if (arg.find("--") == 0) {
        lastarg_pos = i;
      }
    }

    if (!logfile.empty()) {
      cmSystemTools::Touch(logfile, true);
      // execute everything after the last argument with additional environment
      int callarg_pos = lastarg_pos + (nextarg ? 2 : 1);
      if (exec && callarg_pos < argc) {
        std::vector<std::string> callargs{ CMAKE_COMMAND, "-E", "env",
                                           "PSEUDO_LOGFILE=" + logfile };
        callargs.insert(callargs.end(), &argv[callarg_pos], &argv[argc]);
        cmSystemTools::RunSingleCommand(callargs);
      }
    }
  }

  return RETVAL;
}