Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmSeparateArgumentsCommand.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmSeparateArgumentsCommand.h"
4
5
#include <algorithm>
6
7
#include <cm/string_view>
8
#include <cmext/string_view>
9
10
#include "cmArgumentParser.h"
11
#include "cmExecutionStatus.h"
12
#include "cmList.h"
13
#include "cmMakefile.h"
14
#include "cmRange.h"
15
#include "cmStringAlgorithms.h"
16
#include "cmSystemTools.h"
17
#include "cmValue.h"
18
19
// cmSeparateArgumentsCommand
20
bool cmSeparateArgumentsCommand(std::vector<std::string> const& args,
21
                                cmExecutionStatus& status)
22
0
{
23
0
  if (args.empty()) {
24
0
    status.SetError("must be given at least one argument.");
25
0
    return false;
26
0
  }
27
28
0
  std::string const& var = args.front();
29
30
0
  if (args.size() == 1) {
31
    // Original space-replacement version of command.
32
0
    if (cmValue def = status.GetMakefile().GetDefinition(var)) {
33
0
      std::string value = *def;
34
0
      std::replace(value.begin(), value.end(), ' ', ';');
35
0
      status.GetMakefile().AddDefinition(var, value);
36
0
    }
37
38
0
    return true;
39
0
  }
40
41
0
  struct Arguments
42
0
  {
43
0
    bool UnixCommand = false;
44
0
    bool WindowsCommand = false;
45
0
    bool NativeCommand = false;
46
0
    bool Program = false;
47
0
    bool SeparateArgs = false;
48
0
  };
49
50
0
  static auto const parser =
51
0
    cmArgumentParser<Arguments>{}
52
0
      .Bind("UNIX_COMMAND"_s, &Arguments::UnixCommand)
53
0
      .Bind("WINDOWS_COMMAND"_s, &Arguments::WindowsCommand)
54
0
      .Bind("NATIVE_COMMAND"_s, &Arguments::NativeCommand)
55
0
      .Bind("PROGRAM"_s, &Arguments::Program)
56
0
      .Bind("SEPARATE_ARGS"_s, &Arguments::SeparateArgs);
57
58
0
  std::vector<std::string> unparsedArguments;
59
0
  Arguments arguments =
60
0
    parser.Parse(cmMakeRange(args).advance(1), &unparsedArguments);
61
62
0
  if (!arguments.UnixCommand && !arguments.WindowsCommand &&
63
0
      !arguments.NativeCommand) {
64
0
    status.SetError("missing required option: 'UNIX_COMMAND' or "
65
0
                    "'WINDOWS_COMMAND' or 'NATIVE_COMMAND'");
66
0
    return false;
67
0
  }
68
0
  if ((arguments.UnixCommand && arguments.WindowsCommand) ||
69
0
      (arguments.UnixCommand && arguments.NativeCommand) ||
70
0
      (arguments.WindowsCommand && arguments.NativeCommand)) {
71
0
    status.SetError("'UNIX_COMMAND', 'WINDOWS_COMMAND' and 'NATIVE_COMMAND' "
72
0
                    "are mutually exclusive");
73
0
    return false;
74
0
  }
75
0
  if (arguments.SeparateArgs && !arguments.Program) {
76
0
    status.SetError("`SEPARATE_ARGS` option requires `PROGRAM' option");
77
0
    return false;
78
0
  }
79
80
0
  if (unparsedArguments.size() > 1) {
81
0
    status.SetError("given unexpected argument(s)");
82
0
    return false;
83
0
  }
84
85
0
  if (unparsedArguments.empty()) {
86
0
    status.GetMakefile().AddDefinition(var, cm::string_view{});
87
0
    return true;
88
0
  }
89
90
0
  std::string& command = unparsedArguments.front();
91
92
0
  if (command.empty()) {
93
0
    status.GetMakefile().AddDefinition(var, command);
94
0
    return true;
95
0
  }
96
97
0
  if (arguments.Program && !arguments.SeparateArgs) {
98
0
    std::string program;
99
0
    std::string programArgs;
100
101
    // First assume the path to the program was specified with no
102
    // arguments and with no quoting or escaping for spaces.
103
    // Only bother doing this if there is non-whitespace.
104
0
    if (!cmTrimWhitespace(command).empty()) {
105
0
      program = cmSystemTools::FindProgram(command);
106
0
    }
107
108
    // If that failed then assume a command-line string was given
109
    // and split the program part from the rest of the arguments.
110
0
    if (program.empty()) {
111
0
      if (cmSystemTools::SplitProgramFromArgs(command, program, programArgs)) {
112
0
        if (!cmSystemTools::FileExists(program)) {
113
0
          program = cmSystemTools::FindProgram(program);
114
0
        }
115
0
      }
116
0
    }
117
118
0
    if (!program.empty()) {
119
0
      program += cmStrCat(';', programArgs);
120
0
    }
121
122
0
    status.GetMakefile().AddDefinition(var, program);
123
0
    return true;
124
0
  }
125
126
  // split command given
127
0
  std::vector<std::string> values;
128
129
0
  if (arguments.NativeCommand) {
130
#if defined(_WIN32)
131
    arguments.WindowsCommand = true;
132
#else
133
0
    arguments.UnixCommand = true;
134
0
#endif
135
0
  }
136
137
0
  if (arguments.UnixCommand) {
138
0
    cmSystemTools::ParseUnixCommandLine(command.c_str(), values);
139
0
  } else {
140
0
    cmSystemTools::ParseWindowsCommandLine(command.c_str(), values);
141
0
  }
142
143
0
  if (arguments.Program) {
144
    // check program exist
145
0
    if (!cmSystemTools::FileExists(values.front())) {
146
0
      auto result = cmSystemTools::FindProgram(values.front());
147
0
      if (result.empty()) {
148
0
        values.clear();
149
0
      } else {
150
0
        values.front() = result;
151
0
      }
152
0
    }
153
0
  }
154
155
  // preserve semicolons in arguments
156
0
  std::for_each(values.begin(), values.end(), [](std::string& value) {
157
0
    std::string::size_type pos = 0;
158
0
    while ((pos = value.find_first_of(';', pos)) != std::string::npos) {
159
0
      value.insert(pos, 1, '\\');
160
0
      pos += 2;
161
0
    }
162
0
  });
163
0
  auto value = cmList::to_string(values);
164
0
  status.GetMakefile().AddDefinition(var, value);
165
166
0
  return true;
167
0
}