#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Build logic.
CXX := c++

# Output directory for the build
BUILD := debbuild

# Install directory, as understood by dh_install.
DESTDIR := debian/tmp

# Grab the number of threads that are desired.
PARALLEL := $(or $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS))), "1")

# Get variables with information about the package.
# (debhelper depends on the dpkg-dev package that contains this file, so no extra build-deps)
include /usr/share/dpkg/pkg-info.mk

# Skia version, for .so names.
export FULL_VERSION := $(shell echo "$(DEB_VERSION_UPSTREAM)" | sed -E 's/^([0-9.]+).*$$/\1/')
export MAJOR_VERSION := $(shell echo "$(DEB_VERSION_UPSTREAM)" | sed -E 's/^([0-9]+).*$$/\1/')

# GCC fails to do tail- or sibling calls on ppc64el, so use the version
# that don't depend on tail calls for ppc64el, even though the attribute
# clang::musttail is available in the compiler!
ifeq ($(DEB_HOST_ARCH),ppc64el)
export CXXFLAGS := $(CXXFLAGS) -DSKCMS_HAS_MUSTTAIL=0
endif

# Flags to GN, needed to respect CXXFLAGS, CFLAGS, etc.
ALL_ARGS := is_official_build=true \
	extra_cflags_cc=string_split(getenv("CXXFLAGS")) \
	extra_cflags_c=string_split(getenv("CFLAGS")) \
	skia_use_system_expat=true \
	skia_use_system_harfbuzz=true \
	skia_use_system_icu=true \
	skia_use_system_libjpeg_turbo=true \
	skia_use_system_libpng=true \
	skia_use_system_libwebp=true \
	skia_use_system_zlib=true \
	skia_use_dng_sdk=false \
	skia_use_wuffs=false \
	skia_enable_spirv_validation=false

# Note: We can't set LDFLAGS here, the GN build system sets them for us.
# So, tell it to respect them for us.
SHARED_ARGS := is_component_build=true \
	extra_ldflags=string_split(getenv("LDFLAGS")) \
	skia_so_version=$(MAJOR_VERSION)

# Flags to GN, for the static build. Don't enable -fPIC as per 10.2 in debian policy.
STATIC_ARGS := skia_no_pic=true

# Headers to be removed (= not installed) because we have disabled
# the underlying functionality:
REMOVE_HEADERS := \
	include/codec/SkRawDecoder.h \
	include/codec/SkJpegxlDecoder.h \
	include/codec/SkAvifDecocer.h

# Main build target. Just build the two versions in sequence, we don't win
# much in parallelism anyway (we might be able to re-use the first compilation
# for the second one though...)
override_dh_auto_build:
	mkdir -p $(BUILD)/shared/
	gn gen $(BUILD)/shared --args='$(ALL_ARGS) $(SHARED_ARGS)'
	ninja -C $(BUILD)/shared -j $(PARALLEL) --verbose skia

	mkdir -p $(BUILD)/static/
	gn gen $(BUILD)/static --args='$(ALL_ARGS) $(STATIC_ARGS)'
	ninja -C $(BUILD)/static -j $(PARALLEL) --verbose skia

# Custom clean target.
override_dh_auto_clean:
	rm -rf $(BUILD)

# Custom installation.
override_dh_auto_install:
	install -D -m 0755 $(BUILD)/shared/libskia.so $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/libskia.so.$(FULL_VERSION)
	ln -s libskia.so.$(FULL_VERSION) $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/libskia.so.$(MAJOR_VERSION)
	ln -s libskia.so.$(MAJOR_VERSION) $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/libskia.so
	install -D -m 0644 $(BUILD)/static/libskia.a $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/libskia.a
	python3 bin/install-headers.py --src include/ --dest $(DESTDIR)/usr/include/skia --ignore '.*/android/.*' $(addprefix --ignore ,$(REMOVE_HEADERS))
	cp modules/skcms/src/skcms_public.h $(DESTDIR)/usr/include/skia/skcms.h
	python3 bin/install-pc.py $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/skia.pc


# Default rule.
%:
	dh $@

