#! /usr/bin/make -f

CFLAGS = -g -O2

nop:
	@:

configure:
	# Generate the main makefile.
	#   * Use (optionally sysroot-prefixed) multiarch library search
	#     directories.
	#     - The library path is given to ld/configure through host_configargs in
	#       ld/Makefile's "configure-host" target.
	#     - If we're building a sysroot ld, search only (native or target-
	#       specific) library paths under the sysroot.
	#     - Otherwise, if we're building a cross ld, search only target-specific
	#       library paths.
	#   * Use shared BFD and opcodes libraries for all utilities.
	#     - That is, dynamically link the utilities against a single copy of the
	#       BFD and opcodes libraries.
	#   * Don't enable multilib (multiarch is better).
	#   * Enable plugins.
	#   * Don't expect to find Gettext.
	for target in $$(cat ../targets); do \
		if [ -n '$(OPK_SYSROOT)' ]; then \
			LIB_PATH="=/usr/local/lib/$${target}:=/usr/local/lib"; \
			LIB_PATH="$${LIB_PATH}:=/lib/$${target}:=/lib"; \
			LIB_PATH="$${LIB_PATH}:=/usr/lib/$${target}:=/usr/lib"; \
		elif [ '$(OPK_HOST_ARCH)' = "$${target}" ]; then \
			LIB_PATH="/usr/local/lib/$${target}:/usr/local/lib"; \
			LIB_PATH="$${LIB_PATH}:/lib/$${target}:/lib"; \
			LIB_PATH="$${LIB_PATH}:/usr/lib/$${target}:/usr/lib"; \
		else \
			LIB_PATH="/usr/local/lib/$${target}"; \
			LIB_PATH="$${LIB_PATH}:/lib/$${target}"; \
			LIB_PATH="$${LIB_PATH}:/usr/lib/$${target}"; \
		fi; \
		CFLAGS='$(CFLAGS)' host_configargs="--with-lib-path=$${LIB_PATH}" \
			oh-autoconfigure -B "build-$${target}" -t "$${target}" -- \
			$$([ -n "$${OPK_SYSROOT}" ] && \
				printf "--with-sysroot='%s'" "$${OPK_SYSROOT}") \
			--program-transform-name="s&^&$${target}-&" \
			--enable-shared --disable-multilib \
			--enable-plugins --disable-nls \
			--with-pkgversion='GNU Binutils for ProteanOS' || \
			exit 1; \
		done
	# Generate all the individual program makefiles.
	for target in $$(cat ../targets); do \
		oh-autobuild -B "build-$${target}" -T configure-host \
			tooldir='/usr/tool' scriptdir="/usr/lib/$${target}" \
			bfdlibdir="/usr/lib/$(OPK_HOST_ARCH)/$${target}" \
			bfdincludedir="/usr/include/$(OPK_HOST_ARCH)/$${target}" || \
			exit 1; \
		done
	touch $@

build: configure
	# Build BFD header files.
	for target in $$(cat ../targets); do \
		oh-autobuild -B "build-$${target}/bfd" -T headers \
			tooldir='/usr/tool' scriptdir="/usr/lib/$${target}" \
			bfdlibdir="/usr/lib/$(OPK_HOST_ARCH)/$${target}" \
			bfdincludedir="/usr/include/$(OPK_HOST_ARCH)/$${target}" || \
			exit 1; \
		done
	# Build ALL the things!
	for target in $$(cat ../targets); do \
		oh-autobuild -B "build-$${target}" CFLAGS='$(CFLAGS)' \
			tooldir='/usr/tool' scriptdir="/usr/lib/$${target}" \
			bfdlibdir="/usr/lib/$(OPK_HOST_ARCH)/$${target}" \
			bfdincludedir="/usr/include/$(OPK_HOST_ARCH)/$${target}" || \
			exit 1; \
		done
	touch $@

install: build
	# Install everything.
	#   * scriptdir contains target ldscripts.
	#   * ldscripts should be independent of the host architecture and therefore
	#     coinstallable with themselves across host architectures (I hope).
	#   * All of these macro settings are effective thanks to 
	#     `patches/01_pass-some-dirs-to-child-makes.patch`.
	for target in $$(cat ../targets); do \
		oh-autoinstall -B "build-$${target}" -d "binutils-$${target}.data" \
			CFLAGS='$(CFLAGS)' \
			tooldir='/usr/tool' scriptdir="/usr/lib/$${target}" \
			bfdlibdir="/usr/lib/$(OPK_HOST_ARCH)/$${target}" \
			bfdincludedir="/usr/include/$(OPK_HOST_ARCH)/$${target}" || \
			exit 1; \
		done
	# Do some cleanup.
	#   * /usr/tool/bin (TOOLBINDIR) contains hard links to tools.  A quick look
	#     at the code suggests we don't need them; ld *should* work without them
	#     in any sane configuration.
	#   * We don't need a static libiberty.
	#   * Remove static library dependency metadata generated by libtool.
	for target in $$(cat ../targets); do \
		rm -Rf "binutils-$${target}.data/usr/tool" && \
		find "binutils-$${target}.data/usr/lib" -name 'libiberty.a' \
			-exec rm -f '{}' ';' && \
		find "binutils-$${target}.data/usr/lib" -name '*.la' \
			-exec rm -f '{}' ';' || \
		exit 1; \
	done
	# Make symbolic links to native utilities.
	for target in $$(cat ../targets); do \
		if [ '$(OPK_HOST_ARCH)' = "$${target}" ]; then \
			for util in "binutils-$${target}.data/usr/bin/$${target}-"*; do \
				ln -sf "$${util##*/}" "$${util%/*}/$${util##*/$${target}-}"; \
			done \
		fi; \
		done
	# Write GNU ld configuration to point to BFD libraries.
	for target in $$(cat ../targets); do \
		ld_so_conf_d="binutils-$${target}.data/etc/ld.so.conf.d" && \
		ld_so_conf="$${ld_so_conf_d}/bfd_$(OPK_HOST_ARCH)_$${target}" && \
		mkdir -p "$${ld_so_conf_d}" && \
		printf '# GNU BFD libraries\n/usr/lib/%s/%s\n' \
			'$(OPK_HOST_ARCH)' "$${target}" \
			>"$${ld_so_conf}"; \
		done
	# Move documentation into binutils-doc data directory.
	mkdir -p 'binutils-doc.data/usr'
	for target in $$(cat ../targets); do \
		if [ ! -d 'binutils-doc.data/usr/share' ]; then \
			mv "binutils-$${target}.data/usr/share" 'binutils-doc.data/usr' || \
				exit 1; \
			for file in 'binutils-doc.data/usr/share/man/man1/'*; do \
				mv "$${file}" "$${file%/*}/$${file##*/$${target}-}" || \
					exit 1; \
			done; \
		else \
			rm -Rf "binutils-$${target}.data/usr/share" || \
				exit 1; \
		fi; \
		done
	# Move BFD development files into libbfd-dev-* data directory.
	for target in $$(cat ../targets); do \
		if [ -d "libbfd-dev-$${target}.data/usr" ]; then \
			rm -Rf "libbfd-dev-$${target}.data/usr" || \
			exit 1; \
		fi; \
		mkdir -p "libbfd-dev-$${target}.data/usr/lib/$(OPK_HOST_ARCH)/$${target}" && \
		mv "binutils-$${target}.data/usr/include" \
			"libbfd-dev-$${target}.data/usr" && \
		mv "binutils-$${target}.data/usr/lib/$(OPK_HOST_ARCH)/$${target}/libbfd.so" \
			"binutils-$${target}.data/usr/lib/$(OPK_HOST_ARCH)/$${target}/libbfd.a" \
			"binutils-$${target}.data/usr/lib/$(OPK_HOST_ARCH)/$${target}/libopcodes.so" \
			"binutils-$${target}.data/usr/lib/$(OPK_HOST_ARCH)/$${target}/libopcodes.a" \
			"libbfd-dev-$${target}.data/usr/lib/$(OPK_HOST_ARCH)/$${target}" || \
		exit 1; \
		done
	# Post-process and install everything.
	for target in $$(cat ../targets); do \
		oh-fixperms -d "binutils-$${target}.data" && \
		oh-strip -d "binutils-$${target}.data" || \
		exit 1; \
		done

clean: clean-build clean-install

clean-build:
	for target in $$(cat ../targets); do \
		oh-autoclean -B "build-$${target}" || \
			exit 1; \
		done
	rm -f configure build

clean-install:
	rm -Rf *.data