resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmOutputConverter.h
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt 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
20   /**
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.
26    */
27   std::string MaybeRelativeToTopBinDir(std::string const& path) const;
28   std::string MaybeRelativeToCurBinDir(std::string const& path) const;
29
30   std::string const& GetRelativePathTopSource() const;
31   std::string const& GetRelativePathTopBinary() const;
32   void SetRelativePathTop(std::string const& topSource,
33                           std::string const& topBinary);
34
35   enum OutputFormat
36   {
37     SHELL,
38     WATCOMQUOTE,
39     NINJAMULTI,
40     RESPONSE
41   };
42   std::string ConvertToOutputFormat(cm::string_view source,
43                                     OutputFormat output) const;
44   std::string ConvertDirectorySeparatorsForShell(cm::string_view source) const;
45
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;
49
50   void SetLinkScriptShell(bool linkScriptShell);
51
52   /**
53    * Flags to pass to Shell_GetArgument.  These modify the generated
54    * quoting and escape sequences to work under alternative
55    * environments.
56    */
57   enum Shell_Flag_e
58   {
59     /** The target shell is in a makefile.  */
60     Shell_Flag_Make = (1 << 0),
61
62     /** The target shell is in a VS project file.  Do not use with
63         Shell_Flag_Make.  */
64     Shell_Flag_VSIDE = (1 << 1),
65
66     /** In a windows shell the argument is being passed to "echo".  */
67     Shell_Flag_EchoWindows = (1 << 2),
68
69     /** The target shell is in a Watcom WMake makefile.  */
70     Shell_Flag_WatcomWMake = (1 << 3),
71
72     /** The target shell is in a MinGW Make makefile.  */
73     Shell_Flag_MinGWMake = (1 << 4),
74
75     /** The target shell is in a NMake makefile.  */
76     Shell_Flag_NMake = (1 << 5),
77
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),
84
85     /** The target shell quoting uses extra single Quotes for Watcom tools.  */
86     Shell_Flag_WatcomQuote = (1 << 7),
87
88     Shell_Flag_IsUnix = (1 << 8),
89
90     Shell_Flag_UnescapeNinjaConfiguration = (1 << 9),
91
92     Shell_Flag_IsResponse = (1 << 10)
93   };
94
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;
99
100   enum class WrapQuotes
101   {
102     Wrap,
103     NoWrap,
104   };
105   static std::string EscapeForCMake(cm::string_view str,
106                                     WrapQuotes wrapQuotes = WrapQuotes::Wrap);
107
108   /** Compute an escaped version of the given argument for use in a
109       windows shell.  */
110   static std::string EscapeWindowsShellArgument(cm::string_view arg,
111                                                 int shell_flags);
112
113   enum FortranFormat
114   {
115     FortranFormatNone,
116     FortranFormatFixed,
117     FortranFormatFree
118   };
119   static FortranFormat GetFortranFormat(cm::string_view value);
120
121   enum class FortranPreprocess
122   {
123     Unset,
124     NotNeeded,
125     Needed
126   };
127   static FortranPreprocess GetFortranPreprocess(cm::string_view value);
128
129 protected:
130   cmStateSnapshot StateSnapshot;
131
132 private:
133   cmState* GetState() const;
134
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);
140
141   bool LinkScriptShell = false;
142
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
151   {
152     Separate,
153     BinInSrc,
154     SrcInBin,
155     InSource,
156   };
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;
163 };