How to generate a file from within a build.zig script?

I’m writing a build.zig in which I need to generate a file in order to set the libc for Android.

Right now I have the following in my build.zig:

    const write_file = b.addWriteFile("android-29-armabi.conf",
        \\include_dir=/home/juan/Downloads/android/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
        \\sys_include_dir=/home/juan/Downloads/android/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/arm-linux-androideabi
        \\crt_dir=/home/juan/Downloads/android/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/29
        \\msvc_lib_dir=
        \\kernel32_lib_dir=
        \\gcc_dir=
        \\
    );
    b.getInstallStep().dependOn(&write_file.step);

After that I need to use the file in the following lines:

    lib.libc_file = b.path("android-29-armabi.conf");
    lib.libc_file.?.addStepDependencies(&lib.step);

The problem is that the generated file lives somewhere in the .zig-cache folder.

How can I access the generated file?

Do I have to copy it from the cache into the root of my project?

Is this the right/intended way to generate files from within the build.zig script?

Please help me!

I’m not being able to understand how to do this, as described in: https://ziglang.org/learn/build-system/.

I think you can

const wf = b.addWriteFiles();
const f = wf.add("android-29-armabi.conf",
        \\include_dir=/home/juan/Downloads/android/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
        \\sys_include_dir=/home/juan/Downloads/android/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/arm-linux-androideabi
        \\crt_dir=/home/juan/Downloads/android/android-ndk-r26c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/29
        \\msvc_lib_dir=
        \\kernel32_lib_dir=
        \\gcc_dir=
        \\
    );
lib.libc_file = f;

f is already a LazyPath there. And I think you wouldn’t even need to add the dependency on the write files step because if the lib depends on f being written out first, the dependency is inferred.

4 Likes

Nice! That works! Thank you very much!

Btw, love your videos on YT Dude! Learned a lot watching them! :slight_smile:

2 Likes