#!/usr/bin/make -f
SHELL = /bin/sh -e

# get DEB_VERSION
include /usr/share/dpkg/pkg-info.mk
# get DEB_HOST_ARCH DEB_HOST_ARCH_OS DEB_HOST_GNU_TYPE DEB_HOST_MULTIARCH DEB_BUILD_GNU_TYPE
include /usr/share/dpkg/architecture.mk
# get CFLAGS LDFLAGS etc
include /usr/share/dpkg/buildflags.mk

ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
VENDOR := UBUNTU
DEB_BUILD_PARALLEL = yes
else
VENDOR := DEBIAN
endif

# support parallel build using DEB_BUILD_OPTIONS=parallel=N
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  MAKEFLAGS += -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# verbose build
V ?= 1

# list of packages we're supposed to build
BUILD_PACKAGES = $(call dpkg_late_eval,BUILD_PACKAGES,dh_listpackages)

enable_system = $(if $(filter qemu-system,${BUILD_PACKAGES}),enable,disable)
enable_linux_user = $(if $(filter qemu-user,${BUILD_PACKAGES}),enable,disable)

FIRMWAREPATH = /usr/share/qemu:/usr/share/seabios:/usr/lib/ipxe/qemu

S390X_CROSSPFX = s390x-linux-gnu-
ALPHAEV67_CROSSPFX = alpha-linux-gnu-

# we add another set of configure options from debian/control
common_configure_opts = \
	--with-pkgversion="Debian $(DEB_VERSION)" \
	--extra-cflags="$(CFLAGS) $(CPPFLAGS)" --extra-ldflags="$(LDFLAGS) -Wl,--as-needed" \
	--prefix=/usr \
	--sysconfdir=/etc \
	--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	--libexecdir=/usr/lib/qemu \
	--firmwarepath=${FIRMWAREPATH} \
	--localstatedir=/var \
	--disable-blobs \
	--disable-strip \
	--interp-prefix=/etc/qemu-binfmt/%M \
	--localstatedir=/var \

# Cross compiling support
ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
common_configure_opts  += --cross-prefix=$(DEB_HOST_GNU_TYPE)-
endif

ifeq (${enable_system},enable)

# list of system (softmmu) targets, from ./configure
system_targets = \
 i386 x86_64 alpha aarch64 arm cris lm32 hppa m68k microblaze microblazeel \
 mips mipsel mips64 mips64el moxie nios2 or1k ppc ppc64 riscv32 riscv64 \
 sh4 sh4eb sparc sparc64 s390x tricore xtensa xtensaeb unicore32

# qemu-system subpackages, from d/control
sys_systems = arm mips ppc sparc x86
systems = ${sys_systems} misc
sysarch_arm   = $(filter aarch64 arm,${system_targets})
sysarch_mips  = $(filter mips mipsel mips64 mips64el,${system_targets})
sysarch_ppc   = $(filter ppc ppc64,${system_targets})
sysarch_sparc = $(filter sparc sparc64,${system_targets})
sysarch_x86   = $(filter i386 x86_64,${system_targets})
sysarch_misc  = $(filter-out $(foreach s,${sys_systems},${sysarch_$s}),${system_targets})

else

systems =

endif # enable_system

ifeq (${enable_linux_user},enable)

# list of linux-user targets, from ./configure
user_targets = \
 i386 x86_64 alpha aarch64 aarch64_be arm armeb cris hppa m68k microblaze microblazeel \
 mips mipsel mips64 mips64el mipsn32 mipsn32el nios2 or1k \
 ppc ppc64 ppc64abi32 ppc64le riscv32 riscv64 sh4 sh4eb sparc sparc64 sparc32plus \
 s390x tilegx xtensa xtensaeb

endif	# enable_linux_user

b/configure-stamp: configure
	dh_testdir

	# system build
	rm -rf b/qemu; mkdir -p b/qemu
	cd b/qemu && \
	    ../../configure ${common_configure_opts} --disable-user \
		--${enable_system}-system \
		--${enable_linux_user}-linux-user \
		--enable-modules \
		$(shell sh debian/extract-config-opts \
		    $(DEB_HOST_ARCH_OS)-$(DEB_HOST_ARCH) debian/control) \
		$(QEMU_CONFIGURE_OPTIONS) || \
	 { echo ===== CONFIGURE FAILED ===; tail -n 50 config.log; exit 1; }

# switch some simple UI and simple audio modules to be built-ins,
# but keep more complex to be modules to move to a separate package
	sed -i -r 's/(CONFIG_(CURSES|-GTK|-SDL|AUDIO_(-PA|OSS|ALSA|-SDL)))=m/\1=y/' \
		b/qemu/config-host.mak

ifeq ($(enable_linux_user),enable)
# do not use debian/configure-opts here, all optional stuff will be enabled
# automatically, dependencies are already verified in the main build
	rm -rf b/user-static; mkdir b/user-static
	cd b/user-static && \
	    ../../configure ${common_configure_opts} \
		--static --disable-system \
		--target-list="$(addsuffix -linux-user,${user_targets})"
endif
	touch $@

build-arch: b/build-stamp
b/build-stamp: b/configure-stamp
	dh_testdir

	# system and utils build
	$(MAKE) -C b/qemu V=${V}
ifeq (${enable_system},enable)
	dtc -o b/qemu/pc-bios/bamboo.dtb pc-bios/bamboo.dts
endif

ifeq ($(enable_linux_user),enable)
	# user-static build
	# we use this invocation to build just the binaries
	$(MAKE) -C b/user-static V=${V} $(patsubst %,subdir-%-linux-user,${user_targets})
	sed -e 's/qemu\\-user\\-static/qemu\\-user/g' \
	    -e 's/ (static version)//' \
	    debian/qemu-user-static.1 > debian/qemu-user.1
endif
	touch $@

define inst-system
	mkdir -p debian/qemu-system-$1/usr/share/man/man1 debian/qemu-system-$1/usr/bin
	for t in ${sysarch_$1}; do \
	    mv debian/tmp/usr/bin/qemu-system-$$t debian/qemu-system-$1/usr/bin/; \
	    echo ".so man1/qemu-system.1" > debian/qemu-system-$1/usr/share/man/man1/qemu-system-$$t.1; \
	done
	echo sysarch:$1=\
$(if $(wordlist 10,20,${sysarch_$1}),\
$(wordlist 1,8,${sysarch_$1})\$${Newline}   $(wordlist 9,20,${sysarch_$1}),\
${sysarch_$1}) \
> debian/qemu-system-$1.substvars
	echo sysprovides:$1=${addprefix qemu-system-,${filter-out $1,${sysarch_$1}}} | \
	  sed -e 's/ /, /g' -e 'y/_/-/' >> debian/qemu-system-$1.substvars
	dh_link -pqemu-system-$1 usr/share/doc/qemu-system-common usr/share/doc/qemu-system-$1/common

endef

binary-arch:
	dh_testdir
	dh_testroot
	dh_prep -a
	dh_installdirs -a

	# system and utils install
	$(MAKE) -C b/qemu DESTDIR=$(CURDIR)/debian/tmp install KEYMAPS=

ifeq (${enable_system},enable)

	# qemu-system subpackages
	mv debian/tmp/usr/share/man/man1/qemu.1 debian/tmp/usr/share/man/man1/qemu-system.1
	$(foreach s,${systems},$(call inst-system,$s))

ifneq ($(filter $(DEB_HOST_ARCH),amd64 i386),)
# We used to load kvm module on x86 using an initscript,
# but modern kernels do that automatically.  Remove the initscript.
# Remove this fragment for stretch+1.
	for x in preinst postinst postrm; do \
	  echo 'dpkg-maintscript-helper rm_conffile /etc/init.d/qemu-system-x86 1:2.2+dfsg-3~ -- "$$@"' \
	    >> debian/qemu-system-x86.$$x.debhelper ; \
	done
# there's no need to restore the links b/c kernel autoloads our modules
	echo 'if [ configure = "$$1" ] && dpkg --compare-versions "$$2" le-nl 1:2.2+dfsg-3~; then update-rc.d qemu-system-x86 remove >/dev/null; fi' \
	  >> debian/qemu-system-x86.postinst.debhelper
endif

# remove now-unused target-x86_64.conf (and /etc/qemu dir too, see #797608)
	for x in preinst postinst postrm; do \
	  echo 'dpkg-maintscript-helper rm_conffile /etc/qemu/target-x86_64.conf 1:2.4+dfsg-1~ -- "$$@"' \
            >> debian/qemu-system-x86.$$x.debhelper ; \
	  echo '[ -d /etc/qemu ] && rmdir /etc/qemu 2>/dev/null || :' \
	    >> debian/qemu-system-x86.$$x.debhelper; \
	done

ifeq ($(DEB_HOST_ARCH_OS),linux)

# /usr/bin/kvm compat script.  This needs some more work for other arches
# How about a manpage for it?
ifneq ($(filter $(DEB_HOST_ARCH),amd64 i386),)
	dh_install -pqemu-kvm debian/kvm /usr/bin/
	dh_install -pqemu-kvm debian/kvm.1 /usr/share/man/man1/
ifeq ($(VENDOR),UBUNTU)
# on ubuntu there was kvm-spice package/binary which was used in libvirt xml
	dh_link -pqemu-kvm usr/bin/kvm usr/bin/kvm-spice \
		usr/bin/qemu-system-x86_64 usr/bin/qemu-system-x86_64-spice
endif
endif
ifeq ($(VENDOR),UBUNTU)
# apport hook is ubuntu-specific
	dh_install -pqemu-system-common debian/source_qemu.py \
		usr/share/apport/package-hooks/
endif

# virtfs-proxy-helper and qemu-bridge-helper are linux-specific
	for f in usr/bin/virtfs-proxy-helper \
	         usr/share/man/man1/virtfs-proxy-helper.1 \
		 usr/lib/qemu/qemu-bridge-helper \
	; do \
	  mkdir -p debian/qemu-system-common/$${f%/*} ; \
	  mv debian/tmp/$$f debian/qemu-system-common/$$f ; \
	done

endif # linux

endif # enable_system

ifeq ($(enable_linux_user),enable)
	dh_movefiles -p qemu-user $(patsubst %,usr/bin/qemu-%,${user_targets})
	dh_link -p qemu-user $(patsubst %,usr/share/man/man1/qemu-user.1 usr/share/man/man1/qemu-%.1,${user_targets})

	# qemu-user-static
	mkdir -p debian/qemu-user-static/usr/bin debian/qemu-user-static/usr/share/man/man1
	for t in ${user_targets}; do \
	   cp -p b/user-static/$$t-linux-user/qemu-$$t \
	       debian/qemu-user-static/usr/bin/qemu-$$t-static ; \
	   ln -s qemu-user-static.1 \
	       debian/qemu-user-static/usr/share/man/man1/qemu-$$t-static.1 ; \
	done

	# binfmt support
	for x in postinst prerm; do \
	    sed -e s/@SUFFIX@/-static/ -e s/@PACKAGE@/qemu-user-static/ -e "s/@FIX_BINARY@/--fix-binary yes/" \
		debian/binfmt-update-in >> debian/qemu-user-static.$$x.debhelper ; \
	    sed -e s/@SUFFIX@// -e s/@PACKAGE@/qemu-user-binfmt/ -e s/@FIX_BINARY@// \
		debian/binfmt-update-in >> debian/qemu-user-binfmt.$$x.debhelper ; \
	done

endif	# enable_linux_user

	dh_install -a
	dh_missing --list-missing
	dh_installdocs -a -Nqemu-user-binfmt
	dh_installchangelogs -a -Nqemu-user-binfmt
	dh_installdocs -a -pqemu-user-binfmt --link-doc=qemu-user
	dh_installman -a
	dh_installudev -a
ifeq (${enable_system},enable)
# qemu-ifup is arch-specific
	install -D debian/qemu-ifup.$(DEB_HOST_ARCH_OS) \
		debian/qemu-system-common/etc/qemu-ifup
endif
	dh_installinit -a -pqemu-guest-agent
	dh_installsystemd -a -pqemu-guest-agent --no-start --no-enable
	dh_link -a
	dh_lintian -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_shlibdeps -a
	dh_installdeb -a
ifeq ($(enable_linux_user),enable)
# after shlibdeps finished, grab ${shlibs:Depends} from -user package
# and transform it into Built-Using field for -user-static.
# See also dpkg-query bug #588505
	if [ -f debian/qemu-user.substvars ]; then \
	  pkgs=$$(sed -n -e's/([^)]*)//g' -e's/,//g' -e's/^shlibs:Depends=//p' debian/qemu-user.substvars); \
	  srcs=; for p in $$pkgs; do \
	    srcs="$$srcs $$(dpkg-query -f '$${source:Package} (= $${source:Version}),' -W $$p)"; \
	  done ; \
	  echo "built-using=$$srcs" >> debian/qemu-user-static.substvars ; \
	fi
endif
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

ifneq (,$(wildcard debian/control-in))
# only include rules for debian/control if debian/control-in is present
debian/control: debian/control-in debian/rules
	echo '# autogenerated file, please edit debian/control-in' > $@.tmp
	sed -e 's/^:$(shell echo ${VENDOR} | tr '[A-Z]' '[a-z]')://' \
		-e '/^:[a-z]*:/D' $< >> $@.tmp
	mv -f $@.tmp $@
	chmod -w $@
endif


# s390x firmware in pc-bios/s390-ccw
s390ccw_srcs = start.S main.c bootmap.c jump2ipl.c sclp.c menu.c virtio.c virtio-scsi.c virtio-blkdev.c libc.c

build-indep:

	mkdir -p b/fw

# x86 optionrom
	${MAKE} -f ${CURDIR}/debian/optionrom.mak -C ${CURDIR}/b/fw SRC_PATH=${CURDIR}

# alpha firmware in roms/palcode-clipper
	cp -al roms/qemu-palcode b/
	${MAKE} -C ${CURDIR}/b/qemu-palcode CROSS=${ALPHAEV67_CROSSPFX}
	${ALPHAEV67_CROSSPFX}strip b/qemu-palcode/palcode-clipper

# s390x firmware in pc-bios/s390-ccw
	${S390X_CROSSPFX}gcc -Wl,-pie -fPIE -nostdlib -o b/fw/s390-ccw.img \
	  -ffreestanding -fno-delete-null-pointer-checks -msoft-float \
	  -march=z900 -fPIE -fno-strict-aliasing -fno-asynchronous-unwind-tables -fno-stack-protector \
	   $(addprefix pc-bios/s390-ccw/,${s390ccw_srcs})
	${S390X_CROSSPFX}strip --strip-unneeded b/fw/s390-ccw.img
	chmod -x b/fw/s390-ccw.img

binary-indep:
	dh_testdir
	dh_testroot
	dh_prep -i -Xdebian/tmp

	dh_install -i
	rm -f debian/qemu-system-data/usr/share/qemu/keymaps/Makefile
	dh_installdocs -i
	dh_installchangelogs -i
	dh_lintian -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

build: build-arch build-indep
binary: binary-arch binary-indep

clean:	debian/control
	dh_testdir
	rm -rf b
	find scripts/ -name '*.pyc' -delete || :
	rm -f debian/qemu-user.1
	dh_clean

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

get-orig-source:
	./debian/get-orig-source.sh ${DEB_VERSION}

.PHONY: get-orig-source

