#!/usr/bin/make -f
# -*- mode: makefile-gmake; coding: utf-8 -*-
# SPDX-FileCopyrightText: © 2012-2019 Hunter Kaller <hunter_kaller@yahoo.com>
# SPDX-FileCopyrightText: © 2014-2018 Sergio Benjamim <sergio_br2@yahoo.com.br>
# SPDX-FileCopyrightText: © 2021-2024 Peter J. Mello <admin@petermello.net>
#
# SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0

SHELL                        ::= $(realpath /bin/bash)

# Fix hardening-no-bindnow and hardening-no-pie
DEB_BUILD_MAINT_OPTIONS      ::= hardening=+bindnow,+pie
DEB_CFLAGS_MAINT_APPEND      ::= -DLUA_USE_POSIX
DEB_CFLAGS_MAINT_STRIP       ::= -O2
DEB_CXXFLAGS_MAINT_APPEND    ::= -DLUA_USE_POSIX
DEB_CXXFLAGS_MAINT_STRIP     ::= -O2
DEB_LDFLAGS_MAINT_APPEND     ::= -Wl,--as-needed -lGL -lpthread
DPKG_EXPORT_BUILDFLAGS       ::= 1
DPKG_EXPORT_BUILDTOOLS       ::= 1
MAKE_ARGS                    ::= V=1
QT_SELECT                    ::= 5

ifeq (,$(findstring terse,$(DEB_BUILD_OPTIONS)))
  DH_VERBOSE                 ::= 1
endif

# Use Bash's built-in printf to pass the Ubuntu version when the dpkg flag set
# was introduced, and compare it to the version of the build system (host) by
# supplying them on separate lines to 'sort' with greatest first ordering, then
# pipe to 'head' to isolate the larger value. If the output matches the build
# system (i.e. it's ≥ the version of feature introduction), then set the flags.
uver ::= $(shell lsb_release -rs | cut -c-5)
include /usr/share/dpkg/vendor.mk
ifeq (Ubuntu,$(DEB_VENDOR))
  ifeq ($(uver),$(shell printf "$(uver)\n19.04\n" | sort -gr | head -1))
    DEB_BUILD_MAINT_OPTIONS   += reproducible=+fixfilepath
  else ifeq ($(uver),$(shell printf "$(uver)\n16.10\n" | sort -gr | head -1))
    DEB_BUILD_MAINT_OPTIONS   += reproducible=+fixdebugpath
  endif

  ifeq ($(uver),$(shell printf "$(uver)\n21.04\n" | sort -gr | head -1))
    DEB_BUILD_MAINT_OPTIONS   += optimize=+lto
    LTO                      ::= 1
  endif

  # Add definitions for Large File Support
  ifeq ($(uver),$(shell printf "$(uver)\n23.10\n" | sort -gr | head -1))
    DEB_BUILD_MAINT_OPTIONS   += abi=+all
    include /usr/share/dpkg/buildtools.mk
  else ifeq ($(uver),$(shell printf "$(uver)\n17.10\n" | sort -gr | head -1))
    DEB_BUILD_MAINT_OPTIONS   += future=+lfs
    include /usr/share/dpkg/buildtools.mk
  endif
endif

include /usr/share/dpkg/default.mk

dv       ::= $(DEB_VERSION)
glsl_v   ::= $(shell dpkg-query -f '$${Version}' -W glslang-dev)
glsltest ::= $(shell dpkg --compare-versions $(glsl_v) ge 14.0.0 && echo "true")
new_glsl ::= $(if $(glsltest),yes,no)
usrgames ::= $(CURDIR)/debian/retroarch/usr/games
usrshare ::= $(CURDIR)/debian/retroarch/usr/share
GIT_HASH ::= $(shell echo '$(dv)' | sed -Ene 's/.*~([0-9a-f]{7,10})-\d*.*/\1/p')

export DEB_BUILD_MAINT_OPTIONS DEB_CFLAGS_MAINT_APPEND DEB_CFLAGS_MAINT_STRIP \
	DEB_CXXFLAGS_MAINT_APPEND DEB_CXXFLAGS_MAINT_STRIP \
	DEB_LDFLAGS_MAINT_APPEND DH_VERBOSE LTO QT_SELECT SHELL

# See ./configure --help for valid flags for the QuickBuild framework
QB_BUILD_FLAGS ::= --prefix=/usr --bindir=/usr/games --datarootdir=/usr/share \
	--docdir=/usr/share/doc/retroarch --mandir=/usr/share/man \
	--sysconfdir=/etc --build=$(DEB_BUILD_GNU_TYPE)
QB_LIBS_FLAGS  ::= --enable-al --enable-alsa --disable-audioio \
	--enable-blissbox --disable-builtinbearssl --disable-builtinflac \
	--enable-builtinglslang --enable-builtinmbedtls --disable-builtinzlib \
	--enable-caca --enable-cdrom --enable-check --enable-command \
	--disable-core_info_cache --disable-coreaudio --disable-d3d9 \
	--disable-d3dx --enable-dbus --disable-dinput --enable-dr_mp3 \
	--disable-dsound --enable-dylib --disable-dynamic_egl --enable-egl \
	--enable-ffmpeg --enable-flac --enable-freetype --disable-gdi \
	--enable-glslang --enable-hid --enable-jack --enable-kms --enable-libdecor \
	--enable-libusb --enable-lua --enable-materialui --enable-memfd_create \
	--enable-mmap --disable-mpv --enable-networkgamepad --enable-networking \
	--disable-nvda --enable-opengl --disable-osmesa --disable-oss \
	--enable-ozone --enable-parport --enable-plain_drm --enable-pulse \
	--enable-qt --enable-rgui --disable-roar --disable-rsound --disable-sdl \
	--enable-sdl2 --enable-sixel --enable-slang --enable-spirv_cross \
	--enable-ssa --enable-ssl --enable-systemd \
	--enable-threads --enable-thread_storage --disable-tinyalsa --enable-udev \
	--enable-v4l2 --disable-vg --disable-videocore --enable-videoprocessor \
	--enable-vulkan --disable-wasapi --enable-wayland --disable-winmm \
	--disable-winrawinput --enable-x11 --disable-xaudio --enable-xdelta \
	--enable-xinerama --enable-xmb --enable-xrandr --enable-xshm \
	--enable-xvideo --enable-zlib

ifeq ($(DEB_HOST_GNU_CPU),$(filter $(DEB_HOST_GNU_CPU),x86_64 i386))
  QB_LIBS_FLAGS += --disable-cg --disable-opengles --disable-rpiled --enable-sse
else ifeq ($(DEB_HOST_GNU_CPU),aarch64)
  QB_LIBS_FLAGS += --disable-cg --enable-opengles --enable-opengles3 \
					--enable-opengles3_1 --enable-opengles3_2
else ifeq ($(DEB_HOST_ARCH),armhf)
  QB_LIBS_FLAGS += --disable-cg --enable-floathard --enable-neon \
					--enable-opengles --enable-opengles3 --enable-opengles3_1 \
					--enable-opengles3_2 --enable-sunxi
else ifeq ($(DEB_HOST_ARCH),armel)
  QB_LIBS_FLAGS += --disable-cg --enable-floatsoftfp --enable-opengles \
					--enable-opengles3 --enable-opengles3_1 --enable-opengles3_2
else
  QB_LIBS_FLAGS += --disable-cg --disable-opengles
endif

ifneq ($(GIT_HASH),)
  MAKE_ARGS     += GIT_VERSION="'$(GIT_HASH)'"
endif


%:
	dh $@ -v -Smakefile --without autoreconf


execute_before_dh_auto_clean:
	touch "$(CURDIR)/config.mk"

execute_after_dh_auto_clean:
	dh_auto_clean -O-v -O-Smakefile -O-Dgfx/video_filters
	dh_auto_clean -O-v -O-Smakefile -O-Dlibretro-common/audio/dsp_filters

override_dh_autoreconf_clean:
	$(info Project doesn't use Autoconf.)

override_dh_autoreconf:
	$(info Project doesn't use Autoconf.)

execute_before_dh_auto_configure:
	LC_ALL=C.UTF-8 sed -e 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' \
		"$(CURDIR)/retroarch.cfg" >"$(CURDIR)/debian/retroarch.cfg"
	LC_ALL=C.UTF-8 sed -i \
		-e 's/org\.libretro\.RetroArch/com\.libretro\.RetroArch/g' \
		"$(CURDIR)/gfx/common/wayland_common.h"

override_dh_auto_configure:
	$(CURDIR)/configure $(QB_BUILD_FLAGS) $(QB_LIBS_FLAGS)

# GLSL v14.0.0 dropped legacy stub libraries libIGLCompiler and libHLSL and a
# patch was added to the series to remove them from QuickBuild. This conditional
# tests if we are using such a GLSL, and if not, adds back the linker flags for
# both libraries as arguments to the $(MAKE) invocation. Drop this once builds
# are no longer needed for Ubuntu 23.10 "Mantic Minotaur" and its predecessors.
override_dh_auto_build:
ifeq ($(new_glsl),yes)
	dh_auto_build -O-v -O-Smakefile -- $(MAKE_ARGS)
else
	dh_auto_build -O-v -O-Smakefile -- \
		GLSLANG_LIBS+="-lglslang -lOGLCompiler -lHLSL" $(MAKE_ARGS)
endif
	# SoftFilters
	dh_auto_build -O-v -O-Smakefile -O-Dgfx/video_filters -- V=1
	# Audio DSP plugins
	dh_auto_build -O-v -O-Smakefile -O-Dlibretro-common/audio/dsp_filters -- V=1

override_dh_auto_install:

execute_after_dh_install:
	chrpath -d "$(CURDIR)/debian/retroarch/usr/games/retroarch"
	@cp -fv "$(CURDIR)/tools/cg2glsl.py" "$(usrgames)/retroarch-cg2glsl"
	@chmod -v 0755 "$(usrgames)/retroarch-cg2glsl"
#	@cp -fv "$(CURDIR)/org.libretro.RetroArch.desktop" \
#		"$(usrshare)/applications/com.libretro.RetroArch.desktop"
#	LC_ALL=C.UTF-8 sed -e \
#		's/org.libretro.RetroArch.desktop/com.libretro.RetroArch.desktop/g' \
#		"$(CURDIR)/com.libretro.RetroArch.appdata.xml" \
#		>"$(usrshare)/metainfo/com.libretro.RetroArch.metainfo.xml"
	LC_ALL=C.UTF-8 sed -i \
		-e 's/com\.libretro\.RetroArch/org\.libretro\.RetroArch/g' \
		"$(CURDIR)/gfx/common/wayland_common.h"

override_dh_shlibdeps:
	dh_shlibdeps -O-v -O-Smakefile -l/lib/$(DEB_HOST_MULTIARCH)

.PHONY: execute_before_dh_auto_clean execute_after_dh_auto_clean \
		execute_before_dh_auto_configure override_dh_auto_configure \
		override_dh_auto_build execute_after_dh_install override_dh_shlibdeps
