Skip to content

Instantly share code, notes, and snippets.

@ytomino
Last active November 24, 2017 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytomino/dce1b591a0232bc26ff227679803e2aa to your computer and use it in GitHub Desktop.
Save ytomino/dce1b591a0232bc26ff227679803e2aa to your computer and use it in GitHub Desktop.
Generate drake.pc for pkg-config
#!/bin/bash
function usage {
echo "$0 [--gcc=...] [--name=...] --RTS=... > FILENAME.pc" > /dev/stderr
exit 1
}
LOG=/dev/null
URL=https://github.com/ytomino/drake
GCC=gcc
NAME=drake
RTSDIR=
LIBS=
while [[ -n $1 ]] ; do
case $1 in
--gcc)
shift
GCC=$1 ;;
--gcc=*)
GCC=${1#--gcc=} ;;
--name)
shift
NAME=$1 ;;
--name=*)
NAME=${1#--name=} ;;
--RTS)
shift
RTSDIR=$1 ;;
--RTS=*)
RTSDIR=${1#--RTS=} ;;
-v)
LOG=/dev/stderr ;;
*)
usage ;;
esac
shift
done
if [[ -z $RTSDIR ]] ; then
usage
fi
TARGET=$(gcc -dumpmachine)
GCCVERSION=$(gcc -dumpversion)
GCCPREFIX=${GCC/gcc*/}
if [[ $GCCPREFIX == $GCC ]]; then
GCCPREFIX=
fi
GCCSUFFIX=${GCC/*gcc/}
if [[ $GCCSUFFIX == $GCC ]]; then
GCCSUFFIX=
fi
VERSION=${GCCVERSION/.[0-9].[0-9]/}
if [[ VERSION -le 4 ]]; then
VERSION=${GCCVERSION/.[0-9]/}
fi
TMPADB=$TMPDIR/test1.adb
TMPOUT=$TMPDIR/test1
function rm_temps {
rm $TMPOUT $TMPDIR/test1.o $TMPDIR/test1.ali 2> /dev/null
}
function try_make {
rm_temps
${GCCPREFIX}gnatmake -D $TMPDIR -o $TMPOUT -gnatwI $TMPADB --RTS=$RTSDIR -largs $LIBS >& $LOG
}
echo 'procedure test1 is begin null; end;' > $TMPADB
try_make
if [[ $? -ne 0 ]]; then
echo "$0: --RTS is not correctly." > /dev/stderr
exit 1
fi
# DEBUG
DEBUG=$(grep -E '^A -ggdb[0-2]?$' $RTSDIR/adalib/system.ali)
if [[ $DEBUG == "A -ggdb" ]]; then
DEBUG=2
elif [[ -n $DEBUG ]]; then
DEBUG=${DEBUG#A -ggdb}
else
DEBUG=$(grep -E '^A -g[0-2]?$' $RTSDIR/adalib/system.ali)
if [[ $DEBUG == "A -g" ]]; then
DEBUG=2
elif [[ -n $DEBUG ]]; then
DEBUG=${DEBUG#A -g}
else
DEBUG=0
fi
fi
# NOSIG
NOSIG=adalib/nosig.o
if [[ ! -e $RTSDIR/$NOSIG ]]; then
NOSIG=
fi
# NOTB
NOTB=adalib/notb.o
if [[ ! -e $RTSDIR/$NOTB ]]; then
NOTB=
fi
# LIBS
echo 'with Text_IO; use Text_IO;' > $TMPADB
echo 'procedure test1 is S : String := Get_Line; begin null; end;' >> $TMPADB
try_make
if [[ $? -ne 0 ]]; then
old_LIBS=$LIBS
LIBS="$LIBS -lpthread"
try_make
if [[ $? -ne 0 ]]; then
echo "$0: failed on Ada.Text_IO." > /dev/stderr
exit 1
fi
fi
echo 'with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;' > $TMPADB
echo 'procedure test1 is X : Float := Sin (1.0); begin null; end;' >> $TMPADB
try_make
if [[ $? -ne 0 ]]; then
old_LIBS=$LIBS
LIBS="$LIBS -lm"
try_make
if [[ $? -ne 0 ]]; then
echo "$0: failed on Ada.Numerics.Elementary_Functions." > /dev/stderr
exit 1
fi
fi
echo 'with Ada.Real_Time; use Ada.Real_Time;' > $TMPADB
echo 'procedure test1 is T : Time := Clock; begin null; end;' >> $TMPADB
try_make
if [[ $? -ne 0 ]]; then
old_LIBS=$LIBS
LIBS="$LIBS -lrt"
try_make
if [[ $? -ne 0 ]]; then
echo "$0: failed on Ada.Real_Time." > /dev/stderr
exit 1
fi
fi
# Note: -lrt is unnecessary on recent Linux, but necessary on old Linux.
echo 'with Ada.Environment_Encoding.Strings; use Ada.Environment_Encoding; use Ada.Environment_Encoding.Strings;' > $TMPADB
echo 'procedure test1 is D : Decoder := From (Current_Encoding); begin null; end;' >> $TMPADB
try_make
if [[ $? -ne 0 ]]; then
old_LIBS=$LIBS
LIBS="$LIBS -licucore"
try_make
if [[ $? -ne 0 ]]; then
LIBS="$old_LIBS -liconv"
try_make
if [[ $? -ne 0 ]]; then
echo "$0: failed on Ada.Environment_Encoding." > /dev/stderr
exit 1
fi
fi
fi
echo 'with System.Program.Dynamic_Linking; use System.Program.Dynamic_Linking;' > $TMPADB
echo 'procedure test1 is L : Library := Open ("libz.so"); begin null; end;' >> $TMPADB
try_make
if [[ $? -ne 0 ]]; then
old_LIBS=$LIBS
LIBS="$LIBS -ldl"
try_make
if [[ $? -ne 0 ]]; then
echo "$0: failed on System.Program.Dynamic_Linking." > /dev/stderr
exit 1
fi
fi
# output
echo "TARGET=$TARGET"
echo "GCCVERSION=$GCCVERSION"
echo "GCCPREFIX=$GCCPREFIX"
echo "GCCSUFFIX=$GCCSUFFIX"
echo "RTSDIR=$RTSDIR"
echo "DEBUG=$DEBUG"
if [[ -n $NOSIG ]]; then
echo "nosig.o=\${RTSDIR}/$NOSIG"
fi
if [[ -n $NOTB ]]; then
echo "notb.o=\${RTSDIR}/$NOTB"
fi
echo
echo "Name: $NAME"
echo "Description: A Runtime Library for gcc-Ada"
echo "URL: $URL"
echo "Version: $VERSION"
echo 'Cflags: -gnatwI --RTS=${RTSDIR}'
echo "Libs:$LIBS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment