wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

genconfig (5654B)


      1 #!/bin/sh -f
      2 
      3 # What I wouldn't do for rc...
      4 # Well... it's better than m4shgmake.
      5 
      6 CONFIG="$ROOT/config.mk"
      7 CONFSTR='# Generated by "make config"'
      8 
      9 # ==================== The Messy Part ====================
     10 
     11 #XXX Ignores lines ending in \
     12 parseconfig() {
     13 	cat <<'!' | sed "s/CONFSTR/$CONFSTR/"
     14 	/^CONFSTR/ {exit}
     15 	/^( *#|	)/ {next}
     16 	function fixup() {
     17 		sub(/ #.*/, "")
     18 		sub(/^ */, "")
     19 		gsub(/'/, "'\"'\"'")
     20 		sub(/[	 ]*\+?=[	 ]*/, "='")
     21 		sub(/[	]*$/, "'")
     22 		var = $0
     23 		val = $0
     24 		sub(/\+?=.*/, "", var)
     25 		sub(/^[^=]+=/, "", val)
     26 	}
     27 	/\+=/ && !/^	/ {
     28 		fixup()
     29 		if(!set[var]) {
     30 			print var"=''"
     31 			append[var]++
     32 		}
     33 		print var"=\"$" var " \"" val
     34 		print var"_orig=\"$" var "\""
     35 		next
     36 	}
     37 	/=/ && !/^	/ {
     38 		fixup()
     39 		delete append[var]
     40 		set[var]++
     41 
     42 		print var"="val
     43 		print var"_orig=\"$" var "\""
     44 	}
     45 	END{
     46 		for(v in set)
     47 			print v "_append=''"
     48 		for(v in append)
     49 			print v "_append=true"
     50 	}
     51 !
     52 }
     53 
     54 findinc() {
     55 	var="$1"; file="$2"; shift 2
     56 	for d in "$@"; do
     57 		if [ -d "$d" -a -f "$d/$file" ]; then
     58 			eval "$var=\"-I$d\""
     59 			break; fi; done
     60 }
     61 soext=.so
     62 aext=.a
     63 findlib() {
     64 	var="$1"; lib="$2"; shift 2
     65 	for d in "$@"; do
     66 		if [ -d "$d" -a -f "$d/$lib.so" -o -f "$d/lib$lib.a" ]; then
     67 			_libdir="$d"; _libname="$lib"
     68 			eval "$var=\"-L$d -l$lib\""
     69 			break; fi; done
     70 }
     71 
     72 expand() {
     73 	_expand="$@"; _expand_old=''
     74 	while [ "$_expand" != "$_expand_old" ]; do
     75 		_expand_old="$_expand"
     76 		eval "_expand=\"$_expand\""; done
     77 	echo -n "$_expand"
     78 }
     79 
     80 cfg() {
     81 	CFG="$CFG
     82 $@"
     83 }
     84 
     85 equals() {
     86 	if [ -z "$append" ]; then
     87 		echo -n "=";
     88 	else
     89 		echo -n "+="; fi
     90 }
     91 
     92 prompt() {
     93 	var=$1; shift
     94 	eval "def=\$$var; orig=\$${var}_orig; append=\$${var}_append"
     95 
     96 	unset val
     97 	if [ -z "$def" -o -n "$force" ]; then
     98 		echo "$@"
     99 		echo -n "$var[$def]$(equals) "
    100 		read val
    101 		echo
    102 	fi
    103 
    104 	if [ -z "$val" ]; then
    105 		val="$def"; fi
    106 	if [ "$(expand $val)" != "$(expand "$orig")" ]; then
    107 		cfg "$var$(equals)$val"; fi
    108 }
    109 
    110 # The messy sed... Turns $(VAR) into ${VAR}, but not $$(VAR) into $${VAR}
    111 eval "$(sed 's/\([^$]\)\$(\([A-Za-z_]*\))/\1${\2}/g' <"$CONFIG" | awk "`parseconfig`")"
    112 CFG="$(sed -n "/^$CONFSTR/q; p" "$CONFIG"; echo "$CONFSTR")"
    113 
    114 # ==================== The Fun Part ====================
    115 
    116 cat <<!
    117 Configuring for the wmii build.
    118 
    119 You'll be prompted for a number of settings which wmii needs to build,
    120 install, and run properly. In most cases, the defaults will be sufficient,
    121 in which case, you may simply press return to accept them.
    122 
    123 !
    124 
    125 # Guess...
    126 AWKPATH=$(which awk 2>/dev/null)
    127 
    128 prompt AWKPATH "Full path to your system's 'awk' program"
    129 prompt PLAN9 "Path of a Plan 9 Port or 9base installation"
    130 
    131 force=1
    132 prompt PREFIX Installation prefix
    133 
    134 echo
    135 echo "Compilation details (if you don't understand these, just leave the defaults)"
    136 echo
    137 prompt CC C object compiler
    138 prompt LD 'Linker (this should normally not be "ld")'
    139 
    140 set -- $CC
    141 if $1 -v 2>&1 | grep 'gcc version' >/dev/null; then
    142 	echo -n You appear to be using gcc. Is this correct? '[yes] '
    143 	while :; do
    144 		read resp
    145 		case "$resp" in
    146 		[Yy][Ee][Ss]|'')
    147 			cfg 'include $(ROOT)/mk/gcc.mk'
    148 			break;;
    149 		[Nn][Oo])
    150 			cfg 'CFLAGS=""'
    151 			# Not perfect. Botches system cflags, but we
    152 			# need to ditch the gcc ones.
    153 			break;;
    154 		*)
    155 			echo -n 'Please answer yes or no: '
    156 		esac
    157 	done
    158 	echo
    159 fi
    160 
    161 prompt INCPATH Search path for include files
    162 prompt LIBS Libraries to be linked with every executable
    163 
    164 prompt CFLAGS Flags for the C compiler
    165 prompt LDFLAGS Flags for the linker
    166 case $(uname -s) in
    167 	[Dd]arwin) cfg 'STATIC=""';;
    168 	*) prompt STATIC Extra linker flags to produce a static executable;;
    169 esac
    170 unset force
    171 
    172 # Make some guesses
    173 
    174 # Extract the -L paths from ldflags.
    175 ldlibs="$(awk 'BEGIN{
    176 		for(i=1; i <= ARGC; i++)
    177 			if(ARGV[i] ~ /-L/)
    178 				print ":" substr(ARGV[i], 3)
    179 	}' $LDFLAGS)"
    180 # Turn include paths into ../lib paths.
    181 inclibs="$(expand "$INCPATH"|sed 's,/include:,/lib:,g; s,/include$,/lib,')"
    182 # Lace them all together, and expand them.
    183 libs="$(expand "$LIBDIR:$ldlibs:$inclibs:/usr/local/lib:/opt/local/lib")"
    184 
    185 LIBIXP=${LIBIXP%/libixp.a}
    186 INCPATH="$INCPATH:/usr/local/include:/opt/local/include"
    187 incpath="$(expand "$INCPATH")"
    188 
    189 oifs="$IFS"; IFS=:
    190 findinc INCX11 X11/Xlib.h $incpath \
    191 	/usr/X11R6/include /usr/x11/include /usr/x11/include /usr/X11/include \
    192 	/usr/openwin/include /opt/x11/include /opt/X11/include
    193 findinc INCICONV iconv.h $incpath
    194 
    195 findlib LIBX11 X11 $libs \
    196 	/usr/X11R6/lib /usr/X11/lib /usr/openwin/lib /usr/x11/lib \
    197 	/opt/X11 /opt/x11 /usr/local/lib /opt/local/lib
    198 findlib LIBICONV iconv $libs
    199 
    200 if [ -d "$ROOT/libixp" ]; then
    201 	_libdir="$ROOT/lib"; else
    202 	soext=.a findlib LIBIXP ixp "$ROOT/lib" $libs; fi
    203 LIBIXP="$_libdir/libixp.a"
    204 IFS="$oifs"
    205 
    206 # Back to prompting
    207 echo Library paths...
    208 prompt INCX11 Compiler flags to find X11 includes
    209 prompt LIBX11 Linker flags to link against libX11
    210 
    211 # Find out if the system libc includes iconv...
    212 #   GNU systems tend to make /usr/lib/libc.so or /lib/libc.so
    213 # linker scripts rather than libraries, so I can't parse them.
    214 # As a kludge, I ask ldd what actual libc.so /bin/sh links to,
    215 # which is pretty reliable.  It's very unlikely that anyone will
    216 # actually be prompted for this.
    217 #   I suppose the auto*hell way would be to just compile
    218 # something against libc. Perhaps another day.
    219 if nm -D $(ldd /bin/sh | awk '$1 ~ /^libc\.so/ {print $3}') |
    220 	awk -ve=1 '$2 == "T" && $3 == "iconv" {e=0; exit}; END {exit e}'
    221 then
    222 	echo
    223 	echo "Your system's libc appears to include iconv."
    224 	echo
    225 else
    226 	prompt LIBICONV Linker flags to link agains libiconv \
    227 		'(may be left blank if iconv is part of your system libc)'
    228 fi
    229 prompt INCICONV Compiler flags to find iconv.h
    230 prompt LIBIXP Path to libixp.a
    231 
    232 echo Writing config.mk
    233 echo "$CFG
    234 " >config.mk
    235 echo Done.
    236