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