1# $Id: sys.mk,v 1.41 2015/11/14 20:20:34 sjg Exp $
2#
3#	@(#) Copyright (c) 2003-2009, Simon J. Gerraty
4#
5#	This file is provided in the hope that it will
6#	be of use.  There is absolutely NO WARRANTY.
7#	Permission to copy, redistribute or otherwise
8#	use this file is hereby granted provided that 
9#	the above copyright notice and this notice are
10#	left intact. 
11#      
12#	Please send copies of changes and bug-fixes to:
13#	sjg@crufty.net
14#
15
16# Avoid putting anything platform specific in here.
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 ?= ${.PARSEFILE}
23# .if !target(__${_this}__)
24# __${_this}__:
25#
26.if ${MAKE_VERSION:U0} > 20100408
27_this = ${.PARSEDIR:tA}/${.PARSEFILE}
28.else
29_this = ${.PARSEDIR}/${.PARSEFILE}
30.endif
31
32# Sometimes we want to turn on debugging in just one or two places
33# if .CURDIR is matched by any entry in DEBUG_MAKE_SYS_DIRS we
34# will apply DEBUG_MAKE_FLAGS now.
35# if an entry in DEBUG_MAKE_DIRS matches, we at the end of sys.mk
36# eg.  DEBUG_MAKE_FLAGS=-dv DEBUG_MAKE_SYS_DIRS="*lib/sjg"
37# use DEBUG_MAKE_FLAGS0 to apply only to .MAKE.LEVEL 0
38#
39.if ${.MAKE.LEVEL:U1} == 0
40# we use indirection, to simplify the tests below, and incase
41# DEBUG_* were given on our command line.
42_DEBUG_MAKE_FLAGS = ${DEBUG_MAKE_FLAGS0}
43_DEBUG_MAKE_SYS_DIRS = ${DEBUG_MAKE_SYS_DIRS0:U${DEBUG_MAKE_SYS_DIRS}}
44_DEBUG_MAKE_DIRS = ${DEBUG_MAKE_DIRS0:U${DEBUG_MAKE_DIRS}}
45.else
46_DEBUG_MAKE_FLAGS = ${DEBUG_MAKE_FLAGS}
47_DEBUG_MAKE_SYS_DIRS = ${DEBUG_MAKE_SYS_DIRS}
48_DEBUG_MAKE_DIRS = ${DEBUG_MAKE_DIRS}
49.endif
50
51.if !empty(_DEBUG_MAKE_FLAGS)
52.if ${_DEBUG_MAKE_SYS_DIRS:Uno:@x@${.CURDIR:M$x}@} != ""
53.MAKEFLAGS: ${_DEBUG_MAKE_FLAGS}
54.endif
55.endif
56
57# if this is an ancient version of bmake
58MAKE_VERSION ?= 0
59.if ${MAKE_VERSION:M*make-*}
60# turn it into what we want - just the date
61MAKE_VERSION := ${MAKE_VERSION:[1]:C,.*-,,}
62.endif
63
64# some useful modifiers
65
66# A useful trick for testing multiple :M's against something
67# :L says to use the variable's name as its value - ie. literal
68# got = ${clean* destroy:${M_ListToMatch:S,V,.TARGETS,}}
69M_ListToMatch = L:@m@$${V:M$$m}@
70# match against our initial targets (see above)
71M_L_TARGETS = ${M_ListToMatch:S,V,_TARGETS,}
72
73# turn a list into a set of :N modifiers
74# NskipFoo = ${Foo:${M_ListToSkip}}
75M_ListToSkip= O:u:ts::S,:,:N,g:S,^,N,
76
77# type should be a builtin in any sh since about 1980,
78# but sadly there are exceptions!
79.if ${.MAKE.OS:Unknown:NBSD/OS} == ""
80_type_sh = which
81.endif
82# AUTOCONF := ${autoconf:L:${M_whence}}
83M_type = @x@(${_type_sh:Utype} $$x) 2> /dev/null; echo;@:sh:[0]:N* found*:[@]:C,[()],,g
84M_whence = ${M_type}:M/*:[1]
85
86# convert a path to a valid shell variable
87M_P2V = tu:C,[./-],_,g
88
89# convert path to absolute
90.if ${MAKE_VERSION:U0} > 20100408
91M_tA = tA
92.else
93M_tA = C,.*,('cd' & \&\& 'pwd') 2> /dev/null || echo &,:sh
94.endif
95
96# absoulte path to what we are reading.
97_PARSEDIR = ${.PARSEDIR:${M_tA}}
98
99# we expect a recent bmake
100.if !defined(_TARGETS)
101# some things we do only once
102_TARGETS := ${.TARGETS}
103.-include <sys.env.mk>
104.endif
105
106# we need HOST_TARGET etc below.
107.include <host-target.mk>
108
109# early customizations
110.-include <local.sys.env.mk>
111
112# find the OS specifics
113.if defined(SYS_OS_MK)
114.include <${SYS_OS_MK}>
115.else
116_sys_mk =
117.for x in ${HOST_OSTYPE} ${HOST_TARGET} ${HOST_OS} ${MACHINE} Generic
118.if empty(_sys_mk)
119.-include <sys/$x.mk>
120_sys_mk := ${.MAKE.MAKEFILES:M*/$x.mk}
121.if !empty(_sys_mk)
122_sys_mk := sys/${_sys_mk:T}
123.endif
124.endif
125.if empty(_sys_mk)
126# might be an old style
127.-include <$x.sys.mk>
128_sys_mk := ${.MAKE.MAKEFILES:M*/$x.sys.mk:T}
129.endif
130.endfor
131
132SYS_OS_MK := ${_sys_mk}
133.export SYS_OS_MK
134.endif
135
136# some options we need to know early
137OPTIONS_DEFAULT_NO += \
138	DIRDEPS_BUILD \
139	DIRDEPS_CACHE \
140	META_MODE
141
142OPTIONS_DEFAULT_DEPENDENT += \
143	AUTO_OBJ/DIRDEPS_BUILD \
144	STAGING/DIRDEPS_BUILD \
145
146.-include "options.mk"
147
148.if ${MK_DIRDEPS_BUILD:Uno} == "yes"
149MK_META_MODE = yes
150.-include <meta.sys.mk>
151.elif ${MK_META_MODE:Uno} == "yes"
152.MAKE.MODE = meta verbose
153.endif
154# make sure we have a harmless value
155.MAKE.MODE ?= normal
156
157# if you want objdirs make them automatic
158# and do it early before we compute .PATH
159.if ${MK_AUTO_OBJ:Uno} == "yes" || ${MKOBJDIRS:Uno} == "auto"
160.include <auto.obj.mk>
161.endif
162
163.if !empty(SRCTOP)
164.if ${.CURDIR} == ${SRCTOP}
165RELDIR = .
166.elif ${.CURDIR:M${SRCTOP}/*}
167RELDIR := ${.CURDIR:S,${SRCTOP}/,,}
168.endif
169.endif
170
171MACHINE_ARCH.host ?= ${_HOST_ARCH}
172MACHINE_ARCH.${MACHINE} ?= ${MACHINE}
173.if empty(MACHINE_ARCH)
174MACHINE_ARCH = ${MACHINE_ARCH.${MACHINE}}
175.endif
176
177.ifndef ROOT_GROUP
178ROOT_GROUP != sed -n /:0:/s/:.*//p /etc/group
179.export ROOT_GROUP
180.endif
181
182unix ?= We run ${_HOST_OSNAME}.
183
184# A race condition in mkdir, means that it can bail if another
185# process made a dir that mkdir expected to.
186# We repeat the mkdir -p a number of times to try and work around this.
187# We stop looping as soon as the dir exists.
188# If we get to the end of the loop, a plain mkdir will issue an error.
189Mkdirs= Mkdirs() { \
190	for d in $$*; do \
191		for i in 1 2 3 4 5 6; do \
192			mkdir -p $$d; \
193			test -d $$d && return 0; \
194		done; \
195		mkdir $$d || exit $$?; \
196	done; }
197
198# this often helps with debugging
199.SUFFIXES:      .cpp-out
200
201.c.cpp-out:
202	@${COMPILE.c:N-c} -E ${.IMPSRC} | grep -v '^[ 	]*$$'
203
204.cc.cpp-out:
205	@${COMPILE.cc:N-c} -E ${.IMPSRC} | grep -v '^[ 	]*$$'
206
207# late customizations
208.-include <local.sys.mk>
209
210# if .CURDIR is matched by any entry in DEBUG_MAKE_DIRS we
211# will apply DEBUG_MAKE_FLAGS, now.
212.if !empty(_DEBUG_MAKE_FLAGS)
213.if ${_DEBUG_MAKE_DIRS:Uno:@x@${.CURDIR:M$x}@} != ""
214.MAKEFLAGS: ${_DEBUG_MAKE_FLAGS}
215.endif
216.endif
217