config.sh (3462B)
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 LD=${LD:-ld} 8 SCDOC=${SCDOC:-scdoc} 9 10 for arg 11 do 12 case "$arg" in 13 --bindir=*) 14 BINDIR=${arg#*=} 15 ;; 16 --libdir=*) 17 LIBDIR=${arg#*=} 18 ;; 19 --mandir=*) 20 MANDIR=${arg#*=} 21 ;; 22 --prefix=*) 23 PREFIX=${arg#*=} 24 ;; 25 esac 26 done 27 28 subdir() { 29 eval ". $srcdir/$1/configure" 30 } 31 32 genrules() { 33 target="$1" 34 shift 35 printf '# Begin generated rules for %s\n' "$target" 36 for file in "$@" 37 do 38 ext="${file#*.}" 39 file="${file%.*}" 40 deps= 41 printf '%s.o: %s.%s%s\n' "$file" "$file" "$ext" "$deps" 42 done 43 printf '%s_objects=\\\n' "$target" 44 n=0 45 for file in "$@" 46 do 47 file="${file%.*}" 48 n=$((n+1)) 49 if [ $n -eq $# ] 50 then 51 printf '\t%s.o\n' "$file" 52 else 53 printf '\t%s.o \\\n' "$file" 54 fi 55 done 56 printf '# End generated rules for %s\n' "$target" 57 } 58 59 append_cflags() { 60 for flag 61 do 62 CFLAGS="$(printf '%s \\\n\t%s' "$CFLAGS" "$flag")" 63 done 64 } 65 66 test_cflags() { 67 [ ! -e "$outdir"/check.c ] && cat <<-EOF > "$outdir"/check.c 68 int main(void) { return 0; } 69 EOF 70 werror="" 71 case "$CFLAGS" in 72 *-Werror*) 73 werror="-Werror" 74 ;; 75 esac 76 if $CC $werror "$@" -o "$outdir"/check "$outdir"/check.c >/dev/null 2>&1 77 then 78 append_cflags "$@" 79 else 80 return 1 81 fi 82 } 83 84 find_library() { 85 name="$1" 86 pc="$2" 87 printf "Checking for %s... " "$name" 88 if ! command -v pkg-config >/dev/null 89 then 90 printf "ERROR: pkg-config not found\n" 91 return 1 92 fi 93 if ! pkg-config "$pc" 2>/dev/null 94 then 95 printf "NOT FOUND\n" 96 printf "Tried pkg-config %s\n" "$pc" 97 return 1 98 fi 99 printf "OK\n" 100 CFLAGS="$CFLAGS $(pkg-config --cflags "$pc")" 101 LIBS="$LIBS $(pkg-config --libs "$pc")" 102 } 103 104 docs() { true; } 105 106 run_configure() { 107 mkdir -p $outdir 108 109 for flag in -g -std=c11 -D_XOPEN_SOURCE=700 -Wall -Wextra -Werror -pedantic 110 do 111 printf "Checking for %s... " "$flag" 112 if test_cflags "$flag" 113 then 114 echo yes 115 else 116 echo no 117 fi 118 done 119 120 # XXX: Asked the maintainer to provide a .pc file 121 LIBS="$LIBS -lbearssl" 122 123 printf "Checking for scdoc... " 124 if scdoc -v >/dev/null 2>&1 125 then 126 echo yes 127 all="$all docs" 128 install_docs=" 129 mkdir -p \$(MANDIR)/man1 130 install -m644 doc/gmni.1 \$(MANDIR)/man1/gmni.1 131 install -m644 doc/gmnlm.1 \$(MANDIR)/man1/gmnlm.1" 132 else 133 echo no 134 fi 135 136 printf "Creating $outdir/config.mk... " 137 cat <<-EOF > "$outdir"/config.mk 138 CC=$CC 139 SCDOC=$SCDOC 140 LIBS=$LIBS 141 PREFIX=${PREFIX:-/usr/local} 142 OUTDIR=${outdir} 143 _INSTDIR=\$(DESTDIR)\$(PREFIX) 144 BINDIR?=${BINDIR:-\$(_INSTDIR)/bin} 145 LIBDIR?=${LIBDIR:-\$(_INSTDIR)/lib} 146 INCLUDEDIR?=${INCLUDEDIR:-\$(_INSTDIR)/include} 147 MANDIR?=${MANDIR:-\$(_INSTDIR)/share/man} 148 CACHE=\$(OUTDIR)/cache 149 CFLAGS=${CFLAGS} 150 CFLAGS+=-Iinclude -I\$(OUTDIR) 151 CFLAGS+=-DPREFIX='"\$(PREFIX)"' 152 CFLAGS+=-DLIBDIR='"\$(LIBDIR)"' 153 154 all: ${all} 155 install_docs: ${install_docs} 156 EOF 157 158 for target in $(printf '%s\n' $all | tr '.' '_') 159 do 160 $target 161 done >>"$outdir"/config.mk 162 echo done 163 164 touch $outdir/cppcache 165 166 if [ "$srcdir" = "." ] 167 then 168 return 169 fi 170 171 populate() ( 172 path="$1" 173 mkdir -p "${path#$srcdir/}" 174 fullpath() ( cd "$1" && pwd ) 175 for orig in "$path"/* 176 do 177 link="${orig#$srcdir/}" 178 if [ -d "$orig" ] 179 then 180 mkdir -p $link 181 populate "$orig" 182 elif [ -f "$orig" ] 183 then 184 ln -sf "$(fullpath "$path")"/"$(basename "$orig")" "$link" 185 fi 186 done 187 ) 188 189 printf "Populating build dir... " 190 populate "$srcdir/include" 191 populate "$srcdir/src" 192 populate "$srcdir/doc" 193 ln -sf "$srcdir"/Makefile ./ 194 echo done 195 }