xref: /dragonfly/contrib/bmake/mk/mkopt.sh (revision cae2835b)
1:
2# $Id: mkopt.sh,v 1.10 2015/06/07 17:29:08 sjg Exp $
3#
4#	@(#) Copyright (c) 2014, Simon J. Gerraty
5#
6#	This file is provided in the hope that it will
7#	be of use.  There is absolutely NO WARRANTY.
8#	Permission to copy, redistribute or otherwise
9#	use this file is hereby granted provided that
10#	the above copyright notice and this notice are
11#	left intact.
12#
13#	Please send copies of changes and bug-fixes to:
14#	sjg@crufty.net
15#
16
17# handle WITH[OUT]_* options in a manner compatible with
18# options.mk and bsd.mkopt.mk in recent FreeBSD
19
20# no need to be included more than once
21_MKOPT_SH=:
22_MKOPT_PREFIX=${_MKOPT_PREFIX:-MK_}
23
24#
25# _mk_opt default OPT
26#
27# Set MK_$OPT
28#
29# The semantics are simple, if MK_$OPT has no value
30# WITHOUT_$OPT results in MK_$OPT=no
31# otherwise WITH_$OPT results in MK_$OPT=yes.
32# Note WITHOUT_$OPT overrides WITH_$OPT.
33#
34# For backwards compatability reasons we treat WITH_$OPT=no
35# the same as WITHOUT_$OPT.
36#
37_mk_opt() {
38    _d=$1
39    _mo=${_MKOPT_PREFIX}$2 _wo=WITHOUT_$2 _wi=WITH_$2
40    eval "_mov=\$$_mo _wov=\$$_wo _wiv=\$$_wi"
41
42    case "$_wiv" in
43    no) _wov=no;;
44    esac
45    _v=${_mov:-${_wov:+no}}
46    _v=${_v:-${_wiv:+yes}}
47    _v=${_v:-$_d}
48    _opt_list="$_opt_list $_mo"
49    case "$_v" in
50    yes|no) ;;			# sane
51    0|[NnFf]*) _v=no;;		# they mean no
52    1|[YyTt]*) _v=yes;;		# they mean yes
53    *) _v=$_d;;			# ignore bogus value
54    esac
55    eval "$_mo=$_v"
56}
57
58#
59# _mk_opts default opt ... [default [opt] ...]
60#
61# see _mk_opts_defaults for example
62#
63_mk_opts() {
64    _d=no
65    for _o in "$@"
66    do
67	case "$_o" in
68	*/*) # option is dirname default comes from basename
69	    eval "_d=\$${_MKOPT_PREFIX}${_o#*/}"
70	    _o=${_o%/*}
71	    ;;
72	yes|no) _d=$_o; continue;;
73	esac
74	_mk_opt $_d $_o
75    done
76}
77
78# handle either options.mk style OPTIONS_DEFAULT_*
79# or FreeBSD's new bsd.mkopt.mk style __DEFAULT_*_OPTIONS
80_mk_opts_defaults() {
81    _mk_opts no $OPTIONS_DEFAULT_NO $__DEFAULT_NO_OPTIONS \
82	yes $OPTIONS_DEFAULT_YES $__DEFAULT_YES_OPTIONS \
83	$OPTIONS_DEFAULT_DEPENDENT $__DEFAULT_DEPENDENT_OPTIONS
84}
85
86case "/$0" in
87*/mkopt*)
88    _list=no
89    while :
90    do
91	case "$1" in
92	*=*) eval "$1"; shift;;
93	--no|no) _list="$_list no"; shift;;
94	--yes|yes) _list="$_list yes"; shift;;
95	-DWITH*) eval "${1#-D}=1"; shift;;
96	[A-Z]*) _list="$_list $1"; shift;;
97	*) break;;
98	esac
99    done
100    _mk_opts $_list
101    ;;
102esac
103
104