Coverage Report

Created: 2026-10-28 06:38

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