xref: /freebsd/contrib/bmake/mk/jobs.mk (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1# $Id: jobs.mk,v 1.9 2023/04/27 18:10:27 sjg Exp $
2#
3#	@(#) Copyright (c) 2012-2023, 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# This makefile is used by top-level makefile.
17# With the following:
18#
19#	.if make(*-jobs)
20#	.include <jobs.mk>
21#	.endif
22#
23#
24# Then if you do:
25#
26#	mk target-jobs
27#
28# We will run:
29#
30#	${MAKE} -j${JOB_MAX} target > ${JOB_LOGDIR}/target.log 2>&1
31#
32# JOB_MAX defaults to 8 but should normally be derrived based on the
33# number of cpus available.  The wrapper script 'mk' makes that easy.
34#
35
36now_utc ?= ${%s:L:gmtime}
37.if !defined(start_utc)
38start_utc := ${now_utc}
39.endif
40
41.if make(*-jobs)
42.info ${.newline}${TIME_STAMP} Start ${.TARGETS}
43
44JOB_LOGDIR ?= ${SRCTOP:H}
45JOB_LOG = ${JOB_LOGDIR}/${.TARGET:S,-jobs,,:S,/,_,g}.log
46JOB_LOG_GENS ?= 4
47# we like to rotate logs
48.if empty(NEWLOG_SH)
49.for d in ${.SYSPATH:U${.PARSEDIR}:@x@$x $x/scripts@}
50.if exists($d/newlog.sh)
51NEWLOG_SH := $d/newlog.sh
52.if ${MAKE_VERSION} > 20220924
53.break
54.endif
55.endif
56.endfor
57.if empty(NEWLOG_SH)
58.ifdef M_whence
59NEWLOG_SH := ${newlog.sh:L:${M_whence}}
60.else
61NEWLOG_SH := ${(type newlog.sh) 2> /dev/null:L:sh:M/*}
62.endif
63.endif
64.endif
65.if !empty(NEWLOG_SH) && exists(${NEWLOG_SH})
66NEWLOG := sh ${NEWLOG_SH}
67JOB_NEWLOG_ARGS ?= -S -n ${JOB_LOG_GENS}
68.else
69NEWLOG = :
70.endif
71
72.if ${.MAKE.JOBS:U0} > 0
73JOB_MAX= ${.MAKE.JOBS}
74.else
75# This should be derrived from number of cpu's
76JOB_MAX?= 8
77JOB_ARGS+= -j${JOB_MAX}
78.endif
79
80# we need to reset .MAKE.LEVEL to 0 do that
81# build orchestration works as expected (DIRDEPS_BUILD)
82${.TARGETS:M*-jobs}:
83	@${NEWLOG} ${JOB_NEWLOG_ARGS} ${JOB_LOG}
84	@echo "${TIME_STAMP} Start ${.TARGET:S,-jobs,,} ${JOB_ARGS} ${JOB_LOG_START} log=${JOB_LOG}" | tee ${JOB_LOG}
85	@cd ${.CURDIR} && env MAKELEVEL=0 \
86	${.MAKE} ${JOB_ARGS} _TARGETS=${.TARGET:S,-jobs,,} ${.TARGET:S,-jobs,,} >> ${JOB_LOG} 2>&1
87
88.endif
89
90.END: _build_finish
91.ERROR: _build_failed
92
93_build_finish:  .NOMETA
94	@echo "${TIME_STAMP} Finished ${.TARGETS} seconds=`expr ${now_utc} - ${start_utc}`"
95
96_build_failed: .NOMETA
97	@echo "${TIME_STAMP} Failed ${.TARGETS} seconds=`expr ${now_utc} - ${start_utc}`"
98