#!/bin/bash # Try compiling sources against various C compiler + standard combinations. set -euo pipefail cpu_count=$(cat /proc/cpuinfo | grep "^processor" | wc -l) # Use files listed on command line, if specified. file_pattern=*.c if [[ $#ARGV > 1 ]]; then file_pattern=$@ fi # Get list of files that require sys headers. These won't compile with MingW. declare -A require_sys for f in $(grep -l "#include.*sys/" $file_pattern /dev/null); do require_sys[$f]=1 done # Initialize options for each compiler we want to test. declare -A cflags cflags[gcc]="-Wall -Werror -pedantic" cflags[clang]="-Wall -Werror -pedantic" cflags[x86_64-w64-mingw32-gcc]="-Wall -Werror -pedantic" # Generate commands for each C compiler+standard combination. commands=$(mktemp) found_compilers=0 for cc in ${!cflags[@]}; do # Only include command of the compiler can report its own version. # # This is so that we skip compilers that are not installed. if ! ( $cc --version > /dev/null 2>&1 ); then continue fi found_compilers=1 for input in $file_pattern; do if [[ ! -s $input ]]; then continue fi # Add commands for each standard. for std in c89 c99 c11 c17; do # Skip files that includes sys headers for mingw. if [[ "$cc" = *"mingw"* ]]; then if [[ ${require_sys[$input]+_} ]]; then continue fi fi echo "$cc ${cflags[$cc]} --std=$std -c $input -o /dev/null" >> $commands done done done # Run commands. if [[ -s $commands ]]; then xargs -a $commands -d '\n' -P $cpu_count -n 1 bash -c else if [[ $found_compilers -eq 0 ]]; then echo "Did not find any usable compilers" else echo "No files matched" fi rm $commands exit 1 fi # Cleanup. rm $commands exit 0