#!/bin/sh fail() { echo "$0: $*" >&2 exit 1 } prefix=/usr/local bindir='$(PREFIX)/bin' host= target= gcclibdir= for arg ; do case "$arg" in --prefix=*) prefix=${arg#*=} ;; --bindir=*) bindir=${arg#*=} ;; --host=*) host=${arg#*=} ;; --target=*) target=${arg#*=} ;; --with-cpp=*) DEFAULT_PREPROCESSOR=${arg#*=} ;; --with-qbe=*) DEFAULT_QBE=${arg#*=} ;; --with-as=*) DEFAULT_ASSEMBLER=${arg#*=} ;; --with-ld=*) DEFAULT_LINKER=${arg#*=} ;; --with-ldso=*) DEFAULT_DYNAMIC_LINKER=${arg#*=} ;; --with-gcc-libdir=*) gcclibdir=${arg#*=} ;; CC=*) CC=${arg#*=} ;; CFLAGS=*) CFLAGS=${arg#*=} ;; LDFLAGS=*) LDFLAGS=${arg#*=} ;; *) fail "unknown option '$arg'" esac done : ${CC:=cc} printf 'checking host system type... ' test -n "$host" || host=$($CC -dumpmachine 2>/dev/null) || fail "could not determine host" printf '%s\n' "$host" printf 'checking target system type... ' test -n "$target" || target=$host printf '%s\n' "$target" toolprefix= if [ "$host" != "$target" ] ; then toolprefix=$target- fi startfiles=0 endfiles=0 defines= linkflags= case "$target" in *-linux-*musl*) test "${DEFAULT_DYNAMIC_LINKER+set}" || case "$target" in x86_64*) DEFAULT_DYNAMIC_LINKER=/lib/ld-musl-x86_64.so.1 ;; aarch64*) DEFAULT_DYNAMIC_LINKER=/lib/ld-musl-aarch64.so.1 ;; riscv64*) DEFAULT_DYNAMIC_LINKER=/lib/ld-musl-riscv64.so.1 ;; *) fail "unsuported target '$target'" esac startfiles='"-l", ":crt1.o", "-l", ":crti.o"' endfiles='"-l", "c", "-l", ":crtn.o"' ;; *-linux-*gnu*) test "${DEFAULT_DYNAMIC_LINKER+set}" || case "$target" in x86_64*) DEFAULT_DYNAMIC_LINKER=/lib64/ld-linux-x86-64.so.2 ;; aarch64*) DEFAULT_DYNAMIC_LINKER=/lib/ld-linux-aarch64.so.1 ;; riscv64*) DEFAULT_DYNAMIC_LINKER=/lib/ld-linux-riscv64-lp64d.so.1 ;; *) fail "unsuported target '$target'" esac startfiles='"-l", ":crt1.o", "-l", ":crti.o", "-l", ":crtbegin.o"' endfiles='"-l", "c", "-l", ":crtend.o", "-l", ":crtn.o"' if [ -z "$gcclibdir" ] ; then test "$host" = "$target" || fail "gcc libdir must be specified when building a cross-compiler" crtbegin=$($CC -print-file-name=crtbegin.o 2>/dev/null) gcclibdir=${crtbegin%/*} fi linkflags='"-L", "'$gcclibdir'",' ;; *-*freebsd*) : ${DEFAULT_DYNAMIC_LINKER:=/libexec/ld-elf.so.1} startfiles='"-l", ":crt1.o", "-l", ":crti.o"' endfiles='"-l", "c", "-l", ":crtn.o"' linkflags='"-L", "/usr/lib",' defines=' "-D", "_Pragma(x)=", "-D", "_Nullable=", "-D", "_Nonnull=", "-D", "__GNUCLIKE_BUILTIN_STDARG", "-D", "__GNUCLIKE_BUILTIN_VARARGS", /* required to define _RuneLocale, needed by xlocale/_ctype.h */ "-D", "_USE_CTYPE_INLINE_", /* workaround for #42 */ "-D", "_XLOCALE_INLINE=static inline", /* used like attribute after declarator, so _Alignas will not work here */ "-D", "__aligned(x)=", /* TLS is not yet supported (#8) */ "-D", "__NO_TLS", /* disable warnings for redefining _Pragma */ "-Wno-builtin-macro-redefined", ' ;; *-*openbsd*) : ${DEFAULT_DYNAMIC_LINKER:=/usr/libexec/ld.so} test "$host" = "$target" && : ${DEFAULT_PREPROCESSOR:=/usr/libexec/cpp} startfiles='"-l", ":crt0.o", "-l", ":crtbegin.o"' endfiles='"-l", "c", "-l", ":crtend.o"' linkflags='"-L", "/usr/lib", "-nopie",' defines=' /* required to prevent libc headers from declaring functions with conflicting linkage */ "-D", "_ANSI_LIBRARY", /* used like attribute after declarator, so _Alignas will not work here */ "-D", "__aligned(x)=", ' ;; *-*netbsd*) : ${DEFAULT_DYNAMIC_LINKER:=/usr/libexec/ld.elf_so} startfiles='"-l", ":crt0.o", "-l", ":crti.o"' endfiles='"-l", "c", "-l", ":crtn.o"' defines='"-D", "__builtin_stdarg_start(ap, last)=__builtin_va_start(ap, last)"' ;; *) fail "unknown target '$target', please create config.h manually" esac : ${DEFAULT_PREPROCESSOR:=${toolprefix}cpp} : ${DEFAULT_QBE:=qbe} : ${DEFAULT_ASSEMBLER:=${toolprefix}as} : ${DEFAULT_LINKER:=${toolprefix}ld} test "$DEFAULT_DYNAMIC_LINKER" && linkflags=$linkflags' "--dynamic-linker", "'$DEFAULT_DYNAMIC_LINKER'"' printf "creating config.h... " cat >config.h <config.mk <