1246149Ssjg# $Id: sys.mk,v 1.35 2012/11/12 06:27:51 sjg Exp $
2246149Ssjg#
3246149Ssjg#	@(#) Copyright (c) 2003-2009, Simon J. Gerraty
4246149Ssjg#
5246149Ssjg#	This file is provided in the hope that it will
6246149Ssjg#	be of use.  There is absolutely NO WARRANTY.
7246149Ssjg#	Permission to copy, redistribute or otherwise
8246149Ssjg#	use this file is hereby granted provided that 
9246149Ssjg#	the above copyright notice and this notice are
10246149Ssjg#	left intact. 
11246149Ssjg#      
12246149Ssjg#	Please send copies of changes and bug-fixes to:
13246149Ssjg#	sjg@crufty.net
14246149Ssjg#
15246149Ssjg
16246149Ssjg# Avoid putting anything platform specific in here.
17246149Ssjg
18246149Ssjg# We use the following paradigm for preventing multiple inclusion.
19246149Ssjg# It relies on the fact that conditionals and dependencies are resolved 
20246149Ssjg# at the time they are read.
21246149Ssjg#
22246149Ssjg# _this ?= ${.PARSEFILE}
23246149Ssjg# .if !target(__${_this}__)
24246149Ssjg# __${_this}__:
25246149Ssjg#
26246149Ssjg.if ${MAKE_VERSION:U0} > 20100408
27246149Ssjg_this = ${.PARSEDIR:tA}/${.PARSEFILE}
28246149Ssjg.else
29246149Ssjg_this = ${.PARSEDIR}/${.PARSEFILE}
30246149Ssjg.endif
31246149Ssjg
32246149Ssjg# Sometimes we want to turn on debugging in just one or two places
33246149Ssjg# if .CURDIR is matched by any entry in DEBUG_MAKE_SYS_DIRS we
34246149Ssjg# will apply DEBUG_MAKE_FLAGS now.
35246149Ssjg# if an entry in DEBUG_MAKE_DIRS matches, we at the end of sys.mk
36246149Ssjg# eg.  DEBUG_MAKE_FLAGS=-dv DEBUG_MAKE_SYS_DIRS="*lib/sjg"
37246149Ssjg# use DEBUG_MAKE_FLAGS0 to apply only to .MAKE.LEVEL 0
38246149Ssjg#
39246149Ssjg.if ${.MAKE.LEVEL:U1} == 0
40246149Ssjg# we use indirection, to simplify the tests below, and incase
41246149Ssjg# DEBUG_* were given on our command line.
42246149Ssjg_DEBUG_MAKE_FLAGS = ${DEBUG_MAKE_FLAGS0}
43246149Ssjg_DEBUG_MAKE_SYS_DIRS = ${DEBUG_MAKE_SYS_DIRS0:U${DEBUG_MAKE_SYS_DIRS}}
44246149Ssjg_DEBUG_MAKE_DIRS = ${DEBUG_MAKE_DIRS0:U${DEBUG_MAKE_DIRS}}
45246149Ssjg.else
46246149Ssjg_DEBUG_MAKE_FLAGS = ${DEBUG_MAKE_FLAGS}
47246149Ssjg_DEBUG_MAKE_SYS_DIRS = ${DEBUG_MAKE_SYS_DIRS}
48246149Ssjg_DEBUG_MAKE_DIRS = ${DEBUG_MAKE_DIRS}
49246149Ssjg.endif
50246149Ssjg
51246149Ssjg.if !empty(_DEBUG_MAKE_FLAGS)
52246149Ssjg.if ${_DEBUG_MAKE_SYS_DIRS:Uno:@x@${.CURDIR:M$x}@} != ""
53246149Ssjg.MAKEFLAGS: ${_DEBUG_MAKE_FLAGS}
54246149Ssjg.endif
55246149Ssjg.endif
56246149Ssjg
57246149Ssjg# if this is an ancient version of bmake
58246149SsjgMAKE_VERSION ?= 0
59246149Ssjg.if ${MAKE_VERSION:M*make-*}
60246149Ssjg# turn it into what we want - just the date
61246149SsjgMAKE_VERSION := ${MAKE_VERSION:[1]:C,.*-,,}
62246149Ssjg.endif
63246149Ssjg
64246149Ssjg# some useful modifiers
65246149Ssjg
66246149Ssjg# A useful trick for testing multiple :M's against something
67246149Ssjg# :L says to use the variable's name as its value - ie. literal
68246149Ssjg# got = ${clean* destroy:${M_ListToMatch:S,V,.TARGETS,}}
69246149SsjgM_ListToMatch = L:@m@$${V:M$$m}@
70246149Ssjg# match against our initial targets (see above)
71246149SsjgM_L_TARGETS = ${M_ListToMatch:S,V,_TARGETS,}
72246149Ssjg
73246149Ssjg# turn a list into a set of :N modifiers
74246149Ssjg# NskipFoo = ${Foo:${M_ListToSkip}}
75246149SsjgM_ListToSkip= O:u:ts::S,:,:N,g:S,^,N,
76246149Ssjg
77246149Ssjg# type should be a builtin in any sh since about 1980,
78246149Ssjg# AUTOCONF := ${autoconf:L:${M_whence}}
79246149SsjgM_type = @x@(type $$x 2> /dev/null); echo;@:sh:[0]:N* found*:[@]:C,[()],,g
80246149SsjgM_whence = ${M_type}:M/*
81246149Ssjg
82246149Ssjg# convert a path to a valid shell variable
83246149SsjgM_P2V = tu:C,[./-],_,g
84246149Ssjg
85246149Ssjg# convert path to absolute
86246149Ssjg.if ${MAKE_VERSION:U0} > 20100408
87246149SsjgM_tA = tA
88246149Ssjg.else
89246149SsjgM_tA = C,.*,('cd' & \&\& 'pwd') 2> /dev/null || echo &,:sh
90246149Ssjg.endif
91246149Ssjg
92246149Ssjg# absoulte path to what we are reading.
93246149Ssjg_PARSEDIR = ${.PARSEDIR:${M_tA}}
94246149Ssjg
95246149Ssjg# we expect a recent bmake
96246149Ssjg.if !defined(_TARGETS)
97246149Ssjg# some things we do only once
98246149Ssjg_TARGETS := ${.TARGETS}
99246149Ssjg.-include <sys.env.mk>
100246149Ssjg.endif
101246149Ssjg
102246149Ssjg# we need HOST_TARGET etc below.
103246149Ssjg.include <host-target.mk>
104246149Ssjg
105246149Ssjg# find the OS specifics
106246149Ssjg.if defined(SYS_OS_MK)
107246149Ssjg.include <${SYS_OS_MK}>
108246149Ssjg.else
109246149Ssjg_sys_mk =
110246149Ssjg.for x in ${HOST_OSTYPE} ${HOST_TARGET} ${HOST_OS} ${MACHINE} Generic
111246149Ssjg.if empty(_sys_mk)
112246149Ssjg.-include <sys/$x.mk>
113246149Ssjg_sys_mk := ${.MAKE.MAKEFILES:M*/$x.mk}
114246149Ssjg.if !empty(_sys_mk)
115246149Ssjg_sys_mk := sys/${_sys_mk:T}
116246149Ssjg.endif
117246149Ssjg.endif
118246149Ssjg.if empty(_sys_mk)
119246149Ssjg# might be an old style
120246149Ssjg.-include <$x.sys.mk>
121246149Ssjg_sys_mk := ${.MAKE.MAKEFILES:M*/$x.sys.mk:T}
122246149Ssjg.endif
123246149Ssjg.endfor
124246149Ssjg
125246149SsjgSYS_OS_MK := ${_sys_mk}
126246149Ssjg.export SYS_OS_MK
127246149Ssjg.endif
128246149Ssjg
129246149Ssjg# allow customization without editing.
130246149Ssjg.-include <local.sys.mk>
131246149Ssjg
132246149Ssjg# if you want objdirs make them automatic
133246149Ssjg.if ${MKOBJDIRS:Uno} == "auto"
134246149Ssjg.include <auto.obj.mk>
135246149Ssjg.endif
136246149Ssjg
137246149Ssjg.if !empty(SRCTOP)
138246149Ssjg.if ${.CURDIR} == ${SRCTOP}
139246149SsjgRELDIR = .
140246149Ssjg.elif ${.CURDIR:M${SRCTOP}/*}
141246149SsjgRELDIR := ${.CURDIR:S,${SRCTOP}/,,}
142246149Ssjg.endif
143246149Ssjg.endif
144246149Ssjg
145246149SsjgMACHINE_ARCH.host ?= ${_HOST_ARCH}
146246149SsjgMACHINE_ARCH.${MACHINE} ?= ${MACHINE}
147246149Ssjg.if empty(MACHINE_ARCH)
148246149SsjgMACHINE_ARCH = ${MACHINE_ARCH.${MACHINE}}
149246149Ssjg.endif
150246149Ssjg
151246149Ssjg.ifndef ROOT_GROUP
152246149SsjgROOT_GROUP != sed -n /:0:/s/:.*//p /etc/group
153246149Ssjg.export ROOT_GROUP
154246149Ssjg.endif
155246149Ssjg
156246149Ssjgunix ?= We run ${_HOST_OSNAME}.
157246149Ssjg
158246149Ssjg# A race condition in mkdir, means that it can bail if another
159246149Ssjg# process made a dir that mkdir expected to.
160246149Ssjg# We repeat the mkdir -p a number of times to try and work around this.
161246149Ssjg# We stop looping as soon as the dir exists.
162246149Ssjg# If we get to the end of the loop, a plain mkdir will issue an error.
163246149SsjgMkdirs= Mkdirs() { \
164246149Ssjg	for d in $$*; do \
165246149Ssjg		for i in 1 2 3 4 5 6; do \
166246149Ssjg			mkdir -p $$d; \
167246149Ssjg			test -d $$d && return 0; \
168246149Ssjg		done; \
169246149Ssjg		mkdir $$d || exit $$?; \
170246149Ssjg	done; }
171246149Ssjg
172246149Ssjg# this often helps with debugging
173246149Ssjg.SUFFIXES:      .cpp-out
174246149Ssjg
175246149Ssjg.c.cpp-out:
176246149Ssjg	@${COMPILE.c:N-c} -E ${.IMPSRC} | grep -v '^[ 	]*$$'
177246149Ssjg
178246149Ssjg.cc.cpp-out:
179246149Ssjg	@${COMPILE.cc:N-c} -E ${.IMPSRC} | grep -v '^[ 	]*$$'
180246149Ssjg
181246149Ssjg# we don't include own.mk but user can expect -DWITH_META_MODE to work
182246149Ssjg.if defined(WITHOUT_META_MODE)
183246149SsjgUSE_META= no
184246149Ssjg.elif defined(WITH_META_MODE)
185246149SsjgUSE_META= yes
186246149Ssjg.endif
187246149Ssjg.if ${USE_META:Uno} == "yes"
188246149Ssjg.-include <meta.sys.mk>
189246149Ssjg.endif
190246149Ssjg# make sure we have a harmless value
191246149Ssjg.MAKE.MODE ?= normal
192246149Ssjg
193246149Ssjg# if .CURDIR is matched by any entry in DEBUG_MAKE_DIRS we
194246149Ssjg# will apply DEBUG_MAKE_FLAGS, now.
195246149Ssjg.if !empty(_DEBUG_MAKE_FLAGS)
196246149Ssjg.if ${_DEBUG_MAKE_DIRS:Uno:@x@${.CURDIR:M$x}@} != ""
197246149Ssjg.MAKEFLAGS: ${_DEBUG_MAKE_FLAGS}
198246149Ssjg.endif
199246149Ssjg.endif
200