xref: /freebsd/contrib/bmake/mk/whats.mk (revision c59c3bf34db360695f07735bebc76a768cac5afc)
1# SPDX-License-Identifier: BSD-2-Clause
2#
3# $Id: whats.mk,v 1.12 2024/02/17 17:26:57 sjg Exp $
4#
5#	@(#) Copyright (c) 2014-2020, Simon J. Gerraty
6#
7#	This file is provided in the hope that it will
8#	be of use.  There is absolutely NO WARRANTY.
9#	Permission to copy, redistribute or otherwise
10#	use this file is hereby granted provided that
11#	the above copyright notice and this notice are
12#	left intact.
13#
14#	Please send copies of changes and bug-fixes to:
15#	sjg@crufty.net
16#
17
18.if ${MK_WHATSTRING:Uno} == "yes"
19# it can be useful to embed a what(1) string in binaries
20# so that the build location can be seen from a core file.
21.if defined(KMOD)
22what_thing ?= ${KMOD}
23.elif defined(LIB)
24what_thing ?= lib${LIB}
25.elif defined(PROG)
26what_thing ?= ${PROG}
27SRCS ?= ${PROG}.c
28.elif defined(SHLIB)
29what_thing ?= lib${SHLIB}
30.endif
31
32.if !empty(what_thing)
33# a unique name that won't conflict with anything
34what_uuid = what_${what_thing}_${.CURDIR:T:hash}
35what_var = what_${.CURDIR:T:hash}
36
37SRCS += ${what_uuid}.c
38CLEANFILES += ${what_uuid}.c
39# we do not need to capture this
40SUPPRESS_DEPEND += *${what_uuid}.c
41
42SB ?= ${SRCTOP:H}
43SB_LOCATION ?= ${HOST}:${SB}
44# make customization easy
45WHAT_LOCATION ?= ${.OBJDIR:S,${SB},${SB_LOCATION},}
46WHAT_1 ?= ${what_thing:tu} built ${%Y%m%d:L:localtime} by ${USER}
47WHAT_2 ?= ${what_location}
48WHAT_LINE_IDS ?= 1 2
49WHAT_NOCMP_LINE_IDS ?= 1
50# you can add other WHAT_* just be sure to set WHAT_LINE_IDS
51# and WHAT_NOCMP_LINE_IDS accordingly
52
53# this works with clang and gcc
54what_t = const char __attribute__ ((section(".data")))
55what_location := ${WHAT_LOCATION}
56
57# this script is done in multiple lines so we can
58# use the token ${.OODATE:MNO_META_CMP}
59# to prevent the variable parts making this constantly out-of-date
60${what_uuid}.c:	.NOTMAIN
61	echo 'extern const char ${WHAT_LINE_IDS:@i@${what_var}_$i[]@:ts,};' > $@
62.for i in ${WHAT_LINE_IDS}
63.if ${WHAT_NOCMP_LINE_IDS:M$i} != ""
64	echo '${what_t} ${what_var}_$i[] = "@(#)${WHAT_$i}";' >> $@ ${.OODATE:MNO_META_CMP}
65.else
66	echo '${what_t} ${what_var}_$i[] = "@(#)${WHAT_$i}";' >> $@
67.endif
68.endfor
69
70.endif
71.endif
72