config.sh (5029B)
1 outdir=${OUTDIR:-.build} 2 srcdir=${SRCDIR:-$(dirname "$0")} 3 AR=${AR:-ar} 4 AS=${AS:-as} 5 CC=${CC:-cc} 6 CFLAGS=${CFLAGS:-} 7 DEFAULT_TARGET= 8 LDFLAGS=${LDFLAGS:-} 9 LD=${LD:-ld} 10 QBE=${QBE:-qbe} 11 12 if [ `uname -s` = "OpenBSD" ] 13 then 14 LD="${LD:-ld} -nopie" 15 fi 16 17 for arg 18 do 19 case "$arg" in 20 --bindir=*) 21 BINDIR=${arg#*=} 22 ;; 23 --libdir=*) 24 LIBDIR=${arg#*=} 25 ;; 26 --mandir=*) 27 MANDIR=${arg#*=} 28 ;; 29 --prefix=*) 30 PREFIX=${arg#*=} 31 if [ "$PREFIX" = "/usr" ] 32 then 33 SYSCONFDIR=/etc 34 fi 35 ;; 36 --sharedir=*) 37 SHAREDIR=${arg#*=} 38 ;; 39 --sysconfdir=*) 40 SYSCONFDIR=${arg#*=} 41 ;; 42 --target=*) 43 DEFAULT_TARGET=${arg#*=} 44 ;; 45 --help) 46 cat >&2 <<'EOF' 47 Usage: configure [options...] 48 49 Options: 50 --help 51 --prefix=<path> 52 default: /usr/local 53 --bindir=<path> 54 default: (prefix)/bin 55 --libdir=<path> 56 default: (prefix)/lib 57 --mandir=<path> 58 default: (prefix)/share/man 59 --sharedir=<path> 60 default: (prefix)/share 61 --sysconfdir=<path> 62 default: /etc if prefix is /usr, otherwise (prefix)/etc 63 --target=<arch> 64 default: (current arch) 65 66 Environment variables: 67 AR (default: "ar") 68 AS (default: "as") 69 CC (default: "cc") 70 CFLAGS (default: "") 71 LD (default: "ld") 72 LDFLAGS (default: "") 73 OUTDIR (default: ".build") 74 QBE (default: "qbe") 75 SRCDIR (default: directory of configure script) 76 EOF 77 exit 78 ;; 79 *) 80 printf 'Error: unknown configure option %s\n' "$arg" >&2 81 exit 1 82 ;; 83 esac 84 done 85 86 subdir() { 87 eval ". $srcdir/$1/configure" 88 } 89 90 headers() { 91 printf 'HEADERS=\\\n' 92 while [ $# -ne 0 ] 93 do 94 printf '\tinclude/%s \\\n' "$1" 95 shift 96 done 97 } 98 99 genrules() { 100 target="$1" 101 shift 102 printf '# Begin generated rules for %s\n' "$target" 103 for file in "$@" 104 do 105 ext="${file#*.}" 106 file="${file%.*}" 107 deps= 108 if [ $ext = "ha" ] 109 then 110 deps=" harec" 111 fi 112 printf '%s.o: %s.%s%s $(HEADERS)\n' "$file" "$file" "$ext" "$deps" 113 done 114 printf '%s_objects=\\\n' "$target" 115 n=0 116 for file in "$@" 117 do 118 file="${file%.*}" 119 n=$((n+1)) 120 if [ $n -eq $# ] 121 then 122 printf '\t%s.o\n' "$file" 123 else 124 printf '\t%s.o \\\n' "$file" 125 fi 126 done 127 printf '# End generated rules for %s\n' "$target" 128 } 129 130 append_cflags() { 131 for flag 132 do 133 CFLAGS="$(printf '%s \\\n\t%s' "$CFLAGS" "$flag")" 134 done 135 } 136 137 test_cflags() { 138 [ ! -e "$outdir"/check.c ] && cat <<-EOF > "$outdir"/check.c 139 int main(void) { return 0; } 140 EOF 141 werror="" 142 case "$CFLAGS" in 143 *-Werror*) 144 werror="-Werror" 145 ;; 146 esac 147 if $CC $werror "$@" -o /dev/null "$outdir"/check.c >/dev/null 2>&1 148 then 149 append_cflags "$@" 150 else 151 return 1 152 fi 153 } 154 155 find_library() { 156 name="$1" 157 pc="$2" 158 printf "Checking for %s... " "$name" 159 if ! pkg-config "$pc" 2>/dev/null 160 then 161 printf "NOT FOUND\n" 162 printf "Tried pkg-config %s\n" "$pc" 163 return 1 164 fi 165 printf "OK\n" 166 CFLAGS="$CFLAGS $(pkg-config --cflags "$pc")" 167 LIBS="$LIBS $(pkg-config --libs "$pc")" 168 } 169 170 run_configure() { 171 mkdir -p "$outdir" 172 173 for flag in -g -std=c11 -D_XOPEN_SOURCE=700 \ 174 -Wall -Wextra -Werror -pedantic \ 175 -Wno-unused-parameter 176 do 177 printf "Checking for %s... " "$flag" 178 if test_cflags "$flag" 179 then 180 echo yes 181 else 182 echo no 183 fi 184 done 185 186 if [ -z "$DEFAULT_TARGET" ] 187 then 188 printf "Detecting machine architecture... " 189 arch="$(uname -m)" 190 case "$arch" in 191 x86_64|amd64) 192 DEFAULT_TARGET=x86_64 193 ;; 194 aarch64|arm64) 195 DEFAULT_TARGET=aarch64 196 ;; 197 riscv64) 198 DEFAULT_TARGET=riscv64 199 ;; 200 *) 201 printf "Error: unsupported or unrecognized architecture %s\n" "$arch" 202 ;; 203 esac 204 printf '%s\n' "$DEFAULT_TARGET" 205 fi 206 append_cflags -DDEFAULT_TARGET="\\\"$DEFAULT_TARGET\\\"" 207 208 printf "Checking for qbe... " 209 if $QBE -h > /dev/null 2>&1 210 then 211 echo yes 212 else 213 echo no 214 echo "Error: no qbe binary found" 215 exit 1 216 fi 217 218 printf "Creating %s/config.mk... " "$outdir" 219 cat <<EOF > "$outdir"/config.mk 220 CC=$CC 221 AS=$AS 222 LD=$LD 223 AR=$AR 224 QBE=$QBE 225 LIBS=$LIBS 226 PREFIX=${PREFIX:-/usr/local} 227 OUTDIR=${outdir} 228 SRCDIR=${srcdir} 229 BINDIR=${BINDIR:-\$(PREFIX)/bin} 230 SHAREDIR=${SHAREDIR:-\$(PREFIX)/share} 231 SYSCONFDIR=${SYSCONFDIR:-\$(PREFIX)/etc} 232 LIBDIR=${LIBDIR:-\$(PREFIX)/lib} 233 MANDIR=${MANDIR:-\$(PREFIX)/share/man} 234 VARLIBDIR=${MANDIR:-\$(PREFIX)/var/lib} 235 CACHE=\$(OUTDIR)/cache 236 CFLAGS=${CFLAGS} \\ 237 -Iinclude -I\$(OUTDIR) \\ 238 -DPREFIX='"\$(PREFIX)"' \\ 239 -DLIBDIR='"\$(LIBDIR)"' \\ 240 -DVARLIBDIR='"\$(VARLIBDIR)"' \\ 241 -DSYSCONFDIR='"\$(SYSCONFDIR)"' 242 LDFLAGS=${LDFLAGS} 243 HARECACHE=./mod 244 245 all: ${all} 246 EOF 247 248 for target in $all 249 do 250 $target >>"$outdir"/config.mk 251 done 252 echo done 253 254 cat <<-EOF >harec.sh 255 export PATH="$(pwd)":\$PATH 256 EOF 257 258 if [ "$srcdir" = "." ] 259 then 260 return 261 fi 262 263 populate() ( 264 path="$1" 265 mkdir -p "${path#$srcdir/}" 266 fullpath() ( cd "$1" && pwd ) 267 for orig in "$path"/* 268 do 269 link="${orig#$srcdir/}" 270 if [ -d "$orig" ] 271 then 272 mkdir -p $link 273 populate "$orig" 274 elif [ -f "$orig" ] 275 then 276 ln -sf "$(fullpath "$path")"/"$(basename "$orig")" "$link" 277 fi 278 done 279 ) 280 281 printf "Populating build dir... " 282 populate "$srcdir/doc" 283 populate "$srcdir/include" 284 populate "$srcdir/rt" 285 populate "$srcdir/src" 286 populate "$srcdir/tests" 287 populate "$srcdir/testmod" 288 ln -sf "$srcdir"/Makefile ./ 289 echo done 290 }