Skip to content

Instantly share code, notes, and snippets.

@yesmar
Last active January 27, 2018 00:59
Show Gist options
  • Save yesmar/fab0d7ae864049e1b4a0c9f8eb2ce680 to your computer and use it in GitHub Desktop.
Save yesmar/fab0d7ae864049e1b4a0c9f8eb2ce680 to your computer and use it in GitHub Desktop.
Display default compiler symbols
#!/bin/bash
# showdefines.bash: display default compiler symbols.
# Copyright © 2011,2017, Ramsey Dow. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause
# Usage: showdefines.bash [-c compiler] [-l language] [additional flags ...]
# Defaults to clang++, but works fine with C and groks the GNU and Intel compilers, as well.
# http://clang.llvm.org/
# http://gcc.gnu.org/
# http://software.intel.com/en-us/articles/intel-compilers/
script=$(basename "$0" '.bash')
compiler=clang
language=c++
tempfile=/tmp/$$.c
function usage {
echo "Usage: $script [-c clang|clang++|gcc|g++|icc|i++] [-l c|c++]"
exit 1
}
OPTERR=0
while getopts 'c:l:' opt; do
case "$opt" in
c) compiler=$OPTARG ;;
l) language=$OPTARG ;;
\?) usage ;;
esac
done
case $compiler in
clang|clang++) # LLVM Clang compiler
flags='-dM -E'
;;
gcc|g++) # GNU compiler Collection
flags='-dM -E'
;;
icc|i++) # Intel compiler
flags='-dM -E'
;;
*) echo "${script}: ${1}: unrecognized compiler"
exit 1
;;
esac
case $language in
c++) if [ "$compiler" = "clang" ]; then
compiler=clang++
all_flags="$flags -x $language"
elif [ "$compiler" = "gcc" ]; then
compiler=g++
all_flags=$flags
elif [ "$compiler" = "icc" ]; then
compiler=i++
all_flags="$flags -x $language"
fi
;;
*) echo "${script}: unsupported langage ${language}"
exit 1
;;
esac
case $compiler in
clang|clang++) # shellcheck disable=SC2086
$compiler $all_flags /dev/null
;;
gcc|g++) # shellcheck disable=SC2086
touch $tempfile && $compiler $flags "$@" $tempfile && rm -f $tempfile
;;
icc|i++) # shellcheck disable=SC2086
touch $tempfile && $compiler $flags "$@" $tempfile && rm -f $tempfile
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment