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.include <bsd.linker.mk> 7 8.SUFFIXES: .out .o .bc .c .cc .cpp .cxx .C .m .y .l .ll .ln .s .S .asm 9 10# XXX The use of COPTS in modern makefiles is discouraged. 11.if defined(COPTS) 12.warning ${.CURDIR}: COPTS should be CFLAGS. 13CFLAGS+=${COPTS} 14.endif 15 16.if ${MK_ASSERT_DEBUG} == "no" 17CFLAGS+= -DNDEBUG 18# XXX: shouldn't we ensure that !asserts marks potentially unused variables as 19# __unused instead of disabling -Werror globally? 20MK_WERROR= no 21.endif 22 23.if defined(DEBUG_FLAGS) 24CFLAGS+=${DEBUG_FLAGS} 25CXXFLAGS+=${DEBUG_FLAGS} 26 27.if ${MK_CTF} != "no" && ${DEBUG_FLAGS:M-g} != "" 28CTFFLAGS+= -g 29.endif 30.endif 31 32.if defined(PROG_CXX) 33PROG= ${PROG_CXX} 34.endif 35 36.if !empty(LDFLAGS:M-Wl,*--oformat,*) || !empty(LDFLAGS:M-static) 37MK_DEBUG_FILES= no 38.endif 39 40# ELF hardening knobs 41.if ${MK_BIND_NOW} != "no" 42LDFLAGS+= -Wl,-znow 43.endif 44.if ${MK_PIE} != "no" 45# Static PIE is not yet supported/tested. 46.if !defined(NO_SHARED) || ${NO_SHARED:tl} == "no" 47CFLAGS+= -fPIE 48CXXFLAGS+= -fPIE 49LDFLAGS+= -pie 50.endif 51.endif 52.if ${MK_RETPOLINE} != "no" 53.if ${COMPILER_FEATURES:Mretpoline} && ${LINKER_FEATURES:Mretpoline} 54CFLAGS+= -mretpoline 55CXXFLAGS+= -mretpoline 56# retpolineplt is broken with static linking (PR 233336) 57.if !defined(NO_SHARED) || ${NO_SHARED:tl} == "no" 58LDFLAGS+= -Wl,-zretpolineplt 59.endif 60.else 61.warning Retpoline requested but not supported by compiler or linker 62.endif 63.endif 64 65# Initialize stack variables on function entry 66.if ${MK_INIT_ALL_ZERO} == "yes" 67.if ${COMPILER_FEATURES:Minit-all} 68CFLAGS+= -ftrivial-auto-var-init=zero \ 69 -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang 70CXXFLAGS+= -ftrivial-auto-var-init=zero \ 71 -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang 72.else 73.warning InitAll (zeros) requested but not support by compiler 74.endif 75.elif ${MK_INIT_ALL_PATTERN} == "yes" 76.if ${COMPILER_FEATURES:Minit-all} 77CFLAGS+= -ftrivial-auto-var-init=pattern 78CXXFLAGS+= -ftrivial-auto-var-init=pattern 79.else 80.warning InitAll (pattern) requested but not support by compiler 81.endif 82.endif 83 84# bsd.sanitizer.mk is not installed, so don't require it (e.g. for ports). 85.sinclude "bsd.sanitizer.mk" 86 87.if ${MACHINE_CPUARCH} == "riscv" && ${LINKER_FEATURES:Mriscv-relaxations} == "" 88CFLAGS += -mno-relax 89.endif 90 91.if defined(CRUNCH_CFLAGS) 92CFLAGS+=${CRUNCH_CFLAGS} 93.else 94.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \ 95 empty(DEBUG_FLAGS:M-gdwarf-*) 96CFLAGS+= ${DEBUG_FILES_CFLAGS} 97CTFFLAGS+= -g 98.endif 99.endif 100 101.if !defined(DEBUG_FLAGS) 102STRIP?= -s 103.endif 104 105.if defined(NO_ROOT) 106.if !defined(TAGS) || ! ${TAGS:Mpackage=*} 107TAGS+= package=${PACKAGE:Uutilities} 108.endif 109TAG_ARGS= -T ${TAGS:[*]:S/ /,/g} 110.endif 111 112.if defined(NO_SHARED) && ${NO_SHARED:tl} != "no" 113LDFLAGS+= -static 114.endif 115 116# clang currently defaults to dynamic TLS for mips64 binaries 117.if ${MACHINE_ARCH:Mmips64*} && ${COMPILER_TYPE} == "clang" 118CFLAGS+= -ftls-model=initial-exec 119.endif 120 121.if ${MK_DEBUG_FILES} != "no" 122PROG_FULL=${PROG}.full 123# Use ${DEBUGDIR} for base system debug files, else .debug subdirectory 124.if defined(BINDIR) && (\ 125 ${BINDIR} == "/bin" ||\ 126 ${BINDIR:C%/libexec(/.*)?%/libexec%} == "/libexec" ||\ 127 ${BINDIR} == "/sbin" ||\ 128 ${BINDIR:C%/usr/(bin|bsdinstall|libexec|lpr|sendmail|sm.bin|sbin|tests)(/.*)?%/usr/bin%} == "/usr/bin" ||\ 129 ${BINDIR} == "/usr/lib" \ 130 ) 131DEBUGFILEDIR= ${DEBUGDIR}${BINDIR} 132.else 133DEBUGFILEDIR?= ${BINDIR}/.debug 134.endif 135.if !exists(${DESTDIR}${DEBUGFILEDIR}) 136DEBUGMKDIR= 137.endif 138.else 139PROG_FULL= ${PROG} 140.endif 141 142.if defined(PROG) 143PROGNAME?= ${PROG} 144 145.if defined(SRCS) 146 147OBJS+= ${SRCS:N*.h:${OBJS_SRCS_FILTER:ts:}:S/$/.o/g} 148 149# LLVM bitcode / textual IR representations of the program 150BCOBJS+=${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.bco/g} 151LLOBJS+=${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.llo/g} 152 153.if target(beforelinking) 154beforelinking: ${OBJS} 155${PROG_FULL}: beforelinking 156.endif 157${PROG_FULL}: ${OBJS} 158.if defined(PROG_CXX) 159 ${CXX:N${CCACHE_BIN}} ${CXXFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} \ 160 ${OBJS} ${LDADD} 161.else 162 ${CC:N${CCACHE_BIN}} ${CFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} ${OBJS} \ 163 ${LDADD} 164.endif 165.if ${MK_CTF} != "no" 166 ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS} 167.endif 168 169.else # !defined(SRCS) 170 171.if !target(${PROG}) 172.if defined(PROG_CXX) 173SRCS= ${PROG}.cc 174.else 175SRCS= ${PROG}.c 176.endif 177 178# Always make an intermediate object file because: 179# - it saves time rebuilding when only the library has changed 180# - the name of the object gets put into the executable symbol table instead of 181# the name of a variable temporary object. 182# - it's useful to keep objects around for crunching. 183OBJS+= ${PROG}.o 184BCOBJS+= ${PROG}.bc 185LLOBJS+= ${PROG}.ll 186CLEANFILES+= ${PROG}.o ${PROG}.bc ${PROG}.ll 187 188.if target(beforelinking) 189beforelinking: ${OBJS} 190${PROG_FULL}: beforelinking 191.endif 192${PROG_FULL}: ${OBJS} 193.if defined(PROG_CXX) 194 ${CXX:N${CCACHE_BIN}} ${CXXFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} \ 195 ${OBJS} ${LDADD} 196.else 197 ${CC:N${CCACHE_BIN}} ${CFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} ${OBJS} \ 198 ${LDADD} 199.endif 200.if ${MK_CTF} != "no" 201 ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS} 202.endif 203.endif # !target(${PROG}) 204 205.endif # !defined(SRCS) 206 207.if ${MK_DEBUG_FILES} != "no" 208${PROG}: ${PROG_FULL} ${PROGNAME}.debug 209 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROGNAME}.debug \ 210 ${PROG_FULL} ${.TARGET} 211 212${PROGNAME}.debug: ${PROG_FULL} 213 ${OBJCOPY} --only-keep-debug ${PROG_FULL} ${.TARGET} 214.endif 215 216.if defined(LLVM_LINK) 217${PROG_FULL}.bc: ${BCOBJS} 218 ${LLVM_LINK} -o ${.TARGET} ${BCOBJS} 219 220${PROG_FULL}.ll: ${LLOBJS} 221 ${LLVM_LINK} -S -o ${.TARGET} ${LLOBJS} 222 223CLEANFILES+= ${PROG_FULL}.bc ${PROG_FULL}.ll 224.endif # defined(LLVM_LINK) 225 226.if ${MK_MAN} != "no" && !defined(MAN) && \ 227 !defined(MAN1) && !defined(MAN2) && !defined(MAN3) && \ 228 !defined(MAN4) && !defined(MAN5) && !defined(MAN6) && \ 229 !defined(MAN7) && !defined(MAN8) && !defined(MAN9) 230MAN= ${PROG}.1 231MAN1= ${MAN} 232.endif 233.endif # defined(PROG) 234 235.if defined(_SKIP_BUILD) 236all: 237.else 238.if target(afterbuild) 239.ORDER: ${PROG} afterbuild 240all: ${PROG} ${SCRIPTS} afterbuild 241.else 242all: ${PROG} ${SCRIPTS} 243.endif 244.if ${MK_MAN} != "no" 245all: all-man 246.endif 247.endif 248 249.if defined(PROG) 250CLEANFILES+= ${PROG} ${PROG}.bc ${PROG}.ll 251.if ${MK_DEBUG_FILES} != "no" 252CLEANFILES+= ${PROG_FULL} ${PROGNAME}.debug 253.endif 254.endif 255 256.if defined(OBJS) 257CLEANFILES+= ${OBJS} ${BCOBJS} ${LLOBJS} 258.endif 259 260.include <bsd.libnames.mk> 261 262.if defined(PROG) 263.if !defined(NO_EXTRADEPEND) 264_EXTRADEPEND: 265.if defined(LDFLAGS) && !empty(LDFLAGS:M-nostdlib) 266.if defined(DPADD) && !empty(DPADD) 267 echo ${PROG_FULL}: ${DPADD} >> ${DEPENDFILE} 268.endif 269.else 270 echo ${PROG_FULL}: ${LIBC} ${DPADD} >> ${DEPENDFILE} 271.if defined(PROG_CXX) 272 echo ${PROG_FULL}: ${LIBCPLUSPLUS} >> ${DEPENDFILE} 273.endif 274.endif 275.endif # !defined(NO_EXTRADEPEND) 276.endif 277 278.if !target(install) 279 280.if defined(PRECIOUSPROG) 281.if !defined(NO_FSCHG) 282INSTALLFLAGS+= -fschg 283.endif 284INSTALLFLAGS+= -S 285.endif 286 287_INSTALLFLAGS:= ${INSTALLFLAGS} 288.for ie in ${INSTALLFLAGS_EDIT} 289_INSTALLFLAGS:= ${_INSTALLFLAGS${ie}} 290.endfor 291 292.if !target(realinstall) && !defined(INTERNALPROG) 293realinstall: _proginstall 294.ORDER: beforeinstall _proginstall 295_proginstall: 296.if defined(PROG) 297 ${INSTALL} ${TAG_ARGS} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \ 298 ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${BINDIR}/${PROGNAME} 299.if ${MK_DEBUG_FILES} != "no" 300.if defined(DEBUGMKDIR) 301 ${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -d ${DESTDIR}${DEBUGFILEDIR}/ 302.endif 303 ${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -o ${BINOWN} -g ${BINGRP} -m ${DEBUGMODE} \ 304 ${PROGNAME}.debug ${DESTDIR}${DEBUGFILEDIR}/${PROGNAME}.debug 305.endif 306.endif 307.endif # !target(realinstall) 308 309.if defined(SCRIPTS) && !empty(SCRIPTS) 310realinstall: _scriptsinstall 311.ORDER: beforeinstall _scriptsinstall 312 313SCRIPTSDIR?= ${BINDIR} 314SCRIPTSOWN?= ${BINOWN} 315SCRIPTSGRP?= ${BINGRP} 316SCRIPTSMODE?= ${BINMODE} 317 318STAGE_AS_SETS+= scripts 319stage_as.scripts: ${SCRIPTS} 320FLAGS.stage_as.scripts= -m ${SCRIPTSMODE} 321STAGE_FILES_DIR.scripts= ${STAGE_OBJTOP} 322.for script in ${SCRIPTS} 323.if defined(SCRIPTSNAME) 324SCRIPTSNAME_${script:T}?= ${SCRIPTSNAME} 325.else 326SCRIPTSNAME_${script:T}?= ${script:T:R} 327.endif 328SCRIPTSDIR_${script:T}?= ${SCRIPTSDIR} 329SCRIPTSOWN_${script:T}?= ${SCRIPTSOWN} 330SCRIPTSGRP_${script:T}?= ${SCRIPTSGRP} 331SCRIPTSMODE_${script:T}?= ${SCRIPTSMODE} 332STAGE_AS_${script:T}= ${SCRIPTSDIR_${script:T}}/${SCRIPTSNAME_${script:T}} 333_scriptsinstall: _SCRIPTSINS_${script:T} 334_SCRIPTSINS_${script:T}: ${script} 335 ${INSTALL} ${TAG_ARGS} -o ${SCRIPTSOWN_${.ALLSRC:T}} \ 336 -g ${SCRIPTSGRP_${.ALLSRC:T}} -m ${SCRIPTSMODE_${.ALLSRC:T}} \ 337 ${.ALLSRC} \ 338 ${DESTDIR}${SCRIPTSDIR_${.ALLSRC:T}}/${SCRIPTSNAME_${.ALLSRC:T}} 339.endfor 340.endif 341 342NLSNAME?= ${PROG} 343.include <bsd.nls.mk> 344 345.include <bsd.confs.mk> 346.include <bsd.files.mk> 347.include <bsd.incs.mk> 348 349LINKOWN?= ${BINOWN} 350LINKGRP?= ${BINGRP} 351LINKMODE?= ${BINMODE} 352.include <bsd.links.mk> 353 354.if ${MK_MAN} != "no" 355realinstall: maninstall 356.ORDER: beforeinstall maninstall 357.endif 358 359.endif # !target(install) 360 361.if ${MK_MAN} != "no" 362.include <bsd.man.mk> 363.endif 364 365.if defined(HAS_TESTS) 366MAKE+= MK_MAKE_CHECK_USE_SANDBOX=yes 367SUBDIR_TARGETS+= check 368TESTS_LD_LIBRARY_PATH+= ${.OBJDIR} 369TESTS_PATH+= ${.OBJDIR} 370.endif 371 372.if defined(PROG) 373OBJS_DEPEND_GUESS+= ${SRCS:M*.h} 374.endif 375 376.include <bsd.dep.mk> 377.include <bsd.clang-analyze.mk> 378.include <bsd.obj.mk> 379.include <bsd.sys.mk> 380