1//===--- MSVC.h - MSVC ToolChain Implementations ----------------*- 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_MSVC_H
10#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_MSVC_H
11
12#include "Cuda.h"
13#include "clang/Basic/DebugInfoOptions.h"
14#include "clang/Driver/Compilation.h"
15#include "clang/Driver/Tool.h"
16#include "clang/Driver/ToolChain.h"
17
18namespace clang {
19namespace driver {
20namespace tools {
21
22/// Visual studio tools.
23namespace visualstudio {
24class LLVM_LIBRARY_VISIBILITY Linker : public Tool {
25public:
26  Linker(const ToolChain &TC)
27      : Tool("visualstudio::Linker", "linker", TC, RF_Full,
28             llvm::sys::WEM_UTF16) {}
29
30  bool hasIntegratedCPP() const override { return false; }
31  bool isLinkJob() const override { return true; }
32
33  void ConstructJob(Compilation &C, const JobAction &JA,
34                    const InputInfo &Output, const InputInfoList &Inputs,
35                    const llvm::opt::ArgList &TCArgs,
36                    const char *LinkingOutput) const override;
37};
38
39class LLVM_LIBRARY_VISIBILITY Compiler : public Tool {
40public:
41  Compiler(const ToolChain &TC)
42      : Tool("visualstudio::Compiler", "compiler", TC, RF_Full,
43             llvm::sys::WEM_UTF16) {}
44
45  bool hasIntegratedAssembler() const override { return true; }
46  bool hasIntegratedCPP() const override { return true; }
47  bool isLinkJob() const override { return false; }
48
49  void ConstructJob(Compilation &C, const JobAction &JA,
50                    const InputInfo &Output, const InputInfoList &Inputs,
51                    const llvm::opt::ArgList &TCArgs,
52                    const char *LinkingOutput) const override;
53
54  std::unique_ptr<Command> GetCommand(Compilation &C, const JobAction &JA,
55                                      const InputInfo &Output,
56                                      const InputInfoList &Inputs,
57                                      const llvm::opt::ArgList &TCArgs,
58                                      const char *LinkingOutput) const;
59};
60} // end namespace visualstudio
61
62} // end namespace tools
63
64namespace toolchains {
65
66class LLVM_LIBRARY_VISIBILITY MSVCToolChain : public ToolChain {
67public:
68  MSVCToolChain(const Driver &D, const llvm::Triple &Triple,
69                const llvm::opt::ArgList &Args);
70
71  llvm::opt::DerivedArgList *
72  TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
73                Action::OffloadKind DeviceOffloadKind) const override;
74
75  bool IsIntegratedAssemblerDefault() const override;
76  bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override;
77  bool isPICDefault() const override;
78  bool isPIEDefault() const override;
79  bool isPICDefaultForced() const override;
80
81  /// Set CodeView as the default debug info format for non-MachO binary
82  /// formats, and to DWARF otherwise. Users can use -gcodeview and -gdwarf to
83  /// override the default.
84  codegenoptions::DebugInfoFormat getDefaultDebugFormat() const override {
85    return getTriple().isOSBinFormatMachO() ? codegenoptions::DIF_DWARF
86                                            : codegenoptions::DIF_CodeView;
87  }
88
89  /// Set the debugger tuning to "default", since we're definitely not tuning
90  /// for GDB.
91  llvm::DebuggerKind getDefaultDebuggerTuning() const override {
92    return llvm::DebuggerKind::Default;
93  }
94
95  enum class SubDirectoryType {
96    Bin,
97    Include,
98    Lib,
99  };
100  std::string getSubDirectoryPath(SubDirectoryType Type,
101                                  llvm::StringRef SubdirParent,
102                                  llvm::Triple::ArchType TargetArch) const;
103
104  // Convenience overload.
105  // Uses the current target arch.
106  std::string getSubDirectoryPath(SubDirectoryType Type,
107                                  llvm::StringRef SubdirParent = "") const {
108    return getSubDirectoryPath(Type, SubdirParent, getArch());
109  }
110
111  enum class ToolsetLayout {
112    OlderVS,
113    VS2017OrNewer,
114    DevDivInternal,
115  };
116  bool getIsVS2017OrNewer() const { return VSLayout == ToolsetLayout::VS2017OrNewer; }
117
118  void
119  AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
120                            llvm::opt::ArgStringList &CC1Args) const override;
121  void AddClangCXXStdlibIncludeArgs(
122      const llvm::opt::ArgList &DriverArgs,
123      llvm::opt::ArgStringList &CC1Args) const override;
124
125  void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
126                          llvm::opt::ArgStringList &CC1Args) const override;
127
128  bool getWindowsSDKLibraryPath(std::string &path) const;
129  /// Check if Universal CRT should be used if available
130  bool getUniversalCRTLibraryPath(std::string &path) const;
131  bool useUniversalCRT() const;
132  VersionTuple
133  computeMSVCVersion(const Driver *D,
134                     const llvm::opt::ArgList &Args) const override;
135
136  std::string ComputeEffectiveClangTriple(const llvm::opt::ArgList &Args,
137                                          types::ID InputType) const override;
138  SanitizerMask getSupportedSanitizers() const override;
139
140  void printVerboseInfo(raw_ostream &OS) const override;
141
142  bool FoundMSVCInstall() const { return !VCToolChainPath.empty(); }
143
144protected:
145  void AddSystemIncludeWithSubfolder(const llvm::opt::ArgList &DriverArgs,
146                                     llvm::opt::ArgStringList &CC1Args,
147                                     const std::string &folder,
148                                     const Twine &subfolder1,
149                                     const Twine &subfolder2 = "",
150                                     const Twine &subfolder3 = "") const;
151
152  Tool *buildLinker() const override;
153  Tool *buildAssembler() const override;
154private:
155  std::string VCToolChainPath;
156  ToolsetLayout VSLayout = ToolsetLayout::OlderVS;
157  CudaInstallationDetector CudaInstallation;
158};
159
160} // end namespace toolchains
161} // end namespace driver
162} // end namespace clang
163
164#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_MSVC_H
165