xref: /freebsd/contrib/bmake/mk/whats.mk (revision dc1b8c9a846e78a87789fe1473350c3260722148)
1# $Id: whats.mk,v 1.11 2023/01/29 17:29:13 sjg Exp $
2#
3#	@(#) Copyright (c) 2014-2020, 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.if ${MK_WHATSTRING:Uno} == "yes"
17# it can be useful to embed a what(1) string in binaries
18# so that the build location can be seen from a core file.
19.if defined(KMOD)
20what_thing ?= ${KMOD}
21.elif defined(LIB)
22what_thing ?= lib${LIB}
23.elif defined(PROG)
24what_thing ?= ${PROG}
25SRCS ?= ${PROG}.c
26.elif defined(SHLIB)
27what_thing ?= lib${SHLIB}
28.endif
29
30.if !empty(what_thing)
31# a unique name that won't conflict with anything
32what_uuid = what_${what_thing}_${.CURDIR:T:hash}
33what_var = what_${.CURDIR:T:hash}
34
35SRCS += ${what_uuid}.c
36CLEANFILES += ${what_uuid}.c
37# we do not need to capture this
38SUPPRESS_DEPEND += *${what_uuid}.c
39
40SB ?= ${SRCTOP:H}
41SB_LOCATION ?= ${HOST}:${SB}
42# make customization easy
43WHAT_LOCATION ?= ${.OBJDIR:S,${SB},${SB_LOCATION},}
44WHAT_1 ?= ${what_thing:tu} built ${%Y%m%d:L:localtime} by ${USER}
45WHAT_2 ?= ${what_location}
46WHAT_LINE_IDS ?= 1 2
47WHAT_NOCMP_LINE_IDS ?= 1
48# you can add other WHAT_* just be sure to set WHAT_LINE_IDS
49# and WHAT_NOCMP_LINE_IDS accordingly
50
51# this works with clang and gcc
52what_t = const char __attribute__ ((section(".data")))
53what_location := ${WHAT_LOCATION}
54
55# this script is done in multiple lines so we can
56# use the token ${.OODATE:MNO_META_CMP}
57# to prevent the variable parts making this constantly out-of-date
58${what_uuid}.c:	.NOTMAIN
59	echo 'extern const char ${WHAT_LINE_IDS:@i@${what_var}_$i[]@:ts,};' > $@
60.for i in ${WHAT_LINE_IDS}
61.if ${WHAT_NOCMP_LINE_IDS:M$i} != ""
62	echo '${what_t} ${what_var}_$i[] = "@(#)${WHAT_$i}";' >> $@ ${.OODATE:MNO_META_CMP}
63.else
64	echo '${what_t} ${what_var}_$i[] = "@(#)${WHAT_$i}";' >> $@
65.endif
66.endfor
67
68.endif
69.endif
70