#!/usr/bin/make -f
# -*- makefile -*-
# Build mingw-w64.
# Copyright © 2010 Stephen Kitt <steve@sk2.org>

SHELL=/bin/bash

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

top_dir := $(shell pwd)
build_dir := $(top_dir)/build
stampdir := $(top_dir)/stamp
deb_version := $(shell dpkg-parsechangelog | sed -ne "s/^Version: \(.*\)/\1/p")
deb_upstream_version := $(shell echo $(deb_version) | cut -d- -f1)
gnu_upstream_version := $(shell echo $(deb_upstream_version) | cut -d. -f1-3)

# Build the full MinGW-w64 development package.
package := mingw-w64-dev
subprojects := headers crt

targets := i686-w64-mingw32 x86_64-w64-mingw32

CFLAGS = -Wall -g
# TODO Handle install with nostrip?

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -O0
else
    CFLAGS += -O2
endif

#number of jobs to run for build
ifeq ($(USE_NJOBS),no)
  NJOBS :=
  USE_CPUS := 1
else
  MEM_PER_CPU = 128
  NUM_CPUS := $(shell if echo $(USE_NJOBS) | grep -q -E '^[0-9]+$$'; \
                        then echo $(USE_NJOBS); \
                        else getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1; fi)
  USE_CPUS := $(shell mt=`awk '/^MemTotal/ { print $$2 }' /proc/meminfo`; \
                        awk -vn=$(NUM_CPUS) -vmt=$$mt -vm=$(MEM_PER_CPU) \
                                'END { mt/=1024; n2 = int(mt/m); print n==1 ? 1 : n2<n+1 ? n2 : n+1}' < /dev/null)
  ifneq (,$(strip $(USE_CPUS)))
    NJOBS := -j $(USE_CPUS)
  endif
endif

# Support parallel=<n> in DEB_BUILD_OPTIONS (see #209008)
ifneq (,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
  NJOBS := -j $(subst parallel=,,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
endif

clean:
	dh_testdir
	dh_testroot
	dh_clean
	rm -rf $(stampdir) $(build_dir) $(upstream_dir) autotools_files series

CONFFLAGS = --prefix=/usr
CFLAGS += -I$(top_dir)/mingw-w64-headers/include

configure-stamp: $(patsubst %,configure-stamp-%,$(subprojects))
	touch $@

configure-stamp-headers:
	dh_testdir
	for target in $(targets); do \
		mkdir -p $(build_dir)/$$target/headers && \
		cd $(build_dir)/$$target/headers && \
		$(top_dir)/mingw-w64-headers/configure $(CONFFLAGS) --host=$$target --with-sdk=all; \
	done
	touch $@

configure-stamp-crt:
	dh_testdir
	for target in $(targets); do \
		mkdir -p $(build_dir)/$$target/crt && \
		cd $(build_dir)/$$target/crt && \
		$(top_dir)/mingw-w64-crt/configure $(CONFFLAGS) --host=$$target $$(if [ "$${target/-w64-mingw32/}" == "i686" ]; then echo --enable-lib32 --disable-lib64; else echo --disable-lib32 --enable-lib64; fi); \
	done
	touch $@

build: build-indep
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir
	for target in $(targets); do \
		for subproject in $(subprojects); do \
			cd $(build_dir)/$$target/$$subproject && \
			$(MAKE) $(NJOBS); \
		done; \
	done
	touch $@

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	for target in $(targets); do \
		for subproject in $(subprojects); do \
			mkdir -p $(build_dir)/$$target/$$subproject && \
			cd $(build_dir)/$$target/$$subproject && \
			$(MAKE) install DESTDIR=$(top_dir)/debian/$(package); \
		done; \
	done
	rmdir $(top_dir)/debian/$(package)/usr/include
	touch $@

binary-indep: build-indep install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installman
	dh_installchangelogs
	dh_lintian
	dh_strip -Xw64-mingw32/lib
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch:

binary: binary-indep

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