1# SPDX-License-Identifier: BSD-2-Clause
2#
3# $Id: sys.vars.mk,v 1.16 2024/02/17 17:26:57 sjg Exp $
4#
5#	@(#) Copyright (c) 2003-2023, 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# We use the following paradigm for preventing multiple inclusion.
19# It relies on the fact that conditionals and dependencies are resolved
20# at the time they are read.
21#
22# _this ?= ${.PARSEDIR:tA}/${.PARSEFILE}
23# .if !target(__${_this}__)
24# __${_this}__: .NOTMAIN
25#
26
27# if this is an ancient version of bmake
28MAKE_VERSION ?= 0
29.if ${MAKE_VERSION:M*make-*}
30# turn it into what we want - just the date
31MAKE_VERSION := ${MAKE_VERSION:[1]:C,.*-,,}
32.endif
33
34.if ${MAKE_VERSION} < 20100414
35_this = ${.PARSEDIR}/${.PARSEFILE}
36.else
37_this = ${.PARSEDIR:tA}/${.PARSEFILE}
38.endif
39
40# some useful modifiers
41
42# A useful trick for testing multiple :M's against something
43# :L says to use the variable's name as its value - ie. literal
44# got = ${clean* destroy:${M_ListToMatch:S,V,.TARGETS,}}
45M_ListToMatch = L:@m@$${V:U:M$$m}@
46# match against our initial targets (see above)
47M_L_TARGETS = ${M_ListToMatch:S,V,_TARGETS,}
48
49# turn a list into a set of :N modifiers
50# NskipFoo = ${Foo:${M_ListToSkip}}
51M_ListToSkip= O:u:S,^,N,:ts:
52
53# type should be a builtin in any sh since about 1980,
54# but sadly there are exceptions!
55.if ${.MAKE.OS:Unknown:NBSD/OS} == ""
56_type_sh = which
57.endif
58
59# AUTOCONF := ${autoconf:L:${M_whence}}
60M_type = @x@(${_type_sh:Utype} $$x) 2> /dev/null; echo;@:sh:[0]:N* found*:[@]:C,[()],,g
61M_whence = ${M_type}:M/*:[1]
62
63# produce similar output to jot(1) or seq(1)
64# eg. ${LIST:[#]:${M_JOT}}
65# would be 1 2 3 4 5 if LIST has 5 words
66# ${9:L:${M_JOT}}
67# would be 1 2 3 4 5 6 7 8 9
68.if ${.MAKE.LEVEL} == 0
69.for x in jot seq
70.if empty(JOT_CMD)
71JOT_CMD := ${$x:L:${M_whence}}
72.endif
73.endfor
74.if !empty(JOT_CMD)
75.export JOT_CMD
76.endif
77.endif
78.if !empty(JOT_CMD)
79M_JOT = [1]:S,^,${JOT_CMD} ,:sh
80.else
81M_JOT = [1]:@x@i=1;while [ $$$$i -le $$x ]; do echo $$$$i; i=$$$$((i + 1)); done;@:sh
82.endif
83
84# ${LIST:${M_RANGE}} is 1 2 3 4 5 if LIST has 5 words
85.if ${MAKE_VERSION} < 20170130
86M_RANGE = [#]:${M_JOT}
87.else
88M_RANGE = range
89.endif
90
91# convert a path to a valid shell variable
92M_P2V = tu:C,[./-],_,g
93
94# convert path to absolute
95.if ${MAKE_VERSION} < 20100414
96M_tA = C,.*,('cd' & \&\& 'pwd') 2> /dev/null || echo &,:sh
97.else
98M_tA = tA
99.endif
100
101# absoulte path to what we are reading.
102_PARSEDIR = ${.PARSEDIR:${M_tA}}
103
104.if ${MAKE_VERSION} >= 20170130
105# M_cmpv allows comparing dotted versions like 3.1.2
106# ${3.1.2:L:${M_cmpv}} -> 3001002
107# we use big jumps to handle 3 digits per dot:
108# ${123.456.789:L:${M_cmpv}} -> 123456789
109M_cmpv.units = 1 1000 1000000 1000000000 1000000000000
110M_cmpv = S,., ,g:C,^0*([0-9]),\1,:_:range:@i@+ $${_:[-$$i]} \* $${M_cmpv.units:[$$i]}@:S,^,expr 0 ,1:sh
111.endif
112
113# many projects use MAJOR MINOR PATCH versioning
114# ${OPENSSL:${M_M.M.P_VERSION}} is equivalent to
115# ${OPENSSL_MAJOR_VERSION}.${OPENSSL_MINOR_VERSION}.${OPENSSL_PATCH_VERSION}
116M_M.M.P_VERSION = L:@v@$${MAJOR MINOR PATCH:L:@t@$${$$v_$$t_VERSION:U0}@}@:ts.
117
118# numeric sort
119.if ${MAKE_VERSION} < 20210803
120M_On = O
121M_Onr = O
122.else
123M_On = On
124M_Onr = Onr
125.endif
126
127# Index of a word in a list.
128# eg. ${LIST:${M_Index:S,K,key,}} is the index of
129# the word "key" in ${LIST}, of course any pattern can be used.
130# If "key" appears more than once, there will be multiple
131# index values use ${M_Index:S,K,key,}:[1] to select only the first.
132M_Index = _:${M_RANGE}:@i@$${"$${_:[$$i]:MK}":?$$i:}@
133
134# mtime of each word - assumed to be a valid pathname
135.if ${.MAKE.LEVEL} < 20230510
136M_mtime = tW:S,^,${STAT:Ustat} -f %m ,:sh
137.else
138# M_mtime_fallback can be =error to throw an error
139# or =0 to use 0, default is to use current time
140M_mtime = mtime${M_mtime_fallback:U}
141.endif
142
143