h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

configure.sh (3445B)


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