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
17using namespace clang::driver;
18using namespace clang::driver::toolchains;
19using namespace clang;
20using namespace llvm::opt;
21
22using tools::addPathIfExists;
23
24Cygwin::Cygwin(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
25 : Generic_GCC(D, Triple, Args) {
26 GCCInstallation.init(TargetTriple: 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, OSLibDir: "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(Prefix: SysRoot))
42 addPathIfExists(D, Path: D.Dir + "/../lib", Paths);
43
44 addPathIfExists(D, Path: SysRoot + "/lib", Paths);
45 addPathIfExists(D, Path: SysRoot + "/usr/lib", Paths);
46 addPathIfExists(D, Path: SysRoot + "/usr/lib/w32api", Paths);
47}
48
49llvm::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
56void Cygwin::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
57 ArgStringList &CC1Args) const {
58 const Driver &D = getDriver();
59 std::string SysRoot = computeSysRoot();
60
61 if (DriverArgs.hasArg(Ids: clang::driver::options::OPT_nostdinc))
62 return;
63
64 if (!DriverArgs.hasArg(Ids: options::OPT_nostdlibinc))
65 addSystemInclude(DriverArgs, CC1Args, Path: SysRoot + "/usr/local/include");
66
67 if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) {
68 SmallString<128> P(D.ResourceDir);
69 llvm::sys::path::append(path&: P, a: "include");
70 addSystemInclude(DriverArgs, CC1Args, Path: P);
71 }
72
73 if (DriverArgs.hasArg(Ids: 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(A&: Dirs, Separator: ":");
81 for (StringRef Dir : Dirs) {
82 StringRef Prefix =
83 llvm::sys::path::is_absolute(path: Dir) ? "" : StringRef(SysRoot);
84 addExternCSystemInclude(DriverArgs, CC1Args, Path: 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(Path: SysRoot + "/usr/include/" + MultiarchIncludeDir))
99 addExternCSystemInclude(DriverArgs, CC1Args,
100 Path: 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, Path: SysRoot + "/include");
106
107 addExternCSystemInclude(DriverArgs, CC1Args, Path: SysRoot + "/usr/include");
108 addExternCSystemInclude(DriverArgs, CC1Args, Path: SysRoot + "/usr/include/w32api");
109}
110