xref: /freebsd/contrib/bmake/mk/mkopt.sh (revision 3a3af6b2a160bea72509a9d5ef84e25906b0478a)
1#!/bin/sh
2
3# $Id: mkopt.sh,v 1.15 2022/06/06 21:34:21 sjg Exp $
4#
5#	@(#) Copyright (c) 2014-2022, 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
87# _mk_cmdline_opts opt ...
88# look at the command line (saved in _cmdline)
89# to see any options we care about are being set with -DWITH*
90# or MK_*= if 'opt' is '*' then all options are of interest.
91_cmdline="$0 $@"
92_mk_cmdline_opts() {
93    for _x in $_cmdline
94    do
95	case "$_x" in
96	-DWITH*|${_MKOPT_PREFIX:-MK_}*)
97	    for _o in "$@"
98	    do
99		case "$_x" in
100		-DWITH_$_o|-DWITHOUT_$_o) eval ${_x#-D}=1;;
101		-DWITH_$_o=*|-DWITHOUT_$_o=*) eval ${_x#-D};;
102		${_MKOPT_PREFIX:-MK_}$_o=*) eval "$_x";;
103		esac
104	    done
105	    ;;
106	esac
107    done
108}
109
110
111case "/$0" in
112*/mkopt*)
113    _list=no
114    _mk_cmdline_opts '*'
115    _mk_opts no DEBUG
116    [ $MK_DEBUG = no ] || set -x
117    while :
118    do
119	case "$1" in
120	*=*) eval "$1"; shift;;
121	--no|no) _list="$_list no"; shift;;
122	--yes|yes) _list="$_list yes"; shift;;
123	-DWITH*) eval "${1#-D}=1"; shift;;
124	[A-Z]*) _list="$_list $1"; shift;;
125	*) break;;
126	esac
127    done
128    _mk_opts $_list
129    ;;
130esac
131
132