1 | //===--- VE.cpp - VE 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 | #include "VEToolchain.h" |
10 | #include "clang/Driver/CommonArgs.h" |
11 | #include "clang/Driver/Compilation.h" |
12 | #include "clang/Driver/Driver.h" |
13 | #include "clang/Driver/Options.h" |
14 | #include "llvm/Option/ArgList.h" |
15 | #include "llvm/Support/Path.h" |
16 | #include <cstdlib> // ::getenv |
17 | |
18 | using namespace clang::driver; |
19 | using namespace clang::driver::toolchains; |
20 | using namespace clang; |
21 | using namespace llvm::opt; |
22 | |
23 | /// VE tool chain |
24 | VEToolChain::VEToolChain(const Driver &D, const llvm::Triple &Triple, |
25 | const ArgList &Args) |
26 | : Linux(D, Triple, Args) { |
27 | getProgramPaths().push_back(Elt: "/opt/nec/ve/bin" ); |
28 | // ProgramPaths are found via 'PATH' environment variable. |
29 | |
30 | // Default library paths are following: |
31 | // ${RESOURCEDIR}/lib/ve-unknown-linux-gnu, |
32 | // These are OK. |
33 | |
34 | // Default file paths are following: |
35 | // ${RESOURCEDIR}/lib/ve-unknown-linux-gnu, (== getArchSpecificLibPaths) |
36 | // ${RESOURCEDIR}/lib/linux/ve, (== getArchSpecificLibPaths) |
37 | // /lib/../lib64, |
38 | // /usr/lib/../lib64, |
39 | // ${BINPATH}/../lib, |
40 | // /lib, |
41 | // /usr/lib, |
42 | // These are OK for host, but no go for VE. |
43 | |
44 | // Define file paths from scratch here. |
45 | getFilePaths().clear(); |
46 | |
47 | // Add library directories: |
48 | // ${BINPATH}/../lib/ve-unknown-linux-gnu, (== getStdlibPath) |
49 | // ${RESOURCEDIR}/lib/ve-unknown-linux-gnu, (== getArchSpecificLibPaths) |
50 | // ${RESOURCEDIR}/lib/linux/ve, (== getArchSpecificLibPaths) |
51 | // ${SYSROOT}/opt/nec/ve/lib, |
52 | if (std::optional<std::string> Path = getStdlibPath()) |
53 | getFilePaths().push_back(Elt: std::move(*Path)); |
54 | for (const auto &Path : getArchSpecificLibPaths()) |
55 | getFilePaths().push_back(Elt: Path); |
56 | getFilePaths().push_back(Elt: computeSysRoot() + "/opt/nec/ve/lib" ); |
57 | } |
58 | |
59 | Tool *VEToolChain::buildAssembler() const { |
60 | return new tools::gnutools::Assembler(*this); |
61 | } |
62 | |
63 | Tool *VEToolChain::buildLinker() const { |
64 | return new tools::gnutools::Linker(*this); |
65 | } |
66 | |
67 | bool VEToolChain::isPICDefault() const { return false; } |
68 | |
69 | bool VEToolChain::isPIEDefault(const llvm::opt::ArgList &Args) const { |
70 | return false; |
71 | } |
72 | |
73 | bool VEToolChain::isPICDefaultForced() const { return false; } |
74 | |
75 | bool VEToolChain::SupportsProfiling() const { return false; } |
76 | |
77 | bool VEToolChain::hasBlocksRuntime() const { return false; } |
78 | |
79 | void VEToolChain::AddClangSystemIncludeArgs(const ArgList &DriverArgs, |
80 | ArgStringList &CC1Args) const { |
81 | if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc)) |
82 | return; |
83 | |
84 | if (DriverArgs.hasArg(options::OPT_nobuiltininc) && |
85 | DriverArgs.hasArg(options::OPT_nostdlibinc)) |
86 | return; |
87 | |
88 | if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) { |
89 | SmallString<128> P(getDriver().ResourceDir); |
90 | llvm::sys::path::append(path&: P, a: "include" ); |
91 | addSystemInclude(DriverArgs, CC1Args, Path: P); |
92 | } |
93 | |
94 | if (!DriverArgs.hasArg(options::OPT_nostdlibinc)) { |
95 | if (const char *cl_include_dir = getenv(name: "NCC_C_INCLUDE_PATH" )) { |
96 | SmallVector<StringRef, 4> Dirs; |
97 | const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'}; |
98 | StringRef(cl_include_dir).split(A&: Dirs, Separator: StringRef(EnvPathSeparatorStr)); |
99 | ArrayRef<StringRef> DirVec(Dirs); |
100 | addSystemIncludes(DriverArgs, CC1Args, Paths: DirVec); |
101 | } else { |
102 | addSystemInclude(DriverArgs, CC1Args, |
103 | Path: getDriver().SysRoot + "/opt/nec/ve/include" ); |
104 | } |
105 | } |
106 | } |
107 | |
108 | void VEToolChain::addClangTargetOptions(const ArgList &DriverArgs, |
109 | ArgStringList &CC1Args, |
110 | Action::OffloadKind) const { |
111 | CC1Args.push_back(Elt: "-nostdsysteminc" ); |
112 | bool UseInitArrayDefault = true; |
113 | if (!DriverArgs.hasFlag(options::OPT_fuse_init_array, |
114 | options::OPT_fno_use_init_array, UseInitArrayDefault)) |
115 | CC1Args.push_back(Elt: "-fno-use-init-array" ); |
116 | } |
117 | |
118 | void VEToolChain::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs, |
119 | ArgStringList &CC1Args) const { |
120 | if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc) || |
121 | DriverArgs.hasArg(options::OPT_nostdlibinc) || |
122 | DriverArgs.hasArg(options::OPT_nostdincxx)) |
123 | return; |
124 | if (const char *cl_include_dir = getenv(name: "NCC_CPLUS_INCLUDE_PATH" )) { |
125 | SmallVector<StringRef, 4> Dirs; |
126 | const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'}; |
127 | StringRef(cl_include_dir).split(A&: Dirs, Separator: StringRef(EnvPathSeparatorStr)); |
128 | ArrayRef<StringRef> DirVec(Dirs); |
129 | addSystemIncludes(DriverArgs, CC1Args, Paths: DirVec); |
130 | } else { |
131 | // Add following paths for multiple target installation. |
132 | // ${INSTALLDIR}/include/ve-unknown-linux-gnu/c++/v1, |
133 | // ${INSTALLDIR}/include/c++/v1, |
134 | addLibCxxIncludePaths(DriverArgs, CC1Args); |
135 | } |
136 | } |
137 | |
138 | void VEToolChain::AddCXXStdlibLibArgs(const ArgList &Args, |
139 | ArgStringList &CmdArgs) const { |
140 | assert((GetCXXStdlibType(Args) == ToolChain::CST_Libcxx) && |
141 | "Only -lc++ (aka libxx) is supported in this toolchain." ); |
142 | |
143 | tools::addArchSpecificRPath(TC: *this, Args, CmdArgs); |
144 | |
145 | // Add paths for libc++.so and other shared libraries. |
146 | if (std::optional<std::string> Path = getStdlibPath()) { |
147 | CmdArgs.push_back(Elt: "-rpath" ); |
148 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: *Path)); |
149 | } |
150 | |
151 | CmdArgs.push_back(Elt: "-lc++" ); |
152 | if (Args.hasArg(options::OPT_fexperimental_library)) |
153 | CmdArgs.push_back(Elt: "-lc++experimental" ); |
154 | CmdArgs.push_back(Elt: "-lc++abi" ); |
155 | CmdArgs.push_back(Elt: "-lunwind" ); |
156 | // libc++ requires -lpthread under glibc environment |
157 | CmdArgs.push_back(Elt: "-lpthread" ); |
158 | // libunwind requires -ldl under glibc environment |
159 | CmdArgs.push_back(Elt: "-ldl" ); |
160 | } |
161 | |
162 | llvm::ExceptionHandling |
163 | VEToolChain::GetExceptionModel(const ArgList &Args) const { |
164 | // VE uses SjLj exceptions. |
165 | return llvm::ExceptionHandling::SjLj; |
166 | } |
167 | |