floooh / sokol-zig

Zig bindings for the sokol headers (https://github.com/floooh/sokol)
zlib License
341 stars 46 forks source link

[Help] Compilation for Android #58

Closed 20kano closed 5 months ago

20kano commented 5 months ago

When I compiled it after a long time, the zig specification seems to have changed and the following code returned an error. (0.12.0-dev.3154+0b744da84)

Any advice on how to resolve this would be appreciated.

const std = @import("std");

pub fn build(b: *std.Build) void {
    // const lib = try build_linux(b,);
    const lib = try build_android(b);
    b.installArtifact(lib);
}

fn build_android(
    b: *std.Build,
) !*std.Build.Step.Compile {
    const target = b.resolveTargetQuery(.{
        .cpu_arch = .aarch64,
        .os_tag = .linux,
        .abi = .android,
        .cpu_model = .baseline,
        .cpu_features_add = std.Target.aarch64.featureSet(&.{.v8a}),
    });
    const optimize = b.standardOptimizeOption(.{});

    const lib = b.addStaticLibrary(.{
        .name = "sokol-android",
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });
    lib.setLibCFile(.{.path="android-libc.conf"});
    lib.libc_file.?.addStepDependencies(&lib.step);

    lib.linkSystemLibrary("GLESv3");
    lib.linkSystemLibrary("EGL");
    lib.linkSystemLibrary("android");
    lib.linkSystemLibrary("log");

    const csrc_root = "src/sokol/c/";
    const cflags = &.{ "-DIMPL", "-DSOKOL_GLES3"};
    const csources = [_][]const u8{
        "sokol_log.c",
        // "sokol_app.c",
        // "sokol_gfx.c",
        // "sokol_gl.c",
        // "sokol_debugtext.c",
        // "sokol_debugtext.c",
        // "sokol_fontstash.c",
    };
    inline for (csources) |csrc| lib.addCSourceFile(.{
        .file = .{ .path = csrc_root ++ csrc },
        .flags = cflags,
    });
    return lib;
}

android-libc.conf

include_dir=/opt/android-sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
sys_include_dir=/opt/android-sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
crt_dir=/opt/android-sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/33
msvc_lib_dir=
kernel32_lib_dir=
gcc_dir=
[18:34:41] tsukasa@TO21 /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid [1] 
> zig build
install
└─ install sokol-android
   └─ zig build-lib sokol-android Debug aarch64-linux-android failure
error: error: unable to find Dynamic system library 'GLESv3' using strategy 'paths_first'. searched paths: none
error: unable to find Dynamic system library 'EGL' using strategy 'paths_first'. searched paths: none
error: unable to find Dynamic system library 'android' using strategy 'paths_first'. searched paths: none
error: unable to find Dynamic system library 'log' using strategy 'paths_first'. searched paths: none

error: the following command exited with error code 1:
/home/tsukasa/lang/zig/zig build-lib -lGLESv3 -lEGL -landroid -llog -cflags -DIMPL -DSOKOL_GLES3 -- /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid/src/sokol/c/sokol_log.c -ODebug -target aarch64-linux-android -mcpu baseline+v8a -Mroot -lc --libc /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid/android-libc.conf --cache-dir /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid/zig-cache --global-cache-dir /home/tsukasa/.cache/zig --name sokol-android -static --listen=- 
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install sokol-android transitive failure
   └─ zig build-lib sokol-android Debug aarch64-linux-android failure
error: the following build command failed with exit code 1:
/home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid/zig-cache/o/947db9d3280989bb219b99d0e0d3c84a/build /home/tsukasa/lang/zig/zig /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid /home/tsukasa/develop/zigprojects/android-learn/buildSokolAndroid/zig-cache /home/tsukasa/.cache/zig --seed 0x647efe82 -Zcdadd5fcc5e174a0
floooh commented 5 months ago

Building sokol-zig for Android is pretty much untested, so not sure how much I can help.

The problem seems to be that the linker can't find libraries from the Android NDK anymore. Don't know how that was supposed to work before, I guess via lib.setLibCFile(.{.path="android-libc.conf"});?

Are the paths in that android-libc.conf file still pointing to the right directories? But as I said, that's all just guessing from my side since I didn't investigate how to build sokol-zig for Android so far.

20kano commented 5 months ago

Thank you for your reply.

The path exists, so I don't think it is a problem. This worked before.

https://github.com/ziglang/zig/pull/16058

Since the last compilation was before the commit, I thought maybe additional configuration was needed.

floooh commented 5 months ago

Hmm, not sure tbh, as I said, I haven't tried building sokol-zig for Android before.

20kano commented 5 months ago

OK, thanks!

20kano commented 5 months ago

It was solved by

    ....
    lib.setLibCFile(.{.path="android-libc.conf"});
    lib.libc_file.?.addStepDependencies(&lib.step);

    lib.addLibraryPath(.{.path="/opt/android-sdk/ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/33"});

    lib.linkSystemLibrary("GLESv3");
    lib.linkSystemLibrary("EGL");
    lib.linkSystemLibrary("android");
    lib.linkSystemLibrary("log");
    ....
floooh commented 5 months ago

Hmm, I would expect that this information (the library path) is defined inside android-libc.conf.

20kano commented 5 months ago

Could this be a bug?

floooh commented 5 months ago

Might be a regression in the Zig build system, not sure.