JINX_MAJOR_VER=0.2

export CFLAGS="-O2 -pipe -march=x86-64 -mtune=generic -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer"
export CXXFLAGS="${CFLAGS} -Wp,-D_GLIBCXX_ASSERTIONS"
export LDFLAGS="-Wl,-O1 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now"

OS_TRIPLET=x86_64-pc-vinix-mlibc

post_package_strip() {
    if [ -z "$strip_command" ]; then
        strip_command="${OS_TRIPLET}-strip"
    fi

    for f in $(find "${dest_dir}"); do
        if file "$f" | grep 'not stripped' >/dev/null; then
            echo "* stripping '$f'..."
            stripped_file="$(mktemp)"
            ${strip_command} "$f" -o "$stripped_file"
            chmod --reference="$f" "$stripped_file"
            mv -f "$stripped_file" "$f"
        fi
    done
}

autotools_configure() {
    if [ -z "${configure_script_path}" ]; then
        configure_script_path="${source_dir}/configure"
    fi

        ac_cv_func_malloc_0_nonnull=yes \
        ac_cv_func_calloc_0_nonnull=yes \
        ac_cv_func_realloc_0_nonnull=yes \
    ${configure_script_path} \
        --host=${OS_TRIPLET} \
        --with-sysroot=${sysroot_dir} \
        --prefix=${prefix} \
        --sysconfdir=/etc \
        --localstatedir=/var \
        --bindir=${prefix}/bin \
        --sbindir=${prefix}/bin \
        --libdir=${prefix}/lib \
        --disable-static \
        --enable-shared \
        --disable-malloc0returnsnull \
        "$@"
}

autotools_recursive_regen() {
    ACLOCAL_INCLUDE=""
    if [ -d ${sysroot_dir}/usr/share/aclocal ]; then
        ACLOCAL_INCLUDE="-I${sysroot_dir}/usr/share/aclocal"
    fi

    for f in $(find . -name configure.ac -o -name configure.in -type f); do
        echo "* autotools regen in '$(dirname $f)'..."
        ( cd "$(dirname "$f")" && autoreconf -fvi "$@" $ACLOCAL_INCLUDE )
    done
}

meson_configure() {
    if [ -z "${meson_source_dir}" ]; then
        meson_source_dir="${source_dir}"
    fi

    meson setup "${meson_source_dir}" \
        --cross-file "${base_dir}/build-support/cross_file.txt" \
        --prefix=${prefix} \
        --sysconfdir=/etc \
        --localstatedir=/var \
        --libdir=lib \
        --sbindir=bin \
        --buildtype=release \
        -Ddefault_library=shared \
        "$@"
}

if [ "$in_container" = true ]; then
    export VMODULES="/tmp/.vmodules"
    mkdir -p "${VMODULES}"/cache
    mkdir -p /tmp/v_$(id -u)
fi
