aboutsummaryrefslogtreecommitdiffhomepage
path: root/configure
blob: f48555ba1dbbf10122c5dcd9937db2dc6182adbf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/sh

die() {
    echo "$0: Error: $@" >&2
    exit 1
}

prihelp() {
    host="[$(cc -dumpmachine)]"
    : ${host:=""}
    cat << EOF
Usage: $0 [options]
Options:
  --help                    Print this help mesage
  --host=target-triple      $host
  --cc=cc                   C compiler [cc]
  --prefix=PREFIX           Installation path [/usr/local]
  --sysroot=/path           System root [/usr]
  --includedir="/a/b",...   System include dirs
EOF
}

sysroot=/usr
prefix=/usr/local
host=

for arg ; do
	case "$arg" in
	--host=*) host=${arg#*=} ;;
    --sysroot=*) sysroot=${arg#*=} ;;
    --includedir=*) host_include_dirs=${arg#*=} ;;
	CC=*) CC=${arg#*=} ;;
	--cc=*) CC=${arg#*=} ;;
	CFLAGS=*) CFLAGS=${arg#*=} ;;
	--cflags=*) CFLAGS=${arg#*=} ;;
    --prefix=*) prefix=$(sh -c "echo ${arg#*=}") ;;
    --help) prihelp ; exit ;;
	*) die "$0: unknown option '$arg'"
	esac
done

: ${CC:=cc}
: ${host:=$($CC -dumpmachine)}
test -n "$host" || die "cannot determine host"

host_arch=
host_os=
host_abi=

echo "host: $host"
case "$host" in
x86_64-*) host_arch=x86_64 ;;
amd64-*) host_arch=x86_64 ;;
aarch64-*) host_arch=aarch64 ;;
arm64-*) host_arch=aarch64 ;;
*) host_arch=unk ;;
esac

case "$host" in
*-linux-*) host_os=linux ;;
*) host_os=unknown ;;
esac

case "$host" in
*-gnu) host_abi=gnu ;;
*-musl) host_abi=musl ;;
*) host_abi=none ;;
esac

tryincldir() {
    if stat "$1"/ >/dev/null 2>/dev/null; then
        host_include_dirs="$host_include_dirs\"$1\","
    fi
}

if ! test -n "$host_include_dirs"; then
    # try to query host toolchain first
    if hostccdirs=$($CC -E -v -xc /dev/null 2>&1 | sed -n '/#include <...> search starts here:/,/End of search list./{
    s/^[[:space:]]*//
    /^\/.*/p
}') && test -n "$hostccdirs"; then
        echo "Using $CC's system include paths:"
        for d in $hostccdirs; do
            dir=$(realpath "$d")
            case "$dir" in
                #exclude internal compiler headers, we have our own
                */gcc/*) ;; */clang/*) ;;
                *) echo "    $dir"
                   tryincldir "$dir" ;;
            esac
        done
    else
        echo "Falling back to generic system include paths"
        tryincldir "$sysroot/local/include/$host"
        tryincldir "$sysroot/local/include"
        tryincldir "$sysroot/include/$host"
        tryincldir "$sysroot/include"
    fi
fi

set -e

echo using arch: "$host_arch"
echo using os: "$host_os"
echo using abi: "$host_abi"
echo host include paths: "$host_include_dirs"

echo "/** GENERATED WITH $0 $@ **/
#ifndef HOSTCONFIG_H_
#define HOSTCONFIG_H_

#define HOST_TRIPLE \"$host\"
#define HOST_ARCH   IS$host_arch
#define HOST_OS     OS$host_os
#define HOST_ABI    ABI$host_abi
#define HOST_INCLUDE_DIRS $host_include_dirs
#define HOST_CC \"$CC\"

#endif // HOSTCONFIG_H_"  > hostconfig.h

echo "# GENERATED WITH $0 $@
PREFIX = $prefix
CC     = $CC
CFLAGS = ${CFLAGS:--Wall -std=c11 -pedantic}" > config.mk

echo config written to hostconfig.h, config.mk