xref: /freebsd/contrib/bmake/mk/jobs.mk (revision 8a271827e7b5d5310e06df1f9f49ba0ef9efd263)
1# $Id: jobs.mk,v 1.7 2023/04/18 23:32:28 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.info ${.newline}${TIME_STAMP} Start ${.TARGETS}
42
43.if make(*-jobs)
44
45JOB_LOGDIR ?= ${SRCTOP:H}
46JOB_LOG = ${JOB_LOGDIR}/${.TARGET:S,-jobs,,:S,/,_,g}.log
47JOB_LOG_GENS ?= 4
48# we like to rotate logs
49.if empty(NEWLOG_SH)
50.ifdef M_whence
51NEWLOG_SH := ${newlog.sh:L:${M_whence}}
52.else
53NEWLOG_SH := ${(type newlog.sh) 2> /dev/null:L:sh:M/*}
54.endif
55.endif
56.if !empty(NEWLOG_SH) && exists(${NEWLOG_SH})
57NEWLOG := sh ${NEWLOG_SH}
58JOB_NEWLOG_ARGS ?= -S -n ${JOB_LOG_GENS}
59.else
60NEWLOG = :
61.endif
62
63.if ${.MAKE.JOBS:U0} > 0
64JOB_MAX= ${.MAKE.JOBS}
65.else
66# This should be derrived from number of cpu's
67JOB_MAX?= 8
68JOB_ARGS+= -j${JOB_MAX}
69.endif
70
71# we need to reset .MAKE.LEVEL to 0 do that
72# build orchestration works as expected (DIRDEPS_BUILD)
73${.TARGETS:M*-jobs}:
74	@${NEWLOG} ${JOB_NEWLOG_ARGS} ${JOB_LOG}
75	@echo Logging to ${JOB_LOG}
76	@cd ${.CURDIR} && env MAKELEVEL=0 \
77	${.MAKE} ${JOB_ARGS} _TARGETS=${.TARGET:S,-jobs,,} ${.TARGET:S,-jobs,,} >> ${JOB_LOG} 2>&1
78
79.endif
80
81.END: _build_finish
82.ERROR: _build_failed
83
84_build_finish:  .NOMETA
85	@echo "${TIME_STAMP} Finished ${.TARGETS} seconds=`expr ${now_utc} - ${start_utc}`"
86
87_build_failed: .NOMETA
88	@echo "${TIME_STAMP} Failed ${.TARGETS} seconds=`expr ${now_utc} - ${start_utc}`"
89