1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
9 #include <cm/string_view>
11 #include "cmStateSnapshot.h"
15 class cmOutputConverter
18 cmOutputConverter(cmStateSnapshot const& snapshot);
21 * Convert the given remote path to a relative path with respect to
22 * one of our common work directories. The path must use forward
23 * slashes and not already be escaped or quoted.
24 * The conversion is skipped if the paths are not both in the source
25 * or both in the binary tree.
27 std::string MaybeRelativeToTopBinDir(std::string const& path) const;
28 std::string MaybeRelativeToCurBinDir(std::string const& path) const;
30 std::string const& GetRelativePathTopSource() const;
31 std::string const& GetRelativePathTopBinary() const;
32 void SetRelativePathTop(std::string const& topSource,
33 std::string const& topBinary);
42 std::string ConvertToOutputFormat(cm::string_view source,
43 OutputFormat output) const;
44 std::string ConvertDirectorySeparatorsForShell(cm::string_view source) const;
46 //! for existing files convert to output path and short path if spaces
47 std::string ConvertToOutputForExisting(const std::string& remote,
48 OutputFormat format = SHELL) const;
50 void SetLinkScriptShell(bool linkScriptShell);
53 * Flags to pass to Shell_GetArgument. These modify the generated
54 * quoting and escape sequences to work under alternative
59 /** The target shell is in a makefile. */
60 Shell_Flag_Make = (1 << 0),
62 /** The target shell is in a VS project file. Do not use with
64 Shell_Flag_VSIDE = (1 << 1),
66 /** In a windows shell the argument is being passed to "echo". */
67 Shell_Flag_EchoWindows = (1 << 2),
69 /** The target shell is in a Watcom WMake makefile. */
70 Shell_Flag_WatcomWMake = (1 << 3),
72 /** The target shell is in a MinGW Make makefile. */
73 Shell_Flag_MinGWMake = (1 << 4),
75 /** The target shell is in a NMake makefile. */
76 Shell_Flag_NMake = (1 << 5),
78 /** Make variable reference syntax $(MAKEVAR) should not be escaped
79 to allow a build tool to replace it. Replacement values
80 containing spaces, quotes, backslashes, or other
81 non-alphanumeric characters that have significance to some makes
82 or shells produce undefined behavior. */
83 Shell_Flag_AllowMakeVariables = (1 << 6),
85 /** The target shell quoting uses extra single Quotes for Watcom tools. */
86 Shell_Flag_WatcomQuote = (1 << 7),
88 Shell_Flag_IsUnix = (1 << 8),
90 Shell_Flag_UnescapeNinjaConfiguration = (1 << 9),
92 Shell_Flag_IsResponse = (1 << 10)
95 std::string EscapeForShell(cm::string_view str, bool makeVars = false,
96 bool forEcho = false, bool useWatcomQuote = false,
97 bool unescapeNinjaConfiguration = false,
98 bool forResponse = false) const;
100 enum class WrapQuotes
105 static std::string EscapeForCMake(cm::string_view str,
106 WrapQuotes wrapQuotes = WrapQuotes::Wrap);
108 /** Compute an escaped version of the given argument for use in a
110 static std::string EscapeWindowsShellArgument(cm::string_view arg,
119 static FortranFormat GetFortranFormat(cm::string_view value);
121 enum class FortranPreprocess
127 static FortranPreprocess GetFortranPreprocess(cm::string_view value);
130 cmStateSnapshot StateSnapshot;
133 cmState* GetState() const;
135 static bool Shell_CharNeedsQuotes(char c, int flags);
136 static cm::string_view::iterator Shell_SkipMakeVariables(
137 cm::string_view::iterator begin, cm::string_view::iterator end);
138 static bool Shell_ArgumentNeedsQuotes(cm::string_view in, int flags);
139 static std::string Shell_GetArgument(cm::string_view in, int flags);
141 bool LinkScriptShell = false;
143 // The top-most directories for relative path conversion. Both the
144 // source and destination location of a relative path conversion
145 // must be underneath one of these directories (both under source or
146 // both under binary) in order for the relative path to be evaluated
147 // safely by the build tools.
148 std::string RelativePathTopSource;
149 std::string RelativePathTopBinary;
150 enum class TopRelation
157 TopRelation RelativePathTopRelation = TopRelation::Separate;
158 void ComputeRelativePathTopSource();
159 void ComputeRelativePathTopBinary();
160 void ComputeRelativePathTopRelation();
161 std::string MaybeRelativeTo(std::string const& local_path,
162 std::string const& remote_path) const;