#!/bin/sh

#
# Copyright (C) 2016 Eistec AB
#
# This file is subject to the terms and conditions of the GNU Lesser General
# Public License v2.1. See the file LICENSE in the top level directory for more
# details.
#
# Usage: $0 [CFLAGS]...
#
# Extract all macros from CFLAGS and generate a header file format"
#

# exit on any errors below this line
set -e

echo "/* DO NOT edit this file, your changes will be overwritten and won't take any effect! */"
echo "/* Generated from CFLAGS: $@ */"

[ -n "${LTOFLAGS}" ] && echo "/* LTOFLAGS=${LTOFLAGS} */"

for arg in "$@"; do
  case ${arg} in
    -D*)
      # Strip leading -D
      d=${arg#-D}
      if [ -z "${d##*=*}" ]; then
        # key=value pairs
        key=${d%%=*}
        value=${d#*=}
        echo "#define $key $value"
      else
        # simple #define
        echo "#define $d 1"
      fi
      ;;
    -U*)
      # Strip leading -U
      d=${arg#-U}
      echo "#undef $d"
      ;;
    *)
      continue
      ;;
  esac
done
