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