Skip to content

Commit e434fc0

Browse files
committed
gn build: Support cross-compiling libunwind for Android.
- Usual cross-compilation fix: s/target_/current_/g - Define _LIBUNWIND_IS_NATIVE_ONLY to enable unwinding past functions with return pointer authentication. - Android needs two libunwind static libraries: one with symbols exported and one without. These both need to be in the same build tree so the libunwind_hermetic_static_library configuration option doesn't help here. Replace it with build rules that build both libraries. - Install the libraries in the location that Android expects them to be. Differential Revision: https://reviews.llvm.org/D96563
1 parent 79401b4 commit e434fc0

File tree

2 files changed

+67
-28
lines changed

2 files changed

+67
-28
lines changed
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
import("//llvm/utils/gn/build/toolchain/compiler.gni")
2+
3+
supported_toolchains = [ "//llvm/utils/gn/build/toolchain:stage2_unix" ]
4+
if (android_ndk_path != "") {
5+
supported_toolchains += [
6+
"//llvm/utils/gn/build/toolchain:stage2_android_aarch64",
7+
"//llvm/utils/gn/build/toolchain:stage2_android_arm",
8+
]
9+
}
10+
111
group("libunwind") {
2-
deps = [ "//libunwind/src(//llvm/utils/gn/build/toolchain:stage2_unix)" ]
12+
deps = []
13+
foreach(toolchain, supported_toolchains) {
14+
deps += [ "//libunwind/src($toolchain)" ]
15+
}
316
}

llvm/utils/gn/secondary/libunwind/src/BUILD.gn

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import("//clang/runtimes.gni")
2+
import("//compiler-rt/target.gni")
23

34
declare_args() {
45
# Build libunwind as a shared library.
56
libunwind_enable_shared = true
67

78
# Build libunwind as a static library.
89
libunwind_enable_static = true
9-
10-
# Do not export any symbols from the static library.
11-
libunwind_hermetic_static_library = true
1210
}
1311

1412
unwind_headers = [
1513
"../include/libunwind.h",
1614
"../include/unwind.h",
1715
]
18-
if (target_os == "mac") {
16+
if (current_os == "mac") {
1917
unwind_headers += [
2018
# Make `gn format` not collapse this, for sync_source_lists_from_cmake.py.
2119
"../include/mach-o/compact_unwind_encoding.h",
@@ -44,33 +42,47 @@ unwind_sources = [
4442
"libunwind.cpp",
4543
"libunwind_ext.h",
4644
]
47-
if (target_os == "mac") {
45+
if (current_os == "mac") {
4846
unwind_sources += [ "Unwind_AppleExtras.cpp" ]
4947
}
5048

49+
if (current_os == "android") {
50+
if (current_cpu == "arm64") {
51+
unwind_output_dir = "$crt_current_out_dir/aarch64"
52+
} else if (current_cpu == "arm") {
53+
unwind_output_dir = "$crt_current_out_dir/arm"
54+
}
55+
} else {
56+
unwind_output_dir = runtimes_dir
57+
}
58+
5159
config("unwind_config") {
5260
cflags = []
5361
cflags_c = [ "-std=c99" ]
5462
cflags_cc = [ "-fno-rtti" ]
63+
defines = [ "_LIBUNWIND_IS_NATIVE_ONLY" ]
5564
include_dirs = [ "//libunwind/include" ]
56-
if (target_os == "mac") {
65+
if (current_os == "mac") {
5766
cflags += [ "-U__STRICT_ANSI__" ]
5867
}
68+
if (current_os == "android") {
69+
defines += [ "_LIBUNWIND_USE_DLADDR=0" ]
70+
}
5971
}
6072

6173
if (libunwind_enable_shared) {
6274
shared_library("unwind_shared") {
63-
output_dir = runtimes_dir
75+
output_dir = unwind_output_dir
6476
output_name = "unwind"
65-
if (target_os == "linux" || target_os == "mac") {
77+
if (current_os == "linux" || current_os == "mac") {
6678
cflags = [ "-fPIC" ]
6779
ldflags = [ "-nostdlib++" ]
6880
libs = [
6981
"dl",
7082
"pthread",
7183
]
7284
}
73-
if (target_os == "mac") {
85+
if (current_os == "mac") {
7486
ldflags += [
7587
"-compatibility_version 1",
7688
"-install_name /usr/lib/libunwind.1.dylib",
@@ -88,24 +100,35 @@ if (libunwind_enable_shared) {
88100
}
89101

90102
if (libunwind_enable_static) {
91-
static_library("unwind_static") {
92-
output_dir = runtimes_dir
93-
output_name = "unwind"
94-
complete_static_lib = true
95-
configs -= [ "//llvm/utils/gn/build:thin_archive" ]
96-
sources = unwind_sources
97-
public = unwind_headers
98-
if (libunwind_hermetic_static_library) {
99-
cflags = [ "-fvisibility=hidden" ]
100-
cflags_cc = [ "-fvisibility-global-new-delete-hidden" ]
101-
defines = [ "_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS" ]
103+
template("libunwind_static_library") {
104+
static_library(target_name) {
105+
output_dir = unwind_output_dir
106+
output_name = invoker.output_name
107+
complete_static_lib = true
108+
configs -= [ "//llvm/utils/gn/build:thin_archive" ]
109+
sources = unwind_sources
110+
public = unwind_headers
111+
if (!invoker.export) {
112+
cflags = [ "-fvisibility=hidden" ]
113+
cflags_cc = [ "-fvisibility-global-new-delete-hidden" ]
114+
defines = [ "_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS" ]
115+
}
116+
deps = [ "//compiler-rt/lib/builtins" ]
117+
configs += [ ":unwind_config" ]
118+
configs -= [
119+
"//llvm/utils/gn/build:no_exceptions",
120+
"//llvm/utils/gn/build:no_rtti",
121+
]
102122
}
103-
deps = [ "//compiler-rt/lib/builtins" ]
104-
configs += [ ":unwind_config" ]
105-
configs -= [
106-
"//llvm/utils/gn/build:no_exceptions",
107-
"//llvm/utils/gn/build:no_rtti",
108-
]
123+
}
124+
125+
libunwind_static_library("unwind_static_exported") {
126+
output_name = "unwind-exported"
127+
export = true
128+
}
129+
libunwind_static_library("unwind_static") {
130+
output_name = "unwind"
131+
export = false
109132
}
110133
}
111134

@@ -115,6 +138,9 @@ group("src") {
115138
deps += [ ":unwind_shared" ]
116139
}
117140
if (libunwind_enable_static) {
118-
deps += [ ":unwind_static" ]
141+
deps += [
142+
":unwind_static",
143+
":unwind_static_exported",
144+
]
119145
}
120146
}

0 commit comments

Comments
 (0)