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