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