#!/bin/sh
# Copyright (c) 2015-2021 Contributors as noted in the AUTHORS file
#
# This file is part of Solo5, a sandboxed execution environment.
#
# Permission to use, copy, modify, and/or distribute this software
# for any purpose with or without fee is hereby granted, provided
# that the above copyright notice and this permission notice appear
# in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# x86_64-solo5-none-static-cc: Solo5 wrapper for 'cc'.
# Generated by configure.sh at build time.
#
# Passes through to @@CONFIG_TARGET_CC@ for actual work. Defines all flags
# required for correct operation when building C code intended for inclusion in
# a Solo5 unikernel.
#
# Provides a '-z solo5-abi=ABI' option to select the Solo5 bindings to use if
# linking. ABI defaults to 'stub' to accomodate non-Solo5-aware build systems,
# such as GNU autoconf, which use 'cc'-as-linker to test for the presence of
# symbols.

prog="$(basename $0)"
I="$(realpath $0 | xargs dirname)/../include"
[ ! -d "${I}" ] && echo "$prog: Could not determine include path" 1>&2 && exit 1
L="$(realpath $0 | xargs dirname)/../lib/x86_64-solo5-none-static"
[ ! -d "${L}" ] && echo "$prog: Could not determine library path" 1>&2 && exit 1
# we can't really tell if 'cc' is called with no input, but work around the
# most obvious cases and stop them from "succeeding" and producing an "a.out"
[ "$#" -lt 1 ] && \
    echo "$prog: No input files. Compilation terminated." 1>&1 && exit 1
[ "$#" -eq 1 -a "$1" = "-v" ] && exec cc "$@"
# default to cc as linker, unless we see args to the contrary
M=link
# cc as linker with no -z solo5-abi= defaults to stub
B=stub
Z=
for arg do
    shift
    if [ -n "${Z}" ]; then
        # handle -z linker-arg
        Z=
        case "$arg" in
            solo5-abi=*)
                B="${arg##*=}"
                continue
                ;;
            *)
                set -- "$@" "-z" "$arg"
                continue
                ;;
        esac
    fi

    case "$arg" in
        -c|-S|-E)
            M=compile
            ;;
        -z)
            if [ -z "${Z}" ]; then
                Z=1
                continue
            fi
            ;;
    esac
    set -- "$@" "$arg"
done
case ${M} in
    compile)
        [ -n "${__V}" ] && set -x
        exec cc \
            -nostdinc -mstack-protector-guard=global \
            -isystem ${I}/x86_64-solo5-none-static \
            -I ${I}/solo5 \
            -ffreestanding \
            -fstack-protector-strong \
            "$@"
        ;;
    link)
        [ -n "${B}" ] && B="-Wl,-T,solo5_${B}.lds -l :solo5_${B}.o"
        [ -n "${__V}" ] && set -x
        exec cc \
            -nostdinc -mstack-protector-guard=global \
            -isystem ${I}/x86_64-solo5-none-static \
            -I ${I}/solo5 \
            -ffreestanding \
            -fstack-protector-strong \
            -Wl,--build-id=none \
            -nostdlib \
            -L ${L} \
            ${B} \
            -z max-page-size=0x1000 \
            -static \
            "$@"
        ;;
esac
