#!/usr/bin/make -f

ifeq ($(DEB_HOST_ARCH_OS), linux)
	selinux        = --enable-xselinux
	wayland        = --disable-xwayland
	logind         = --enable-systemd-logind
	systemd-daemon = --with-systemd-daemon
else
	selinux        = --disable-xselinux
	wayland        = --disable-xwayland
	logind         = --disable-systemd-logind
	systemd-daemon = --without-systemd-daemon
endif

ifeq ($(DEB_HOST_ARCH_OS), hurd)
	dri = --disable-dri --disable-dri2
	dri += --disable-glamor
	xshmfence = --disable-xshmfence
else
	dri = --enable-dri --enable-dri2
	dri += --enable-glamor
	xshmfence = --enable-xshmfence
endif

ifeq ($(DEB_HOST_ARCH_OS), linux)
	dri += --enable-dri3
else
	dri += --disable-dri3
endif

config_backend = --disable-config-hal
ifeq ($(DEB_HOST_ARCH_OS), linux)
	config_backend += --enable-config-udev
else ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
	config_backend += --disable-config-udev --enable-config-devd
	libs = $(shell pkg-config --libs libbsd-overlay)
	cppflags = $(shell pkg-config --cflags libbsd-overlay)
else # hurd
	config_backend += --disable-config-udev
endif

# linux: libdrm needed for kms (hw/xfree86/os-support/linux/lnx_platform.c)
#        even in udeb
# kfreebsd: libdrm for dri in main build
# hurd: no libdrm
ifeq ($(DEB_HOST_ARCH_OS), linux)
	libdrm_main = --enable-libdrm
	libdrm_udeb = --enable-libdrm
else ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
	libdrm_main = --enable-libdrm
	libdrm_udeb = --disable-libdrm
else # hurd
	libdrm_main = --disable-libdrm
	libdrm_udeb = --disable-libdrm
endif

vars = $(shell DEB_BUILD_MAINT_OPTIONS="hardening=+pie" DEB_LDFLAGS_MAINT_APPEND="-Wl,-Bsymbolic" DEB_CPPFLAGS_MAINT_APPEND="-DPRE_RELEASE=0 $(cppflags)" dpkg-buildflags --export=configure) LIBS="$(libs)"

ifneq (,$(filter nocheck, $(DEB_BUILD_OPTIONS)))
	config_tests = --disable-unit-tests
else
	config_tests = --enable-unit-tests
endif

VENDOR = $(shell lsb_release -i -s)
SUPPORT = https://www.debian.org/support

SOURCE_NAME    :=  xorg-server
SOURCE_VERSION := $(shell dpkg-parsechangelog | awk -F': ' '/^Version: / {print $$2}')

# disable-static is so we don't get libfoo.a for modules.  now if only we could
# kill the .las.
confflags += \
	     --disable-silent-rules \
	     --disable-static \
	     --without-dtrace \
	     --disable-strict-compilation \
	     --disable-debug \
	     $(config_tests) \
	     --with-int10=x86emu \
	     --with-os-vendor="$(VENDOR)" \
	     --with-builderstring="$(SOURCE_NAME) $(SOURCE_VERSION) ($(SUPPORT))" \
	     --with-xkb-path=/usr/share/X11/xkb \
	     --with-xkb-output=/var/lib/xkb \
	     --with-shared-memory-dir=/dev/shm \
	     --enable-mitshm \
	     --enable-xres \
	     --disable-xcsecurity \
	     --disable-tslib \
	     --enable-dbe \
	     --disable-xf86bigfont \
	     --enable-dpms \
	     $(xshmfence) \
	     $(config_backend) \
	     --enable-xorg \
	     --disable-linux-acpi \
	     --disable-linux-apm \
	     --disable-xquartz \
	     --disable-xwin \
	     --disable-xfake \
	     --disable-xfbdev \
	     --disable-install-setuid

confflags_main = \
	--with-default-font-path="/usr/share/fonts/X11/misc,/usr/share/fonts/X11/cyrillic,/usr/share/fonts/X11/100dpi/:unscaled,/usr/share/fonts/X11/75dpi/:unscaled,/usr/share/fonts/X11/Type1,/usr/share/fonts/X11/100dpi,/usr/share/fonts/X11/75dpi,built-ins" \
	--enable-composite \
	--enable-record \
	--enable-xv \
	--enable-xvmc \
	--enable-dga \
	--enable-screensaver \
	--enable-xdmcp \
	--enable-xdm-auth-1 \
	--enable-glx \
	$(dri) \
	$(libdrm_main) \
	--enable-present \
	--enable-xinerama \
	--enable-xf86vidmode \
	--enable-xace \
	$(selinux) \
	--enable-xfree86-utils \
	$(wayland) \
	$(logind) \
	$(systemd-daemon) \
	--enable-suid-wrapper \
	--enable-dmx \
	--enable-xvfb \
	--enable-xnest \
	--enable-kdrive \
	--enable-xephyr \
	--with-sha1=libgcrypt \
	--enable-xcsecurity \
	$(void)

confflags_udeb = \
	--with-default-font-path="built-ins" \
	--disable-composite \
	--disable-record \
	--disable-xv \
	--disable-xvmc \
	--enable-dga \
	--disable-screensaver \
	--disable-xdmcp \
	--disable-xdm-auth-1 \
	--disable-glx \
	--disable-dri \
	--disable-dri2 \
	--disable-glamor \
	$(libdrm_udeb) \
	--disable-xinerama \
	--disable-xf86vidmode \
	--disable-xace \
	--disable-xselinux \
	--disable-xfree86-utils \
	--disable-xwayland \
	--disable-systemd-logind \
	--without-systemd-daemon \
	--disable-suid-wrapper \
	--disable-dmx \
	--disable-xvfb \
	--disable-xnest \
	--disable-kdrive \
	--disable-xephyr \
	--with-sha1=libnettle \
	$(void)

%:
	dh $@ --with quilt,autoreconf --parallel

build-source-stamp:
	mkdir -p build-source
	tar \
	--owner=0 --group=0 \
        --transform 's,^,xorg-server/,' \
	--exclude=debian \
	--exclude=autom4te.cache \
        -cf - * | xz > build-source/xorg-server.tar.xz
	>$@

override_dh_auto_configure:
	dh_auto_configure --builddirectory=debian/build/main -- \
		--libdir=\$${prefix}/lib \
		--libexecdir=\$${libdir}/xorg \
		$(confflags) \
		$(confflags_main) \
		$(vars)

override_dh_auto_build:
	dh_auto_build --builddirectory=debian/build/main

override_dh_auto_test:
	dh_auto_test -- -j1 VERBOSE=1

build: abibumpcheck
build-indep: build-source-stamp

override_dh_auto_install:
	dh_auto_install --builddirectory=debian/build/main \
		--destdir=debian/tmp/main

	# oh, yuck.
	find debian/tmp/*/usr/lib/xorg -type f -name '*.la' | \
	  xargs rm -f

	install -m 755 debian/local/xvfb-run debian/tmp/main/usr/bin
	install debian/local/xvfb-run.1 debian/tmp/main/usr/share/man/man1
ifneq ($(DEB_HOST_ARCH_OS), linux)
	install -d debian/tmp/main/usr/share/X11/xorg.conf.d
	install -m 644 debian/local/10-*.conf debian/tmp/main/usr/share/X11/xorg.conf.d
endif

# Only read the first line, the rest of the file is used to determine
# when the minimal version is to be bumped:
SERVERMINVER = debian/serverminver
serverminver = $(shell head -1 $(SERVERMINVER))

.PHONY: abibumpcheck
abibumpcheck: debian/serverminver $(QUILT_STAMPFN)
	@echo Checking for the need of an ABI bump
	# Build an updated version of the file:
	head -1 $(SERVERMINVER) > $(SERVERMINVER).new
	perl -ne 'print "$$1:$$2.$$3\n" if /^#define\s+(ABI_(?:VIDEODRV|XINPUT)_VERSION)\s+SET_ABI_VERSION\(\s*(\d+)\s*,\s*(\d+)\s*\)/' hw/xfree86/common/xf86Module.h|sort >> $(SERVERMINVER).new
	# Compare both files:
	@if ! cmp --quiet $(SERVERMINVER) $(SERVERMINVER).new; then \
	echo "serverminver bump required, ABI changed!";\
	echo "When bumping major or minor, always bump required xorg-server minimum";\
	echo "version too, the newly built drivers are not backwards compatible!";\
	diff -u $(SERVERMINVER) $(SERVERMINVER).new; \
	exit 1; \
	else \
	echo "ABI unchanged"; \
	rm -f $(SERVERMINVER).new; \
	fi

override_dh_fixperms-arch:
	dh_fixperms
	chown root:root $(CURDIR)/debian/xserver-xorg-legacy/usr/lib/xorg/Xorg.wrap
	chmod ug+s $(CURDIR)/debian/xserver-xorg-legacy/usr/lib/xorg/Xorg.wrap

override_dh_install:
	dh_install --fail-missing
	install -d $(CURDIR)/debian/xserver-xorg-dev/usr/share/xserver-xorg
	# Extract only the major ABI version:
	abi_videodrv=`PKG_CONFIG_PATH=debian/tmp/main/usr/lib/pkgconfig pkg-config --variable=abi_videodrv xorg-server|cut -d . -f 1`; \
	echo videoabi=xorg-video-abi-$$abi_videodrv > debian/xserver-xorg-core.substvars && \
	echo "xorg-video-abi-$$abi_videodrv, xserver-xorg-core (>= $(serverminver))" > debian/xserver-xorg-dev/usr/share/xserver-xorg/videodrvdep
	abi_xinput=`PKG_CONFIG_PATH=debian/tmp/main/usr/lib/pkgconfig pkg-config --variable=abi_xinput xorg-server|cut -d . -f 1`; \
	echo inputabi=xorg-input-abi-$$abi_xinput >> debian/xserver-xorg-core.substvars && \
	echo "xorg-input-abi-$$abi_xinput, xserver-xorg-core (>= $(serverminver))" > debian/xserver-xorg-dev/usr/share/xserver-xorg/xinputdep


	# save the configure flags so that packages like vnc, tightvnc, tigervnc
	# know how the package was built.
	echo 'xserver_confflags = $(confflags) $(confflags_main)' \
		> debian/xserver-xorg-dev/usr/share/xserver-xorg/configure_flags.mk

	install -m 755 -d debian/xserver-xorg-core/usr/share/bug/xserver-xorg-core
	install -m 755 debian/xserver-xorg-core.bug.script debian/xserver-xorg-core/usr/share/bug/xserver-xorg-core/script
ifeq ($(DEB_HOST_ARCH_OS), linux)
	install -d debian/xserver-xorg-core/lib/udev/rules.d
	install -m 644 debian/local/64-xorg-xkb.rules debian/xserver-xorg-core/lib/udev/rules.d
endif

override_dh_strip:
	dh_strip -pxserver-xorg-core --dbgsym-migration="xserver-xorg-core-dbg (<< 2:1.19.0-3~)"
	dh_strip -s -Nxserver-xorg-core -Nxserver-xorg-core-dbg
