#!/usr/bin/make -f
# export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS=hardening=-fortify,-stackprotector

TARGET=arm-none-eabi

MULTILIB_LIST="--with-multilib-list=rmprofile"

GCC_PACKAGE=gcc-arm-none-eabi

include /usr/share/dpkg/pkg-info.mk

SVERSION := $(shell dpkg-query -W -f="\$${Version}\n" $(GCC_PACKAGE)-source)
DVERSION := $(SVERSION)+$(DEB_VERSION)
UVERSION := $(shell echo $(DVERSION) | cut -d- -f1)
BASE_VERSION := $(shell echo $(DVERSION) | sed -e 's/\([1-9]\.[0-9]\).*-.*/\1/')

TOP_DIR=$(shell pwd)
LIBSTDCXX_SDIR=$(TOP_DIR)/src/libstdc++-v3
STAMP_DIR=$(TOP_DIR)/debian/stamp
UNPACK=$(STAMP_DIR)/unpack
APPY_PATCHES=$(STAMP_DIR)/patches
BUILD_DIR=$(TOP_DIR)/build/libstdc++
BUILD_NANO_DIR=$(TOP_DIR)/build_nano/libstdc++
BUILD_PICOLIBC_DIR=$(TOP_DIR)/build_picolibc/libstdc++
BUILD_PICOLIBC_RELEASE_DIR=$(TOP_DIR)/build_picolibc_release/libstdc++
PNEWLIB=libstdc\+\+-arm-none-eabi-newlib
PPICOLIBC=libstdc\+\+-arm-none-eabi-picolibc

CFLAGS := -ffile-prefix-map=$(TOP_DIR)=. -Wformat -Werror=format-security
CXXFLAGS := $(CFLAGS)

BUILDFLAGS=CFLAGS="$(CFLAGS) -g -O2 -ffunction-sections -fdata-sections" CXXFLAGS="$(CXXFLAGS) -g -O2 -ffunction-sections -fdata-sections" LDFLAGS="$(LDFLAGS)"
BUILDFLAGS_NANO=CFLAGS="$(CFLAGS) -g -Os -ffunction-sections -fdata-sections -fno-exceptions" CXXFLAGS="$(CXXFLAGS) -g -Os -ffunction-sections -fdata-sections -fno-exceptions" LDFLAGS="$(LDFLAGS)"
BUILDFLAGS_PICOLIBC=CFLAGS="--specs=picolibc.specs $(CFLAGS) -g -Os -ffunction-sections -fdata-sections" CXXFLAGS="--specs=picolibcpp.specs $(CXXFLAGS) -g -Os -ffunction-sections -fdata-sections" LDFLAGS="--specs=picolibc.specs $(LDFLAGS)"
BUILDFLAGS_PICOLIBC_RELEASE=CFLAGS="--specs=picolibc.specs --picolibc-buildtype=release $(CFLAGS) -g -O3 -ffunction-sections -fdata-sections" CXXFLAGS="--specs=picolibcpp.specs --picolibc-buildtype=release $(CXXFLAGS) -g -Os -ffunction-sections -fdata-sections" LDFLAGS="--specs=picolibc.specs --picolibc-buildtype=release $(LDFLAGS)"

TARGET_TOOLS=\
	AR_FOR_TARGET=$(TARGET)-ar \
	AS_FOR_TARGET=$(TARGET)-as \
	LD_FOR_TARGET=$(TARGET)-ld \
	NM_FOR_TARGET=$(TARGET)-nm \
	OBJDUMP_FOR_TARGET=$(TARGET)-objdump \
	RANLIB_FOR_TARGET=$(TARGET)-ranlib \
	READELF_FOR_TARGET=$(TARGET)-readelf \
	STRIP_FOR_TARGET=$(TARGET)-strip

CONFIGURE_FLAGS = \
	--with-cross-host=$(DEB_HOST_GNU_TYPE) \
	--with-target-subdir="." \
	--enable-multilib \
	--disable-decimal-float \
	--disable-libffi \
	--disable-libgomp \
	--disable-libmudflap \
	--disable-libquadmath \
	--disable-libssp \
	--disable-libstdcxx-pch \
	--disable-nls \
	--disable-shared \
	--disable-threads \
	--enable-tls \
	--disable-plugin \
	--disable-libstdcxx-verbose \
	--mandir=/usr/share/man \
	--with-system-zlib \
	--with-gnu-as \
	--with-gnu-ld \
	--with-newlib \
	--with-headers=yes \
	"--with-pkgversion=$(DVERSION)" \
	--without-included-gettext \
	--with-host-libstdcxx="-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm" \
	--enable-languages=c,c++ \
	--disable-option-checking \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--target=$(TARGET) \
	--host=$(TARGET) \
	$(MULTILIB_LIST) \
	$(TARGET_TOOLS)

CONFIGURE_FLAGS_PICOLIBC= \
	--enable-cstdio=stdio_pure \
	--disable-wchar_t \

%:
	dh $@

$(UNPACK):
	mkdir -p $(STAMP_DIR)
	mkdir -p $(TOP_DIR)/src
	tar xf /usr/src/gcc-arm-none-eabi-source.tar.* -C $(TOP_DIR)/src
	touch $@

override_dh_auto_configure: $(UNPACK)
	mkdir -p $(BUILD_DIR) $(BUILD_NANO_DIR)
	dh_auto_configure -D$(LIBSTDCXX_SDIR) -B$(BUILD_DIR) -- $(CONFIGURE_FLAGS) $(BUILDFLAGS)
	dh_auto_configure -D$(LIBSTDCXX_SDIR) -B$(BUILD_NANO_DIR) -- $(CONFIGURE_FLAGS) $(BUILDFLAGS_NANO)
	dh_auto_configure -D$(LIBSTDCXX_SDIR) -B$(BUILD_PICOLIBC_DIR) -- $(CONFIGURE_FLAGS) $(CONFIGURE_FLAGS_PICOLIBC) $(BUILDFLAGS_PICOLIBC)
	dh_auto_configure -D$(LIBSTDCXX_SDIR) -B$(BUILD_PICOLIBC_RELEASE_DIR) -- $(CONFIGURE_FLAGS) $(CONFIGURE_FLAGS_PICOLIBC) $(BUILDFLAGS_PICOLIBC_RELEASE)

override_dh_auto_clean:
	rm -rf src build* debian/*tmp* $(STAMP_DIR)

override_dh_auto_test:
	@echo "no testing, that's way too painful"

override_dh_gencontrol:
	dh_gencontrol -- -v$(DVERSION) -Vlocal:Version=$(UVERSION) -Vgcc:Version=$(SVERSION)

override_dh_auto_build:
	dh_auto_build -B $(BUILD_DIR)
	dh_auto_build -B $(BUILD_NANO_DIR)
	dh_auto_build -B $(BUILD_PICOLIBC_DIR)
	dh_auto_build -B $(BUILD_PICOLIBC_RELEASE_DIR)

override_dh_auto_install:
	dh_auto_install -B $(BUILD_DIR) --destdir debian/tmp
	dh_auto_install -B $(BUILD_NANO_DIR) --destdir debian/tmp_nano
	dh_auto_install -B $(BUILD_PICOLIBC_DIR) --destdir debian/tmp/picolibc
	dh_auto_install -B $(BUILD_PICOLIBC_RELEASE_DIR) --destdir debian/tmp/picolibc-release
	find debian/tmp_nano \
		-name "*.a" \
		-exec rename 's@debian/tmp_nano/(.*).a@debian/tmp/$$1_nano.a@' \{\} \;
	find debian/tmp -name "*.la" -exec rm -f {} +

override_dh_install:
	dh_install

override_dh_strip:
	dh_strip -X.a