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.if ${MACHINE_CPUARCH} == "riscv" && ${LINKER_FEATURES:Mriscv-relaxations} == "" 85CFLAGS += -mno-relax 86.endif 87 88.if defined(CRUNCH_CFLAGS) 89CFLAGS+=${CRUNCH_CFLAGS} 90.else 91.if ${MK_DEBUG_FILES} != "no" && empty(DEBUG_FLAGS:M-g) && \ 92 empty(DEBUG_FLAGS:M-gdwarf-*) 93CFLAGS+= ${DEBUG_FILES_CFLAGS} 94CTFFLAGS+= -g 95.endif 96.endif 97 98.if !defined(DEBUG_FLAGS) 99STRIP?= -s 100.endif 101 102.if defined(NO_ROOT) 103.if !defined(TAGS) || ! ${TAGS:Mpackage=*} 104TAGS+= package=${PACKAGE:Uutilities} 105.endif 106TAG_ARGS= -T ${TAGS:[*]:S/ /,/g} 107.endif 108 109.if defined(NO_SHARED) && ${NO_SHARED:tl} != "no" 110LDFLAGS+= -static 111.endif 112 113# clang currently defaults to dynamic TLS for mips64 binaries 114.if ${MACHINE_ARCH:Mmips64*} && ${COMPILER_TYPE} == "clang" 115CFLAGS+= -ftls-model=initial-exec 116.endif 117 118.if ${MK_DEBUG_FILES} != "no" 119PROG_FULL=${PROG}.full 120# Use ${DEBUGDIR} for base system debug files, else .debug subdirectory 121.if defined(BINDIR) && (\ 122 ${BINDIR} == "/bin" ||\ 123 ${BINDIR:C%/libexec(/.*)?%/libexec%} == "/libexec" ||\ 124 ${BINDIR} == "/sbin" ||\ 125 ${BINDIR:C%/usr/(bin|bsdinstall|libexec|lpr|sendmail|sm.bin|sbin|tests)(/.*)?%/usr/bin%} == "/usr/bin" ||\ 126 ${BINDIR} == "/usr/lib" \ 127 ) 128DEBUGFILEDIR= ${DEBUGDIR}${BINDIR} 129.else 130DEBUGFILEDIR?= ${BINDIR}/.debug 131.endif 132.if !exists(${DESTDIR}${DEBUGFILEDIR}) 133DEBUGMKDIR= 134.endif 135.else 136PROG_FULL= ${PROG} 137.endif 138 139.if defined(PROG) 140PROGNAME?= ${PROG} 141 142.if defined(SRCS) 143 144OBJS+= ${SRCS:N*.h:${OBJS_SRCS_FILTER:ts:}:S/$/.o/g} 145 146# LLVM bitcode / textual IR representations of the program 147BCOBJS+=${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.bco/g} 148LLOBJS+=${SRCS:N*.[hsS]:N*.asm:${OBJS_SRCS_FILTER:ts:}:S/$/.llo/g} 149 150.if target(beforelinking) 151beforelinking: ${OBJS} 152${PROG_FULL}: beforelinking 153.endif 154${PROG_FULL}: ${OBJS} 155.if defined(PROG_CXX) 156 ${CXX:N${CCACHE_BIN}} ${CXXFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} \ 157 ${OBJS} ${LDADD} 158.else 159 ${CC:N${CCACHE_BIN}} ${CFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} ${OBJS} \ 160 ${LDADD} 161.endif 162.if ${MK_CTF} != "no" 163 ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS} 164.endif 165 166.else # !defined(SRCS) 167 168.if !target(${PROG}) 169.if defined(PROG_CXX) 170SRCS= ${PROG}.cc 171.else 172SRCS= ${PROG}.c 173.endif 174 175# Always make an intermediate object file because: 176# - it saves time rebuilding when only the library has changed 177# - the name of the object gets put into the executable symbol table instead of 178# the name of a variable temporary object. 179# - it's useful to keep objects around for crunching. 180OBJS+= ${PROG}.o 181BCOBJS+= ${PROG}.bc 182LLOBJS+= ${PROG}.ll 183CLEANFILES+= ${PROG}.o ${PROG}.bc ${PROG}.ll 184 185.if target(beforelinking) 186beforelinking: ${OBJS} 187${PROG_FULL}: beforelinking 188.endif 189${PROG_FULL}: ${OBJS} 190.if defined(PROG_CXX) 191 ${CXX:N${CCACHE_BIN}} ${CXXFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} \ 192 ${OBJS} ${LDADD} 193.else 194 ${CC:N${CCACHE_BIN}} ${CFLAGS:N-M*} ${LDFLAGS} -o ${.TARGET} ${OBJS} \ 195 ${LDADD} 196.endif 197.if ${MK_CTF} != "no" 198 ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${OBJS} 199.endif 200.endif # !target(${PROG}) 201 202.endif # !defined(SRCS) 203 204.if ${MK_DEBUG_FILES} != "no" 205${PROG}: ${PROG_FULL} ${PROGNAME}.debug 206 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${PROGNAME}.debug \ 207 ${PROG_FULL} ${.TARGET} 208 209${PROGNAME}.debug: ${PROG_FULL} 210 ${OBJCOPY} --only-keep-debug ${PROG_FULL} ${.TARGET} 211.endif 212 213.if defined(LLVM_LINK) 214${PROG_FULL}.bc: ${BCOBJS} 215 ${LLVM_LINK} -o ${.TARGET} ${BCOBJS} 216 217${PROG_FULL}.ll: ${LLOBJS} 218 ${LLVM_LINK} -S -o ${.TARGET} ${LLOBJS} 219 220CLEANFILES+= ${PROG_FULL}.bc ${PROG_FULL}.ll 221.endif # defined(LLVM_LINK) 222 223.if ${MK_MAN} != "no" && !defined(MAN) && \ 224 !defined(MAN1) && !defined(MAN2) && !defined(MAN3) && \ 225 !defined(MAN4) && !defined(MAN5) && !defined(MAN6) && \ 226 !defined(MAN7) && !defined(MAN8) && !defined(MAN9) 227MAN= ${PROG}.1 228MAN1= ${MAN} 229.endif 230.endif # defined(PROG) 231 232.if defined(_SKIP_BUILD) 233all: 234.else 235all: ${PROG} ${SCRIPTS} 236.if ${MK_MAN} != "no" 237all: all-man 238.endif 239.endif 240 241.if defined(PROG) 242CLEANFILES+= ${PROG} ${PROG}.bc ${PROG}.ll 243.if ${MK_DEBUG_FILES} != "no" 244CLEANFILES+= ${PROG_FULL} ${PROGNAME}.debug 245.endif 246.endif 247 248.if defined(OBJS) 249CLEANFILES+= ${OBJS} ${BCOBJS} ${LLOBJS} 250.endif 251 252.include <bsd.libnames.mk> 253 254.if defined(PROG) 255.if !defined(NO_EXTRADEPEND) 256_EXTRADEPEND: 257.if defined(LDFLAGS) && !empty(LDFLAGS:M-nostdlib) 258.if defined(DPADD) && !empty(DPADD) 259 echo ${PROG_FULL}: ${DPADD} >> ${DEPENDFILE} 260.endif 261.else 262 echo ${PROG_FULL}: ${LIBC} ${DPADD} >> ${DEPENDFILE} 263.if defined(PROG_CXX) 264 echo ${PROG_FULL}: ${LIBCPLUSPLUS} >> ${DEPENDFILE} 265.endif 266.endif 267.endif # !defined(NO_EXTRADEPEND) 268.endif 269 270.if !target(install) 271 272.if defined(PRECIOUSPROG) 273.if !defined(NO_FSCHG) 274INSTALLFLAGS+= -fschg 275.endif 276INSTALLFLAGS+= -S 277.endif 278 279_INSTALLFLAGS:= ${INSTALLFLAGS} 280.for ie in ${INSTALLFLAGS_EDIT} 281_INSTALLFLAGS:= ${_INSTALLFLAGS${ie}} 282.endfor 283 284.if !target(realinstall) && !defined(INTERNALPROG) 285realinstall: _proginstall 286.ORDER: beforeinstall _proginstall 287_proginstall: 288.if defined(PROG) 289 ${INSTALL} ${TAG_ARGS} ${STRIP} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \ 290 ${_INSTALLFLAGS} ${PROG} ${DESTDIR}${BINDIR}/${PROGNAME} 291.if ${MK_DEBUG_FILES} != "no" 292.if defined(DEBUGMKDIR) 293 ${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -d ${DESTDIR}${DEBUGFILEDIR}/ 294.endif 295 ${INSTALL} ${TAG_ARGS:D${TAG_ARGS},dbg} -o ${BINOWN} -g ${BINGRP} -m ${DEBUGMODE} \ 296 ${PROGNAME}.debug ${DESTDIR}${DEBUGFILEDIR}/${PROGNAME}.debug 297.endif 298.endif 299.endif # !target(realinstall) 300 301.if defined(SCRIPTS) && !empty(SCRIPTS) 302realinstall: _scriptsinstall 303.ORDER: beforeinstall _scriptsinstall 304 305SCRIPTSDIR?= ${BINDIR} 306SCRIPTSOWN?= ${BINOWN} 307SCRIPTSGRP?= ${BINGRP} 308SCRIPTSMODE?= ${BINMODE} 309 310STAGE_AS_SETS+= scripts 311stage_as.scripts: ${SCRIPTS} 312FLAGS.stage_as.scripts= -m ${SCRIPTSMODE} 313STAGE_FILES_DIR.scripts= ${STAGE_OBJTOP} 314.for script in ${SCRIPTS} 315.if defined(SCRIPTSNAME) 316SCRIPTSNAME_${script:T}?= ${SCRIPTSNAME} 317.else 318SCRIPTSNAME_${script:T}?= ${script:T:R} 319.endif 320SCRIPTSDIR_${script:T}?= ${SCRIPTSDIR} 321SCRIPTSOWN_${script:T}?= ${SCRIPTSOWN} 322SCRIPTSGRP_${script:T}?= ${SCRIPTSGRP} 323SCRIPTSMODE_${script:T}?= ${SCRIPTSMODE} 324STAGE_AS_${script:T}= ${SCRIPTSDIR_${script:T}}/${SCRIPTSNAME_${script:T}} 325_scriptsinstall: _SCRIPTSINS_${script:T} 326_SCRIPTSINS_${script:T}: ${script} 327 ${INSTALL} ${TAG_ARGS} -o ${SCRIPTSOWN_${.ALLSRC:T}} \ 328 -g ${SCRIPTSGRP_${.ALLSRC:T}} -m ${SCRIPTSMODE_${.ALLSRC:T}} \ 329 ${.ALLSRC} \ 330 ${DESTDIR}${SCRIPTSDIR_${.ALLSRC:T}}/${SCRIPTSNAME_${.ALLSRC:T}} 331.endfor 332.endif 333 334NLSNAME?= ${PROG} 335.include <bsd.nls.mk> 336 337.include <bsd.confs.mk> 338.include <bsd.files.mk> 339.include <bsd.incs.mk> 340 341LINKOWN?= ${BINOWN} 342LINKGRP?= ${BINGRP} 343LINKMODE?= ${BINMODE} 344.include <bsd.links.mk> 345 346.if ${MK_MAN} != "no" 347realinstall: maninstall 348.ORDER: beforeinstall maninstall 349.endif 350 351.endif # !target(install) 352 353.if ${MK_MAN} != "no" 354.include <bsd.man.mk> 355.endif 356 357.if defined(HAS_TESTS) 358MAKE+= MK_MAKE_CHECK_USE_SANDBOX=yes 359SUBDIR_TARGETS+= check 360TESTS_LD_LIBRARY_PATH+= ${.OBJDIR} 361TESTS_PATH+= ${.OBJDIR} 362.endif 363 364.if defined(PROG) 365OBJS_DEPEND_GUESS+= ${SRCS:M*.h} 366.endif 367 368.include <bsd.dep.mk> 369.include <bsd.clang-analyze.mk> 370.include <bsd.obj.mk> 371.include <bsd.sys.mk> 372