# $FreeWRT$
#-
# This file is part of the FreeWRT project. FreeWRT is copyrighted
# material, please see the LICENCE file in the top-level directory
# or at http://www.freewrt.org/licence for details.

GMAKE?=		$(PWD)/lbin/gmake
GMAKE_FMK=	${GMAKE} -f $(PWD)/mk/build.mk
GMAKE_INV=	${GMAKE_FMK} V=99

all verbose: .prereq_done
	@${GMAKE_INV} all

v: .prereq_done
	@(echo; echo "Build started on $$(LC_ALL=C LANGUAGE=C TZ=UTC date)"; \
	    set -x; ${GMAKE_FMK} V=99 all) 2>&1 | tee -a make.log

# this is not to be called by the user
v_q: .prereq_done
	@(echo; echo "Build started on $$(LC_ALL=C LANGUAGE=C TZ=UTC date)"; \
	    set -x; ${GMAKE_FMK} V=99 all) >>make.log 2>&1

slow:
	@(nohup make v_q 2>&- & while sleep 2; do tail -n 3 make.log; done; :)

help:
	@echo 'Cleaning targets:'
	@echo '  clean        - Remove bin and build_dir directories'
	@echo '  cleandir     - Same as "clean", but also remove built toolchain'
	@echo '  distclean    - Same as "cleandir", but also remove downloaded'
	@echo '                 distfiles and .config'
	@echo ''
	@echo 'Configuration targets:'
	@echo '  config       - Update current config utilising a line-oriented program'
	@echo '  menuconfig   - Update current config utilising a menu based program'
	@echo '                 (default when .config does not exist)'
	@echo '  oldconfig    - Update current config utilising a provided .configs base'
	@echo '  wconfig      - Same as "oldconfig", but also writes out hidden symbols'
	@echo '  allmodconfig - New config selecting modules when possible'
	@echo '  allnoconfig  - New config where all options are answered with no'
	@echo ''
	@echo 'Help targets:'
	@echo '  help         - Print this help text'
	@echo '  pkg-help     - Print help about selectively compiling single packages'
	@echo '  dev-help     - Print help for developers / package maintainers'
	@echo ''
	@echo 'Other generic targets:'
	@echo '  all          - Build everything as specified in .config'
	@echo '                 (default if .config exists)'
	@echo '  v            - Same as "all" but with logging to make.log enabled'
	@echo '  slow         - Same, but with sparse output for slow serial lines'
	@echo '  targz|tarbz2 - Create a distribution tarball (runs distclean)'

pkg-help:
	@echo 'Package specific targets (use with "package=<pkg-name>" parameter):'
	@echo '  fetch        - Download the necessary distfile'
	@echo '  extract      - Same as "fetch", but also extract the distfile'
	@echo '  patch        - Same as "extract", but also patch the source'
	@echo '  build        - Same as "patch", but also build the binaries'
	@echo '  fake         - Same as "build", but also install the binaries'
	@echo '  package      - Same as "fake", but also create the ipkg package'
	@echo '  clean        - Deinstall and remove the build area'
	@echo '  distclean    - Same as "clean", but also remove the distfiles'
	@echo ''
	@echo 'Short package rebuilding guide:'
	@echo '  run "make package=<pkgname> clean" to remove all generated binaries'
	@echo '  run "make package=<pkgname> package" to build everything and create the ipkg'
	@echo ''
	@echo 'This does not automatically resolve package dependencies!'

dev-help:
	@echo 'Fast way of updating package patches:'
	@echo '  run "make package=<pkgname> clean" to start with a good base'
	@echo '  run "make package=<pkgname> patch" to fetch, unpack and patch the source'
	@echo '  edit the package sources at build_dir/w-<pkgname>-*/<pkgname>-<version>'
	@echo '  run "make package=<pkgname> update-patches to regenerate patch files'
	@echo ''
	@echo 'All changed patches will be opened with your $$EDITOR,'
	@echo 'so you can add a description and verify the modifications.'

clean: .prereq_done
	-rm -f nohup.out
	@${GMAKE_INV} clean

config: .prereq_done
	@${GMAKE_INV} _config W=

oldconfig: .prereq_done
	@${GMAKE_INV} _config W=-o

wconfig: .prereq_done
	@${GMAKE_INV} _config W=-A

cleandir: .prereq_done
	@${GMAKE_INV} cleandir
	@-rm -rf lbin
	@-rm -f make.log .prereq_done

distclean: .prereq_done
	@${GMAKE_INV} distclean
	@-rm -rf lbin
	@-rm -f make.log .prereq_done _pax* freewrt.tar.*

image_clean: .prereq_done
	@${GMAKE_INV} image_clean

menuconfig: .prereq_done
	@${GMAKE_INV} menuconfig

allnoconfig: .prereq_done
	@${GMAKE_INV} _config W=-n

allmodconfig: .prereq_done
	@${GMAKE_INV} _mconfig W=-o RCONFIG=Config.in

package_index: .prereq_done
	@${GMAKE_INV} package_index

world: .prereq_done
	@${GMAKE_INV} world

prereq:
	@rm -f .prereq_done
	@${MAKE} .prereq_done

prereq-noerror:
	@rm -f .prereq_done
	@${MAKE} .prereq_done NO_ERROR=1

NO_ERROR=0
.prereq_done:
	@-rm -rf .prereq_done lbin
	@if ! bash --version 2>&1 | fgrep 'GNU bash' >/dev/null 2>&1; then \
		echo "FreeWRT requires GNU bash to be installed, sorry."; \
		exit 1; \
	fi
	@mkdir lbin
	@if which nonexistent 2>&1 | grep -q '^no '; then \
		cp scripts/which.mac lbin/which; \
		chmod 555 lbin/which; \
	else \
		ln -s $$(which which) lbin/which; \
	fi
	@if ! lbin/which md5sum >/dev/null 2>&1; then \
		cp scripts/md5sum.bsd lbin/md5sum; \
		chmod 555 lbin/md5sum; \
	fi
	@echo "TOPDIR:=$$(readlink -nf . 2>/dev/null || pwd -P)" >lbin/prereq.mk
	@echo "BASH:=$$(lbin/which bash)" >>lbin/prereq.mk
	@echo 'GMAKE:=$${TOPDIR}/lbin/gmake' >>lbin/prereq.mk
	@echo "GNU_HOST_NAME:=$$(${CC} -dumpmachine | sed \
	    -e 's!mirbsd!openbsd!g' \
	    )" >>lbin/prereq.mk
	@echo "HOST_ARCH:=$$(${CC} -dumpmachine | sed -e s'/-.*//' \
	    -e 's/sparc.*/sparc/' \
	    -e 's/arm.*/arm/g' \
	    -e 's/m68k.*/m68k/' \
	    -e 's/ppc/powerpc/g' \
	    -e 's/v850.*/v850/g' \
	    -e 's/sh[234]/sh/' \
	    -e 's/mips-.*/mips/' \
	    -e 's/mipsel-.*/mipsel/' \
	    -e 's/cris.*/cris/' \
	    -e 's/i[3-9]86/i386/' \
	    )" >>lbin/prereq.mk
	@echo 'HOSTCC:=${CC}' >>lbin/prereq.mk
	@x="$$(echo 'f:\n\techo P:$${CFLAGS}\n.include <bsd.prog.mk>' | \
	    ${MAKE} -f - f EXPERIMENTAL=yes 2>/dev/null | grep '^P:' \
	    2>/dev/null | sed 's/^P://')"; y='${CFLAGS}'; echo \
	    "HOSTCFLAGS:=$${x:-$${y:--O2}}" | sed 's/ *$$//' >>lbin/prereq.mk
	@echo 'LANGUAGE:=C' >>lbin/prereq.mk
	@echo 'LC_ALL:=C' >>lbin/prereq.mk
	@echo 'MAKE:=$${GMAKE}' >>lbin/prereq.mk
	@echo "OStype:=$$(env NOFAKE=yes uname)" >>lbin/prereq.mk
	@echo "_PATH:=$$PATH" >>lbin/prereq.mk
	@echo "PATH:=\$${TOPDIR}/lbin:$$PATH" >>lbin/prereq.mk
	@echo "SHELL:=$$(lbin/which bash)" >>lbin/prereq.mk
	@env NO_ERROR=${NO_ERROR} BASH="$$(lbin/which bash)" \
	    CC='${CC}' CPPFLAGS='${CPPFLAGS}' \
	    bash ${_PX} scripts/scan-tools.sh
	@TOPDIR=$$(readlink -nf . 2>/dev/null || pwd -P); \
	    printf '%s\n%s\nwq\n' ",g#$$TOPDIR#s##\$${TOPDIR}#g" \
	    "1s#^.*\$$#TOPDIR:=$$TOPDIR#" | ed -s lbin/prereq.mk
	@cc='${CC}'; if test x"$$cc" = x"gcc"; then \
		ln -s $$(lbin/which "$$cc") lbin/gcc; \
	else \
		( echo "#!$$(lbin/which bash)"; \
		  echo "export PATH=$$PATH"; \
		  echo "exec $$cc" '"$$@"' ) >lbin/gcc; \
		chmod 555 lbin/gcc; \
	fi
	@${GMAKE_INV} tools/install-lbin
	@echo '===> Prerequisites checked successfully.'
	@touch $@

_tarball: .prereq_done
	rm -f _pax*
	${GMAKE_INV} tools/paxmirabilis-compile
	install -c -m 555 tools_build/paxmirabilis/pax _pax
	${GMAKE_INV} distclean
	-rm -rf lbin
	-rm -f make.log .prereq_done freewrt.tar.*
	find . | \
	    grep -v -e '^.$$' -e '^./_pax' -e '^./prefs\.mk$$' -e '/\.svn' | \
	    sed -e 's#^\./##' | \
	    sort >_pax.flst
	echo 'package/base-files/.svn/entries' >>_pax.flst

targz: _tarball
	./_pax -w -b 512 -d -M dist -P -t -v -x ustar <_pax.flst | \
	    gzip -n9 >freewrt.tar.gz
	rm -f _pax*

tarbgz2: _tarball
	./_pax -w -b 512 -d -M dist -P -t -v -x ustar <_pax.flst | \
	    bzip2 --best >freewrt.tar.bz2
	rm -f _pax*

.PHONY: prereq prereq-noerror _tarball targz tarbz2
