1 //===-- MSVCPaths.h - MSVC path-parsing helpers -----------------*- 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_WINDOWSDRIVER_MSVCPATHS_H
10 #define LLVM_WINDOWSDRIVER_MSVCPATHS_H
11 
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Triple.h"
16 #include <string>
17 
18 namespace llvm {
19 
20 namespace vfs {
21 class FileSystem;
22 }
23 
24 enum class SubDirectoryType {
25   Bin,
26   Include,
27   Lib,
28 };
29 
30 enum class ToolsetLayout {
31   OlderVS,
32   VS2017OrNewer,
33   DevDivInternal,
34 };
35 
36 // Windows SDKs and VC Toolchains group their contents into subdirectories based
37 // on the target architecture. This function converts an llvm::Triple::ArchType
38 // to the corresponding subdirectory name.
39 const char *archToWindowsSDKArch(llvm::Triple::ArchType Arch);
40 
41 // Similar to the above function, but for Visual Studios before VS2017.
42 const char *archToLegacyVCArch(llvm::Triple::ArchType Arch);
43 
44 // Similar to the above function, but for DevDiv internal builds.
45 const char *archToDevDivInternalArch(llvm::Triple::ArchType Arch);
46 
47 bool appendArchToWindowsSDKLibPath(int SDKMajor, llvm::SmallString<128> LibPath,
48                                    llvm::Triple::ArchType Arch,
49                                    std::string &path);
50 
51 // Get the path to a specific subdirectory in the current toolchain for
52 // a given target architecture.
53 // VS2017 changed the VC toolchain layout, so this should be used instead
54 // of hardcoding paths.
55 std::string getSubDirectoryPath(SubDirectoryType Type, ToolsetLayout VSLayout,
56                                 const std::string &VCToolChainPath,
57                                 llvm::Triple::ArchType TargetArch,
58                                 llvm::StringRef SubdirParent = "");
59 
60 // Check if the Include path of a specified version of Visual Studio contains
61 // specific header files. If not, they are probably shipped with Universal CRT.
62 bool useUniversalCRT(ToolsetLayout VSLayout, const std::string &VCToolChainPath,
63                      llvm::Triple::ArchType TargetArch,
64                      llvm::vfs::FileSystem &VFS);
65 
66 /// Get Windows SDK installation directory.
67 bool getWindowsSDKDir(vfs::FileSystem &VFS,
68                       llvm::Optional<llvm::StringRef> WinSdkDir,
69                       llvm::Optional<llvm::StringRef> WinSdkVersion,
70                       llvm::Optional<llvm::StringRef> WinSysRoot,
71                       std::string &Path, int &Major,
72                       std::string &WindowsSDKIncludeVersion,
73                       std::string &WindowsSDKLibVersion);
74 
75 bool getUniversalCRTSdkDir(vfs::FileSystem &VFS,
76                            llvm::Optional<llvm::StringRef> WinSdkDir,
77                            llvm::Optional<llvm::StringRef> WinSdkVersion,
78                            llvm::Optional<llvm::StringRef> WinSysRoot,
79                            std::string &Path,
80                            std::string &UCRTVersion);
81 
82 // Check command line arguments to try and find a toolchain.
83 bool findVCToolChainViaCommandLine(
84     vfs::FileSystem &VFS, llvm::Optional<llvm::StringRef> VCToolsDir,
85     llvm::Optional<llvm::StringRef> VCToolsVersion,
86     llvm::Optional<llvm::StringRef> WinSysRoot, std::string &Path,
87     ToolsetLayout &VSLayout);
88 
89 // Check various environment variables to try and find a toolchain.
90 bool findVCToolChainViaEnvironment(vfs::FileSystem &VFS, std::string &Path,
91                                    ToolsetLayout &VSLayout);
92 
93 // Query the Setup Config server for installs, then pick the newest version
94 // and find its default VC toolchain.
95 // This is the preferred way to discover new Visual Studios, as they're no
96 // longer listed in the registry.
97 bool findVCToolChainViaSetupConfig(vfs::FileSystem &VFS, std::string &Path,
98                                    ToolsetLayout &VSLayout);
99 
100 // Look in the registry for Visual Studio installs, and use that to get
101 // a toolchain path. VS2017 and newer don't get added to the registry.
102 // So if we find something here, we know that it's an older version.
103 bool findVCToolChainViaRegistry(std::string &Path, ToolsetLayout &VSLayout);
104 
105 } // namespace llvm
106 
107 #endif
108