aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Forney <mforney@mforney.org>2019-04-26 00:36:06 -0700
committerMichael Forney <mforney@mforney.org>2019-04-26 18:24:19 -0700
commitbdefdc936a6c2be8f6c814c82ef9b36a432051e4 (patch)
tree65ab15fbab5c5d794ace7d91813362c56577aac7
parentabc80f19c55ae45742cf7d2bc54c2c4a4dc8236c (diff)
Replace config.def.h with a configure script
-rw-r--r--.builds/alpine.yml15
-rw-r--r--.builds/debian.yml14
-rw-r--r--.builds/freebsd.yml39
-rw-r--r--README.md18
-rw-r--r--config.def.h49
-rwxr-xr-xconfigure154
6 files changed, 196 insertions, 93 deletions
diff --git a/.builds/alpine.yml b/.builds/alpine.yml
index 71ed175..df36db3 100644
--- a/.builds/alpine.yml
+++ b/.builds/alpine.yml
@@ -1,9 +1,12 @@
image: alpine/edge
sources:
- - https://git.sr.ht/~mcf/cc
- - git://c9x.me/qbe.git
+- https://git.sr.ht/~mcf/cc
+- git://c9x.me/qbe.git
+environment:
+ PATH: $HOME/qbe/obj:$PATH
tasks:
- - build: |
- make -C qbe
- echo 'CFLAGS=-DDYNAMICLINKER=\"/lib/ld-musl-x86_64.so.1\"' >cc/config.mk
- PATH=$PWD/qbe/obj:$PATH make -C cc all check bootstrap
+- build: |
+ make -C qbe
+ cd cc
+ ./configure
+ make all check bootstrap
diff --git a/.builds/debian.yml b/.builds/debian.yml
index bcba5fc..ac0eb97 100644
--- a/.builds/debian.yml
+++ b/.builds/debian.yml
@@ -1,8 +1,12 @@
image: debian/stretch
sources:
- - https://git.sr.ht/~mcf/cc
- - git://c9x.me/qbe.git
+- https://git.sr.ht/~mcf/cc
+- git://c9x.me/qbe.git
+environment:
+ PATH: $HOME/qbe/obj:$PATH
tasks:
- - build: |
- make -C qbe
- PATH=$PWD/qbe/obj:$PATH make -C cc all check bootstrap
+- build: |
+ make -C qbe
+ cd cc
+ ./configure
+ make all check bootstrap
diff --git a/.builds/freebsd.yml b/.builds/freebsd.yml
index 8eb359d..280081a 100644
--- a/.builds/freebsd.yml
+++ b/.builds/freebsd.yml
@@ -1,33 +1,14 @@
image: freebsd/latest
sources:
- - https://git.sr.ht/~mcf/cc
- - git://c9x.me/qbe.git
+- https://git.sr.ht/~mcf/cc
+- git://c9x.me/qbe.git
packages:
- - gmake
+- gmake
+environment:
+ PATH: $HOME/qbe/obj:$PATH
tasks:
- - build: |
- gmake -C qbe
- cd cc
- cat >config.h <<EOF
- static char *startfiles[] = {"-l", ":crt1.o", "-l", ":crti.o"};
- static char *endfiles[] = {"-l", ":crtn.o", "-l", "c"};
- static char *preprocesscmd[] = {
- "cpp", "-P",
- "-U", "__GNUC__", "-D", "__GNUC__=3",
- "-U", "__GNUC_MINOR__", "-D", "__GNUC_MINOR__=3",
- "-D", "__asm(x)=",
- "-D", "__attribute__(x)=",
- "-D", "__inline=",
- "-D", "_Pragma(x)=",
- "-D", "_Nullable=",
- "-D", "_Nonnull=",
- "-D", "_XLOCALE_INLINE=static inline",
- "-D", "__NO_TLS",
- };
- static char *compilecmd[] = {"cc-qbe"};
- static char *codegencmd[] = {"qbe"};
- static char *assemblecmd[] = {"as"};
- static char *linkcmd[] = {"ld", "-L", "/usr/lib", "--dynamic-linker", "/libexec/ld-elf.so.1"};
- EOF
- echo 'CFLAGS=-Wno-parentheses -Wno-switch' >config.mk
- PATH=$HOME/qbe/obj:$PATH make all check bootstrap
+- build: |
+ gmake -C qbe
+ cd cc
+ ./configure
+ make all check bootstrap
diff --git a/README.md b/README.md
index 693d13f..0e87989 100644
--- a/README.md
+++ b/README.md
@@ -27,10 +27,20 @@ one is currently required as well.
## Building
-You will need to create a `config.h` appropriate for the target system. If
-missing, a default version will be created from `config.def.h`,
-which should work for most glibc systems, or musl systems with
-`-D 'DYNAMICLINKER="/lib/ld-musl-x86_64.so.1"'`.
+Run `./configure` to create a `config.h` and `config.mk` appropriate for
+your system. If your system is not supported by the configure script,
+you can create these files manually. `config.h` should define several
+string arrays (`static char *[]`):
+
+- **`startfiles`**: Objects to pass to the linker at the beginning of
+ the link command.
+- **`endfiles`**: Objects to pass to the linker at the end of the link
+ command (including libc).
+- **`preprocesscmd`**: The preprocessor command, and any necessary flags
+ for the target system.
+- **`codegencmd`**: The QBE command, and possibly explicit target flags.
+- **`assemblecmd`**: The assembler command.
+- **`linkcmd`**: The linker command.
You may also want to customize your environment or `config.mk` with the
appropriate `CC`, `CFLAGS` and `LDFLAGS`.
diff --git a/config.def.h b/config.def.h
deleted file mode 100644
index 2c87cdd..0000000
--- a/config.def.h
+++ /dev/null
@@ -1,49 +0,0 @@
-#ifndef DYNAMICLINKER
-#define DYNAMICLINKER "/lib64/ld-linux-x86-64.so.2"
-#endif
-
-/*
-glibc systems might need crtbegin.o at the end of `startfiles` and
-crtend.o at the beginning of `endfiles`. These are provided by gcc and
-not usually in the linker's default search path, so we just leave it to
-the user to configure as needed.
-*/
-
-static char *startfiles[] = {
- "-l", ":crt1.o",
- "-l", ":crti.o",
-};
-static char *endfiles[] = {
- "-l", ":crtn.o",
- "-l", "c",
-};
-
-static char *preprocesscmd[] = {
- "cpp", "-P",
-
- /* we don't yet support these optional features */
- "-D", "__STDC_NO_ATOMICS__",
- "-D", "__STDC_NO_COMPLEX__",
- "-D", "__STDC_NO_VLA__",
-
- /* specify the GNU C extensions we support */
- "-U", "__GNUC__", "-D", "__GNUC__=1",
- "-U", "__GNUC_MINOR__", "-D", "__GNUC_MINOR__=0",
-
- /* prevent glibc from using statement expressions for assert */
- "-D", "__STRICT_ANSI__",
-
- /* ignore attributes and extension markers */
- "-D", "__attribute__(x)=",
- "-D", "__extension__=",
-
- /* alternate keywords */
- "-D", "__alignof__=_Alignof",
- "-D", "__inline=inline",
- "-D", "__inline__=inline",
- "-D", "__signed__=signed",
- "-D", "__thread=_Thread_local",
-};
-static char *codegencmd[] = {"qbe"};
-static char *assemblecmd[] = {"as"};
-static char *linkcmd[] = {"ld", "--dynamic-linker=" DYNAMICLINKER};
diff --git a/configure b/configure
new file mode 100755
index 0000000..5a1165a
--- /dev/null
+++ b/configure
@@ -0,0 +1,154 @@
+#!/bin/sh
+
+fail() {
+ echo "$0: $*" >&2
+ exit 1
+}
+
+host=
+target=
+gcclibdir=
+
+for arg ; do
+ case "$arg" in
+ --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-gcc-libdir=*) gcclibdir=${arg#*=} ;;
+ CC=*) CC=${arg#*=} ;;
+ CFLAGS=*) CFLAGS=${arg#*=} ;;
+ LDFLAGS=*) LDFLAGS=${arg#*=} ;;
+ *) fail "unknown option '$arg'"
+ esac
+done
+
+: ${CC:=cc}
+: ${DEFAULT_PREPROCESSOR:=cpp}
+: ${DEFAULT_QBE:=qbe}
+: ${DEFAULT_ASSEMBLER:=as}
+: ${DEFAULT_LINKER:=ld}
+
+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"
+
+case "$target" in
+x86_64*|amd64*) qbetarget=amd64_sysv ;;
+aarch64*) qbetarget=arm64 ;;
+*) fail "unsupported architecture '${target%%-*}'"
+esac
+
+gnuc='
+ /* specify the GNU C extensions we support */
+ "-D", "__GNUC__=1",
+ "-D", "__GNUC_MINOR__=0",
+'
+
+case "$target" in
+*-linux-*musl*)
+ case "$target" in
+ x86_64*) dynamiclinker=/lib/ld-musl-x86_64.so.1 ;;
+ aarch64*) dynamiclinker=/lib/ld-musl-aarch64.so.1 ;;
+ *) fail "unsuported target '$target'"
+ esac
+ startfiles='"-l", ":crt1.o", "-l", ":crti.o"'
+ endfiles='"-l", ":crtn.o", "-l", "c"'
+ ;;
+*-linux-*gnu*)
+ case "$target" in
+ x86_64*) dynamiclinker=/lib64/ld-linux-x86-64.so.2 ;;
+ aarch64*) dynamiclinker=/lib/ld-linux-aarch64.so.1 ;;
+ *) fail "unsuported target '$target'"
+ esac
+ startfiles='"-l", ":crt1.o", "-l", ":crti.o", "-l", ":crtbegin.o"'
+ endfiles='"-l", ":crtend.o", "-l", ":crtn.o", "-l", "c"'
+ 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'"',
+ defines='
+ /* prevent glibc from using statement expressions for assert */
+ "-D", "__STRICT_ANSI__",
+'
+ ;;
+*-*freebsd*)
+ dynamiclinker=/libexec/ld-elf.so.1
+ startfiles='"-l", ":crt1.o", "-l", ":crti.o"'
+ endfiles='"-l", ":crtn.o", "-l", "c"'
+ # any value of __GNUC__ will cause FreeBSD's headers to use asm for bswap
+ gnuc=
+ 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",
+'
+ ;;
+*)
+ fail "unknown target '$target', please create config.h manually"
+esac
+
+printf "creating config.h... "
+cat >config.h <<EOF
+static char *startfiles[] = {$startfiles};
+static char *endfiles[] = {$endfiles};
+static char *preprocesscmd[] = {
+ "$DEFAULT_PREPROCESSOR", "-P", "-std=c11",
+
+ /* clear preprocessor GNU C version */
+ "-U", "__GNUC__",
+ "-U", "__GNUC_MINOR__",
+
+ /* we don't yet support these optional features */
+ "-D", "__STDC_NO_ATOMICS__",
+ "-D", "__STDC_NO_COMPLEX__",
+ "-D", "__STDC_NO_VLA__",
+
+ /* ignore attributes and extension markers */
+ "-D", "__attribute__(x)=",
+ "-D", "__extension__=",
+
+ /* alternate keywords */
+ "-D", "__alignof__=_Alignof",
+ "-D", "__inline=inline",
+ "-D", "__inline__=inline",
+ "-D", "__signed__=signed",
+ "-D", "__thread=_Thread_local",
+$gnuc$defines};
+static char *codegencmd[] = {"$DEFAULT_QBE", "-t", "$qbetarget"};
+static char *assemblecmd[] = {"$DEFAULT_ASSEMBLER"};
+static char *linkcmd[] = {"$DEFAULT_LINKER", "--dynamic-linker", "$dynamiclinker", $linkflags};
+EOF
+echo done
+
+printf "creating config.mk... "
+cat >config.mk <<EOF
+CC=${CC:-cc}
+CFLAGS=${CFLAGS:--std=c11 -Wall -Wno-parentheses -Wno-switch -g -pipe}
+LDFLAGS=$LDFLAGS
+EOF
+echo done