#!/usr/bin/make -f
# -*- makefile -*-
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
# Default/i386/amd64 options from sdlmame rules file by Cesare Falco.

#export DH_VERBOSE = 1

DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_HOST_ARCH_BITS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_BITS)

# Define DEB_HOST_MULTIARCH for QT prefix handling
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# add c++ hardening options (upstream makefile doesn't use CPPFLAGS)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = $(shell dpkg-buildflags --get CPPFLAGS)

# taken verbatim from debian policy
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# g++ 10 seems buggy on some archs, use to build with g++ 9 as a workaround
# if enabled, remember to add dependency on g++-9 in debian/control as well!
COMPILE_WITH_GCC9 =

# Override relevant make variables in original makefile
DEB_OPTS = \
    NOWERROR=1 \
    OSD=sdl \
    DEBUG= \
    TARGETOS=linux \
    DISTRO=debian-stable \
    PTR64= \
    BIGENDIAN= \
    PYTHON_EXECUTABLE=python3 \
    QT_HOME=/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/ \
    USE_SYSTEM_LIB_ASIO=1 \
    USE_SYSTEM_LIB_EXPAT=1 \
    USE_SYSTEM_LIB_ZLIB=1 \
    USE_SYSTEM_LIB_JPEG=1 \
    USE_SYSTEM_LIB_FLAC=1 \
    USE_SYSTEM_LIB_SQLITE3=1 \
    USE_SYSTEM_LIB_PORTMIDI=1 \
    USE_SYSTEM_LIB_PORTAUDIO=1 \
    USE_BUNDLED_LIB_SDL2= \
    USE_SYSTEM_LIB_UTF8PROC=1 \
    USE_SYSTEM_LIB_GLM=1 \
    USE_SYSTEM_LIB_RAPIDJSON=1 \
    USE_SYSTEM_LIB_PUGIXML=1 \
    VERBOSE=1

# verbatim from Mame 0.253 release notes:
# MAME now requires Lua compiled as C++ to work correctly.
# This prevents the use of Lua libraries from Linux distribution
# package repositories, as they are compiled as C.
#DEB_OPTS += \
#    USE_SYSTEM_LIB_LUA=lua5.3:/usr/include/lua5.3

# Make build less verbose if requested
ifneq (,$(filter terse,$(DEB_BUILD_OPTIONS)))
DEB_OPTS += \
    VERBOSE=
endif

# Set specific arch options
ifeq ($(DEB_HOST_ARCH),alpha)
DEB_OPTS += \
    PTR64=1
endif

ifeq ($(DEB_HOST_ARCH),amd64)
DEB_OPTS += \
    PTR64=1
endif

ifeq ($(DEB_HOST_ARCH),arm64)
DEB_OPTS += \
    ARCHOPTS="-mabi=lp64 -fPIC"
LINK_WITH_GOLD = 1
ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
# reduce jobs to release memory on Launchpad builder VM
NUMJOBS = 2
endif
endif

ifeq ($(DEB_HOST_ARCH),armel)
export DEB_LDFLAGS_MAINT_APPEND += -Wl,--long-plt
endif

ifeq ($(DEB_HOST_ARCH),armhf)
export DEB_LDFLAGS_MAINT_APPEND += -Wl,--allow-multiple-definition
endif

ifeq ($(DEB_HOST_ARCH),hppa)
DEB_OPTS += \
    BIGENDIAN=1 \
    NOASM=1
endif

ifeq ($(DEB_HOST_ARCH),ia64)
DEB_OPTS += \
    PTR64=1
endif

ifeq ($(DEB_HOST_ARCH),loong64)
DEB_OPTS += \
    PTR64=1
endif

ifeq ($(DEB_HOST_ARCH),m68k)
DEB_OPTS += \
    NOASM=1
endif

ifeq ($(DEB_HOST_ARCH),mips)
DEB_OPTS += \
    ARCHOPTS=-Umips \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),mipsel)
DEB_OPTS += \
    ARCHOPTS=-Umips \
    NOASM=1
endif

ifeq ($(DEB_HOST_ARCH),mips64el)
DEB_OPTS += \
    NOASM=1
export DEB_CXXFLAGS_MAINT_APPEND += -mxgot
endif

ifeq ($(DEB_HOST_ARCH),powerpc)
DEB_OPTS += \
    ARCHOPTS=-Upowerpc \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),ppc64)
DEB_OPTS += \
    PTR64=1 \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),ppc64el)
DEB_OPTS += \
    PTR64=1
ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
# reduce jobs to release memory on Launchpad builder VM
NUMJOBS = 2
endif
endif

ifeq ($(DEB_HOST_ARCH),riscv64)
DEB_OPTS += \
    OPTIMIZE=1 \
    PTR64=1
endif

ifeq ($(DEB_HOST_ARCH),s390)
DEB_OPTS += \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),s390x)
DEB_OPTS += \
    PTR64=1 \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),sh4)
DEB_OPTS += \
    NOASM=1
endif

ifeq ($(DEB_HOST_ARCH),sparc)
DEB_OPTS += \
    BIGENDIAN=1
endif

ifeq ($(DEB_HOST_ARCH),sparc64)
DEB_OPTS += \
    PTR64=1 \
    BIGENDIAN=1 \
    NOASM=1
endif

# GNU/Hurd architecture
ifeq ($(DEB_HOST_ARCH),hurd-i386)
DEB_OPTS += \
    TARGETOS=gnu \
    USE_SYSTEM_LIB_PORTMIDI= \
    NO_USE_MIDI=1
endif

DEB_MAME_OPTS = \
    FULLNAME=mame \
    TARGET=mame \
    SUBTARGET=mame \
    SDL_INI_PATH=/etc/mame \
    TOOLS=1

ifdef COMPILE_WITH_GCC9
DEB_OPTS += \
	CC=gcc-9 \
	LD=g++-9 \
	CXX=g++-9
else
# gcc-10 complains about implicit declarations, tell it we don't care
CFLAGS := $(patsubst -Werror=implicit-function-declaration,-Wno-error=implicit-function-declaration,$(CFLAGS))
endif

ifdef LINK_WITH_GOLD
export DEB_LDFLAGS_MAINT_PREPEND += \
    -fuse-ld=gold -Wl,--no-keep-memory
else
# Sacrifice linking speed to avoid using so much memory
export DEB_LDFLAGS_MAINT_APPEND += \
    -Wl,--no-keep-memory,--reduce-memory-overheads,--hash-size=31
endif

ifdef NUMJOBS
MAKEFLAGS += -j$(NUMJOBS)
endif

ifdef DH_VERBOSE
export DEB_LDFLAGS_MAINT_APPEND += -Wl,--verbose,--trace
endif

ifndef DEBUG
# Disable -g entirely
CFLAGS := $(CFLAGS:-g=-g0)
endif

%:
	dh $@

override_dh_auto_build-arch:
	$(MAKE) $(DEB_OPTS) $(DEB_MAME_OPTS)
	-mv mame64 mame #FIXME
	$(CURDIR)/mame -createconfig
	mv mame.ini default.mame.ini
	mv plugin.ini default.plugin.ini
	mv ui.ini default.ui.ini

override_dh_auto_build-indep:
	$(MAKE) -C docs singlehtml
	find language -mindepth 1 -type d -execdir msgfmt {}/strings.po -o {}/strings.mo \;

override_dh_auto_clean:
	PYTHON_EXECUTABLE=python3 dh_auto_clean
	find language -name "*.mo" -delete

override_dh_auto_test-arch:
	# Run MAME's internal validation tests
	-$(CURDIR)/mame -validate

override_dh_install:
	dh_install --exclude=dir.txt \
		   --exclude=LICENSE \
		   --exclude=README.md

override_dh_missing:
	dh_missing --fail-missing

override_dh_fixperms-arch:
	dh_fixperms
	find debian/mame/etc/mame -type f -execdir chmod 644 {} \;

override_dh_fixperms-indep:
	dh_fixperms
	find debian/mame-data/usr/share/games/mame/artwork -type f -execdir chmod 644 {} \;
	find debian/mame-data/usr/share/games/mame/bgfx -type f -execdir chmod 644 {} \;
	find debian/mame-data/usr/share/games/mame/ctrlr -type f -execdir chmod 644 {} \;
	find debian/mame-data/usr/share/games/mame/fonts -type f -execdir chmod 644 {} \;
	find debian/mame-data/usr/share/games/mame/hash -type f -execdir chmod 644 {} \;
	find debian/mame-data/usr/share/games/mame/keymaps -type f -execdir chmod 644 {} \;
	find debian/mame-data/usr/share/games/mame/language -type f -name "*.po" -delete
	find debian/mame-data/usr/share/games/mame/language -type f -execdir chmod 644 {} \;
	find debian/mame-data/usr/share/games/mame/plugins -type f -execdir chmod 644 {} \;
