summaryrefslogtreecommitdiff
path: root/devel/bmake/files/mk/mkopt.sh
blob: 7df45662aa1e2508b93c32332c8016ceeb97809d (plain)
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
:
# $Id: mkopt.sh,v 1.1.1.1 2020/05/24 05:35:53 nia Exp $
#
#	@(#) Copyright (c) 2014, Simon J. Gerraty
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

# handle WITH[OUT]_* options in a manner compatible with
# options.mk and bsd.mkopt.mk in recent FreeBSD

# no need to be included more than once
_MKOPT_SH=:
_MKOPT_PREFIX=${_MKOPT_PREFIX:-MK_}

#
# _mk_opt default OPT
#
# Set MK_$OPT
#
# The semantics are simple, if MK_$OPT has no value
# WITHOUT_$OPT results in MK_$OPT=no
# otherwise WITH_$OPT results in MK_$OPT=yes.
# Note WITHOUT_$OPT overrides WITH_$OPT.
#
# For backwards compatability reasons we treat WITH_$OPT=no
# the same as WITHOUT_$OPT.
#
_mk_opt() {
    _d=$1
    _mo=${_MKOPT_PREFIX}$2 _wo=WITHOUT_$2 _wi=WITH_$2
    eval "_mov=\$$_mo _wov=\$$_wo _wiv=\$$_wi"

    case "$_wiv" in
    [Nn][Oo]) _wov=no;;
    esac
    _v=${_mov:-${_wov:+no}}
    _v=${_v:-${_wiv:+yes}}
    _v=${_v:-$_d}
    _opt_list="$_opt_list $_mo"
    case "$_v" in
    yes|no) ;;			# sane
    0|[NnFf]*) _v=no;;		# they mean no
    1|[YyTt]*) _v=yes;;		# they mean yes
    *) _v=$_d;;			# ignore bogus value
    esac
    eval "$_mo=$_v"
}

#
# _mk_opts default opt ... [default [opt] ...]
#
# see _mk_opts_defaults for example
#
_mk_opts() {
    _d=no
    for _o in "$@"
    do
	case "$_o" in
	*/*) # option is dirname default comes from basename
	    eval "_d=\$${_MKOPT_PREFIX}${_o#*/}"
	    _o=${_o%/*}
	    ;;
	yes|no) _d=$_o; continue;;
	esac
	_mk_opt $_d $_o
    done
}

# handle either options.mk style OPTIONS_DEFAULT_*
# or FreeBSD's new bsd.mkopt.mk style __DEFAULT_*_OPTIONS
_mk_opts_defaults() {
    _mk_opts no $OPTIONS_DEFAULT_NO $__DEFAULT_NO_OPTIONS \
	yes $OPTIONS_DEFAULT_YES $__DEFAULT_YES_OPTIONS \
	$OPTIONS_DEFAULT_DEPENDENT $__DEFAULT_DEPENDENT_OPTIONS
}

case "/$0" in
*/mkopt*)
    _list=no
    while :
    do
	case "$1" in
	*=*) eval "$1"; shift;;
	--no|no) _list="$_list no"; shift;;
	--yes|yes) _list="$_list yes"; shift;;
	-DWITH*) eval "${1#-D}=1"; shift;;
	[A-Z]*) _list="$_list $1"; shift;;
	*) break;;
	esac
    done
    _mk_opts $_list
    ;;
esac