xref: /llvm-project/clang/lib/Driver/ToolChains/CommonArgs.h (revision 50046221b8e913ec6506eb96ce4c0cd267a5cc99)
1 //===--- CommonArgs.h - Args handling for multiple toolchains ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_COMMONARGS_H
10 #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_COMMONARGS_H
11 
12 #include "clang/Basic/CodeGenOptions.h"
13 #include "clang/Driver/Driver.h"
14 #include "clang/Driver/InputInfo.h"
15 #include "clang/Driver/Multilib.h"
16 #include "clang/Driver/Tool.h"
17 #include "clang/Driver/ToolChain.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Option/Arg.h"
20 #include "llvm/Option/ArgList.h"
21 #include "llvm/Support/CodeGen.h"
22 
23 namespace clang {
24 namespace driver {
25 namespace tools {
26 
27 void addPathIfExists(const Driver &D, const Twine &Path,
28                      ToolChain::path_list &Paths);
29 
30 void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
31                      const llvm::opt::ArgList &Args,
32                      llvm::opt::ArgStringList &CmdArgs, const JobAction &JA);
33 
34 void addLinkerCompressDebugSectionsOption(const ToolChain &TC,
35                                           const llvm::opt::ArgList &Args,
36                                           llvm::opt::ArgStringList &CmdArgs);
37 
38 void claimNoWarnArgs(const llvm::opt::ArgList &Args);
39 
40 bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args,
41                           llvm::opt::ArgStringList &CmdArgs);
42 
43 void linkSanitizerRuntimeDeps(const ToolChain &TC,
44                               const llvm::opt::ArgList &Args,
45                               llvm::opt::ArgStringList &CmdArgs);
46 
47 bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args,
48                     llvm::opt::ArgStringList &CmdArgs);
49 
50 void linkXRayRuntimeDeps(const ToolChain &TC, const llvm::opt::ArgList &Args,
51                          llvm::opt::ArgStringList &CmdArgs);
52 
53 void AddRunTimeLibs(const ToolChain &TC, const Driver &D,
54                     llvm::opt::ArgStringList &CmdArgs,
55                     const llvm::opt::ArgList &Args);
56 
57 void AddStaticDeviceLibsLinking(Compilation &C, const Tool &T,
58                                 const JobAction &JA,
59                                 const InputInfoList &Inputs,
60                                 const llvm::opt::ArgList &DriverArgs,
61                                 llvm::opt::ArgStringList &CmdArgs,
62                                 StringRef Arch, StringRef Target,
63                                 bool isBitCodeSDL);
64 void AddStaticDeviceLibs(Compilation *C, const Tool *T, const JobAction *JA,
65                          const InputInfoList *Inputs, const Driver &D,
66                          const llvm::opt::ArgList &DriverArgs,
67                          llvm::opt::ArgStringList &CmdArgs, StringRef Arch,
68                          StringRef Target, bool isBitCodeSDL);
69 
70 const char *SplitDebugName(const JobAction &JA, const llvm::opt::ArgList &Args,
71                            const InputInfo &Input, const InputInfo &Output);
72 
73 void SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T,
74                     const JobAction &JA, const llvm::opt::ArgList &Args,
75                     const InputInfo &Output, const char *OutFile);
76 
77 void addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args,
78                    llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output,
79                    const InputInfo &Input, bool IsThinLTO);
80 
81 const char *RelocationModelName(llvm::Reloc::Model Model);
82 
83 std::tuple<llvm::Reloc::Model, unsigned, bool>
84 ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args);
85 
86 unsigned ParseFunctionAlignment(const ToolChain &TC,
87                                 const llvm::opt::ArgList &Args);
88 
89 void addDebugInfoKind(llvm::opt::ArgStringList &CmdArgs,
90                       llvm::codegenoptions::DebugInfoKind DebugInfoKind);
91 
92 llvm::codegenoptions::DebugInfoKind
93 debugLevelToInfoKind(const llvm::opt::Arg &A);
94 
95 // Extract the integer N from a string spelled "-dwarf-N", returning 0
96 // on mismatch. The StringRef input (rather than an Arg) allows
97 // for use by the "-Xassembler" option parser.
98 unsigned DwarfVersionNum(StringRef ArgValue);
99 // Find a DWARF format version option.
100 // This function is a complementary for DwarfVersionNum().
101 const llvm::opt::Arg *getDwarfNArg(const llvm::opt::ArgList &Args);
102 unsigned getDwarfVersion(const ToolChain &TC, const llvm::opt::ArgList &Args);
103 
104 void AddAssemblerKPIC(const ToolChain &ToolChain,
105                       const llvm::opt::ArgList &Args,
106                       llvm::opt::ArgStringList &CmdArgs);
107 
108 void addArchSpecificRPath(const ToolChain &TC, const llvm::opt::ArgList &Args,
109                           llvm::opt::ArgStringList &CmdArgs);
110 void addOpenMPRuntimeLibraryPath(const ToolChain &TC,
111                                  const llvm::opt::ArgList &Args,
112                                  llvm::opt::ArgStringList &CmdArgs);
113 /// Returns true, if an OpenMP runtime has been added.
114 bool addOpenMPRuntime(const Compilation &C, llvm::opt::ArgStringList &CmdArgs,
115                       const ToolChain &TC, const llvm::opt::ArgList &Args,
116                       bool ForceStaticHostRuntime = false,
117                       bool IsOffloadingHost = false, bool GompNeedsRT = false);
118 
119 /// Adds offloading options for OpenMP host compilation to \p CmdArgs.
120 void addOpenMPHostOffloadingArgs(const Compilation &C, const JobAction &JA,
121                                  const llvm::opt::ArgList &Args,
122                                  llvm::opt::ArgStringList &CmdArgs);
123 
124 /// Adds Fortran runtime libraries to \p CmdArgs.
125 void addFortranRuntimeLibs(const ToolChain &TC, const llvm::opt::ArgList &Args,
126                            llvm::opt::ArgStringList &CmdArgs);
127 
128 /// Adds the path for the Fortran runtime libraries to \p CmdArgs.
129 void addFortranRuntimeLibraryPath(const ToolChain &TC,
130                                   const llvm::opt::ArgList &Args,
131                                   llvm::opt::ArgStringList &CmdArgs);
132 
133 void addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C,
134                           const llvm::opt::ArgList &Args,
135                           llvm::opt::ArgStringList &CmdArgs);
136 
137 void addAsNeededOption(const ToolChain &TC, const llvm::opt::ArgList &Args,
138                        llvm::opt::ArgStringList &CmdArgs, bool as_needed);
139 
140 llvm::opt::Arg *getLastCSProfileGenerateArg(const llvm::opt::ArgList &Args);
141 llvm::opt::Arg *getLastProfileUseArg(const llvm::opt::ArgList &Args);
142 llvm::opt::Arg *getLastProfileSampleUseArg(const llvm::opt::ArgList &Args);
143 
144 bool isObjCAutoRefCount(const llvm::opt::ArgList &Args);
145 
146 llvm::StringRef getLTOParallelism(const llvm::opt::ArgList &Args,
147                                   const Driver &D);
148 
149 bool areOptimizationsEnabled(const llvm::opt::ArgList &Args);
150 
151 bool isUseSeparateSections(const llvm::Triple &Triple);
152 // Parse -mtls-dialect=. Return true if the target supports both general-dynamic
153 // and TLSDESC, and TLSDESC is requested.
154 bool isTLSDESCEnabled(const ToolChain &TC, const llvm::opt::ArgList &Args);
155 
156 /// \p EnvVar is split by system delimiter for environment variables.
157 /// If \p ArgName is "-I", "-L", or an empty string, each entry from \p EnvVar
158 /// is prefixed by \p ArgName then added to \p Args. Otherwise, for each
159 /// entry of \p EnvVar, \p ArgName is added to \p Args first, then the entry
160 /// itself is added.
161 void addDirectoryList(const llvm::opt::ArgList &Args,
162                       llvm::opt::ArgStringList &CmdArgs, const char *ArgName,
163                       const char *EnvVar);
164 
165 void AddTargetFeature(const llvm::opt::ArgList &Args,
166                       std::vector<StringRef> &Features,
167                       llvm::opt::OptSpecifier OnOpt,
168                       llvm::opt::OptSpecifier OffOpt, StringRef FeatureName);
169 
170 std::string getCPUName(const Driver &D, const llvm::opt::ArgList &Args,
171                        const llvm::Triple &T, bool FromAs = false);
172 
173 void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
174                        const llvm::opt::ArgList &Args,
175                        llvm::opt::ArgStringList &CmdArgs, bool ForAS,
176                        bool IsAux = false);
177 
178 /// Iterate \p Args and convert -mxxx to +xxx and -mno-xxx to -xxx and
179 /// append it to \p Features.
180 ///
181 /// Note: Since \p Features may contain default values before calling
182 /// this function, or may be appended with entries to override arguments,
183 /// entries in \p Features are not unique.
184 void handleTargetFeaturesGroup(const Driver &D, const llvm::Triple &Triple,
185                                const llvm::opt::ArgList &Args,
186                                std::vector<StringRef> &Features,
187                                llvm::opt::OptSpecifier Group);
188 
189 /// If there are multiple +xxx or -xxx features, keep the last one.
190 SmallVector<StringRef> unifyTargetFeatures(ArrayRef<StringRef> Features);
191 
192 /// Handles the -save-stats option and returns the filename to save statistics
193 /// to.
194 SmallString<128> getStatsFileName(const llvm::opt::ArgList &Args,
195                                   const InputInfo &Output,
196                                   const InputInfo &Input, const Driver &D);
197 
198 /// \p Flag must be a flag accepted by the driver.
199 void addMultilibFlag(bool Enabled, const StringRef Flag,
200                      Multilib::flags_list &Flags);
201 
202 void addX86AlignBranchArgs(const Driver &D, const llvm::opt::ArgList &Args,
203                            llvm::opt::ArgStringList &CmdArgs, bool IsLTO,
204                            const StringRef PluginOptPrefix = "");
205 
206 void checkAMDGPUCodeObjectVersion(const Driver &D,
207                                   const llvm::opt::ArgList &Args);
208 
209 unsigned getAMDGPUCodeObjectVersion(const Driver &D,
210                                     const llvm::opt::ArgList &Args);
211 
212 bool haveAMDGPUCodeObjectVersionArgument(const Driver &D,
213                                          const llvm::opt::ArgList &Args);
214 
215 void addMachineOutlinerArgs(const Driver &D, const llvm::opt::ArgList &Args,
216                             llvm::opt::ArgStringList &CmdArgs,
217                             const llvm::Triple &Triple, bool IsLTO,
218                             const StringRef PluginOptPrefix = "");
219 
220 void addOpenMPDeviceRTL(const Driver &D, const llvm::opt::ArgList &DriverArgs,
221                         llvm::opt::ArgStringList &CC1Args,
222                         StringRef BitcodeSuffix, const llvm::Triple &Triple,
223                         const ToolChain &HostTC);
224 
225 void addOutlineAtomicsArgs(const Driver &D, const ToolChain &TC,
226                            const llvm::opt::ArgList &Args,
227                            llvm::opt::ArgStringList &CmdArgs,
228                            const llvm::Triple &Triple);
229 void addOffloadCompressArgs(const llvm::opt::ArgList &TCArgs,
230                             llvm::opt::ArgStringList &CmdArgs);
231 void addMCModel(const Driver &D, const llvm::opt::ArgList &Args,
232                 const llvm::Triple &Triple,
233                 const llvm::Reloc::Model &RelocationModel,
234                 llvm::opt::ArgStringList &CmdArgs);
235 
236 /// Handle the -f{no}-color-diagnostics and -f{no}-diagnostics-colors options.
237 void handleColorDiagnosticsArgs(const Driver &D, const llvm::opt::ArgList &Args,
238                                 llvm::opt::ArgStringList &CmdArgs);
239 
240 /// Add backslashes to escape spaces and other backslashes.
241 /// This is used for the space-separated argument list specified with
242 /// the -dwarf-debug-flags option.
243 void escapeSpacesAndBackslashes(const char *Arg,
244                                 llvm::SmallVectorImpl<char> &Res);
245 
246 /// Join the args in the given ArgList, escape spaces and backslashes and
247 /// return the joined string. This is used when saving the command line as a
248 /// result of using either the -frecord-command-line or -grecord-command-line
249 /// options. The lifetime of the returned c-string will match that of the Args
250 /// argument.
251 const char *renderEscapedCommandLine(const ToolChain &TC,
252                                      const llvm::opt::ArgList &Args);
253 
254 /// Check if the command line should be recorded in the object file. This is
255 /// done if either -frecord-command-line or -grecord-command-line options have
256 /// been passed. This also does some error checking since -frecord-command-line
257 /// is currently only supported on ELF platforms. The last two boolean
258 /// arguments are out parameters and will be set depending on the command
259 /// line options that were passed.
260 bool shouldRecordCommandLine(const ToolChain &TC,
261                              const llvm::opt::ArgList &Args,
262                              bool &FRecordCommandLine,
263                              bool &GRecordCommandLine);
264 
265 void renderCommonIntegerOverflowOptions(const llvm::opt::ArgList &Args,
266                                         llvm::opt::ArgStringList &CmdArgs);
267 
268 } // end namespace tools
269 } // end namespace driver
270 } // end namespace clang
271 
272 clang::CodeGenOptions::FramePointerKind
273 getFramePointerKind(const llvm::opt::ArgList &Args, const llvm::Triple &Triple);
274 
275 #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_COMMONARGS_H
276