#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS 2>/dev/null)
DEB_HOST_ARCH_CPU := $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU 2>/dev/null)

# Calculate arch-specific defaults
DEFAULT_PIE=0
ifneq (,$(findstring :$(DEB_HOST_ARCH_OS):,:linux:knetbsd:))
  # PIE enabled only on linux/knetbsd
  ifeq (,$(findstring :$(DEB_HOST_ARCH_CPU):,:hppa:m68k:arm:))
    # disabled on hppa, m68k (where it doesn't work).
    # disabled for arm since there is no randomization support in ARM kernels
    DEFAULT_PIE=1
  endif
endif
DEFAULT_STACKPROT=1
ifneq (,$(findstring :$(DEB_HOST_ARCH_CPU):,:ia64:alpha:mips:mipsel:hppa:arm:))
  # Stack protector disabled on ia64, alpha, mips, mipsel, hppa.
  #   "warning: -fstack-protector not supported for this target"
  # Stack protector disabled on arm, armel.
  #   compiler supports it incorrectly (leads to SEGV)
  DEFAULT_STACKPROT=0
endif
OPTIMIZE_BY_DEFAULT=0
ifeq ($(DEB_HOST_ARCH),lpia)
    # lpia needs to be compiled to -Os by default
    OPTIMIZE_BY_DEFAULT=1
endif

DEFAULT_OPTIMIZATION_SIZE=0
ifeq ($(DEB_HOST_ARCH),lpia)
    # Only enabled on lpia by default since that targets intel atom chips
    DEFAULT_OPTIMIZATION_SIZE=1
endif

build: build-stamp test
build-stamp:
	# Building
	dh_testdir

	mkdir -p build-tree
	install lpia-cc lpia-ld build-tree

	# Set defaults, based on OS and CPU
	perl -pi -e 's/ #OS#/ '"$(DEB_HOST_ARCH_OS)"'/; s/ #CPU#/ '"$(DEB_HOST_ARCH)"'/;' build-tree/lpia-cc build-tree/lpia-ld
	perl -pi -e "s/default{'DEB_BUILD_OPTIMIZING'}=0;/default{'DEB_BUILD_OPTIMIZING'}=${OPTIMIZE_BY_DEFAULT};/;" build-tree/lpia-cc
	perl -pi -e "s/default{'DEB_BUILD_OPTIMIZING_SIZE'}=1;/default{'DEB_BUILD_OPTIMIZING_SIZE'}=${DEFAULT_OPTIMIZATION_SIZE};/;" build-tree/lpia-cc
	perl -pi -e "s/default{'DEB_BUILD_HARDENING_PIE'}=1;/default{'DEB_BUILD_HARDENING_PIE'}=$(DEFAULT_PIE);/;" build-tree/lpia-cc build-tree/lpia-ld
	perl -pi -e "s/default{'DEB_BUILD_HARDENING_STACKPROTECTOR'}=1;/default{'DEB_BUILD_HARDENING_STACKPROTECTOR'}=$(DEFAULT_STACKPROT);/;" build-tree/lpia-cc build-tree/lpia-ld

	# Duplicate cc wrapper to c++
	cp build-tree/lpia-cc build-tree/lpia-c++
	perl -pi -e 's/lpia-cc/lpia-c++/g; s|/usr/bin/cc|/usr/bin/c++|g;' build-tree/lpia-c++

	# Set up man pages
	ln -sf lpia-cc.1 lpia-wrapper.1
	cp lpia-cc.1 lpia-c++.1
	perl -pi -e 's/lpia-cc/lpia-c++/g; s/gcc/g++/g;' lpia-c++.1

	# Done building
	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	rm -f build-stamp test-stamp
	rm -rf build-tree
	rm -f lpia-c++.1 lpia-wrapper.1
	dh_clean

test: build-stamp test-stamp
test-stamp:
	(cd tests; make check)
	# Done testing
	touch test-stamp

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs usr/bin
	dh_installdirs -A usr/share/lintian/overrides
	install -m644 debian/lintian.overrides debian/lpia-wrapper/usr/share/lintian/overrides/lpia-wrapper
	install build-tree/lpia-cc build-tree/lpia-c++ build-tree/lpia-ld debian/lpia-wrapper/usr/bin
	# Build links
	for ver in 4.1 4.2 4.3 4.4; do dh_link \
		usr/bin/lpia-cc usr/bin/gcc-$$ver \
		usr/bin/lpia-c++ usr/bin/g++-$$ver \
		;\
	done
	dh_link usr/bin/lpia-ld usr/bin/ld


# Build architecture-dependent files here.
binary-arch: build install
#	dh_testversion
	dh_perl
	dh_testdir
	dh_testroot
	dh_installdocs AUTHORS TODO
	dh_installexamples
	dh_installmenu
#	dh_installinit
	dh_installcron
	dh_installman
#	dh_undocumented
	dh_installchangelogs
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
#	dh_makeshlibs
	dh_md5sums
	dh_builddeb

# Build architecture-independent files here.
binary-indep: binary-arch
	# Nothing to do here

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary test
