Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 149 lines (129 sloc) 3.52 KB
#!/bin/sh
#
# F/2018-07-01
#
# https://github.com/neovim/neovim/wiki/Building-Neovim
# http://fabic.net/notes/2018/07/01/C-C++-includes-chain-list-command/
#
# See also astdump & astdumpinc
bindir="$( cd `dirname "$0"` )"
clang_extra_args=()
should_remove_tempfile=0
if [ $# -lt 1 ]; then
this="`basename "$0"`"
echo "+-- USAGE --"
echo "|"
echo "| $ $this [<header_file.hpp> ...]* [<include/ dir.> ...]* [-- (<any_further_arg_for_clang>)*]"
echo "|"
echo "+-"
echo "| Basically it performs something like this :"
echo "| echo '#include \"./src/nvim/buffer.h\"' | "
echo "| clang -I.deps/usr/include -Isrc -std=c99 -P -E -H - 2>&1 >/dev/null |"
echo "| grep -v /usr/"
echo "|"
echo "+-"
echo "| First it produces a temporary .hpp file that contains #include \"...\" directives"
echo "| for inclusion of all the header files provided as command line arguments;"
echo "| (and for arguments that are directories, we are including header files found inside these);"
echo "| (^ note that directories will also have a -I<dir> compiler argument emitted)."
echo "|"
echo "| Examples:"
echo "|"
echo "| $ $this include/ vendor/foo/include/ lib/bar/include/"
echo "| $ $this src/some-source-file.hpp src/some-other-file.cxx -- -Iinclude -std=c++17"
echo "+-"
echo "| https://github.com/neovim/neovim/wiki/Building-Neovim"
echo "| http://localhost:3838/notes/2018/07/01/C-C++-includes-chain-list-command/"
echo "+-"
exit 1
fi
# We'll write to a temporary file, instead of piping the output
# to Clang (through astdump.sh).
tempfile="`mktemp inc-test-XXXXXX.hpp`"
# Can't be -_-
if [ ! -w "$tempfile" ]; then
echo
echo "ERROR: Can't write to temporary file '$tempfile'"
echo
exit 1
fi
if true; then
# Absurd sugar
cat <<EOF >> "$tempfile"
/** $PWD/$tempfile
*
* This was generated by "`basename "$0"` :"
*
* \$ $0 $@
*
* On `date`
* at `hostname -f`
* by `whoami`
*/
EOF
fi
while [ $# -gt 0 ];
do
arg="$1"
shift
# "--" separator: remaining arguments will be for astdump_sh/Clang.
if [ "$arg" == "--" ]; then
break
# Directories get added as -Idir,
# and we ch.dir. so as to search for header files.
elif [ -d "$arg" ]; then
clang_extra_args=( "${clang_extra_args[@]}" -I"$arg" )
( cd "$arg" &&
find -type f -not -name "$tempfile" -regextype egrep \
-iregex ".*\.(h|hh|hxx|hpp)" \
| sed -e 's@^\./\(.\+\)$@#include "\1"@'
) >> "$tempfile"
# Individual include files are #include-d
else
( echo
echo "#include \"$arg\""
) >> "$tempfile"
fi
done
cat <<EOF >> "$tempfile"
// EOF
EOF
echo "+- File '$tempfile' :"
echo "|"
cat "$tempfile" \
| sed -e "s@^@| @"
echo "+-"
retv=127
cmd=()
cmd=( clang++ -std=c++1z "${clang_extra_args[@]}" "$@" -P -E -H "$tempfile" )
echo "+- Running:"
echo "| With \$clang_extra_args() = ${clang_extra_args[@]}"
echo "| ${cmd[@]} 1>/dev/null"
echo "+-"
echo
"${cmd[@]}" 1>/dev/null
# ^ discard stdout: it has the post-processed source file(s).
retv=$?
# If bad exit.
if [ $retv -gt 0 ]; then
echo
echo -e "\e[97m+~~> Non-zero exit status: $retv\e[0m"
echo -e "| Command was:"
echo -e "| ${cmd[@]}"
echo -e "+--"
should_remove_tempfile=0
else
echo
echo -e "\e[97m+~~> Success (!)\e[0m"
echo -e "| Command was:"
echo -e "| ${cmd[@]}"
echo -e "+--"
should_remove_tempfile=1
fi
if [ $should_remove_tempfile -gt 0 ]; then
rm "$tempfile" &&
echo -e "+- Removed temporary file '$tempfile'."
else
echo -e "+- Left temporary file '$tempfile' at dir. '$PWD'."
fi
exit $retv
You can’t perform that action at this time.