#!/bin/sh
# Print a BINDGEN_EXTRA_CLANG_ARGS value matching the header setup that
# `zig cc -target <zig-triple>` uses, for cross bindgen runs (boring-sys
# generates its bindings at build time; without target headers bindgen
# either picks up the host's glibc or fails outright).
#
# The flags are parsed from zig's own `zig cc -E -v` output, so this tracks
# the installed zig version and works for any zig target triple.
#
# Usage:  export BINDGEN_EXTRA_CLANG_ARGS="$(ci/zig-bindgen-env aarch64-linux-gnu)"
if [ -z "$1" ]; then
    echo "usage: $0 <zig-target-triple> (e.g. aarch64-linux-gnu)" >&2
    exit 1
fi

zig cc -target "$1" -E -x c /dev/null -v 2>&1 \
    | tr ' ' '\n' \
    | awk '
        /^-triple$/ { getline; printf "--target=%s ", $0 }
        /^-isystem$/ { getline; printf "-isystem %s ", $0 }
        /^-D$/ { getline; if ($0 != "_DEBUG") printf "-D%s ", $0 }
    '
