Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Android NDK #3331

Merged
merged 4 commits into from Sep 28, 2019
Merged

Support Android NDK #3331

merged 4 commits into from Sep 28, 2019

Conversation

meme
Copy link
Contributor

@meme meme commented Sep 27, 2019

  • Fixes linking against Android's libc-variant, bionic
  • Producing executables and shared objects that link against libc now link without trouble, provided you use a proper libc.txt

You can test this for yourself by downloading the latest Android NDK at https://developer.android.com/ndk/downloads/ and creating a libc.txt for AArch64, for example (of course directed to where your NDK is) then running zig build-exe:

include_dir=/opt/android-ndk-r20/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
sys_include_dir=/opt/android-ndk-r20/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
static_crt_dir=/opt/android-ndk-r20/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/
crt_dir=/opt/android-ndk-r20/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/21

msvc_lib_dir=
kernel32_lib_dir=

(note that this example uses Android SDK version 21)

$ zig build-exe -target aarch64v8-linux-android --library c --libc aarch64_android_21.txt main.zig

Note that you must pass the path to the gcc runtime, or else you will get errors regarding missing -lgcc -- @andrewrk, can we do away with linking against libgcc on Android entirely?

The same steps above may also be used for producing a static archive, or shared object -- producing statically compiled Android executables is broken with these changes, should be fixed before merge

@andrewrk
Copy link
Member

can we do away with linking against libgcc on Android entirely?

Yes, we don't need -lgcc because zig provides libcompiler_rt.a.

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, just a few tweaks and I think it's ready to merge.

src/link.cpp Outdated
@@ -1653,15 +1653,25 @@ 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) && !is_dyn_lib && g->libc != nullptr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (target_requires_pie(g->zig_target) && !is_dyn_lib && g->libc != nullptr) {
if (target_requires_pie(g->zig_target) && g->out_type == OutTypeExe) {

src/link.cpp Outdated
lj->args.append("-o");
lj->args.append(buf_ptr(&g->output_file_path));

if (lj->link_in_crt) {
const char *crt1o;
if (g->zig_target->os == OsNetBSD) {
crt1o = "crt0.o";
} else if (target_is_android(g->zig_target)) {
crt1o = "crtbegin_dynamic.o";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the logic below, shouldn't it be here?

if (g->have_dynamic_link) {
    crt1o = "crtbegin_dynamic.o";
} else {
    crt1o = "crtbegin_static.o";
}

Then the other logic remains unchanged:

         } else if (!g->have_dynamic_link) { 
             crt1o = "crt1.o";
         }

src/link.cpp Outdated
@@ -1805,7 +1821,9 @@ static void construct_linker_job_elf(LinkJob *lj) {
}

// crt end
if (lj->link_in_crt && target_libc_needs_crti_crtn(g->zig_target)) {
if (target_is_android(g->zig_target) && g->libc != nullptr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning for g->libc != nullptr? I don't think it's necessary - even if you were running zig compiler on android you'd want crtend_android.o on the link line, right? g->libc is just if someone provided a custom libc.txt file with paths.

I think you want g->link_in_crt instead (see the below if statement). This is whether we want to link against libc.

@andrewrk andrewrk merged commit 543e729 into ziglang:master Sep 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants