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