Skip to content

Commit bc28be0

Browse files
authored
[Driver][OHOS] Fix lld link issue for OHOS (#118192)
For ohos targets, libclang_rt.builtins.a, clang_rt.crtbegin.o and clang_rt.crtend.o are installed in clang/20/lib/${arch}-unknown-linux-ohos. However OHOS toolchain search them in clang/20/lib/${arch}-linux-ohos folder. It causes link error. Fix the problem by seaching both folders.
1 parent 67eb05b commit bc28be0

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

clang/lib/Driver/ToolChains/OHOS.cpp

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "llvm/ProfileData/InstrProf.h"
2020
#include "llvm/Support/FileSystem.h"
2121
#include "llvm/Support/Path.h"
22-
#include "llvm/Support/VirtualFileSystem.h"
2322
#include "llvm/Support/ScopedPrinter.h"
23+
#include "llvm/Support/VirtualFileSystem.h"
2424

2525
using namespace clang::driver;
2626
using namespace clang::driver::toolchains;
@@ -58,11 +58,9 @@ static bool findOHOSMuslMultilibs(const Driver &D,
5858
return false;
5959
}
6060

61-
static bool findOHOSMultilibs(const Driver &D,
62-
const ToolChain &TC,
63-
const llvm::Triple &TargetTriple,
64-
StringRef Path, const ArgList &Args,
65-
DetectedMultilibs &Result) {
61+
static bool findOHOSMultilibs(const Driver &D, const ToolChain &TC,
62+
const llvm::Triple &TargetTriple, StringRef Path,
63+
const ArgList &Args, DetectedMultilibs &Result) {
6664
Multilib::flags_list Flags;
6765
bool IsA7 = false;
6866
if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
@@ -172,8 +170,7 @@ OHOS::OHOS(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
172170
Paths);
173171
}
174172

175-
ToolChain::RuntimeLibType OHOS::GetRuntimeLibType(
176-
const ArgList &Args) const {
173+
ToolChain::RuntimeLibType OHOS::GetRuntimeLibType(const ArgList &Args) const {
177174
if (Arg *A = Args.getLastArg(clang::driver::options::OPT_rtlib_EQ)) {
178175
StringRef Value = A->getValue();
179176
if (Value != "compiler-rt")
@@ -184,20 +181,19 @@ ToolChain::RuntimeLibType OHOS::GetRuntimeLibType(
184181
return ToolChain::RLT_CompilerRT;
185182
}
186183

187-
ToolChain::CXXStdlibType
188-
OHOS::GetCXXStdlibType(const ArgList &Args) const {
184+
ToolChain::CXXStdlibType OHOS::GetCXXStdlibType(const ArgList &Args) const {
189185
if (Arg *A = Args.getLastArg(options::OPT_stdlib_EQ)) {
190186
StringRef Value = A->getValue();
191187
if (Value != "libc++")
192188
getDriver().Diag(diag::err_drv_invalid_stdlib_name)
193-
<< A->getAsString(Args);
189+
<< A->getAsString(Args);
194190
}
195191

196192
return ToolChain::CST_Libcxx;
197193
}
198194

199195
void OHOS::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
200-
ArgStringList &CC1Args) const {
196+
ArgStringList &CC1Args) const {
201197
const Driver &D = getDriver();
202198
const llvm::Triple &Triple = getTriple();
203199
std::string SysRoot = computeSysRoot();
@@ -258,7 +254,7 @@ void OHOS::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
258254
}
259255

260256
void OHOS::AddCXXStdlibLibArgs(const ArgList &Args,
261-
ArgStringList &CmdArgs) const {
257+
ArgStringList &CmdArgs) const {
262258
switch (GetCXXStdlibType(Args)) {
263259
case ToolChain::CST_Libcxx:
264260
CmdArgs.push_back("-lc++");
@@ -291,7 +287,8 @@ ToolChain::path_list OHOS::getRuntimePaths() const {
291287

292288
// First try the triple passed to driver as --target=<triple>.
293289
P.assign(D.ResourceDir);
294-
llvm::sys::path::append(P, "lib", D.getTargetTriple(), SelectedMultilib.gccSuffix());
290+
llvm::sys::path::append(P, "lib", D.getTargetTriple(),
291+
SelectedMultilib.gccSuffix());
295292
Paths.push_back(P.c_str());
296293

297294
// Second try the normalized triple.
@@ -340,26 +337,20 @@ std::string OHOS::getDynamicLinker(const ArgList &Args) const {
340337

341338
std::string OHOS::getCompilerRT(const ArgList &Args, StringRef Component,
342339
FileType Type) const {
340+
std::string CRTBasename =
341+
buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/false);
342+
343343
SmallString<128> Path(getDriver().ResourceDir);
344344
llvm::sys::path::append(Path, "lib", getMultiarchTriple(getTriple()),
345-
SelectedMultilib.gccSuffix());
346-
const char *Prefix =
347-
Type == ToolChain::FT_Object ? "" : "lib";
348-
const char *Suffix;
349-
switch (Type) {
350-
case ToolChain::FT_Object:
351-
Suffix = ".o";
352-
break;
353-
case ToolChain::FT_Static:
354-
Suffix = ".a";
355-
break;
356-
case ToolChain::FT_Shared:
357-
Suffix = ".so";
358-
break;
359-
}
360-
llvm::sys::path::append(
361-
Path, Prefix + Twine("clang_rt.") + Component + Suffix);
362-
return static_cast<std::string>(Path.str());
345+
SelectedMultilib.gccSuffix(), CRTBasename);
346+
if (getVFS().exists(Path))
347+
return std::string(Path);
348+
349+
std::string NewPath = ToolChain::getCompilerRT(Args, Component, Type);
350+
if (getVFS().exists(NewPath))
351+
return NewPath;
352+
353+
return std::string(Path);
363354
}
364355

365356
void OHOS::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
@@ -396,7 +387,7 @@ SanitizerMask OHOS::getSupportedSanitizers() const {
396387

397388
// TODO: Make a base class for Linux and OHOS and move this there.
398389
void OHOS::addProfileRTLibs(const llvm::opt::ArgList &Args,
399-
llvm::opt::ArgStringList &CmdArgs) const {
390+
llvm::opt::ArgStringList &CmdArgs) const {
400391
// Add linker option -u__llvm_profile_runtime to cause runtime
401392
// initialization module to be linked in.
402393
if (needsProfileRT(Args))
@@ -413,7 +404,8 @@ ToolChain::path_list OHOS::getArchSpecificLibPaths() const {
413404
return Paths;
414405
}
415406

416-
ToolChain::UnwindLibType OHOS::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
407+
ToolChain::UnwindLibType
408+
OHOS::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
417409
if (Args.getLastArg(options::OPT_unwindlib_EQ))
418410
return Generic_ELF::GetUnwindLibType(Args);
419411
return GetDefaultUnwindLibType();

0 commit comments

Comments
 (0)