summaryrefslogtreecommitdiff
path: root/libguile/guile-snarf.in
blob: 22dc1d3891793467702f0c76771938ab1fd01dc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/sh
# Extract the initialization actions from source files.
#
#  Copyright (C) 1996, 97, 98, 99, 2000, 2001, 2002, 2004, 2006, 2008,
#    2009, 2014 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3, or (at
# your option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this software; see the file COPYING.LESSER.  If
# not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301 USA

# Commentary:

# Usage: guile-snarf [-o OUTFILE] [CPP-ARGS ...]
 
# Initialization actions are extracted to OUTFILE or to standard
# output when no OUTFILE has been specified or when OUTFILE is "-".
# The C preprocessor is called with CPP-ARGS (which usually include a
# input file) and the output is filtered for the actions.
# 
# If there are errors during processing, OUTFILE is deleted and the
# program exits with non-zero status.
# 
# During snarfing, the pre-processor macro SCM_MAGIC_SNARFER is
# defined.  You can use this to avoid including snarfer output files
# that don't yet exist by writing code like this:
# 
#   #ifndef SCM_MAGIC_SNARFER
#   #include "foo.x"
#   #endif
# 
# If the environment variable CPP is set, use its value instead of the
# C pre-processor determined at Guile configure-time: "@CPP@".

# Code:

## funcs

modern_snarf ()                         # writes stdout
{
    ## Apparently, AIX's preprocessor is unhappy if you try to #include an
    ## empty file.
    echo "/* cpp arguments: $@ */" ;
    ${cpp} -DSCM_MAGIC_SNARF_INITS -DSCM_MAGIC_SNARFER "$@" > ${temp} && cpp_ok_p=true
    sed -ne 's/ *\^ *\^ */\
/
s/.*\n//
t x
d
: x
s/ *\^ *: *\^ */;\
/
t y
N
s/\n\(#.*\)/ /
s/\n/ /
t x
: y
P
D' ${temp}
}

## main

# process command line
if [ x"$1" = x--help ] ; then
    @AWK@ '/^#.Commentary:/,/^#.Code:/' $0 | grep -v Code: \
        | sed -e 1,2d -e 's/^. *//g'
    exit 0
fi
if [ x"$1" = x-o ]
    then outfile="$2" ; shift ; shift ;
    else  outfile="-" ;
fi

# set vars and handler -- handle CPP override
cpp_ok_p=false

if [ x"$TMPDIR" = x ]; then TMPDIR="/tmp" ; else : ; fi
tempdir="$TMPDIR/guile-snarf.$$"
(umask 077 && mkdir $tempdir) || exit 1
temp="$tempdir/tmp"

if [ x"$CPP" = x ] ; then cpp="@CPP@" ; else cpp="$CPP" ; fi

trap "rm -rf $tempdir" 0 1 2 15

# filter out -g* flags from commandline
# as some flags like -ggdb3 cause CPP

cpp_args=""
for arg in "$@"
do
    case "$arg" in
        -g*) ;; # skip debug flag
        *) cpp_args="$cpp_args $arg" ;;
    esac
done

if [ ! "$outfile" = "-" ] ; then
    modern_snarf $cpp_args > $outfile
else
    modern_snarf $cpp_args
fi

# zonk outfile if errors occurred
if $cpp_ok_p ; then
    exit 0
else
    [ ! "$outfile" = "-" ] && rm -f $outfile
    exit 1
fi

# guile-snarf ends here