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