aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

configure (8179B)


      1 #!/bin/sh
      2 # aruu dev toolchain configure
      3 #
      4 # generates three headers:
      5 #   config.h        architecture and platform for as, ld, libtcutil
      6 #   cc/config.h     driver configuration (crt files, lib paths, defines)
      7 #   version.h       version string
      8 #
      9 # crt and runtime files are discovered, not assumed from the libc:
     10 # crt1 crti crtn exist everywhere, crtbegin crtend libgcc only with a
     11 # gcc runtime, so the same script works on musl and other libcs
     12 
     13 set -u
     14 
     15 fail() {
     16 	echo "configure: $*" >&2
     17 	exit 1
     18 }
     19 
     20 devdir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
     21 
     22 normpath() {
     23 	( cd "$1" 2>/dev/null && pwd ) || echo "$1"
     24 }
     25 
     26 rootpath() {
     27 	case "$1" in
     28 	/) printf '%s' / ;;
     29 	*) printf '%s' "${1%/}" ;;
     30 	esac
     31 }
     32 
     33 host=
     34 target=
     35 sysroot=
     36 gcclibdir=
     37 CC=${CC:-cc}
     38 
     39 for arg; do
     40 	case "$arg" in
     41 	--host=*) host=${arg#*=} ;;
     42 	--target=*) target=${arg#*=} ;;
     43 	--sysroot=*) sysroot=${arg#*=} ;;
     44 	--with-gcclibdir=*) gcclibdir=${arg#*=} ;;
     45 	CC=*) CC=${arg#*=} ;;
     46 	*) fail "unknown option: $arg" ;;
     47 	esac
     48 done
     49 
     50 # determine host triplet, prefer an explicit --host, then try the c
     51 # compiler, then fall back to uname so the script works without gcc
     52 if [ -z "$host" ]; then
     53 	host=$($CC -dumpmachine 2>/dev/null) || true
     54 fi
     55 if [ -z "$host" ]; then
     56 	m=$(uname -m)
     57 	s=$(uname -s | tr '[:upper:]' '[:lower:]')
     58 	case "$s" in
     59 	linux)
     60 		libc=gnu
     61 		[ -f "/lib/ld-musl-$m.so.1" ] && libc=musl
     62 		host="$m-pc-linux-$libc"
     63 		;;
     64 	*)	host="$m-pc-$s" ;;
     65 	esac
     66 fi
     67 : "${target:=$host}"
     68 
     69 printf 'host:   %s\n' "$host"
     70 printf 'target: %s\n' "$target"
     71 
     72 # map target to architecture constants used by as, ld, libtcutil
     73 case "$target" in
     74 x86_64-*|amd64-*)
     75 	tc_arch=1
     76 	machine=EM_X86_64
     77 	;;
     78 aarch64-*)
     79 	tc_arch=2
     80 	machine=EM_AARCH64
     81 	;;
     82 riscv64-*)
     83 	tc_arch=3
     84 	machine=EM_RISCV
     85 	;;
     86 *) fail "unsupported target: $target" ;;
     87 esac
     88 
     89 case "$target" in
     90 *-apple-darwin*) tc_platform=2 ;;
     91 *) tc_platform=1 ;;
     92 esac
     93 
     94 # an explicit sysroot is a search boundary, not a prefix preference
     95 root=
     96 if [ -n "$sysroot" ]; then
     97 	root=$(rootpath "$(normpath "$sysroot")")
     98 fi
     99 
    100 # find the libc directory: the first standard path that contains crt1.o
    101 crtdir=
    102 if [ -n "$root" ]; then
    103 	for dir in "$root/lib"; do
    104 		if [ -f "$dir/crt1.o" ]; then
    105 			crtdir=$(normpath "$dir")
    106 			break
    107 		fi
    108 	done
    109 else
    110 	printf 'no sysroot: using standalone host runtime paths\n'
    111 	for dir in /usr/lib /lib /usr/local/lib; do
    112 		if [ -f "$dir/crt1.o" ]; then
    113 			crtdir=$(normpath "$dir")
    114 			break
    115 		fi
    116 	done
    117 fi
    118 if [ -z "$crtdir" ]; then
    119 	if [ -n "$root" ]; then
    120 		fail "cannot find crt1.o inside target sysroot"
    121 	fi
    122 	fail "cannot find host crt1.o, use --sysroot for an isolated target"
    123 fi
    124 printf 'libc directory: %s\n' "$crtdir"
    125 
    126 # find the gcc runtime directory: systems built with gcc have one, musl does not,
    127 # and crtbeginT.o is what marks it as usable
    128 if [ -n "$root" ] && [ -n "$gcclibdir" ]; then
    129 	if [ "$root" != / ]; then
    130 		case "$gcclibdir" in
    131 		"$root"/*) ;;
    132 		*) fail "--with-gcclibdir escapes --sysroot" ;;
    133 		esac
    134 	elif [ "${gcclibdir#/}" = "$gcclibdir" ]; then
    135 		fail "--with-gcclibdir must name an absolute target-root path"
    136 	fi
    137 elif [ -n "$root" ]; then
    138 	for dir in "$root"/lib/gcc/*/*/ "$root"/usr/lib/gcc/*/*/; do
    139 		if [ -f "${dir}crtbeginT.o" ]; then
    140 			gcclibdir=$(normpath "$dir")
    141 			break
    142 		fi
    143 	done
    144 elif [ -z "$gcclibdir" ]; then
    145 	for dir in /usr/lib/gcc/*/*/ /usr/local/lib/gcc/*/*/; do
    146 		if [ -f "${dir}crtbeginT.o" ]; then
    147 			gcclibdir=$(normpath "$dir")
    148 			break
    149 		fi
    150 	done
    151 fi
    152 # validate: only treat gcclibdir as real if crtbeginT.o is there
    153 if [ -z "$gcclibdir" ] || [ ! -f "$gcclibdir/crtbeginT.o" ]; then
    154 	gcclibdir=
    155 fi
    156 if [ -n "$gcclibdir" ]; then
    157 	printf 'gcc runtime: %s\n' "$gcclibdir"
    158 fi
    159 
    160 # build library search paths
    161 searchpaths=$crtdir
    162 [ -n "$gcclibdir" ] && searchpaths="$searchpaths $gcclibdir"
    163 
    164 linkflags=
    165 for dir in $searchpaths; do
    166 	linkflags=$linkflags' "-L", "'$dir'",'
    167 done
    168 
    169 # crt file lists: crt1 crti crtn are standard across libcs, crtbegin and
    170 # crtend come with gcc. libgcc is listed when present and the static
    171 # linker skips unused members, so it is harmless on musl. libc wraps
    172 # libgcc because libgcc_eh needs libc symbols (pthread_cond) and libc
    173 # needs libgcc symbols (unwind)
    174 startfiles='-l", ":crt1.o", "-l", ":crti.o"'
    175 endfiles='-l", "c"'
    176 
    177 if [ -n "$gcclibdir" ] && [ -f "$gcclibdir/crtbeginT.o" ]; then
    178 	startfiles=$startfiles', "-l", ":crtbeginT.o"'
    179 
    180 	if [ -f "$gcclibdir/libgcc.a" ]; then
    181 		endfiles='-l", "c", "-l", "gcc"'
    182 		if [ -f "$gcclibdir/libgcc_eh.a" ]; then
    183 			endfiles=$endfiles', "-l", "gcc_eh"'
    184 		fi
    185 		endfiles=$endfiles', "-l", "c"'
    186 	fi
    187 
    188 	endfiles=$endfiles', "-l", ":crtend.o"'
    189 fi
    190 
    191 endfiles=$endfiles', "-l", ":crtn.o"'
    192 
    193 # fix up leading quote (the string starts with -l, needs ")
    194 startfiles='"'"$startfiles"
    195 endfiles='"'"$endfiles"
    196 
    197 # target-specific defines (architecture, os, data model), not libc-specific:
    198 # the type and feature macros system headers expect from a hosted compiler
    199 defines='
    200 	"-D", "__STDC_ENDIAN_LITTLE__=1234",
    201 	"-D", "__STDC_ENDIAN_BIG__=4321",
    202 	"-D", "__STDC_ENDIAN_NATIVE__=__STDC_ENDIAN_LITTLE__",
    203 	"-D", "__ORDER_LITTLE_ENDIAN__=__STDC_ENDIAN_LITTLE__",
    204 	"-D", "__ORDER_BIG_ENDIAN__=__STDC_ENDIAN_BIG__",
    205 	"-D", "__BYTE_ORDER__=__STDC_ENDIAN_NATIVE__",
    206 	"-D", "__FLT_EVAL_METHOD__=1",
    207 	"-D", "__BITINT_MAXWIDTH__=64",
    208 '
    209 
    210 case "$target" in
    211 x86_64-*|amd64-*)
    212 	defines=$defines'
    213 	"-D", "__x86_64__",
    214 	"-D", "__amd64__",
    215 	'
    216 	;;
    217 aarch64-*)
    218 	defines=$defines'
    219 	"-D", "__aarch64__",
    220 	'
    221 	;;
    222 riscv64-*)
    223 	defines=$defines'
    224 	"-D", "__riscv",
    225 	"-D", "__riscv_i=2001000",
    226 	"-D", "__riscv_m=2000000",
    227 	"-D", "__riscv_a=2001000",
    228 	"-D", "__riscv_f=2002000",
    229 	"-D", "__riscv_d=2002000",
    230 	"-D", "__riscv_c=2000000",
    231 	"-D", "__riscv_xlen=64",
    232 	"-D", "__riscv_flen=64",
    233 	'
    234 	;;
    235 esac
    236 
    237 # lp64 data model
    238 defines=$defines'
    239 	"-D", "__LP64__",
    240 	"-D", "__SIZEOF_LONG__=8",
    241 	"-D", "__SIZEOF_POINTER__=8",
    242 	"-D", "__SIZEOF_SIZE_T__=8",
    243 	"-D", "__SIZEOF_PTRDIFF_T__=8",
    244 
    245 	/* standard integer type mappings, needed by system headers */
    246 	"-D", "__SIZE_TYPE__=unsigned long",
    247 	"-D", "__PTRDIFF_TYPE__=long",
    248 	"-D", "__WCHAR_TYPE__=int",
    249 	"-D", "__WINT_TYPE__=unsigned int",
    250 	"-D", "__INTMAX_TYPE__=long",
    251 	"-D", "__UINTMAX_TYPE__=unsigned long",
    252 
    253 	"-D", "__CHAR_BIT__=8",
    254 	"-D", "__SCHAR_MAX__=0x7f",
    255 	"-D", "__SHRT_MAX__=0x7fff",
    256 	"-D", "__INT_MAX__=0x7fffffff",
    257 	"-D", "__LONG_MAX__=0x7fffffffffffffffL",
    258 	"-D", "__LONG_LONG_MAX__=0x7fffffffffffffffL",
    259 '
    260 
    261 # os defines
    262 case "$target" in
    263 *linux*)
    264 	defines=$defines'
    265 	"-D", "__linux__",
    266 	"-D", "__unix__",
    267 	'
    268 	;;
    269 esac
    270 
    271 if [ -n "$root" ]; then
    272 	sysincdirs="$root${ARUU_PATH_INCLUDE:-/include}"
    273 else
    274 	sysincdirs="/usr/include /usr/local/include"
    275 	case "$target" in
    276 	x86_64-*|amd64-*) sysincdirs="$sysincdirs /usr/include/x86_64-linux-gnu" ;;
    277 	esac
    278 fi
    279 includeflags=
    280 for dir in $sysincdirs; do
    281 	includeflags=$includeflags' "-I", "'$dir'",'
    282 	printf 'system include: %s\n' "$dir"
    283 done
    284 
    285 DEFAULT_CPP=$(printf '"%s", ' cpp)
    286 DEFAULT_QBE=$(printf '"%s", ' qbe)
    287 DEFAULT_AS=$(printf '"%s", ' as)
    288 DEFAULT_LD=$(printf '"%s", ' ld)
    289 
    290 # generate config.h
    291 printf 'creating config.h... '
    292 cat >"$devdir/config.h" <<EOF
    293 /* generated by configure, do not edit */
    294 
    295 #define TC_ARCH_X64      1
    296 #define TC_ARCH_AARCH64  2
    297 #define TC_ARCH_RISCV64  3
    298 
    299 #define TC_TARGET_ARCH  $tc_arch
    300 
    301 #define TC_PLATFORM_POSIX  1
    302 #define TC_PLATFORM_APPLE  2
    303 
    304 #define TC_TARGET_PLATFORM  $tc_platform
    305 
    306 #define TARGET_POINTER_SIZE  8
    307 
    308 #define MACHINE_TYPE  $machine
    309 EOF
    310 echo done
    311 
    312 # generate cc/config.h
    313 printf 'creating cc/config.h... '
    314 cat >"$devdir/cc/config.h" <<EOF
    315 /* generated by configure, do not edit */
    316 
    317 static const char target[]               = "$target";
    318 static const char *const startfiles[]    = {$startfiles};
    319 static const char *const endfiles[]      = {$endfiles};
    320 static const char *const preprocesscmd[] = {
    321 	$DEFAULT_CPP"-undef",
    322 	$includeflags
    323 	"-D", "__STDC_NO_COMPLEX__",
    324 
    325 	"-D", "__extension__=",
    326 $defines};
    327 static const char *const codegencmd[]    = {$DEFAULT_QBE};
    328 static const char *const assemblecmd[]   = {$DEFAULT_AS};
    329 static const char *const linkcmd[]       = {$DEFAULT_LD$linkflags};
    330 EOF
    331 echo done
    332 
    333 # generate version.h
    334 printf 'creating version.h... '
    335 cat >"$devdir/version.h" <<EOF
    336 /* generated by configure, do not edit */
    337 #define VERSION "aruu-dev"
    338 EOF
    339 echo done