configure.sh (3615B)
1 #!/bin/sh 2 3 # The following environment variables can be used to configure the build: 4 # 5 # CC="compiler" 6 # Specify the compiler to use. 7 # By default, cc will be used. 8 # The string "compiler" must be the name of an executable in $PATH. 9 # 10 # ARCH="architecture" 11 # You can use this variable to build for a different architecture, for example 12 # if you want to cross-compile or to use the portable version. 13 # By default, the build script will detect which architecture it is running on. 14 # The string "architecture" must be one of "AVX2", "NEON" or "PORTABLE". 15 # 16 # THREADS=n 17 # Choose how many threads to use for multi-threaded oerations. 18 # By default, 8 threads will be used (TODO: in the future this will be 19 # determined base on the system). 20 # The number n must be between 1 and 128. 21 # 22 # SANITIZE="option1,option2,..." 23 # Add the options "-fsanitize=option1", "-fsanitize=option2", ... to the 24 # compilation command when compiling in debug mode. 25 # By default, "-fsanitize=address" and "-fsanitize=undefined" will be used. 26 # If this variable is set, the default is overridden. No check is performed 27 # on the availability of any sanitizer used, make sure the ones you use are 28 # available on your system. 29 # 30 # Examples 31 # 32 # 1. Build using clang and 4 threads 33 # CC=clang THREADS=4 ./configure.sh && make 34 # 35 # 2. Build using thread and undefined behavior sanitizers when in debug mode 36 # SANITIZE="thread,undefined" ./configure.sh && make 37 38 greparch() { 39 $CC -march=native -dM -E - </dev/null 2>/dev/null | grep "$1" 40 } 41 42 detectthreads() { 43 echo 8 # TODO: choose based on system 44 } 45 46 detectarch() { 47 [ -n "$(greparch __AVX2__)" ] && detected="AVX2" 48 [ -n "$(greparch __ARM_NEON)" ] && detected="NEON" 49 [ -z "$detected" ] && detected="PORTABLE" 50 51 echo "$detected" 52 } 53 54 validatecc() { 55 if ! (command -v "$CC" >/dev/null 2>&1) ; then 56 echo "Error: compiler '$CC' not found" 57 exit 1 58 fi 59 } 60 61 validatethreads() { 62 if [ "$THREADS" -le 0 ] || [ "$THREADS" -gt 128 ]; then 63 echo "Error: number of threads must be between 1 and 128" 64 exit 1 65 fi 66 } 67 68 validatearch() { 69 case "$ARCH" in 70 AVX2|NEON|PORTABLE) 71 ;; 72 *) 73 echo "Error: architecture '$ARCH' not supported" 74 echo "Supported architectures: AVX2, NEON, PORTABLE" 75 exit 1 76 ;; 77 esac 78 } 79 80 CC=${CC:-cc} 81 THREADS=${THREADS-"$(detectthreads)"} 82 ARCH=${ARCH-"$(detectarch)"} 83 84 validatecc 85 validatethreads 86 validatearch 87 88 STD="-std=c11" 89 WFLAGS="-pedantic -Wall -Wextra -Werror" 90 WNOFLAGS="-Wno-unused-parameter -Wno-unused-function -Wno-unknown-pragmas" 91 WNOFLAGS="$WNOFLAGS -Wno-unused-command-line-argument" 92 93 [ "$ARCH" = "AVX2" ] && AVX="-mavx2" 94 95 if [ -n "$SANITIZE" ]; then 96 # Use the user-specified comma-separated sanitizers 97 for san in $(echo "$SANITIZE" | tr ',' '\n'); do 98 SAN="$SAN -fsanitize=$san" 99 done 100 else 101 SAN="-fsanitize=address -fsanitize=undefined" 102 fi 103 LIBS="-lpthread" 104 105 CFLAGS="$STD $LIBS $WFLAGS $WNOFLAGS $AVX -O3 -fPIC" 106 DBGFLAGS="$STD $LIBS $WFLAGS $WNOFLAGS $SAN $AVX -g3 -DDEBUG -fPIC" 107 MACROS="-DTHREADS=$THREADS -D$ARCH" 108 109 if (command -v "python3-config" >/dev/null 2>&1) ; then 110 PYTHON3_INCLUDES="$(python3-config --includes)" 111 PYTHON3="version $(echo "$PYTHON3_INCLUDES" | sed 's/.*3\./3./')" 112 else 113 PYTHON3_INCLUDES="" 114 PYTHON3="Not found, Python shell won't be available" 115 fi 116 117 echo "Compiler: $CC" 118 echo "Selected architecture: $ARCH" 119 echo "Number of threads: $THREADS" 120 echo "Sanitizer options (debug build only): $SAN" 121 echo "Python3 development libraries: $PYTHON3" 122 123 { 124 echo "ARCH = $ARCH"; 125 echo ""; 126 echo "CFLAGS = $CFLAGS"; 127 echo ""; 128 echo "DBGFLAGS = $DBGFLAGS"; 129 echo ""; 130 echo "MACROS = $MACROS" 131 echo ""; 132 echo "PYTHON3_INCLUDES = $PYTHON3_INCLUDES" 133 echo "CC = $CC" 134 } > config.mk