#!/usr/bin/make -f

export LC_ALL=C

include /usr/share/dpkg/buildopts.mk

MODES := release profile debug

# The prebuilt Dart SDK that flutter/tools/gn downloads is x64 or arm64 only.
include /usr/share/dpkg/architecture.mk

ifneq (,$(filter amd64,$(DEB_HOST_ARCH)))
  FLUTTER_CPU := x64
else ifneq (,$(filter arm64,$(DEB_HOST_ARCH)))
  FLUTTER_CPU := arm64
else
  $(error $(DEB_HOST_ARCH) is not supported: the prebuilt Dart SDK is linux-x64 or linux-arm64 only, see debian/control)
endif

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

# 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

# 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-$(FLUTTER_CPU)/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; \
	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 \
			--gn-args="$(GN_ARGS)"; \
	done

override_dh_auto_build:
	set -e; for mode in $(MODES); do \
		libs="$(LIBS)"; \
		if [ "$$mode" = debug ]; then libs=""; fi; \
		ninja -C out/linux_$${mode}_$(FLUTTER_CPU) $(DEB_BUILD_OPTION_PARALLEL:%=-j%) \
			$$libs $(TOOLS); \
	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
