1#	$Id: prog.mk,v 1.44 2024/02/19 00:06:19 sjg Exp $
2
3# should be set properly in sys.mk
4_this ?= ${.PARSEFILE:S,bsd.,,}
5
6.if !target(__${_this}__)
7__${_this}__: .NOTMAIN
8
9.include <init.mk>
10
11# FreeBSD at least expects MAN8 etc.
12.if defined(MAN) && !empty(MAN)
13_sect:=${MAN:E}
14MAN${_sect}=${MAN}
15.endif
16
17.SUFFIXES: .out .o .c ${CXX_SUFFIXES} .y .l ${CCM_SUFFIXES} ${PCM}
18
19CFLAGS+=	${COPTS}
20
21.if ${TARGET_OSNAME} == "NetBSD"
22.if ${MACHINE_ARCH} == "sparc64"
23CFLAGS+=	-mcmodel=medlow
24.endif
25
26# ELF platforms depend on crtbegin.o and crtend.o
27.if ${OBJECT_FMT} == "ELF"
28.ifndef LIBCRTBEGIN
29LIBCRTBEGIN=	${DESTDIR}/usr/lib/crtbegin.o
30.MADE: ${LIBCRTBEGIN}
31.endif
32.ifndef LIBCRTEND
33LIBCRTEND=	${DESTDIR}/usr/lib/crtend.o
34.MADE: ${LIBCRTEND}
35.endif
36_SHLINKER=	${SHLINKDIR}/ld.elf_so
37.else
38LIBCRTBEGIN?=
39LIBCRTEND?=
40_SHLINKER=	${SHLINKDIR}/ld.so
41.endif
42
43.ifndef LIBCRT0
44LIBCRT0=	${DESTDIR}/usr/lib/crt0.o
45.MADE: ${LIBCRT0}
46.endif
47.endif	# NetBSD
48
49# here is where you can define what LIB* are
50.-include <libnames.mk>
51.if ${MK_DPADD_MK} == "yes"
52# lots of cool magic, but might not suit everyone.
53.include <dpadd.mk>
54.endif
55
56.if ${MK_GPROF} == "yes"
57CFLAGS+= ${CC_PG} ${PROFFLAGS}
58LDADD+= ${CC_PG}
59.if ${MK_DPADD_MK} == "no"
60LDADD_LIBC_P?= -lc_p
61LDADD_LAST+= ${LDADD_LIBC_P}
62.endif
63.endif
64
65.if defined(SHAREDSTRINGS)
66CLEANFILES+=strings
67.c.o:
68	@${COMPILE.c:N-c} -E ${.IMPSRC} | xstr -c -
69	@${COMPILE.c} x.c -o ${.TARGET}
70	@rm -f x.c
71
72# precompiled C++ Modules
73${CCM_SUFFIXES:%=%${PCM}}:
74	@${COMIPILE.cc:N-c} -E ${.IMPSRC} | xstr -c -
75	@mv -f x.c x.cc
76	@${COMPILE.pcm} x.cc -o ${.TARGET}
77	@rm -f x.cc
78
79${CXX_SUFFIXES:N.c*m:%=%.o}:
80	@${COMIPILE.cc:N-c} -E ${.IMPSRC} | xstr -c -
81	@mv -f x.c x.cc
82	@${COMPILE.cc} x.cc -o ${.TARGET}
83	@rm -f x.cc
84.endif
85
86.if defined(PROG_CXX)
87PROG=		${PROG_CXX}
88_SUPCXX?=	-lstdc++ -lm
89.endif
90
91.if defined(PROG)
92BINDIR ?= ${prefix}/bin
93
94.if empty(SRCS)
95# init.mk handling of QUALIFIED_VAR_LIST means
96# SRCS will be defined - even if empty.
97SRCS = ${PROG}.c
98.endif
99
100SRCS ?=	${PROG}.c
101OBJS_SRCS = ${SRCS:${OBJS_SRCS_FILTER}}
102.for s in ${OBJS_SRCS:M*/*}
103${.o .po .lo:L:@o@${s:T:R}$o@}: $s
104.endfor
105.if !empty(OBJS_SRCS)
106OBJS+=	${OBJS_SRCS:T:R:S/$/.o/g}
107LOBJS+=	${LSRCS:.c=.ln} ${SRCS:M*.c:.c=.ln}
108.endif
109
110.if defined(OBJS) && !empty(OBJS)
111.NOPATH: ${OBJS} ${PROG} ${SRCS:M*.[ly]:C/\..$/.c/} ${YHEADER:D${SRCS:M*.y:.y=.h}}
112
113# this is known to work for NetBSD 1.6 and FreeBSD 4.2
114.if ${TARGET_OSNAME:NFreeBSD:NNetBSD} == ""
115_PROGLDOPTS=
116.if ${SHLINKDIR} != "/usr/libexec"	# XXX: change or remove if ld.so moves
117_PROGLDOPTS+=	-Wl,-dynamic-linker=${_SHLINKER}
118.endif
119.if defined(LIBDIR) && ${SHLIBDIR} != ${LIBDIR}
120_PROGLDOPTS+=	-Wl,-rpath-link,${DESTDIR}${SHLIBDIR}:${DESTDIR}/usr/lib \
121		-L${DESTDIR}${SHLIBDIR}
122.endif
123_PROGLDOPTS+=	-Wl,-rpath,${SHLIBDIR}:/usr/lib
124.endif	# NetBSD
125
126.if ${MK_PROG_LDORDER_MK} != "no"
127${PROG}: ldorder
128
129.include <ldorder.mk>
130.endif
131# avoid -dL errors
132LDADD_LDORDER ?=
133LDSTATIC ?=
134
135.if defined(DESTDIR) && exists(${LIBCRT0}) && ${LIBCRT0} != "/dev/null"
136
137${PROG}: ${LIBCRT0} ${OBJS} ${LIBC} ${DPADD}
138	${_CCLINK} ${LDFLAGS} ${LDSTATIC} -o ${.TARGET} -nostdlib ${_PROGLDOPTS} -L${DESTDIR}/usr/lib ${LIBCRT0} ${LIBCRTBEGIN} ${OBJS} ${LDADD_LDORDER} ${LDADD} -L${DESTDIR}/usr/lib ${_SUPCXX} -lgcc -lc -lgcc ${LIBCRTEND}
139
140.else
141
142${PROG}: ${LIBCRT0} ${OBJS} ${LIBC} ${DPADD}
143	${_CCLINK} ${LDFLAGS} ${LDSTATIC} -o ${.TARGET} ${_PROGLDOPTS} ${OBJS} ${LDADD_LDORDER} ${LDADD}
144
145.endif	# defined(DESTDIR)
146.endif	# defined(OBJS) && !empty(OBJS)
147
148.if	!defined(MAN)
149MAN=	${PROG}.1
150.endif	# !defined(MAN)
151.endif	# defined(PROG)
152
153.if !defined(_SKIP_BUILD)
154realbuild: ${PROG}
155.endif
156
157all: _SUBDIRUSE
158
159.if !target(clean)
160cleanprog:
161	rm -f a.out [Ee]rrs mklog core *.core \
162	    ${PROG} ${OBJS} ${LOBJS} ${CLEANFILES}
163
164clean: _SUBDIRUSE cleanprog
165cleandir: _SUBDIRUSE cleanprog
166.else
167cleandir: _SUBDIRUSE clean
168.endif
169
170.if defined(SRCS) && (!defined(MKDEP) || ${MKDEP} != autodep)
171afterdepend: .depend
172	@(TMP=/tmp/_depend$$$$; \
173	    sed -e 's/^\([^\.]*\).o[ ]*:/\1.o \1.ln:/' \
174	      < .depend > $$TMP; \
175	    mv $$TMP .depend)
176.endif
177
178.if !target(install)
179.if !target(beforeinstall)
180beforeinstall:
181.endif
182.if !target(afterinstall)
183afterinstall:
184.endif
185
186.if !empty(BINOWN)
187PROG_INSTALL_OWN ?= -o ${BINOWN} -g ${BINGRP}
188.endif
189
190.if !target(realinstall)
191realinstall: proginstall
192.endif
193.if !target(proginstall)
194proginstall:
195.if defined(PROG)
196	[ -d ${DESTDIR}${BINDIR} ] || \
197	${INSTALL} -d ${PROG_INSTALL_OWN} -m 775 ${DESTDIR}${BINDIR}
198	${INSTALL} ${COPY} ${STRIP_FLAG} ${PROG_INSTALL_OWN} -m ${BINMODE} \
199	    ${PROG} ${DESTDIR}${BINDIR}/${PROG_NAME}
200.endif
201.if defined(HIDEGAME)
202	(cd ${DESTDIR}/usr/games; rm -f ${PROG}; ln -s dm ${PROG})
203.endif
204.endif
205
206.include <links.mk>
207
208install: maninstall install_links _SUBDIRUSE
209
210install_links:
211.if !empty(SYMLINKS)
212	@set ${SYMLINKS}; ${_SYMLINKS_SCRIPT}
213.endif
214.if !empty(LINKS)
215	@set ${LINKS}; ${_LINKS_SCRIPT}
216.endif
217
218maninstall: afterinstall
219afterinstall: realinstall
220install_links: realinstall
221proginstall: beforeinstall
222realinstall: beforeinstall
223.endif
224
225.if !target(lint)
226lint: ${LOBJS}
227.if defined(LOBJS) && !empty(LOBJS)
228	@${LINT} ${LINTFLAGS} ${LDFLAGS:M-L*} ${LOBJS} ${LDADD}
229.endif
230.endif
231
232.NOPATH:	${PROG}
233.if defined(OBJS) && !empty(OBJS)
234.NOPATH:	${OBJS}
235.endif
236
237.if defined(FILES) || defined(FILESGROUPS)
238.include <files.mk>
239.endif
240
241.if ${MK_MAN} != "no"
242.include <man.mk>
243.endif
244
245.if ${MK_NLS} != "no"
246.include <nls.mk>
247.endif
248
249.include <obj.mk>
250.include <dep.mk>
251.include <subdir.mk>
252
253.if !empty(PROG) && ${MK_STAGING_PROG} == "yes"
254STAGE_BINDIR ?= ${STAGE_OBJTOP}${BINDIR}
255STAGE_DIR.prog ?= ${STAGE_BINDIR}
256.if ${PROG_NAME:U${PROG}} != ${PROG}
257STAGE_AS_SETS += prog
258STAGE_AS_${PROG} = ${PROG_NAME}
259stage_as.prog: ${PROG}
260.else
261STAGE_SETS += prog
262stage_files.prog: ${PROG}
263.endif
264.endif
265
266.include <final.mk>
267
268.endif
269