xref: /freebsd/share/mk/bsd.prog.mk (revision e8e8c939350bdf3c228a411caa9660c607c27a11)
1#	from: @(#)bsd.prog.mk	5.26 (Berkeley) 6/25/91
2# $FreeBSD$
3
4.include <bsd.init.mk>
5.include <bsd.compiler.mk>
6
7.SUFFIXES: .out .o .c .cc .cpp .cxx .C .m .y .l .ln .s .S .asm
8
9# XXX The use of COPTS in modern makefiles is discouraged.
10.if defined(COPTS)
11CFLAGS+=${COPTS}
12.endif
13
14.if ${MK_ASSERT_DEBUG} == "no"
15CFLAGS+= -DNDEBUG
16NO_WERROR=
17.endif
18
19.if defined(DEBUG_FLAGS)
20CFLAGS+=${DEBUG_FLAGS}
21CXXFLAGS+=${DEBUG_FLAGS}
22
23.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != ""
24CTFFLAGS+= -g
25.endif
26.endif
27
28.if defined(PROG_CXX)
29PROG=	${PROG_CXX}
30.endif
31
32.if !empty(LDFLAGS:M-Wl,*--oformat,*) || !empty(LDFLAGS:M-static)
33MK_DEBUG_FILES=	no
34.endif
35
36.if defined(CRUNCH_CFLAGS)
37CFLAGS+=${CRUNCH_CFLAGS}
38.else
39.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \
40    empty(DEBUG_FLAGS:M-gdwarf-*)
41CFLAGS+= -g
42CTFFLAGS+= -g
43.endif
44.endif
45
46.if !defined(DEBUG_FLAGS)
47STRIP?=	-s
48.endif
49
50.if defined(NO_SHARED) && (${NO_SHARED} != "no" && ${NO_SHARED} != "NO")
51LDFLAGS+= -static
52.endif
53
54.if ${MK_DEBUG_FILES} != "no"
55PROG_FULL=${PROG}.full
56# Use ${DEBUGDIR} for base system debug files, else .debug subdirectory
57.if defined(BINDIR) && (\
58    ${BINDIR} == "/bin" ||\
59    ${BINDIR} == "/libexec" ||\
60    ${BINDIR} == "/sbin" ||\
61    ${BINDIR:C%/usr/(bin|bsdinstall|libexec|lpr|sendmail|sm.bin|sbin)(/.*)?%/usr/bin%} == "/usr/bin"\
62     )
63DEBUGFILEDIR=	${DEBUGDIR}${BINDIR}
64.else
65DEBUGFILEDIR?=	${BINDIR}/.debug
66DEBUGMKDIR=
67.endif
68.else
69PROG_FULL=	${PROG}
70.endif
71
72.if defined(PROG)
73PROGNAME?=	${PROG}
74
75.if defined(SRCS)
76
77OBJS+=  ${SRCS:N*.h:R:S/$/.o/g}
78
79.if target(beforelinking)
80beforelinking: ${OBJS}
81${PROG_FULL}: beforelinking
82.endif
83${PROG_FULL}: ${OBJS}
84.if defined(PROG_CXX)
85	${CXX} ${CXXFLAGS} ${LDFLAGS} -o ${.TARGET} ${OBJS} ${LDADD}
86.else
87	${CC} ${CFLAGS} ${LDFLAGS} -o ${.TARGET} ${OBJS} ${LDADD}
88.endif
89.if ${MK_CTF} != "no"
90	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
91.endif
92
93.else	# !defined(SRCS)
94
95.if !target(${PROG})
96.if defined(PROG_CXX)
97SRCS=	${PROG}.cc
98.else
99SRCS=	${PROG}.c
100.endif
101
102# Always make an intermediate object file because:
103# - it saves time rebuilding when only the library has changed
104# - the name of the object gets put into the executable symbol table instead of
105#   the name of a variable temporary object.
106# - it's useful to keep objects around for crunching.
107OBJS+=	${PROG}.o
108
109.if target(beforelinking)
110beforelinking: ${OBJS}
111${PROG_FULL}: beforelinking
112.endif
113${PROG_FULL}: ${OBJS}
114.if defined(PROG_CXX)
115	${CXX} ${CXXFLAGS} ${LDFLAGS} -o ${.TARGET} ${OBJS} ${LDADD}
116.else
117	${CC} ${CFLAGS} ${LDFLAGS} -o ${.TARGET} ${OBJS} ${LDADD}
118.endif
119.if ${MK_CTF} != "no"
120	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS}
121.endif
122.endif # !target(${PROG})
123
124.endif # !defined(SRCS)
125
126.if ${MK_DEBUG_FILES} != "no"
127${PROG}: ${PROG_FULL} ${PROGNAME}.debug
128	${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROGNAME}.debug \
129	    ${PROG_FULL} ${.TARGET}
130
131${PROGNAME}.debug: ${PROG_FULL}
132	${OBJCOPY} --only-keep-debug ${PROG_FULL} ${.TARGET}
133.endif
134
135.if	${MK_MAN} != "no" && !defined(MAN) && \
136	!defined(MAN1) && !defined(MAN2) && !defined(MAN3) && \
137	!defined(MAN4) && !defined(MAN5) && !defined(MAN6) && \
138	!defined(MAN7) && !defined(MAN8) && !defined(MAN9)
139MAN=	${PROG}.1
140MAN1=	${MAN}
141.endif
142.endif # defined(PROG)
143
144.if defined(_SKIP_BUILD)
145all:
146.else
147all: beforebuild .WAIT ${PROG} ${SCRIPTS}
148beforebuild: objwarn
149.if ${MK_MAN} != "no"
150all: _manpages
151.endif
152.endif
153
154.if defined(PROG)
155CLEANFILES+= ${PROG}
156.if ${MK_DEBUG_FILES} != "no"
157CLEANFILES+=	${PROG_FULL} ${PROGNAME}.debug
158.endif
159.endif
160
161.if defined(OBJS)
162CLEANFILES+= ${OBJS}
163.endif
164
165.include <bsd.libnames.mk>
166
167.if defined(PROG)
168_EXTRADEPEND:
169.if defined(LDFLAGS) && !empty(LDFLAGS:M-nostdlib)
170.if defined(DPADD) && !empty(DPADD)
171	echo ${PROG_FULL}: ${DPADD} >> ${DEPENDFILE}
172.endif
173.else
174	echo ${PROG_FULL}: ${LIBC} ${DPADD} >> ${DEPENDFILE}
175.if defined(PROG_CXX)
176.if ${COMPILER_TYPE} == "clang" && empty(CXXFLAGS:M-stdlib=libstdc++)
177	echo ${PROG_FULL}: ${LIBCPLUSPLUS} >> ${DEPENDFILE}
178.else
179	echo ${PROG_FULL}: ${LIBSTDCPLUSPLUS} >> ${DEPENDFILE}
180.endif
181.endif
182.endif
183.endif
184
185.if !target(install)
186
187.if defined(PRECIOUSPROG)
188.if !defined(NO_FSCHG)
189INSTALLFLAGS+= -fschg
190.endif
191INSTALLFLAGS+= -S
192.endif
193
194_INSTALLFLAGS:=	${INSTALLFLAGS}
195.for ie in ${INSTALLFLAGS_EDIT}
196_INSTALLFLAGS:=	${_INSTALLFLAGS${ie}}
197.endfor
198
199.if !target(realinstall) && !defined(INTERNALPROG)
200realinstall: _proginstall
201.ORDER: beforeinstall _proginstall
202_proginstall:
203.if defined(PROG)
204	${INSTALL} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
205	    ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${BINDIR}/${PROGNAME}
206.if ${MK_DEBUG_FILES} != "no"
207.if defined(DEBUGMKDIR)
208	${INSTALL} -T debug -d ${DESTDIR}${DEBUGFILEDIR}
209.endif
210	${INSTALL} -T debug -o ${BINOWN} -g ${BINGRP} -m ${DEBUGMODE} \
211	    ${PROGNAME}.debug ${DESTDIR}${DEBUGFILEDIR}/${PROGNAME}.debug
212.endif
213.endif
214.endif	# !target(realinstall)
215
216.if defined(SCRIPTS) && !empty(SCRIPTS)
217realinstall: _scriptsinstall
218.ORDER: beforeinstall _scriptsinstall
219
220SCRIPTSDIR?=	${BINDIR}
221SCRIPTSOWN?=	${BINOWN}
222SCRIPTSGRP?=	${BINGRP}
223SCRIPTSMODE?=	${BINMODE}
224
225STAGE_AS_SETS+= scripts
226stage_as.scripts: ${SCRIPTS}
227FLAGS.stage_as.scripts= -m ${SCRIPTSMODE}
228STAGE_FILES_DIR.scripts= ${STAGE_OBJTOP}
229.for script in ${SCRIPTS}
230.if defined(SCRIPTSNAME)
231SCRIPTSNAME_${script:T}?=	${SCRIPTSNAME}
232.else
233SCRIPTSNAME_${script:T}?=	${script:T:R}
234.endif
235SCRIPTSDIR_${script:T}?=	${SCRIPTSDIR}
236SCRIPTSOWN_${script:T}?=	${SCRIPTSOWN}
237SCRIPTSGRP_${script:T}?=	${SCRIPTSGRP}
238SCRIPTSMODE_${script:T}?=	${SCRIPTSMODE}
239STAGE_AS_${script:T}=		${SCRIPTSDIR_${script:T}}/${SCRIPTSNAME_${script:T}}
240_scriptsinstall: _SCRIPTSINS_${script:T}
241_SCRIPTSINS_${script:T}: ${script}
242	${INSTALL} -o ${SCRIPTSOWN_${.ALLSRC:T}} \
243	    -g ${SCRIPTSGRP_${.ALLSRC:T}} -m ${SCRIPTSMODE_${.ALLSRC:T}} \
244	    ${.ALLSRC} \
245	    ${DESTDIR}${SCRIPTSDIR_${.ALLSRC:T}}/${SCRIPTSNAME_${.ALLSRC:T}}
246.endfor
247.endif
248
249NLSNAME?=	${PROG}
250.include <bsd.nls.mk>
251
252.include <bsd.files.mk>
253.include <bsd.incs.mk>
254.include <bsd.links.mk>
255
256.if ${MK_MAN} != "no"
257realinstall: _maninstall
258.ORDER: beforeinstall _maninstall
259.endif
260
261.endif
262
263.if !target(lint)
264lint: ${SRCS:M*.c}
265.if defined(PROG)
266	${LINT} ${LINTFLAGS} ${CFLAGS:M-[DIU]*} ${.ALLSRC}
267.endif
268.endif
269
270.if ${MK_MAN} != "no"
271.include <bsd.man.mk>
272.endif
273
274.include <bsd.dep.mk>
275
276.if defined(PROG) && !exists(${.OBJDIR}/${DEPENDFILE})
277${OBJS}: ${SRCS:M*.h}
278.endif
279
280.include <bsd.obj.mk>
281
282.include <bsd.sys.mk>
283