xref: /freebsd/share/mk/jobs.mk (revision 40b9b2995f0f4b09cbdc9aa88f1f21f05bd1cc88)
1*40b9b299SSimon J. Gerraty# $Id: jobs.mk,v 1.14 2023/09/11 16:52:44 sjg Exp $
20df4d8adSSimon J. Gerraty#
30df4d8adSSimon J. Gerraty#	@(#) Copyright (c) 2012-2023, Simon J. Gerraty
40df4d8adSSimon J. Gerraty#
50df4d8adSSimon J. Gerraty#	This file is provided in the hope that it will
60df4d8adSSimon J. Gerraty#	be of use.  There is absolutely NO WARRANTY.
70df4d8adSSimon J. Gerraty#	Permission to copy, redistribute or otherwise
80df4d8adSSimon J. Gerraty#	use this file is hereby granted provided that
90df4d8adSSimon J. Gerraty#	the above copyright notice and this notice are
100df4d8adSSimon J. Gerraty#	left intact.
110df4d8adSSimon J. Gerraty#
120df4d8adSSimon J. Gerraty#	Please send copies of changes and bug-fixes to:
130df4d8adSSimon J. Gerraty#	sjg@crufty.net
140df4d8adSSimon J. Gerraty#
150df4d8adSSimon J. Gerraty
160df4d8adSSimon J. Gerraty# This makefile is used by top-level makefile.
170df4d8adSSimon J. Gerraty# With the following:
180df4d8adSSimon J. Gerraty#
190df4d8adSSimon J. Gerraty#	.if make(*-jobs)
200df4d8adSSimon J. Gerraty#	.include <jobs.mk>
210df4d8adSSimon J. Gerraty#	.endif
220df4d8adSSimon J. Gerraty#
230df4d8adSSimon J. Gerraty#
240df4d8adSSimon J. Gerraty# Then if you do:
250df4d8adSSimon J. Gerraty#
260df4d8adSSimon J. Gerraty#	mk target-jobs
270df4d8adSSimon J. Gerraty#
280df4d8adSSimon J. Gerraty# We will run:
290df4d8adSSimon J. Gerraty#
300df4d8adSSimon J. Gerraty#	${MAKE} -j${JOB_MAX} target > ${JOB_LOGDIR}/target.log 2>&1
310df4d8adSSimon J. Gerraty#
32*40b9b299SSimon J. Gerraty# JOB_MAX should be something like 1.2 - 1.5 times the number of
33*40b9b299SSimon J. Gerraty# available CPUs.
34*40b9b299SSimon J. Gerraty# If bmake sets .MAKE.JOBS.C=yes we can use -jC and
35*40b9b299SSimon J. Gerraty# JOB_MAX defaults to JOB_MAX_C (default 1.33C).
36*40b9b299SSimon J. Gerraty# Otherwise we use 8.
370df4d8adSSimon J. Gerraty#
380df4d8adSSimon J. Gerraty
39*40b9b299SSimon J. Gerratynow_utc ?= ${%s:L:localtime}
400df4d8adSSimon J. Gerraty.if !defined(start_utc)
410df4d8adSSimon J. Gerratystart_utc := ${now_utc}
420df4d8adSSimon J. Gerraty.endif
430df4d8adSSimon J. Gerraty
440df4d8adSSimon J. Gerraty.if make(*-jobs)
45e020aa37SSimon J. Gerraty.info ${.newline}${TIME_STAMP} Start ${.TARGETS}
460df4d8adSSimon J. Gerraty
470df4d8adSSimon J. GerratyJOB_LOGDIR ?= ${SRCTOP:H}
480df4d8adSSimon J. GerratyJOB_LOG = ${JOB_LOGDIR}/${.TARGET:S,-jobs,,:S,/,_,g}.log
490df4d8adSSimon J. GerratyJOB_LOG_GENS ?= 4
500df4d8adSSimon J. Gerraty# we like to rotate logs
510df4d8adSSimon J. Gerraty.if empty(NEWLOG_SH)
52e020aa37SSimon J. Gerraty.for d in ${.SYSPATH:U${.PARSEDIR}:@x@$x $x/scripts@}
53e020aa37SSimon J. Gerraty.if exists($d/newlog.sh)
54e020aa37SSimon J. GerratyNEWLOG_SH := $d/newlog.sh
55e020aa37SSimon J. Gerraty.if ${MAKE_VERSION} > 20220924
56e020aa37SSimon J. Gerraty.break
57e020aa37SSimon J. Gerraty.endif
58e020aa37SSimon J. Gerraty.endif
59e020aa37SSimon J. Gerraty.endfor
60e020aa37SSimon J. Gerraty.if empty(NEWLOG_SH)
610df4d8adSSimon J. Gerraty.ifdef M_whence
620df4d8adSSimon J. GerratyNEWLOG_SH := ${newlog.sh:L:${M_whence}}
630df4d8adSSimon J. Gerraty.else
640df4d8adSSimon J. GerratyNEWLOG_SH := ${(type newlog.sh) 2> /dev/null:L:sh:M/*}
650df4d8adSSimon J. Gerraty.endif
660df4d8adSSimon J. Gerraty.endif
67e020aa37SSimon J. Gerraty.endif
680df4d8adSSimon J. Gerraty.if !empty(NEWLOG_SH) && exists(${NEWLOG_SH})
690df4d8adSSimon J. GerratyNEWLOG := sh ${NEWLOG_SH}
700df4d8adSSimon J. GerratyJOB_NEWLOG_ARGS ?= -S -n ${JOB_LOG_GENS}
710df4d8adSSimon J. Gerraty.else
720df4d8adSSimon J. GerratyNEWLOG = :
730df4d8adSSimon J. Gerraty.endif
740df4d8adSSimon J. Gerraty
750df4d8adSSimon J. Gerraty.if ${.MAKE.JOBS:U0} > 0
760df4d8adSSimon J. GerratyJOB_MAX = ${.MAKE.JOBS}
770df4d8adSSimon J. Gerraty.else
780df4d8adSSimon J. Gerraty# This should be derrived from number of cpu's
79*40b9b299SSimon J. Gerraty.if ${.MAKE.JOBS.C:Uno} == "yes"
80*40b9b299SSimon J. Gerraty# 1.2 - 1.5 times nCPU works well on most machines that support -jC
81*40b9b299SSimon J. GerratyJOB_MAX_C ?= 1.33C
82*40b9b299SSimon J. GerratyJOB_MAX ?= ${JOB_MAX_C}
83*40b9b299SSimon J. Gerraty.endif
840df4d8adSSimon J. GerratyJOB_MAX ?= 8
850df4d8adSSimon J. GerratyJOB_ARGS += -j${JOB_MAX}
860df4d8adSSimon J. Gerraty.endif
870df4d8adSSimon J. Gerraty
88*40b9b299SSimon J. Gerraty# we need to reset .MAKE.LEVEL to 0 so that
890df4d8adSSimon J. Gerraty# build orchestration works as expected (DIRDEPS_BUILD)
900df4d8adSSimon J. Gerraty${.TARGETS:M*-jobs}:
910df4d8adSSimon J. Gerraty	@${NEWLOG} ${JOB_NEWLOG_ARGS} ${JOB_LOG}
92e020aa37SSimon J. Gerraty	@echo "${TIME_STAMP} Start ${.TARGET:S,-jobs,,} ${JOB_ARGS} ${JOB_LOG_START} log=${JOB_LOG}" | tee ${JOB_LOG}
930df4d8adSSimon J. Gerraty	@cd ${.CURDIR} && env MAKELEVEL=0 \
940df4d8adSSimon J. Gerraty	${.MAKE} ${JOB_ARGS} _TARGETS=${.TARGET:S,-jobs,,} ${.TARGET:S,-jobs,,} >> ${JOB_LOG} 2>&1
950df4d8adSSimon J. Gerraty
960df4d8adSSimon J. Gerraty.endif
970df4d8adSSimon J. Gerraty
980df4d8adSSimon J. Gerraty.END: _build_finish
990df4d8adSSimon J. Gerraty.ERROR: _build_failed
1000df4d8adSSimon J. Gerraty
1010df4d8adSSimon J. Gerraty_build_finish:  .NOMETA
1020df4d8adSSimon J. Gerraty	@echo "${TIME_STAMP} Finished ${.TARGETS} seconds=`expr ${now_utc} - ${start_utc}`"
1030df4d8adSSimon J. Gerraty
1040df4d8adSSimon J. Gerraty_build_failed: .NOMETA
1050df4d8adSSimon J. Gerraty	@echo "${TIME_STAMP} Failed ${.TARGETS} seconds=`expr ${now_utc} - ${start_utc}`"
106