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