#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildopts.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# System clang/lld major version (Debian-packaged, as in the chromium/CEF
# packages). This is the system clang, not a bundled one. Pinned rather than
# following the llvm-defaults metapackage so that a change of Debian's default
# LLVM cannot turn into an unannounced FTBFS here; bump it deliberately.
CLANG_MVERS := 22

# Map the Debian host architecture to Dart/GN cpu names (multi-arch).
ifeq ($(DEB_HOST_ARCH),amd64)
  GN_CPU := x64
else ifeq ($(DEB_HOST_ARCH),arm64)
  GN_CPU := arm64
else ifeq ($(DEB_HOST_ARCH),armhf)
  GN_CPU := arm
else ifeq ($(DEB_HOST_ARCH),armel)
  GN_CPU := arm
else ifeq ($(DEB_HOST_ARCH),riscv64)
  GN_CPU := riscv64
else ifeq ($(DEB_HOST_ARCH),i386)
  GN_CPU := x86
else
  $(error Unsupported architecture: $(DEB_HOST_ARCH))
endif

# Bootstrap seed (Dart is self-hosting; see debian/README.source).
#   normal build : the previous dart-sdk from the archive (Build-Depends),
#                  installed at /usr/lib/dart.
#   stage0 build : the dart-sdk Build-Depends is dropped by the
#                  pkg.dart-sdk.stage0 profile and the maintainer provides the
#                  documented upstream Stage-0 seed instead. Its location can be
#                  overridden with DART_STAGE0_SDK; the default is a sibling
#                  directory so it can never be confused with a packaged SDK.
DART_STAGE0_SDK ?= /usr/lib/dart-stage0
ifneq ($(filter pkg.dart-sdk.stage0,$(DEB_BUILD_PROFILES)),)
  DART_SEED := $(DART_STAGE0_SDK)
  SEED_KIND := upstream Stage-0 seed (pkg.dart-sdk.stage0 profile)
else
  DART_SEED := /usr/lib/dart
  SEED_KIND := previous dart-sdk (Build-Depends: dart-sdk)
endif

# Honour DEB_BUILD_OPTIONS=parallel=N; otherwise leave ninja to its own default.
ifneq ($(DEB_BUILD_OPTION_PARALLEL),)
  NINJA_JOBS := -j$(DEB_BUILD_OPTION_PARALLEL)
endif

BUILDDIR := out/Release

# GN args: from-source, system Debian toolchain (gn/ninja/clang/lld), no network.
export GN_DEFINES =  is_debug=false
export GN_DEFINES += is_release=true
export GN_DEFINES += is_product=false
export GN_DEFINES += target_os=\"linux\"
export GN_DEFINES += host_cpu=\"$(GN_CPU)\"
export GN_DEFINES += target_cpu=\"$(GN_CPU)\"
export GN_DEFINES += dart_target_arch=\"$(GN_CPU)\"
export GN_DEFINES += is_clang=true
export GN_DEFINES += clang_prefix=\"/usr/lib/llvm-$(CLANG_MVERS)/bin\"
export GN_DEFINES += dart_sysroot=\"\"
export GN_DEFINES += use_custom_libcxx=false
export GN_DEFINES += dart_version_git_info=false
export GN_DEFINES += verify_sdk_hash=false
# The DevTools web application is a prebuilt Flutter web bundle upstream fetches
# from CIPD; it is not source, so it is not in the orig tarball and not built
# here (see debian/patches/0003-make-prebuilt-devtools-optional.patch).
export GN_DEFINES += dart_include_devtools=false

%:
	dh $@

override_dh_auto_configure:
	# Bootstrap seed: NOT vendored in the source tarball. The build expects a
	# working Dart at tools/sdks/dart-sdk (build/dart/dart_action.gni).
	if [ ! -x "$(DART_SEED)/bin/dart" ]; then \
	  echo "ERROR: no Dart bootstrap seed at $(DART_SEED)" >&2; \
	  echo "  expected the $(SEED_KIND)." >&2; \
	  echo "  See debian/README.source for the bootstrap model and for how to" >&2; \
	  echo "  provide the upstream Stage-0 seed on a new architecture." >&2; \
	  exit 1; \
	fi
	mkdir -p tools/sdks
	ln -sfT $(DART_SEED) tools/sdks/dart-sdk
	# System libraries (de-vendored): the bundled third_party trees are removed
	# from the orig tarball; install a stub BUILD.gn that links the system lib and
	# symlink its headers where the build resolves them (-I../../third_party).
	#   zlib -> system zlib1g-dev
	mkdir -p third_party/zlib
	cp debian/system-libs/zlib.BUILD.gn third_party/zlib/BUILD.gn
	ln -sf /usr/include/zlib.h third_party/zlib/zlib.h
	ln -sf /usr/include/zconf.h third_party/zlib/zconf.h
	#   double-conversion -> system libdouble-conversion-dev
	mkdir -p third_party/double-conversion/src
	cp debian/system-libs/double-conversion.BUILD.gn third_party/double-conversion/src/BUILD.gn
	for h in /usr/include/double-conversion/*.h; do \
	  ln -sf "$$h" third_party/double-conversion/src/; \
	done
	mkdir -p $(BUILDDIR)
	echo '$(GN_DEFINES)' | tr ' ' '\n' | sed 's/\\"/"/g' > $(BUILDDIR)/args.gn
	/usr/bin/gn gen $(BUILDDIR)

override_dh_auto_build:
	/usr/bin/ninja $(NINJA_JOBS) -C $(BUILDDIR) create_sdk

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(BUILDDIR)/dart-sdk/bin/dart --version
	echo 'void main() => print("dart-sdk ok");' > $(BUILDDIR)/smoke.dart
	$(BUILDDIR)/dart-sdk/bin/dart run $(BUILDDIR)/smoke.dart
endif

override_dh_auto_install:
	mkdir -p debian/dart-sdk/usr/lib/dart
	cp -a $(BUILDDIR)/dart-sdk/. debian/dart-sdk/usr/lib/dart/
	mkdir -p debian/dart-sdk/usr/bin
	ln -sf ../lib/dart/bin/dart debian/dart-sdk/usr/bin/dart
	# dartdoc HTML templates link Google Fonts from the network (privacy breach);
	# drop those external <link> tags. `dart doc` still styles output locally.
	if [ -f debian/dart-sdk/usr/lib/dart/bin/resources/dartdoc/templates/_head.html ]; then \
	  sed -i '/fonts\.googleapis\.com/d; /fonts\.gstatic\.com/d' \
	    debian/dart-sdk/usr/lib/dart/bin/resources/dartdoc/templates/_head.html; \
	fi
	# highlight.pack.js is minified Highlight.js vendored without its sources; it
	# is excluded from the source package and every reference to it is removed by
	# debian/patches/0004-dartdoc-drop-vendored-minified-highlight-js.patch.
	# Fail loudly if it ever comes back.
	test ! -e debian/dart-sdk/usr/lib/dart/bin/resources/dartdoc/resources/highlight.pack.js
	! grep -rq highlight.pack.js debian/dart-sdk/usr/lib/dart/bin/resources/dartdoc/
	# Likewise for the prebuilt DevTools bundle, which is not in the source
	# package at all (dart_include_devtools=false, see the patch).
	test ! -e debian/dart-sdk/usr/lib/dart/bin/resources/devtools

override_dh_auto_clean:
	rm -rf out
	rm -f tools/sdks/dart-sdk
	rm -rf third_party/zlib third_party/double-conversion
	# gn and the build run the in-tree python helpers, which leave __pycache__
	# directories behind; without this a second build in the same tree fails in
	# dpkg-source with "unrepresentable changes to source".
	find . -name __pycache__ -type d -prune -exec rm -rf {} +

# Dart binaries (like Go) upset dwz.
override_dh_dwz:

# Strip the real ELF VM executables (dart, dartaotruntime*, dartvm, gen_snapshot,
# wasm-opt). The AOT *.snapshot files are ELF *data* loaded at runtime by the VM
# via dlsym of well-known symbols (kDartVmSnapshotData, ...); their symbol table
# is functional, not debug info, so they must not be stripped (see
# debian/dart-sdk.lintian-overrides).
override_dh_strip:
	dh_strip --exclude=.snapshot
