xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/Cygwin.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===----------------------------------------------------------------------===//
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 #include "Cygwin.h"
10 #include "clang/Config/config.h"
11 #include "clang/Driver/CommonArgs.h"
12 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/Options.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/VirtualFileSystem.h"
16 
17 using namespace clang::driver;
18 using namespace clang::driver::toolchains;
19 using namespace clang;
20 using namespace llvm::opt;
21 
22 using tools::addPathIfExists;
23 
Cygwin(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)24 Cygwin::Cygwin(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
25     : Generic_GCC(D, Triple, Args) {
26   GCCInstallation.init(Triple, Args);
27   std::string SysRoot = computeSysRoot();
28   ToolChain::path_list &PPaths = getProgramPaths();
29 
30   Generic_GCC::PushPPaths(PPaths);
31 
32   path_list &Paths = getFilePaths();
33 
34   Generic_GCC::AddMultiarchPaths(D, SysRoot, "lib", Paths);
35 
36   // Similar to the logic for GCC above, if we are currently running Clang
37   // inside of the requested system root, add its parent library path to those
38   // searched.
39   // FIXME: It's not clear whether we should use the driver's installed
40   // directory ('Dir' below) or the ResourceDir.
41   if (StringRef(D.Dir).starts_with(SysRoot))
42     addPathIfExists(D, D.Dir + "/../lib", Paths);
43 
44   addPathIfExists(D, SysRoot + "/lib", Paths);
45   addPathIfExists(D, SysRoot + "/usr/lib", Paths);
46   addPathIfExists(D, SysRoot + "/usr/lib/w32api", Paths);
47 }
48 
GetExceptionModel(const ArgList & Args) const49 llvm::ExceptionHandling Cygwin::GetExceptionModel(const ArgList &Args) const {
50   if (getArch() == llvm::Triple::x86_64 || getArch() == llvm::Triple::aarch64 ||
51       getArch() == llvm::Triple::arm || getArch() == llvm::Triple::thumb)
52     return llvm::ExceptionHandling::WinEH;
53   return llvm::ExceptionHandling::DwarfCFI;
54 }
55 
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const56 void Cygwin::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
57                                        ArgStringList &CC1Args) const {
58   const Driver &D = getDriver();
59   std::string SysRoot = computeSysRoot();
60 
61   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
62     return;
63 
64   if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
65     addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
66 
67   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
68     SmallString<128> P(D.ResourceDir);
69     llvm::sys::path::append(P, "include");
70     addSystemInclude(DriverArgs, CC1Args, P);
71   }
72 
73   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
74     return;
75 
76   // Check for configure-time C include directories.
77   StringRef CIncludeDirs(C_INCLUDE_DIRS);
78   if (CIncludeDirs != "") {
79     SmallVector<StringRef, 5> Dirs;
80     CIncludeDirs.split(Dirs, ":");
81     for (StringRef Dir : Dirs) {
82       StringRef Prefix =
83           llvm::sys::path::is_absolute(Dir) ? "" : StringRef(SysRoot);
84       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
85     }
86     return;
87   }
88 
89   // Lacking those, try to detect the correct set of system includes for the
90   // target triple.
91 
92   AddMultilibIncludeArgs(DriverArgs, CC1Args);
93 
94   // On systems using multiarch, add /usr/include/$triple before
95   // /usr/include.
96   std::string MultiarchIncludeDir = getTriple().str();
97   if (!MultiarchIncludeDir.empty() &&
98       D.getVFS().exists(SysRoot + "/usr/include/" + MultiarchIncludeDir))
99     addExternCSystemInclude(DriverArgs, CC1Args,
100                             SysRoot + "/usr/include/" + MultiarchIncludeDir);
101 
102   // Add an include of '/include' directly. This isn't provided by default by
103   // system GCCs, but is often used with cross-compiling GCCs, and harmless to
104   // add even when Clang is acting as-if it were a system compiler.
105   addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
106 
107   addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
108   addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include/w32api");
109 }
110