1# SPDX-License-Identifier: BSD-2-Clause 2# 3# $Id: options.mk,v 1.22 2024/10/27 17:33:03 sjg Exp $ 4# 5# @(#) Copyright (c) 2012, 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# Inspired by FreeBSD bsd.own.mk, but intentionally simpler and more flexible. 19 20# Options are normally listed in either OPTIONS_DEFAULT_{YES,NO} 21# We convert these to ${OPTION}/{yes,no} in OPTIONS_DEFAULT_VALUES. 22# We add the OPTIONS_DEFAULT_NO first so they take precedence. 23# This allows override of an OPTIONS_DEFAULT_YES by adding it to 24# OPTIONS_DEFAULT_NO or adding ${OPTION}/no to OPTIONS_DEFAULT_VALUES. 25# An OPTIONS_DEFAULT_NO option can only be overridden by putting 26# ${OPTION}/yes in OPTIONS_DEFAULT_VALUES. 27# A makefile may set NO_* (or NO*) to indicate it cannot do something. 28# User sets WITH_* and WITHOUT_* to indicate what they want. 29# We set ${OPTION_PREFIX:UMK_}* which is then all we need care about. 30OPTIONS_DEFAULT_VALUES += \ 31 ${OPTIONS_DEFAULT_NO:U:O:u:S,$,/no,} \ 32 ${OPTIONS_DEFAULT_YES:U:O:u:S,$,/yes,} 33 34OPTION_PREFIX ?= MK_ 35 36# NO_* takes precedence 37# If both WITH_* and WITHOUT_* are defined, WITHOUT_ wins unless 38# DOMINANT_* is set to "yes" 39# Otherwise WITH_* and WITHOUT_* override the default. 40.for o in ${OPTIONS_DEFAULT_VALUES:M*/*} 41.if defined(WITH_${o:H}) && ${WITH_${o:H}} == "no" 42# a common miss-use - point out correct usage 43.warning use WITHOUT_${o:H}=1 not WITH_${o:H}=no 44.endif 45.if defined(NO_${o:H}) || defined(NO${o:H}) 46# we cannot do it 47${OPTION_PREFIX}${o:H} ?= no 48.elif defined(WITH_${o:H}) && defined(WITHOUT_${o:H}) 49# normally WITHOUT_ wins 50DOMINANT_${o:H} ?= no 51${OPTION_PREFIX}${o:H} ?= ${DOMINANT_${o:H}} 52.elif ${o:T:tl} == "no" 53.if defined(WITH_${o:H}) 54${OPTION_PREFIX}${o:H} ?= yes 55.else 56${OPTION_PREFIX}${o:H} ?= no 57.endif 58.else 59.if defined(WITHOUT_${o:H}) 60${OPTION_PREFIX}${o:H} ?= no 61.else 62${OPTION_PREFIX}${o:H} ?= yes 63.endif 64.endif 65.if defined(DEBUG_OPTIONS) && ${DEBUG_OPTIONS:@x@${o:H:M$x}@} != "" 66.info ${.INCLUDEDFROMFILE}: ${OPTION_PREFIX}${o:H}=${${OPTION_PREFIX}${o:H}} 67.endif 68.endfor 69 70# OPTIONS_DEFAULT_DEPENDENT += FOO_UTILS/FOO 71# If neither WITH[OUT]_FOO_UTILS is set, (see rules above) 72# use the value of ${OPTION_PREFIX}FOO 73.for o in ${OPTIONS_DEFAULT_DEPENDENT:M*/*:O:u} 74.if defined(NO_${o:H}) || defined(NO${o:H}) 75# we cannot do it 76${OPTION_PREFIX}${o:H} ?= no 77.elif defined(WITH_${o:H}) && defined(WITHOUT_${o:H}) 78# normally WITHOUT_ wins 79DOMINANT_${o:H} ?= no 80${OPTION_PREFIX}${o:H} ?= ${DOMINANT_${o:H}} 81.elif defined(WITH_${o:H}) 82${OPTION_PREFIX}${o:H} ?= yes 83.elif defined(WITHOUT_${o:H}) 84${OPTION_PREFIX}${o:H} ?= no 85.else 86${OPTION_PREFIX}${o:H} ?= ${${OPTION_PREFIX}${o:T}} 87.endif 88.if defined(DEBUG_OPTIONS) && ${DEBUG_OPTIONS:@x@${o:H:M$x}@} != "" 89.info ${.INCLUDEDFROMFILE}: ${OPTION_PREFIX}${o:H}=${${OPTION_PREFIX}${o:H}} (${OPTION_PREFIX}${o:T}=${${OPTION_PREFIX}${o:T}}) 90.endif 91.endfor 92 93# allow displaying/describing set options 94.set_options := ${.set_options} \ 95 ${OPTIONS_DEFAULT_VALUES:H:N.} \ 96 ${OPTIONS_DEFAULT_DEPENDENT:U:H:N.} \ 97 98# this can be used in .info as well as target below 99OPTIONS_SHOW ?= ${.set_options:O:u:@o@${OPTION_PREFIX}$o=${${OPTION_PREFIX}$o}@} 100# prefix for variables describing options 101OPTION_DESCRIPTION_PREFIX ?= DESCRIPTION_ 102OPTION_DESCRIPTION_SEPARATOR ?= == 103 104OPTIONS_DESCRIBE ?= ${.set_options:O:u:@o@${OPTION_PREFIX}$o=${${OPTION_PREFIX}$o}${${OPTION_DESCRIPTION_PREFIX}$o:S,^, ${OPTION_DESCRIPTION_SEPARATOR} ,1}${.newline}@} 105 106.if !commands(show-options) 107show-options: .NOTMAIN .PHONY 108 @echo; echo "${OPTIONS_SHOW:ts\n}"; echo 109.endif 110 111.if !commands(describe-options) 112describe-options: .NOTMAIN .PHONY 113 @echo; echo "${OPTIONS_DESCRIBE}"; echo 114.endif 115 116# we expect to be included more than once 117.undef OPTIONS_DEFAULT_DEPENDENT 118.undef OPTIONS_DEFAULT_NO 119.undef OPTIONS_DEFAULT_VALUES 120.undef OPTIONS_DEFAULT_YES 121