xref: /freebsd/contrib/bmake/mk/jobs.mk (revision c59c3bf34db360695f07735bebc76a768cac5afc)
1*c59c3bf3SSimon J. Gerraty# SPDX-License-Identifier: BSD-2-Clause
2*c59c3bf3SSimon J. Gerraty#
3*c59c3bf3SSimon J. Gerraty# $Id: jobs.mk,v 1.17 2024/02/17 17:26:57 sjg Exp $
48c973ee2SSimon J. Gerraty#
58c973ee2SSimon J. Gerraty#	@(#) Copyright (c) 2012-2023, Simon J. Gerraty
68c973ee2SSimon J. Gerraty#
78c973ee2SSimon J. Gerraty#	This file is provided in the hope that it will
88c973ee2SSimon J. Gerraty#	be of use.  There is absolutely NO WARRANTY.
98c973ee2SSimon J. Gerraty#	Permission to copy, redistribute or otherwise
108c973ee2SSimon J. Gerraty#	use this file is hereby granted provided that
118c973ee2SSimon J. Gerraty#	the above copyright notice and this notice are
128c973ee2SSimon J. Gerraty#	left intact.
138c973ee2SSimon J. Gerraty#
148c973ee2SSimon J. Gerraty#	Please send copies of changes and bug-fixes to:
158c973ee2SSimon J. Gerraty#	sjg@crufty.net
168c973ee2SSimon J. Gerraty#
178c973ee2SSimon J. Gerraty
188c973ee2SSimon J. Gerraty# This makefile is used by top-level makefile.
198c973ee2SSimon J. Gerraty# With the following:
208c973ee2SSimon J. Gerraty#
218c973ee2SSimon J. Gerraty#	.if make(*-jobs)
228c973ee2SSimon J. Gerraty#	.include <jobs.mk>
238c973ee2SSimon J. Gerraty#	.endif
248c973ee2SSimon J. Gerraty#
258c973ee2SSimon J. Gerraty#
268c973ee2SSimon J. Gerraty# Then if you do:
278c973ee2SSimon J. Gerraty#
288c973ee2SSimon J. Gerraty#	mk target-jobs
298c973ee2SSimon J. Gerraty#
308c973ee2SSimon J. Gerraty# We will run:
318c973ee2SSimon J. Gerraty#
328c973ee2SSimon J. Gerraty#	${MAKE} -j${JOB_MAX} target > ${JOB_LOGDIR}/target.log 2>&1
338c973ee2SSimon J. Gerraty#
3498875883SSimon J. Gerraty# JOB_MAX should be something like 1.2 - 1.5 times the number of
3598875883SSimon J. Gerraty# available CPUs.
3698875883SSimon J. Gerraty# If bmake sets .MAKE.JOBS.C=yes we can use -jC and
3798875883SSimon J. Gerraty# JOB_MAX defaults to JOB_MAX_C (default 1.33C).
3898875883SSimon J. Gerraty# Otherwise we use 8.
398c973ee2SSimon J. Gerraty#
408c973ee2SSimon J. Gerraty
4198875883SSimon J. Gerratynow_utc ?= ${%s:L:localtime}
428c973ee2SSimon J. Gerraty.if !defined(start_utc)
438c973ee2SSimon J. Gerratystart_utc := ${now_utc}
448c973ee2SSimon J. Gerraty.endif
458c973ee2SSimon J. Gerraty
468c973ee2SSimon J. Gerraty.if make(*-jobs)
47c1d01b5fSSimon J. Gerraty.info ${.newline}${TIME_STAMP} Start ${.TARGETS}
488c973ee2SSimon J. Gerraty
498c973ee2SSimon J. GerratyJOB_LOGDIR ?= ${SRCTOP:H}
508c973ee2SSimon J. GerratyJOB_LOG = ${JOB_LOGDIR}/${.TARGET:S,-jobs,,:S,/,_,g}.log
518c973ee2SSimon J. GerratyJOB_LOG_GENS ?= 4
528c973ee2SSimon J. Gerraty# we like to rotate logs
538c973ee2SSimon J. Gerraty.if empty(NEWLOG_SH)
54c1d01b5fSSimon J. Gerraty.for d in ${.SYSPATH:U${.PARSEDIR}:@x@$x $x/scripts@}
55c1d01b5fSSimon J. Gerraty.if exists($d/newlog.sh)
56c1d01b5fSSimon J. GerratyNEWLOG_SH := $d/newlog.sh
57c1d01b5fSSimon J. Gerraty.if ${MAKE_VERSION} > 20220924
58c1d01b5fSSimon J. Gerraty.break
59c1d01b5fSSimon J. Gerraty.endif
60c1d01b5fSSimon J. Gerraty.endif
61c1d01b5fSSimon J. Gerraty.endfor
62c1d01b5fSSimon J. Gerraty.if empty(NEWLOG_SH)
638c973ee2SSimon J. Gerraty.ifdef M_whence
648c973ee2SSimon J. GerratyNEWLOG_SH := ${newlog.sh:L:${M_whence}}
658c973ee2SSimon J. Gerraty.else
668c973ee2SSimon J. GerratyNEWLOG_SH := ${(type newlog.sh) 2> /dev/null:L:sh:M/*}
678c973ee2SSimon J. Gerraty.endif
688c973ee2SSimon J. Gerraty.endif
69c1d01b5fSSimon J. Gerraty.endif
708c973ee2SSimon J. Gerraty.if !empty(NEWLOG_SH) && exists(${NEWLOG_SH})
718c973ee2SSimon J. GerratyNEWLOG := sh ${NEWLOG_SH}
728c973ee2SSimon J. GerratyJOB_NEWLOG_ARGS ?= -S -n ${JOB_LOG_GENS}
738c973ee2SSimon J. Gerraty.else
748c973ee2SSimon J. GerratyNEWLOG = :
758c973ee2SSimon J. Gerraty.endif
768c973ee2SSimon J. Gerraty
778c973ee2SSimon J. Gerraty.if ${.MAKE.JOBS:U0} > 0
788c973ee2SSimon J. GerratyJOB_MAX = ${.MAKE.JOBS}
798c973ee2SSimon J. Gerraty.else
808c973ee2SSimon J. Gerraty# This should be derrived from number of cpu's
8198875883SSimon J. Gerraty.if ${.MAKE.JOBS.C:Uno} == "yes"
8298875883SSimon J. Gerraty# 1.2 - 1.5 times nCPU works well on most machines that support -jC
83d5e0a182SSimon J. Gerraty# if the factor is floating point, the C suffix isn't needed
84d5e0a182SSimon J. GerratyJOB_MAX_C ?= 1.33
8598875883SSimon J. GerratyJOB_MAX ?= ${JOB_MAX_C}
8698875883SSimon J. Gerraty.endif
878c973ee2SSimon J. GerratyJOB_MAX ?= 8
888c973ee2SSimon J. GerratyJOB_ARGS += -j${JOB_MAX}
898c973ee2SSimon J. Gerraty.endif
908c973ee2SSimon J. Gerraty
9198875883SSimon J. Gerraty# we need to reset .MAKE.LEVEL to 0 so that
928c973ee2SSimon J. Gerraty# build orchestration works as expected (DIRDEPS_BUILD)
938c973ee2SSimon J. Gerraty${.TARGETS:M*-jobs}:
948c973ee2SSimon J. Gerraty	@${NEWLOG} ${JOB_NEWLOG_ARGS} ${JOB_LOG}
95c1d01b5fSSimon J. Gerraty	@echo "${TIME_STAMP} Start ${.TARGET:S,-jobs,,} ${JOB_ARGS} ${JOB_LOG_START} log=${JOB_LOG}" | tee ${JOB_LOG}
968c973ee2SSimon J. Gerraty	@cd ${.CURDIR} && env MAKELEVEL=0 \
978c973ee2SSimon J. Gerraty	${.MAKE} ${JOB_ARGS} _TARGETS=${.TARGET:S,-jobs,,} ${.TARGET:S,-jobs,,} >> ${JOB_LOG} 2>&1
988c973ee2SSimon J. Gerraty
998c973ee2SSimon J. Gerraty.endif
1008c973ee2SSimon J. Gerraty
1018c973ee2SSimon J. Gerraty.END: _build_finish
1028c973ee2SSimon J. Gerraty.ERROR: _build_failed
1038c973ee2SSimon J. Gerraty
1048c973ee2SSimon J. Gerraty_build_finish:  .NOMETA
1058c973ee2SSimon J. Gerraty	@echo "${TIME_STAMP} Finished ${.TARGETS} seconds=`expr ${now_utc} - ${start_utc}`"
1068c973ee2SSimon J. Gerraty
1078c973ee2SSimon J. Gerraty_build_failed: .NOMETA
1088c973ee2SSimon J. Gerraty	@echo "${TIME_STAMP} Failed ${.TARGETS} seconds=`expr ${now_utc} - ${start_utc}`"
109