Skip to content

Instantly share code, notes, and snippets.

@clausecker
Created April 21, 2014 01:26
Show Gist options
  • Save clausecker/11129762 to your computer and use it in GitHub Desktop.
Save clausecker/11129762 to your computer and use it in GitHub Desktop.
set -e
# preprocess an assembly file with cc -E, throwing out C code from system
# header files in the process. This is neccessary as some Linux headers with
# valuable symbolic constants also contain C code we do not want. This script
# gets rid of these.
#
# Please notice that this script does not remove C code from header included
# from local includes. Please also notice that this script eats line directives
# from included files.
BASE=$1 # source / target file w/o extension
CC=$2
RM=$3
CFLAGS=$4
# First, add include sentinels so we can later throw out C code inside headers
# of which we only want definitions.
ed -s <<EOF
r ${BASE}.S
g/^# *include *<.*>/i\\
CCODE_BEGIN\\
.\\
+1a\\
CCODE_END\\
.
w ${BASE}.SS
q
EOF
# Now, pre- and postprocess ${BASE}.SS: Run it through the preprocessor, adjust
# line directives and finally throw out anything between the the sentinels.
ed -s <<EOF
r !${CC} -E -C -x assembler-with-cpp ${CFLAGS} ${BASE}.SS
g/^#/d
g/^CCODE_BEGIN\$/.s/^.*\$//\\
.+1,/^CCODE_END\$/d
1i
# 1 "${BASE}.S"
.
w ${BASE}.s
q
EOF
# cleanup
$RM ${BASE}.SS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment