Merge pull request #3331 from meme/android-ndk

Support Android NDK
This commit is contained in:
Andrew Kelley 2019-09-28 13:30:19 -04:00 committed by GitHub
commit 543e729398
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 16 deletions

View File

@ -1,9 +1,19 @@
const builtin = @import("builtin");
const std = @import("../std.zig");
const maxInt = std.math.maxInt;
usingnamespace std.c;
extern "c" fn __errno_location() *c_int;
pub const _errno = __errno_location;
pub const _errno = switch (builtin.abi) {
.android => struct {
extern "c" var __errno: c_int;
fn getErrno() *c_int {
return &__errno;
}
}.getErrno,
else => struct {
extern "c" fn __errno_location() *c_int;
}.__errno_location,
};
pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize));

View File

@ -1657,6 +1657,10 @@ static void construct_linker_job_elf(LinkJob *lj) {
soname = buf_sprintf("lib%s.so.%" ZIG_PRI_usize, buf_ptr(g->root_out_name), g->version_major);
}
if (target_requires_pie(g->zig_target) && g->out_type == OutTypeExe) {
lj->args.append("-pie");
}
lj->args.append("-o");
lj->args.append(buf_ptr(&g->output_file_path));
@ -1664,6 +1668,12 @@ static void construct_linker_job_elf(LinkJob *lj) {
const char *crt1o;
if (g->zig_target->os == OsNetBSD) {
crt1o = "crt0.o";
} else if (target_is_android(g->zig_target)) {
if (g->have_dynamic_link) {
crt1o = "crtbegin_dynamic.o";
} else {
crt1o = "crtbegin_static.o";
}
} else if (!g->have_dynamic_link) {
crt1o = "crt1.o";
} else {
@ -1771,22 +1781,28 @@ static void construct_linker_job_elf(LinkJob *lj) {
if (g->libc != nullptr) {
if (!g->have_dynamic_link) {
lj->args.append("--start-group");
lj->args.append("-lgcc");
lj->args.append("-lgcc_eh");
if (!target_is_android(g->zig_target)) {
lj->args.append("-lgcc");
lj->args.append("-lgcc_eh");
}
lj->args.append("-lc");
lj->args.append("-lm");
lj->args.append("--end-group");
} else {
lj->args.append("-lgcc");
lj->args.append("--as-needed");
lj->args.append("-lgcc_s");
lj->args.append("--no-as-needed");
if (!target_is_android(g->zig_target)) {
lj->args.append("-lgcc");
lj->args.append("--as-needed");
lj->args.append("-lgcc_s");
lj->args.append("--no-as-needed");
}
lj->args.append("-lc");
lj->args.append("-lm");
lj->args.append("-lgcc");
lj->args.append("--as-needed");
lj->args.append("-lgcc_s");
lj->args.append("--no-as-needed");
if (!target_is_android(g->zig_target)) {
lj->args.append("-lgcc");
lj->args.append("--as-needed");
lj->args.append("-lgcc_s");
lj->args.append("--no-as-needed");
}
}
if (g->zig_target->os == OsFreeBSD) {
@ -1809,8 +1825,12 @@ static void construct_linker_job_elf(LinkJob *lj) {
}
// crt end
if (lj->link_in_crt && target_libc_needs_crti_crtn(g->zig_target)) {
lj->args.append(get_libc_crt_file(g, "crtn.o"));
if (lj->link_in_crt) {
if (target_is_android(g->zig_target)) {
lj->args.append(get_libc_crt_file(g, "crtend_android.o"));
} else if (target_libc_needs_crti_crtn(g->zig_target)) {
lj->args.append(get_libc_crt_file(g, "crtn.o"));
}
}
if (!g->zig_target->is_native) {

View File

@ -1594,10 +1594,14 @@ bool target_supports_stack_probing(const ZigTarget *target) {
bool target_requires_pic(const ZigTarget *target, bool linking_libc) {
// This function returns whether non-pic code is completely invalid on the given target.
return target->os == OsWindows || target->os == OsUefi || target_os_requires_libc(target->os) ||
return target_is_android(target) || target->os == OsWindows || target->os == OsUefi || target_os_requires_libc(target->os) ||
(linking_libc && target_is_glibc(target));
}
bool target_requires_pie(const ZigTarget *target) {
return target_is_android(target);
}
bool target_is_glibc(const ZigTarget *target) {
return target->os == OsLinux && target_abi_is_gnu(target->abi);
}
@ -1880,7 +1884,7 @@ bool target_supports_libunwind(const ZigTarget *target) {
}
bool target_libc_needs_crti_crtn(const ZigTarget *target) {
if (target->arch == ZigLLVM_riscv32 || target->arch == ZigLLVM_riscv64) {
if (target->arch == ZigLLVM_riscv32 || target->arch == ZigLLVM_riscv64 || target_is_android(target)) {
return false;
}
return true;

View File

@ -183,6 +183,7 @@ const char *target_libc_generic_name(const ZigTarget *target);
bool target_is_libc_lib_name(const ZigTarget *target, const char *name);
bool target_supports_fpic(const ZigTarget *target);
bool target_requires_pic(const ZigTarget *target, bool linking_libc);
bool target_requires_pie(const ZigTarget *target);
bool target_abi_is_gnu(ZigLLVM_EnvironmentType abi);
bool target_abi_is_musl(ZigLLVM_EnvironmentType abi);
bool target_is_glibc(const ZigTarget *target);