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/Driver/Driver.h"
13 #include "clang/Driver/InputInfo.h"
14 #include "clang/Driver/Multilib.h"
15 #include "clang/Driver/Tool.h"
16 #include "clang/Driver/ToolChain.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Option/Arg.h"
19 #include "llvm/Option/ArgList.h"
20 #include "llvm/Support/CodeGen.h"
21 
22 namespace clang {
23 namespace driver {
24 namespace tools {
25 
26 void addPathIfExists(const Driver &D, const Twine &Path,
27                      ToolChain::path_list &Paths);
28 
29 void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
30                      const llvm::opt::ArgList &Args,
31                      llvm::opt::ArgStringList &CmdArgs, const JobAction &JA);
32 
33 void addLinkerCompressDebugSectionsOption(const ToolChain &TC,
34                                           const llvm::opt::ArgList &Args,
35                                           llvm::opt::ArgStringList &CmdArgs);
36 
37 void claimNoWarnArgs(const llvm::opt::ArgList &Args);
38 
39 bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args,
40                           llvm::opt::ArgStringList &CmdArgs);
41 
42 void linkSanitizerRuntimeDeps(const ToolChain &TC,
43                               llvm::opt::ArgStringList &CmdArgs);
44 
45 bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args,
46                     llvm::opt::ArgStringList &CmdArgs);
47 
48 void linkXRayRuntimeDeps(const ToolChain &TC,
49                          llvm::opt::ArgStringList &CmdArgs);
50 
51 void AddRunTimeLibs(const ToolChain &TC, const Driver &D,
52                     llvm::opt::ArgStringList &CmdArgs,
53                     const llvm::opt::ArgList &Args);
54 
55 void AddStaticDeviceLibsLinking(Compilation &C, const Tool &T,
56                                 const JobAction &JA,
57                                 const InputInfoList &Inputs,
58                                 const llvm::opt::ArgList &DriverArgs,
59                                 llvm::opt::ArgStringList &CmdArgs,
60                                 StringRef Arch, StringRef Target,
61                                 bool isBitCodeSDL, bool postClangLink);
62 void AddStaticDeviceLibs(Compilation *C, const Tool *T, const JobAction *JA,
63                          const InputInfoList *Inputs, const Driver &D,
64                          const llvm::opt::ArgList &DriverArgs,
65                          llvm::opt::ArgStringList &CmdArgs, StringRef Arch,
66                          StringRef Target, bool isBitCodeSDL,
67                          bool postClangLink);
68 
69 bool SDLSearch(const Driver &D, const llvm::opt::ArgList &DriverArgs,
70                llvm::opt::ArgStringList &CmdArgs,
71                SmallVector<std::string, 8> LibraryPaths, std::string Lib,
72                StringRef Arch, StringRef Target, bool isBitCodeSDL,
73                bool postClangLink);
74 
75 bool GetSDLFromOffloadArchive(Compilation &C, const Driver &D, const Tool &T,
76                               const JobAction &JA, const InputInfoList &Inputs,
77                               const llvm::opt::ArgList &DriverArgs,
78                               llvm::opt::ArgStringList &CC1Args,
79                               SmallVector<std::string, 8> LibraryPaths,
80                               StringRef Lib, StringRef Arch, StringRef Target,
81                               bool isBitCodeSDL, bool postClangLink);
82 
83 const char *SplitDebugName(const JobAction &JA, const llvm::opt::ArgList &Args,
84                            const InputInfo &Input, const InputInfo &Output);
85 
86 void SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T,
87                     const JobAction &JA, const llvm::opt::ArgList &Args,
88                     const InputInfo &Output, const char *OutFile);
89 
90 void addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args,
91                    llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output,
92                    const InputInfo &Input, bool IsThinLTO);
93 
94 const char *RelocationModelName(llvm::Reloc::Model Model);
95 
96 std::tuple<llvm::Reloc::Model, unsigned, bool>
97 ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args);
98 
99 unsigned ParseFunctionAlignment(const ToolChain &TC,
100                                 const llvm::opt::ArgList &Args);
101 
102 void addDebugInfoKind(llvm::opt::ArgStringList &CmdArgs,
103                       llvm::codegenoptions::DebugInfoKind DebugInfoKind);
104 
105 llvm::codegenoptions::DebugInfoKind
106 debugLevelToInfoKind(const llvm::opt::Arg &A);
107 
108 // Extract the integer N from a string spelled "-dwarf-N", returning 0
109 // on mismatch. The StringRef input (rather than an Arg) allows
110 // for use by the "-Xassembler" option parser.
111 unsigned DwarfVersionNum(StringRef ArgValue);
112 // Find a DWARF format version option.
113 // This function is a complementary for DwarfVersionNum().
114 const llvm::opt::Arg *getDwarfNArg(const llvm::opt::ArgList &Args);
115 unsigned getDwarfVersion(const ToolChain &TC, const llvm::opt::ArgList &Args);
116 
117 void AddAssemblerKPIC(const ToolChain &ToolChain,
118                       const llvm::opt::ArgList &Args,
119                       llvm::opt::ArgStringList &CmdArgs);
120 
121 void addArchSpecificRPath(const ToolChain &TC, const llvm::opt::ArgList &Args,
122                           llvm::opt::ArgStringList &CmdArgs);
123 void addOpenMPRuntimeLibraryPath(const ToolChain &TC,
124                                  const llvm::opt::ArgList &Args,
125                                  llvm::opt::ArgStringList &CmdArgs);
126 /// Returns true, if an OpenMP runtime has been added.
127 bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC,
128                       const llvm::opt::ArgList &Args,
129                       bool ForceStaticHostRuntime = false,
130                       bool IsOffloadingHost = false, bool GompNeedsRT = false);
131 
132 /// Adds Fortran runtime libraries to \p CmdArgs.
133 void addFortranRuntimeLibs(const ToolChain &TC,
134                            llvm::opt::ArgStringList &CmdArgs);
135 
136 /// Adds the path for the Fortran runtime libraries to \p CmdArgs.
137 void addFortranRuntimeLibraryPath(const ToolChain &TC,
138                                   const llvm::opt::ArgList &Args,
139                                   llvm::opt::ArgStringList &CmdArgs);
140 
141 void addHIPRuntimeLibArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
142                           llvm::opt::ArgStringList &CmdArgs);
143 
144 const char *getAsNeededOption(const ToolChain &TC, bool as_needed);
145 
146 llvm::opt::Arg *getLastCSProfileGenerateArg(const llvm::opt::ArgList &Args);
147 llvm::opt::Arg *getLastProfileUseArg(const llvm::opt::ArgList &Args);
148 llvm::opt::Arg *getLastProfileSampleUseArg(const llvm::opt::ArgList &Args);
149 
150 bool isObjCAutoRefCount(const llvm::opt::ArgList &Args);
151 
152 llvm::StringRef getLTOParallelism(const llvm::opt::ArgList &Args,
153                                   const Driver &D);
154 
155 bool areOptimizationsEnabled(const llvm::opt::ArgList &Args);
156 
157 bool isUseSeparateSections(const llvm::Triple &Triple);
158 
159 /// \p EnvVar is split by system delimiter for environment variables.
160 /// If \p ArgName is "-I", "-L", or an empty string, each entry from \p EnvVar
161 /// is prefixed by \p ArgName then added to \p Args. Otherwise, for each
162 /// entry of \p EnvVar, \p ArgName is added to \p Args first, then the entry
163 /// itself is added.
164 void addDirectoryList(const llvm::opt::ArgList &Args,
165                       llvm::opt::ArgStringList &CmdArgs, const char *ArgName,
166                       const char *EnvVar);
167 
168 void AddTargetFeature(const llvm::opt::ArgList &Args,
169                       std::vector<StringRef> &Features,
170                       llvm::opt::OptSpecifier OnOpt,
171                       llvm::opt::OptSpecifier OffOpt, StringRef FeatureName);
172 
173 std::string getCPUName(const Driver &D, const llvm::opt::ArgList &Args,
174                        const llvm::Triple &T, bool FromAs = false);
175 
176 void getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
177                        const llvm::opt::ArgList &Args,
178                        llvm::opt::ArgStringList &CmdArgs, bool ForAS,
179                        bool IsAux = false);
180 
181 /// Iterate \p Args and convert -mxxx to +xxx and -mno-xxx to -xxx and
182 /// append it to \p Features.
183 ///
184 /// Note: Since \p Features may contain default values before calling
185 /// this function, or may be appended with entries to override arguments,
186 /// entries in \p Features are not unique.
187 void handleTargetFeaturesGroup(const Driver &D, const llvm::Triple &Triple,
188                                const llvm::opt::ArgList &Args,
189                                std::vector<StringRef> &Features,
190                                llvm::opt::OptSpecifier Group);
191 
192 /// If there are multiple +xxx or -xxx features, keep the last one.
193 SmallVector<StringRef> unifyTargetFeatures(ArrayRef<StringRef> Features);
194 
195 /// Handles the -save-stats option and returns the filename to save statistics
196 /// to.
197 SmallString<128> getStatsFileName(const llvm::opt::ArgList &Args,
198                                   const InputInfo &Output,
199                                   const InputInfo &Input, const Driver &D);
200 
201 /// \p Flag must be a flag accepted by the driver.
202 void addMultilibFlag(bool Enabled, const StringRef Flag,
203                      Multilib::flags_list &Flags);
204 
205 void addX86AlignBranchArgs(const Driver &D, const llvm::opt::ArgList &Args,
206                            llvm::opt::ArgStringList &CmdArgs, bool IsLTO,
207                            const StringRef PluginOptPrefix = "");
208 
209 void checkAMDGPUCodeObjectVersion(const Driver &D,
210                                   const llvm::opt::ArgList &Args);
211 
212 unsigned getAMDGPUCodeObjectVersion(const Driver &D,
213                                     const llvm::opt::ArgList &Args);
214 
215 bool haveAMDGPUCodeObjectVersionArgument(const Driver &D,
216                                          const llvm::opt::ArgList &Args);
217 
218 void addMachineOutlinerArgs(const Driver &D, const llvm::opt::ArgList &Args,
219                             llvm::opt::ArgStringList &CmdArgs,
220                             const llvm::Triple &Triple, bool IsLTO,
221                             const StringRef PluginOptPrefix = "");
222 
223 void addOpenMPDeviceRTL(const Driver &D, const llvm::opt::ArgList &DriverArgs,
224                         llvm::opt::ArgStringList &CC1Args,
225                         StringRef BitcodeSuffix, const llvm::Triple &Triple);
226 } // end namespace tools
227 } // end namespace driver
228 } // end namespace clang
229 
230 #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_COMMONARGS_H
231