11.57Srillig#	$NetBSD: Makefile.gnuhost,v 1.57 2025/05/25 06:06:57 rillig Exp $
21.1Stv#
31.1Stv# Rules used when building a GNU host package.  Expects MODULE to be set.
41.1Stv#
51.1Stv# There's not a lot we can do to build reliably in the face of many
61.1Stv# available configuration options.  To be as low-overhead as possible,
71.1Stv# we follow the following scheme:
81.1Stv#
91.1Stv# * Configuration is only re-run when an autoconf source file (such as
101.1Stv#   "configure" or "config.sub") is changed.
111.1Stv#
121.1Stv# * "config.status" is run to rebuild Makefiles and .h files if an
131.1Stv#   autoconf-parsed file (such as Makefile.in) is changed.
141.1Stv#
151.19Slukem# * If MKUPDATE != "no", "make install" is only run if a build has happened
161.1Stv#   since the last install in the current directory.
171.1Stv
181.1Stv.include <bsd.own.mk>
191.20Sdbj
201.20Sdbj# Disable use of pre-compiled headers on Darwin.
211.36SchristosBUILD_OSTYPE!= uname -s
221.51SmayaHOST_COMPILER_CLANG!= if ${HOST_CC} --version | grep -q -s clang; then echo yes; else echo no; fi
231.50Schristos.if ${HOST_COMPILER_CLANG} == "yes"
241.21SdbjHOST_CFLAGS+=-O2 -no-cpp-precomp
251.46Schristos.endif
261.46Schristos
271.36SchristosMAKE_PROGRAM?=	${MAKE}
281.1Stv
291.44Schristos.for i in 3 2
301.44Schristos.if exists(${.CURDIR}/../../external/gpl${i}/${MODULE}/dist)
311.44SchristosGNUHOSTDIST?=	${.CURDIR}/../../external/gpl${i}/${MODULE}/dist
321.43Schristos.endif
331.44Schristos.endfor
341.1Stv
351.1StvFIND_ARGS+=	\! \( -type d \( \
361.1Stv			-name 'CVS' -o \
371.1Stv			-name 'config' -o \
381.1Stv			-name 'doc' -o \
391.1Stv			-name 'po' -o \
401.8Stv			-name 'nbsd.mt' -o \
411.1Stv			-name 'tests*' \
421.1Stv		\) -prune \)
431.1Stv
441.1Stv# Do this "find" only if actually building something.
451.1Stv.if (${USETOOLS} == "yes") && empty(.MAKEFLAGS:M-V*) && \
461.1Stv    (make(all) || make(realall) || (!make(clean) && !make(cleandir) && !make(obj))) && \
471.1Stv    !defined(_GNU_CFGSRC)
481.1Stv
491.17Slukem_GNU_CFGSRC!=	find ${GNUHOSTDIST} ${FIND_ARGS} \
501.1Stv		-type f \( -name 'config*' -o -name '*.in' \) -print
511.1Stv.MAKEOVERRIDES+= _GNU_CFGSRC
521.1Stv.endif
531.1Stv
541.33SapbCONFIGURE_ENV+= \
551.33Sapb		AR=${HOST_AR:Q} \
561.33Sapb		AWK=${TOOL_AWK:Q} \
571.33Sapb		CC=${HOST_CC:Q} \
581.1Stv		CFLAGS=${HOST_CFLAGS:Q} \
591.42Sdrochner		CONFIG_SHELL=${HOST_SH:Q} \
601.1Stv		CPPFLAGS=${HOST_CPPFLAGS:Q} \
611.1Stv		CXX=${HOST_CXX:Q} \
621.1Stv		CXXFLAGS=${HOST_CXXFLAGS:Q} \
631.9Sperry		INSTALL=${HOST_INSTALL_FILE:Q} \
641.1Stv		LDFLAGS=${HOST_LDFLAGS:Q} \
651.14Stv		LEX=${LEX:Q} \
661.49Smartin		FLEX=${LEX:Q} \
671.41Sapb		M4=${TOOL_M4:Q} \
681.36Schristos		MAKE=${MAKE_PROGRAM:Q} \
691.14Stv		PATH="${TOOLDIR}/bin:$$PATH" \
701.33Sapb		RANLIB=${HOST_RANLIB:Q} \
711.14Stv		YACC=${YACC:Q}
721.1Stv
731.31SjmcBUILD_ENV+=	${CONFIGURE_ENV}
741.31Sjmc
751.25SskrllCONFIGURE_ARGS+=--prefix=${TOOLDIR}
761.26Sskrll.if ${MKPIC} == "no"
771.26SskrllCONFIGURE_ARGS+=--disable-shared
781.26Sskrll.endif
791.1Stv
801.53Slukem.if ${MAKEVERBOSE} == 0
811.53SlukemCONFIGURE_ARGS+=--silent
821.53Slukem.endif
831.53Slukem
841.53Slukem
851.36Schristos.if ${MAKE_PROGRAM} == ${MAKE}
861.2Stv.ifndef _NOWRAPPER
871.18Sthorpej# Some systems have a small ARG_MAX.  On such systems, prevent Make
881.18Sthorpej# variables set on the command line from being exported in the
891.18Sthorpej# environment (they will still be set in MAKEOVERRIDES).
901.18Sthorpej.if ${BUILD_OSTYPE} == "Darwin" || ${BUILD_OSTYPE} == "FreeBSD"
911.18Sthorpej__noenvexport=	-X
921.18Sthorpej.endif
931.18SthorpejMAKE_ARGS:=	${__noenvexport} -f ${.PARSEDIR}/Makefile.gnuwrap ${MAKE_ARGS}
941.2Stv.else
951.2StvMAKE_ARGS+=	_NOWRAPPER=1
961.2Stv.endif
971.36SchristosBUILD_COMMAND=	${BUILD_ENV} ${MAKE} ${MAKE_ARGS}
981.36Schristos.else
991.36Schristos
1001.36Schristos# gmake version of this puts MAKE_ARGS in the environment to be sure that
1011.36Schristos# sub-gmake's get them, otherwise tools/gcc tries to build libgcc and
1021.36Schristos# fails.  it also uses "env -i" to entirely clear out MAKEFLAGS.
1031.56SlukemGMAKE_J_ARGS?=	${MAKEFLAGS:[*]:M*-j*:C/(^|.* )(-j ?[0-9][0-9]*).*/\2/W}
1041.54SrilligBUILD_COMMAND=	/usr/bin/env -i ${BUILD_ENV} ${MAKE_ARGS:N-*} ${TOOL_GMAKE} ${GMAKE_J_ARGS} -e ${MAKE_ARGS}
1051.36Schristos
1061.36Schristos.endif
1071.3Stv
1081.15StvMAKE_ARGS+=	BISON=true DESTDIR= INSTALL=${HOST_INSTALL_FILE:Q}
1091.2Stv
1101.52Slukem.if ${MAKEVERBOSE} < 2
1111.54SrilligMAKE_ARGS+=	-s V=0
1121.55Slukem.else
1131.55SlukemMAKE_ARGS+=	V=1
1141.52Slukem.endif
1151.52Slukem
1161.1StvALL_TARGET?=	all
1171.1StvINSTALL_TARGET?=install
1181.1Stv
1191.38StsutsuiBUILD_PLATFORM!= uname -srm | tr ' ()' '-'
1201.36SchristosCONFIGURE_PLATFORM!= if [ -s .configure_done ]; then cat .configure_done; else echo none; fi
1211.36Schristos.if "${BUILD_PLATFORM}" != "${CONFIGURE_PLATFORM}"
1221.36Schristosconfigure_cleanup:
1231.36Schristos	@mkdir build 2>/dev/null || true
1241.36Schristos	@(echo "Cleaning stale cache files ${BUILD_PLATFORM} != ${CONFIGURE_PLATFORM}")
1251.39Shans	@(cd build && find . -name config.cache | xargs rm -f)
1261.36Schristosconfigure_cleanup=configure_cleanup
1271.36Schristos.endif
1281.36Schristos
1291.36Schristos.configure_done: ${_GNU_CFGSRC} ${.CURDIR}/Makefile ${configure_cleanup}
1301.1Stv	@mkdir build 2>/dev/null || true
1311.32Sjmc	@(cd build && ${CONFIGURE_ENV} ${HOST_SH} ${GNUHOSTDIST}/configure ${CONFIGURE_ARGS})
1321.36Schristos	@echo ${BUILD_PLATFORM} > $@
1331.1Stv
1341.12Stv# The .build_done timestamp is only updated if a file actually changes
1351.12Stv# in the build tree during "make all".  This way, if nothing has changed,
1361.19Slukem# a "make install MKUPDATE=yes" will do nothing.
1371.12Stv
1381.1Stv.build_done: .configure_done
1391.57Srillig	@${:D make}(cd build && ${BUILD_COMMAND} ${ALL_TARGET})
1401.15Stv	@if [ ! -f $@ ] || [ -n "$$(find build -type f -newer .build_done -print)" ]; \
1411.13Stv		then touch $@; fi
1421.1Stv
1431.27Sjmc.install_done! ${BUILD:D.build_done}
1441.57Srillig	@${:D make}(cd ${.OBJDIR}/build && ${BUILD_COMMAND} ${INSTALL_TARGET})
1451.1Stv	@touch $@
1461.12Stv
1471.1Stv# Mapping to standard targets.
1481.1Stv
1491.1Stv.if ${USETOOLS} == "yes"
1501.1Stvrealall: .build_done
1511.1Stvrealinstall: .install_done
1521.1Stv.endif
1531.1Stv
1541.1Stvclean: clean.gnu
1551.1Stvclean.gnu:
1561.29Sjmc	-rm -r -f .*_done build
1571.1Stv
1581.1Stv.include <bsd.hostprog.mk>
159