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