#!/bin/sh

# I used to just check __STDC_HOSTED__ without special CFLAGS, but now I
# want to force freestanding compilation on otherwise hosted processors.
# Thus, the user can set IPC_FREESTANDING=y . If unset, this gets called.

# Accept "-n" to invert the result (select second field, not first)
if [ "x$1" = "x-n" ]; then select=2; shift; else select=1; fi

CC=$1
if [ "x$CC" = "x" ]; then
    echo "$0: pass the compiler path (\$CC) as argument" >& 2
    exit 1
fi

# Check the compiler: if it has no matches is prints the unadorned string
if [ "$($CC -print-file-name=libc.so)" = "libc.so" ]; then
    res=y,n
else
    res=n,y
fi

# If passed from the user, override the autodetected value
if [ "$IPC_FREESTANDING" = "y" ]; then
    res=y,n
fi

echo $res | cut -d, -f $select
