Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmOutputConverter.h
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
#pragma once
4
5
#include "cmConfigure.h" // IWYU pragma: keep
6
7
#include <string>
8
9
#include <cm/string_view>
10
11
#include "cmStateSnapshot.h"
12
13
class cmState;
14
15
class cmOutputConverter
16
{
17
public:
18
  cmOutputConverter(cmStateSnapshot const& snapshot);
19
0
  virtual ~cmOutputConverter() = default;
20
21
  /**
22
   * Convert the given remote path to a relative path with respect to
23
   * one of our common work directories.  The path must use forward
24
   * slashes and not already be escaped or quoted.
25
   * The conversion is skipped if the paths are not both in the source
26
   * or both in the binary tree.
27
   */
28
  std::string MaybeRelativeToTopBinDir(std::string const& path) const;
29
  std::string MaybeRelativeToCurBinDir(std::string const& path) const;
30
31
  /**
32
   * The effective working directory can be different for each generator.
33
   * By default, equivalent to the current binary directory.
34
   */
35
  virtual std::string MaybeRelativeToWorkDir(std::string const& path) const
36
0
  {
37
0
    return this->MaybeRelativeToCurBinDir(path);
38
0
  }
39
40
  std::string const& GetRelativePathTopSource() const;
41
  std::string const& GetRelativePathTopBinary() const;
42
  void SetRelativePathTop(std::string const& topSource,
43
                          std::string const& topBinary);
44
45
  enum OutputFormat
46
  {
47
    SHELL,
48
    NINJAMULTI,
49
    RESPONSE
50
  };
51
  std::string ConvertToOutputFormat(cm::string_view source,
52
                                    OutputFormat output,
53
                                    bool useWatcomQuote = false) const;
54
  std::string ConvertDirectorySeparatorsForShell(cm::string_view source) const;
55
56
  //! for existing files convert to output path and short path if spaces
57
  std::string ConvertToOutputForExisting(cm::string_view path,
58
                                         OutputFormat format = SHELL,
59
                                         bool useWatcomQuote = false) const;
60
61
  void SetLinkScriptShell(bool linkScriptShell);
62
63
  /**
64
   * Flags to pass to Shell_GetArgument.  These modify the generated
65
   * quoting and escape sequences to work under alternative
66
   * environments.
67
   */
68
  enum Shell_Flag_e
69
  {
70
    /** The target shell is in a makefile.  */
71
    Shell_Flag_Make = (1 << 0),
72
73
    /** The target shell is in a VS project file.  Do not use with
74
        Shell_Flag_Make.  */
75
    Shell_Flag_VSIDE = (1 << 1),
76
77
    /** In a windows shell the argument is being passed to "echo".  */
78
    Shell_Flag_EchoWindows = (1 << 2),
79
80
    /** The target shell is in a Watcom WMake makefile.  */
81
    Shell_Flag_WatcomWMake = (1 << 3),
82
83
    /** The target shell is in a MinGW Make makefile.  */
84
    Shell_Flag_MinGWMake = (1 << 4),
85
86
    /** The target shell is in a NMake makefile.  */
87
    Shell_Flag_NMake = (1 << 5),
88
89
    /** Make variable reference syntax $(MAKEVAR) should not be escaped
90
        to allow a build tool to replace it.  Replacement values
91
        containing spaces, quotes, backslashes, or other
92
        non-alphanumeric characters that have significance to some makes
93
        or shells produce undefined behavior.  */
94
    Shell_Flag_AllowMakeVariables = (1 << 6),
95
96
    /** The target shell quoting uses extra single Quotes for Watcom tools.  */
97
    Shell_Flag_WatcomQuote = (1 << 7),
98
99
    Shell_Flag_IsUnix = (1 << 8),
100
101
    Shell_Flag_UnescapeNinjaConfiguration = (1 << 9),
102
103
    Shell_Flag_IsResponse = (1 << 10),
104
105
    /** The target shell is in a Ninja build file.  */
106
    Shell_Flag_Ninja = (1 << 11),
107
    Shell_Flag_Fastbuild = (1 << 12),
108
  };
109
110
  std::string EscapeForShell(cm::string_view str, bool makeVars = false,
111
                             bool forEcho = false, bool useWatcomQuote = false,
112
                             bool unescapeNinjaConfiguration = false,
113
                             bool forResponse = false) const;
114
  static std::string EscapeForShell(cm::string_view str, int flags);
115
116
  enum class WrapQuotes
117
  {
118
    Wrap,
119
    NoWrap,
120
  };
121
  static std::string EscapeForCMake(cm::string_view str,
122
                                    WrapQuotes wrapQuotes = WrapQuotes::Wrap);
123
124
  /** Compute an escaped version of the given argument for use in a
125
      windows shell.  */
126
  static std::string EscapeWindowsShellArgument(cm::string_view arg,
127
                                                int shell_flags);
128
129
  enum FortranFormat
130
  {
131
    FortranFormatNone,
132
    FortranFormatFixed,
133
    FortranFormatFree
134
  };
135
  static FortranFormat GetFortranFormat(cm::string_view value);
136
137
  enum class FortranPreprocess
138
  {
139
    Unset,
140
    NotNeeded,
141
    Needed
142
  };
143
  static FortranPreprocess GetFortranPreprocess(cm::string_view value);
144
145
protected:
146
  cmStateSnapshot StateSnapshot;
147
148
private:
149
  cmState* GetState() const;
150
151
  static bool Shell_CharNeedsQuotes(char c, int flags);
152
  static cm::string_view::iterator Shell_SkipMakeVariables(
153
    cm::string_view::iterator begin, cm::string_view::iterator end);
154
  static bool Shell_ArgumentNeedsQuotes(cm::string_view in, int flags);
155
  static std::string Shell_GetArgument(cm::string_view in, int flags);
156
157
  bool LinkScriptShell = false;
158
159
  // The top-most directories for relative path conversion.  Both the
160
  // source and destination location of a relative path conversion
161
  // must be underneath one of these directories (both under source or
162
  // both under binary) in order for the relative path to be evaluated
163
  // safely by the build tools.
164
  std::string RelativePathTopSource;
165
  std::string RelativePathTopBinary;
166
  enum class TopRelation
167
  {
168
    Separate,
169
    BinInSrc,
170
    SrcInBin,
171
    InSource,
172
  };
173
  TopRelation RelativePathTopRelation = TopRelation::Separate;
174
  void ComputeRelativePathTopSource();
175
  void ComputeRelativePathTopBinary();
176
  void ComputeRelativePathTopRelation();
177
  std::string MaybeRelativeTo(std::string const& local_path,
178
                              std::string const& remote_path) const;
179
};