1#	$NetBSD$
2#
3# Rules used when building a GNU host package.  Expects MODULE to be set.
4#
5# There's not a lot we can do to build reliably in the face of many
6# available configuration options.  To be as low-overhead as possible,
7# we follow the following scheme:
8#
9# * Configuration is only re-run when an autoconf source file (such as
10#   "configure" or "config.sub") is changed.
11#
12# * "config.status" is run to rebuild Makefiles and .h files if an
13#   autoconf-parsed file (such as Makefile.in) is changed.
14#
15# * If MKUPDATE != "no", "make install" is only run if a build has happened
16#   since the last install in the current directory.
17
18.include <bsd.own.mk>
19
20# Disable use of pre-compiled headers on Darwin.
21BUILD_OSTYPE!= uname -s
22.if ${BUILD_OSTYPE} == "Darwin"
23HOST_CFLAGS+=-O2 -no-cpp-precomp
24.endif
25MAKE_PROGRAM?=	${MAKE}
26
27GNUHOSTDIST?=	${.CURDIR}/../../gnu/dist/${MODULE}
28
29FIND_ARGS+=	\! \( -type d \( \
30			-name 'CVS' -o \
31			-name 'config' -o \
32			-name 'doc' -o \
33			-name 'po' -o \
34			-name 'nbsd.mt' -o \
35			-name 'tests*' \
36		\) -prune \)
37
38# Do this "find" only if actually building something.
39.if (${USETOOLS} == "yes") && empty(.MAKEFLAGS:M-V*) && \
40    (make(all) || make(realall) || (!make(clean) && !make(cleandir) && !make(obj))) && \
41    !defined(_GNU_CFGSRC)
42
43_GNU_CFGSRC!=	find ${GNUHOSTDIST} ${FIND_ARGS} \
44		-type f \( -name 'config*' -o -name '*.in' \) -print
45.MAKEOVERRIDES+= _GNU_CFGSRC
46.endif
47
48CONFIGURE_ENV+= \
49		AR=${HOST_AR:Q} \
50		AWK=${TOOL_AWK:Q} \
51		CC=${HOST_CC:Q} \
52		CFLAGS=${HOST_CFLAGS:Q} \
53		CPPFLAGS=${HOST_CPPFLAGS:Q} \
54		CXX=${HOST_CXX:Q} \
55		CXXFLAGS=${HOST_CXXFLAGS:Q} \
56		INSTALL=${HOST_INSTALL_FILE:Q} \
57		LDFLAGS=${HOST_LDFLAGS:Q} \
58		LEX=${LEX:Q} \
59		M4=${TOOL_M4:Q} \
60		MAKE=${MAKE_PROGRAM:Q} \
61		PATH="${TOOLDIR}/bin:$$PATH" \
62		RANLIB=${HOST_RANLIB:Q} \
63		YACC=${YACC:Q}
64
65BUILD_ENV+=	${CONFIGURE_ENV}
66
67CONFIGURE_ARGS+=--prefix=${TOOLDIR}
68.if ${MKPIC} == "no"
69CONFIGURE_ARGS+=--disable-shared
70.endif
71
72.if ${MAKE_PROGRAM} == ${MAKE}
73.ifndef _NOWRAPPER
74# Some systems have a small ARG_MAX.  On such systems, prevent Make
75# variables set on the command line from being exported in the
76# environment (they will still be set in MAKEOVERRIDES).
77.if ${BUILD_OSTYPE} == "Darwin" || ${BUILD_OSTYPE} == "FreeBSD"
78__noenvexport=	-X
79.endif
80MAKE_ARGS:=	${__noenvexport} -f ${.PARSEDIR}/Makefile.gnuwrap ${MAKE_ARGS}
81.else
82MAKE_ARGS+=	_NOWRAPPER=1
83.endif
84BUILD_COMMAND=	${BUILD_ENV} ${MAKE} ${MAKE_ARGS}
85.else
86
87# gmake version of this puts MAKE_ARGS in the environment to be sure that
88# sub-gmake's get them, otherwise tools/gcc tries to build libgcc and
89# fails.  it also uses "env -i" to entirely clear out MAKEFLAGS.
90GMAKE_J_ARGS?=	${MAKEFLAGS:[*]:M*-j*:C/.*(-j ?[0-9]*).*/\1/W}
91BUILD_COMMAND=	/usr/bin/env -i ${BUILD_ENV} ${MAKE_ARGS} ${TOOL_GMAKE} ${GMAKE_J_ARGS} -e ${MAKE_ARGS}
92
93.endif
94
95MAKE_ARGS+=	BISON=true DESTDIR= INSTALL=${HOST_INSTALL_FILE:Q}
96
97ALL_TARGET?=	all
98INSTALL_TARGET?=install
99
100BUILD_PLATFORM!= uname -srm | tr ' ()' '-'
101CONFIGURE_PLATFORM!= if [ -s .configure_done ]; then cat .configure_done; else echo none; fi
102.if "${BUILD_PLATFORM}" != "${CONFIGURE_PLATFORM}"
103configure_cleanup:
104	@mkdir build 2>/dev/null || true
105	@(echo "Cleaning stale cache files ${BUILD_PLATFORM} != ${CONFIGURE_PLATFORM}")
106	@(cd build && find . -name config.cache -print0 | xargs -0 rm -f)
107configure_cleanup=configure_cleanup
108.endif
109
110.configure_done: ${_GNU_CFGSRC} ${.CURDIR}/Makefile ${configure_cleanup}
111	@mkdir build 2>/dev/null || true
112	@(cd build && ${CONFIGURE_ENV} ${HOST_SH} ${GNUHOSTDIST}/configure ${CONFIGURE_ARGS})
113	@echo ${BUILD_PLATFORM} > $@
114
115# The .build_done timestamp is only updated if a file actually changes
116# in the build tree during "make all".  This way, if nothing has changed,
117# a "make install MKUPDATE=yes" will do nothing.
118
119.build_done: .configure_done
120	@(cd build && ${BUILD_COMMAND} ${ALL_TARGET})
121	@if [ ! -f $@ ] || [ -n "$$(find build -type f -newer .build_done -print)" ]; \
122		then touch $@; fi
123
124.install_done! ${BUILD:D.build_done}
125	@(cd ${.OBJDIR}/build && ${BUILD_COMMAND} ${INSTALL_TARGET})
126	@touch $@
127
128# Mapping to standard targets.
129
130.if ${USETOOLS} == "yes"
131realall: .build_done
132realinstall: .install_done
133.endif
134
135clean: clean.gnu
136clean.gnu:
137	-rm -r -f .*_done build
138
139.include <bsd.hostprog.mk>
140