Skip to content

Commit

Permalink
gn build: Support cross-compiling libunwind for Android.
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
pcc committed Feb 12, 2021
1 parent 79401b4 commit e434fc0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 28 deletions.
15 changes: 14 additions & 1 deletion llvm/utils/gn/secondary/libunwind/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import("//llvm/utils/gn/build/toolchain/compiler.gni")

supported_toolchains = [ "//llvm/utils/gn/build/toolchain:stage2_unix" ]
if (android_ndk_path != "") {
supported_toolchains += [
"//llvm/utils/gn/build/toolchain:stage2_android_aarch64",
"//llvm/utils/gn/build/toolchain:stage2_android_arm",
]
}

group("libunwind") {
deps = [ "//libunwind/src(//llvm/utils/gn/build/toolchain:stage2_unix)" ]
deps = []
foreach(toolchain, supported_toolchains) {
deps += [ "//libunwind/src($toolchain)" ]
}
}
80 changes: 53 additions & 27 deletions llvm/utils/gn/secondary/libunwind/src/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import("//clang/runtimes.gni")
import("//compiler-rt/target.gni")

declare_args() {
# Build libunwind as a shared library.
libunwind_enable_shared = true

# Build libunwind as a static library.
libunwind_enable_static = true

# Do not export any symbols from the static library.
libunwind_hermetic_static_library = true
}

unwind_headers = [
"../include/libunwind.h",
"../include/unwind.h",
]
if (target_os == "mac") {
if (current_os == "mac") {
unwind_headers += [
# Make `gn format` not collapse this, for sync_source_lists_from_cmake.py.
"../include/mach-o/compact_unwind_encoding.h",
Expand Down Expand Up @@ -44,33 +42,47 @@ unwind_sources = [
"libunwind.cpp",
"libunwind_ext.h",
]
if (target_os == "mac") {
if (current_os == "mac") {
unwind_sources += [ "Unwind_AppleExtras.cpp" ]
}

if (current_os == "android") {
if (current_cpu == "arm64") {
unwind_output_dir = "$crt_current_out_dir/aarch64"
} else if (current_cpu == "arm") {
unwind_output_dir = "$crt_current_out_dir/arm"
}
} else {
unwind_output_dir = runtimes_dir
}

config("unwind_config") {
cflags = []
cflags_c = [ "-std=c99" ]
cflags_cc = [ "-fno-rtti" ]
defines = [ "_LIBUNWIND_IS_NATIVE_ONLY" ]
include_dirs = [ "//libunwind/include" ]
if (target_os == "mac") {
if (current_os == "mac") {
cflags += [ "-U__STRICT_ANSI__" ]
}
if (current_os == "android") {
defines += [ "_LIBUNWIND_USE_DLADDR=0" ]
}
}

if (libunwind_enable_shared) {
shared_library("unwind_shared") {
output_dir = runtimes_dir
output_dir = unwind_output_dir
output_name = "unwind"
if (target_os == "linux" || target_os == "mac") {
if (current_os == "linux" || current_os == "mac") {
cflags = [ "-fPIC" ]
ldflags = [ "-nostdlib++" ]
libs = [
"dl",
"pthread",
]
}
if (target_os == "mac") {
if (current_os == "mac") {
ldflags += [
"-compatibility_version 1",
"-install_name /usr/lib/libunwind.1.dylib",
Expand All @@ -88,24 +100,35 @@ if (libunwind_enable_shared) {
}

if (libunwind_enable_static) {
static_library("unwind_static") {
output_dir = runtimes_dir
output_name = "unwind"
complete_static_lib = true
configs -= [ "//llvm/utils/gn/build:thin_archive" ]
sources = unwind_sources
public = unwind_headers
if (libunwind_hermetic_static_library) {
cflags = [ "-fvisibility=hidden" ]
cflags_cc = [ "-fvisibility-global-new-delete-hidden" ]
defines = [ "_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS" ]
template("libunwind_static_library") {
static_library(target_name) {
output_dir = unwind_output_dir
output_name = invoker.output_name
complete_static_lib = true
configs -= [ "//llvm/utils/gn/build:thin_archive" ]
sources = unwind_sources
public = unwind_headers
if (!invoker.export) {
cflags = [ "-fvisibility=hidden" ]
cflags_cc = [ "-fvisibility-global-new-delete-hidden" ]
defines = [ "_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS" ]
}
deps = [ "//compiler-rt/lib/builtins" ]
configs += [ ":unwind_config" ]
configs -= [
"//llvm/utils/gn/build:no_exceptions",
"//llvm/utils/gn/build:no_rtti",
]
}
deps = [ "//compiler-rt/lib/builtins" ]
configs += [ ":unwind_config" ]
configs -= [
"//llvm/utils/gn/build:no_exceptions",
"//llvm/utils/gn/build:no_rtti",
]
}

libunwind_static_library("unwind_static_exported") {
output_name = "unwind-exported"
export = true
}
libunwind_static_library("unwind_static") {
output_name = "unwind"
export = false
}
}

Expand All @@ -115,6 +138,9 @@ group("src") {
deps += [ ":unwind_shared" ]
}
if (libunwind_enable_static) {
deps += [ ":unwind_static" ]
deps += [
":unwind_static",
":unwind_static_exported",
]
}
}

0 comments on commit e434fc0

Please sign in to comment.