#!/usr/bin/make -f
# debian/rules for the Debian mesa package
# Copyright © 2006 Thierry Reding <thierry@gilfi.de>

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

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

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_ARCH_CPU  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)

# for finding the correct llvm-config when meson doesn't know about it yet
LLVM_VERSION = 20
export PATH:=/usr/lib/llvm-$(LLVM_VERSION)/bin/:$(PATH)

DEB_CFLAGS_MAINT_APPEND := -Wall
DEB_CXXFLAGS_MAINT_APPEND := -Wall
ifneq (,$(filter $(DEB_HOST_ARCH), armhf))
  # Workaround for a variant of LP: #725126
  DEB_CFLAGS_MAINT_APPEND += -fno-optimize-sibling-calls
  DEB_CXXFLAGS_MAINT_APPEND += -fno-optimize-sibling-calls
else ifneq (,$(filter $(DEB_HOST_ARCH), loong64))
  # Workaround loong64 BFD linker crash related to TLSDESC
  DEB_CFLAGS_MAINT_APPEND += -mtls-dialect=trad
  DEB_CXXFLAGS_MAINT_APPEND += -mtls-dialect=trad
else ifneq (,$(filter $(DEB_HOST_ARCH), m68k))
  # This library has huge jump tables: Debian #1067207
  DEB_CFLAGS_MAINT_APPEND += -mlong-jump-table-offsets
  DEB_CXXFLAGS_MAINT_APPEND += -mlong-jump-table-offsets
else ifneq (,$(filter $(DEB_HOST_ARCH), sh4))
  # Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83143
  DEB_CFLAGS_MAINT_APPEND += -freorder-blocks-algorithm=simple
  DEB_CXXFLAGS_MAINT_APPEND += -freorder-blocks-algorithm=simple
endif
buildflags = $(shell \
	DEB_CFLAGS_MAINT_APPEND='$(DEB_CFLAGS_MAINT_APPEND)' \
	DEB_CXXFLAGS_MAINT_APPEND='$(DEB_CXXFLAGS_MAINT_APPEND)' \
	dpkg-buildflags --export=configure)

GALLIUM_DRIVERS = softpipe
confflags_SSE2 = -Dsse2=true
LLVM_ARCHS     = amd64 arm64 armel armhf i386 loong64 mips64el powerpc ppc64 ppc64el riscv64 s390x sparc64 x32

# hurd doesn't do direct rendering
ifeq ($(DEB_HOST_ARCH_OS), hurd)
  confflags_DIRECT_RENDERING = -Dglx-direct=false
else
  ifneq (,$(filter $(DEB_HOST_ARCH), amd64 i386 x32))
	GALLIUM_DRIVERS += svga
	# svga needs xa state tracker
	confflags_GALLIUM += -Dgallium-xa=enabled
  endif
  # LLVM is required for building llvmpipe driver.
  ifneq (,$(filter $(DEB_HOST_ARCH), $(LLVM_ARCHS)))
	GALLIUM_DRIVERS += llvmpipe
	confflags_GALLIUM += -Dllvm=enabled
  else
	confflags_GALLIUM += -Dllvm=disabled
  endif
endif

ifeq ($(DEB_HOST_ARCH), i386)
	confflags_SSE2 = -Dsse2=false
endif

empty:=
space := $(empty) $(empty)
comma := ,
GALLIUM_DRIVERS := $(patsubst %,'%',$(GALLIUM_DRIVERS))
GALLIUM_DRIVERS_LIST := $(subst $(space),$(comma),$(GALLIUM_DRIVERS))
confflags_GALLIUM += -Dgallium-drivers="[$(GALLIUM_DRIVERS_LIST)]"

confflags += \
	-Dosmesa=true \
	-Ddri-drivers-path=/usr/lib/$(DEB_HOST_MULTIARCH)/dri \
	-Dplatforms= \
	-Dvulkan-drivers= \
	-Dglvnd=disabled \
	-Dshared-glapi=enabled \
	-Dgbm=disabled \
	-Dglx=disabled \
	-Degl=disabled \
	-Dgles1=disabled \
	-Dgles2=disabled \
	-Dbuild-tests=false \
	$(confflags_GALLIUM) \
	$(confflags_SSE2)

rewrite_wrap_files:
	cp -r subprojects subprojects-save
	for crate in paste proc-macro2 quote syn unicode-ident; \
	do \
		export crate_namever=`basename $$MESON_PACKAGE_CACHE_DIR/$$crate-*`; \
		sed -e"/source.*/d" -e"s,$${crate}-.*,$${crate_namever}," -i subprojects/$${crate}.wrap; \
	done
	touch subprojects/rewrite

override_dh_clean: regen_control
	rm -rf build
	rm -rf configure bin/config.guess bin/config.sub config.h.in
	rm -rf $$(find -name Makefile.in)
	rm -rf bin/install-sh bin/ltmain.sh
	rm -rf src/intel/vulkan/grl/parser.out
	rm -rf src/intel/vulkan/grl/parsetab.py
	for file in debian/*.links.in; do rm -f $${file%%.in}; done
	find -name '*.pyc' -delete
	find -name '__pycache__' -delete
	if [ -f subprojects/rewrite ]; then \
		rm -rf subprojects; \
		mv subprojects-save subprojects; \
	fi
	dh_clean

override_dh_auto_configure: rewrite_wrap_files
	dpkg-parsechangelog | awk '/^Version:/ {print $$2}' > VERSION
	$(buildflags) dh_auto_configure -- \
		$(confflags)

override_dh_auto_install:
	dh_auto_install
	for file in debian/*.links.in; \
	do \
		sed -e"s,\$${DEB_HOST_MULTIARCH},$(DEB_HOST_MULTIARCH),g" \
			$${file} > $${file%%.in}; \
	done

# some tests are expected to fail for now, drop this when upstream has
# better cross-build testing in place and expected failures fixed
override_dh_auto_test:
	-dh_auto_test

allpkg = $(shell dh_listpackages -a)

override_dh_install:
	# purge .la files
	find debian/tmp/ -name '*.la' -exec rm '{}' ';'

	# Get rid of some files which aren't installed. Do not
	# use -f to ensure we notice disappearing files:
	rm debian/tmp/usr/include/GL/gl*
	rm -r debian/tmp/usr/include/KHR
	# use -f here though
	rm -f debian/tmp/usr/lib/*/libgrl.a

	dh_install -a

	# Install bug control and script:
	set -e; for p in $(allpkg); do \
		install -d debian/$$p/usr/share/bug/$$p && \
		install -m 644 debian/local/control debian/$$p/usr/share/bug/$$p && \
		install -m 755 debian/local/script  debian/$$p/usr/share/bug/$$p; \
	done

override_dh_makeshlibs:
	dh_makeshlibs -a -- -c4

%:
	dh $@ \
		--builddirectory=build/ \
		--buildsystem=meson

# For maintainer use only, generate a tarball:
gentarball:
	tar --transform 's,^.\/,$(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM)/,' \
		--exclude 'debian' --exclude '.git.*' --exclude-vcs \
		-cJf ../$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.xz .

regen_control:
	sed -e"s,@LLVM_VERSION@,$(LLVM_VERSION),g" \
	    -e"s,@LLVM_ARCHS@,$(LLVM_ARCHS),g" \
	    debian/control.in > debian/control
