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