#!/usr/bin/make -f

export LC_ALL=C

include /usr/share/dpkg/buildopts.mk

MODES := release profile debug

include /usr/share/dpkg/architecture.mk

# flutter/tools/gn calls amd64 "x64" and armhf "arm".
FLUTTER_CPU := $(patsubst armhf,arm,$(patsubst amd64,x64,$(DEB_HOST_ARCH)))

ifeq (,$(filter $(FLUTTER_CPU),x64 arm64 arm))
  $(error $(DEB_HOST_ARCH) is not supported, see debian/control)
endif

# armhf is armv7-a with VFPv3-D16 hard float; upstream defaults to softfp with
# NEON, which is not part of the armhf baseline.
ifeq ($(FLUTTER_CPU),arm)
  ARM_ARGS := --arm-float-abi=hard
  ARM_GN_ARGS := arm_use_neon=false
endif

GN_ARGS := \
	is_official_build=true \
	is_clang=true \
	use_default_linux_sysroot=false \
	flutter_prebuilt_dart_sdk=true \
	enable_unittests=false

ifneq (,$(ARM_GN_ARGS))
GN_ARGS += $(ARM_GN_ARGS)
endif

# The rest of the tool set needs sources the repack excludes.
TOOLS := \
	gen_snapshot \
	impellerc \
	font-subset \
	libpath_ops.so \
	flutter_embedder.h \
	gen/frontend_server_aot.dart.snapshot

# Only the debug build needs these, and only the Flutter test runner uses them.
DEBUG_ONLY := \
	flutter_tester \
	zip_archives/flutter_patched_sdk.zip \
	flutter/sky/packages/sky_engine:sky_engine

# A release build compiles the platform kernel in product mode; the two are
# different files, and both are shipped.
RELEASE_ONLY := \
	zip_archives/flutter_patched_sdk_product.zip

# Debug embeds kernel compiler output the repack leaves out.
LIBS := libflutter_linux_gtk.so libflutter_engine.so

# The toolchain, SDK and header directories upstream expects in-tree.
# link:target pairs, patches cannot symlink.
SYMLINKS := \
	flutter/buildtools/linux-x64/clang:/usr/lib/llvm-22 \
	flutter/buildtools/linux-arm64/clang:/usr/lib/llvm-22 \
	flutter/third_party/gn/gn:/usr/bin/gn \
	flutter/prebuilts/linux-$(FLUTTER_CPU)/dart-sdk:/usr/lib/dart \
	flutter/third_party/dart/tools/sdks/dart-sdk:/usr/lib/dart \
	flutter/third_party/angle/include/GLES2:/usr/include/GLES2 \
	flutter/third_party/angle/include/GLES3:/usr/include/GLES3 \
	flutter/third_party/angle/include/EGL:/usr/include/EGL \
	flutter/third_party/angle/include/KHR:/usr/include/KHR \
	flutter/third_party/icu/source/common/unicode:/usr/include/unicode \
	flutter/third_party/icu/source/i18n/unicode:/usr/include/unicode \
	flutter/third_party/zlib/zlib.h:/usr/include/zlib.h \
	flutter/third_party/zlib/zconf.h:/usr/include/zconf.h

%:
	dh $@

# Upstream is a gclient checkout; debian/README.source says how this rebuilds it.
get-orig-source:
	LC_ALL=C sh debian/get-orig-source.sh $(GET_ORIG_SOURCE_FLAGS)

# Upstream ships no pkg-config metadata, and the .pc names the upstream version,
# not the Debian revision.
UPSTREAM_VERSION := $(shell dpkg-parsechangelog -S Version | sed 's/-[^-]*$$//')

override_dh_auto_configure:
	set -e; for pair in $(SYMLINKS); do \
		link=$${pair%%:*}; \
		target=$${pair#*:}; \
		if [ ! -e "$$target" ]; then \
			echo "missing $$target, which $$link has to point at" >&2; \
			exit 1; \
		fi; \
		rm -f "$$link"; \
		mkdir -p "$$(dirname $$link)"; \
		ln -sfn "$$target" "$$link"; \
	done; \
	install -Dm644 debian/dart_package_config.json \
		flutter/third_party/dart/.dart_tool/package_config.json; \
	for mode in $(MODES); do \
		python3 flutter/tools/gn \
			--target-os linux --linux-cpu $(FLUTTER_CPU) --runtime-mode $$mode \
			--prebuilt-dart-sdk --no-goma --no-lto \
			--no-dart-version-git-info $(ARM_ARGS) \
			--gn-args="$(GN_ARGS)"; \
	done

override_dh_auto_build:
	set -e; for mode in $(MODES); do \
		libs="$(LIBS)"; extra=""; \
		if [ "$$mode" = debug ]; then libs=""; extra="$(DEBUG_ONLY)"; fi; \
		if [ "$$mode" = release ]; then extra="$(RELEASE_ONLY)"; fi; \
		ninja -C out/linux_$${mode}_$(FLUTTER_CPU) $(DEB_BUILD_OPTION_PARALLEL:%=-j%) \
			$$libs $(TOOLS) $$extra; \
	done

override_dh_auto_install:
	dh_auto_install
	set -e; for pc in flutter_linux flutter_engine; do \
		sed -e 's/@UPSTREAM_VERSION@/$(UPSTREAM_VERSION)/' \
		    -e 's/@MULTIARCH@/$(DEB_HOST_MULTIARCH)/' \
			debian/$$pc.pc.in > debian/$$pc.pc; \
	done

# dh-exec reads ${FLUTTER_CPU} from the environment; patches cannot export it.
export FLUTTER_CPU
