Skip to content

Commit f5f3612

Browse files
authored
[clang][LoongArch] Add OHOS target (#127555)
Add support for OHOS on loongarch64.
1 parent 14951a5 commit f5f3612

File tree

10 files changed

+35
-4
lines changed

10 files changed

+35
-4
lines changed

clang/lib/Basic/Targets.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,14 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
749749
case llvm::Triple::loongarch64:
750750
switch (os) {
751751
case llvm::Triple::Linux:
752-
return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(Triple,
753-
Opts);
752+
switch (Triple.getEnvironment()) {
753+
default:
754+
return std::make_unique<LinuxTargetInfo<LoongArch64TargetInfo>>(Triple,
755+
Opts);
756+
case llvm::Triple::OpenHOS:
757+
return std::make_unique<OHOSTargetInfo<LoongArch64TargetInfo>>(Triple,
758+
Opts);
759+
}
754760
case llvm::Triple::FreeBSD:
755761
return std::make_unique<FreeBSDTargetInfo<LoongArch64TargetInfo>>(Triple,
756762
Opts);

clang/lib/Driver/ToolChains/OHOS.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ std::string OHOS::getMultiarchTriple(const llvm::Triple &T) const {
111111
return "x86_64-linux-ohos";
112112
case llvm::Triple::aarch64:
113113
return "aarch64-linux-ohos";
114+
case llvm::Triple::loongarch64:
115+
return "loongarch64-linux-ohos";
114116
}
115117
return T.str();
116118
}
@@ -368,7 +370,9 @@ void OHOS::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
368370
CmdArgs.push_back("-z");
369371
CmdArgs.push_back("relro");
370372
CmdArgs.push_back("-z");
371-
CmdArgs.push_back("max-page-size=4096");
373+
CmdArgs.push_back(getArch() == llvm::Triple::loongarch64
374+
? "max-page-size=16384"
375+
: "max-page-size=4096");
372376
// .gnu.hash section is not compatible with the MIPS target
373377
if (getArch() != llvm::Triple::mipsel)
374378
CmdArgs.push_back("--hash-style=both");

clang/test/Driver/Inputs/ohos_native_tree/llvm/lib/clang/x.y.z/lib/loongarch64-linux-ohos/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/ohos_native_tree/llvm/lib/clang/x.y.z/lib/loongarch64-linux-ohos/clang_rt.crtbegin.o

Whitespace-only changes.

clang/test/Driver/Inputs/ohos_native_tree/llvm/lib/clang/x.y.z/lib/loongarch64-linux-ohos/clang_rt.crtend.o

Whitespace-only changes.

clang/test/Driver/Inputs/ohos_native_tree/llvm/lib/clang/x.y.z/lib/loongarch64-linux-ohos/libclang_rt.builtins.a

Whitespace-only changes.

clang/test/Driver/Inputs/ohos_native_tree/sysroot/usr/include/loongarch64-linux-ohos/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/ohos_native_tree/sysroot/usr/lib/loongarch64-linux-ohos/.keep

Whitespace-only changes.

clang/test/Driver/ohos.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
// RUN: -resource-dir=%S/Inputs/ohos_native_tree/llvm/lib/clang/x.y.z \
77
// RUN: --sysroot=%S/Inputs/ohos_native_tree/sysroot -fuse-ld=ld -march=armv7-a -mcpu=cortex-a7 -mfloat-abi=soft 2>&1 \
88
// RUN: | FileCheck -check-prefixes=CHECK,CHECK-ARM-A7-SOFT %s
9+
// RUN: %clang %s -### -no-canonical-prefixes --target=loongarch64-linux-ohos \
10+
// RUN: -resource-dir=%S/Inputs/ohos_native_tree/llvm/lib/clang/x.y.z \
11+
// RUN: --sysroot=%S/Inputs/ohos_native_tree/sysroot 2>&1 \
12+
// RUN: | FileCheck -check-prefixes=CHECK,CHECK-LOONGARCH %s
913
// CHECK: {{.*}}clang{{.*}}" "-cc1"
1014
// CHECK-NOT: "-munwind-tables"
1115
// CHECK: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
@@ -19,13 +23,17 @@
1923
// CHECK-NOT: "--build-id"
2024
// CHECK: "--hash-style=both"
2125
// CHECK: "-pie"
22-
// CHECK: "-dynamic-linker" "/lib/ld-musl-arm.so.1"
26+
// CHECK-ARM: "-dynamic-linker" "/lib/ld-musl-arm.so.1"
27+
// CHECK-ARM-A7-SOFT: "-dynamic-linker" "/lib/ld-musl-arm.so.1"
28+
// CHECK-LOONGARCH: "-dynamic-linker" "/lib/ld-musl-loongarch64.so.1"
2329
// CHECK: Scrt1.o
2430
// CHECK: crti.o
2531
// CHECK: clang_rt.crtbegin.o
2632
// CHECK-ARM: "-L[[SYSROOT]]{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}arm-liteos-ohos{{/|\\\\}}"
33+
// CHECK-LOONGARCH: "-L[[SYSROOT]]{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}loongarch64-linux-ohos{{/|\\\\}}"
2734
// CHECK-ARM-A7-SOFT: "-L[[SYSROOT]]{{/|\\\\}}usr{{/|\\\\}}lib{{/|\\\\}}arm-liteos-ohos{{/|\\\\}}a7_soft"
2835
// CHECK-ARM: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}arm-liteos-ohos{{/|\\\\}}libclang_rt.builtins.a"
36+
// CHECK-LOONGARCH: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}loongarch64-linux-ohos{{/|\\\\}}libclang_rt.builtins.a"
2937
// CHECK-ARM-A7-SOFT: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}arm-liteos-ohos/a7_soft{{/|\\\\}}libclang_rt.builtins.a"
3038
// CHECK: "-lc"
3139
// CHECK: clang_rt.crtend.o
@@ -235,3 +243,14 @@
235243

236244
// CHECK-OHOS-PTHREAD-NOT: -lpthread
237245

246+
// RUN: %clang -### --target=aarch64-linux-ohos %s 2>&1 | \
247+
// RUN: FileCheck --check-prefix=CHECK-MAXPAGESIZE-4KB %s
248+
// RUN: %clang -### --target=loongarch64-linux-ohos %s 2>&1 | \
249+
// RUN: FileCheck --check-prefix=CHECK-MAXPAGESIZE-16KB %s
250+
// RUN: %clang -### --target=riscv64-linux-ohos %s 2>&1 | \
251+
// RUN: FileCheck --check-prefix=CHECK-MAXPAGESIZE-4KB %s
252+
// RUN: %clang -### --target=x86_64-linux-ohos %s 2>&1 | \
253+
// RUN: FileCheck --check-prefix=CHECK-MAXPAGESIZE-4KB %s
254+
255+
// CHECK-MAXPAGESIZE-4KB: "-z" "max-page-size=4096"
256+
// CHECK-MAXPAGESIZE-16KB: "-z" "max-page-size=16384"

clang/test/Preprocessor/ohos.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=riscv64-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=RISCV64-OHOS-CXX
44
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=mipsel-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=MIPSEL-OHOS-CXX
55
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=x86_64-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=X86_64-OHOS-CXX
6+
// RUN: %clang_cc1 -x c++ -E -dM -ffreestanding -triple=loongarch64-linux-ohos < /dev/null | FileCheck %s -match-full-lines -check-prefix=LOONGARCH64-OHOS-CXX
67
// RUN: %clang_cc1 -E -dM -ffreestanding -triple=arm-linux-ohos < /dev/null | FileCheck %s -check-prefix=OHOS-DEFS
78

89
// ARM-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 8U
910
// ARM64-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
1011
// RISCV64-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
1112
// MIPSEL-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 8U
1213
// X86_64-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
14+
// LOONGARCH64-OHOS-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL
1315
// OHOS-DEFS: __OHOS_FAMILY__
1416
// OHOS-DEFS: __OHOS__
1517
// OHOS-DEFS-NOT: __OHOS__

0 commit comments

Comments
 (0)