Skip to content

Instantly share code, notes, and snippets.

@jflemer-ndp
Last active October 5, 2020 16:19
Show Gist options
  • Save jflemer-ndp/692c5aa5f9a506f8f29960b4d7aa37fb to your computer and use it in GitHub Desktop.
Save jflemer-ndp/692c5aa5f9a506f8f29960b4d7aa37fb to your computer and use it in GitHub Desktop.
Check a c/c++ header by itself
#!/bin/sh
##############################################################################
# Test a C/C++ header file for standalone validity. This will help show if
# there are missing/implicit headers etc.
#
# Usage
# -----
# checkheader.sh foo.h ...
#
# Files
# -----
# This script uses `make`, so it will use the Makefile (or GNUMakefile) in
# the current directory! Maybe run it from /tmp if you do not want this.
#
# Environment
# -----------
# This script uses `make`, so normal variables like `$CC` and `$CXX` apply.
#
# License
# -------
# * BSD-2-Clause
#
##############################################################################
##############################################################################
#
# Copyright 2020, NDP LLC
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
##############################################################################
if [ -z "$1" ] || [ "$1" = "-h" ]; then
echo "usage: $0 foo.h ..." >&2
exit 64
fi
tmpf=""
clean() {
if [ -n "$tmpf" ]; then
rm -f "$tmpf" "${tmpf%%.cc}.o" >/dev/null 2>&1 || true
fi
tmpf=""
}
check() {
f="$1"
srcdir=`dirname "$f"`
srcdir=`(cd "$srcdir" && pwd)`
tmpf=`mktemp --suffix=.cc`
cwd=`pwd`
cat - >"$tmpf" <<EOF
#include "$f"
void foo_() {}
EOF
export CPPFLAGS="${CPPFLAGS}${CPPFLAGS:+ }-I$srcdir -I$cwd"
make "${tmpf%%.cc}.o"
}
trap clean 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
ret=0
for i in "$@"; do
echo "==> $i ..."
check "$i" || ret=1
clean
done
exit $ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment