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