#!/usr/bin/make -f

# enable verbose build messages
export DH_VERBOSE=1

# enable all build hardening flags
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

# build with gcc instead of clang
defines=is_clang=false clang_use_chrome_plugins=false

# more verbose linker output
defines+=target_extra_ldflags=\"-Wl,--stats\"

# avoid error in v8's garbage collector (see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=68853)
defines+=target_extra_cxxflags=\"-fno-delete-null-pointer-checks -Wno-deprecated-declarations\"

# set the appropriate cpu architecture
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
ifeq (i386,$(DEB_HOST_ARCH))
defines+=host_cpu=\"x86\"
endif
ifeq (amd64,$(DEB_HOST_ARCH))
defines+=host_cpu=\"x64\"
endif
ifeq (arm64,$(DEB_HOST_ARCH))
defines+=host_cpu=\"arm64\"
endif
ifeq (armhf,$(DEB_HOST_ARCH))
defines+=host_cpu=\"arm\" arm_use_neon=false
endif

# disabled features
defines+=is_debug=false \
         use_gconf=false \
         use_ozone=false \
         use_sysroot=false \
         use_openh264=false \
         use_kerberos=false \
         use_custom_libcxx=false \
         use_gnome_keyring=false \
         use_unofficial_version_number=false \
         rtc_libvpx_build_vp9=false \
         treat_warnings_as_errors=false \
         optimize_webui=false \
         enable_nacl=false \
         enable_nacl_nonsfi=false \
         enable_google_now=false \
         enable_reading_list=false \
         enable_hangout_services_extension=false \
         enable_iterator_debugging=false \
         goma_dir=\"\" \
         gold_path=\"\" \
         linux_use_bundled_binutils=false \

# enabled features
defines+=use_gold=true \
         use_gtk3=true \
         use_pulseaudio=true \
         link_pulseaudio=true \
         enable_widevine=true \
         use_jumbo_build=true \
         use_system_zlib=true \
         use_system_lcms2=true \
         use_system_freetype=true \
         use_system_harfbuzz=true \
         optimize_for_size=true \
         proprietary_codecs=true \
         ffmpeg_branding=\"Chrome\" \
         fieldtrial_testing_like_official_build=true \

# some notes about embedded libraries
#  can't use system nss since net/third_party/nss is heavily patched
#  can't use system ots (open text *summarizer*) since that's not google's ots (open text *sanitizer*)

# handle parallel build options
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
njobs=-j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# paths to files needed by flot
flotpaths=/usr/share/javascript/jquery/*min.js \
          /usr/share/javascript/jquery-flot/*min.js \

%:
	dh $@

out/Release/gn:
	./tools/gn/bootstrap/bootstrap.py -s $(njobs)

override_dh_auto_configure:
	# output compiler information
	$(CXX) --version
	# use system flot
	for file in $(flotpaths); do ln -sf $$file third_party/flot; done
	# strip out system third_party libraries
	cp third_party/freetype/src/src/psnames/pstables.h .
	./debian/scripts/unbundle
	mkdir -p third_party/freetype/src/src/psnames
	mv pstables.h third_party/freetype/src/src/psnames

override_dh_auto_build-arch: out/Release/gn
	./out/Release/gn gen out/Release --args="$(defines)"
	ninja $(njobs) -C out/Release chrome chrome_sandbox content_shell chromedriver
	cp out/Release/chrome out/Release/chromium
	cp out/Release/content_shell out/Release/chromium-shell
	cp out/Release/chrome_sandbox out/Release/chrome-sandbox
	cp out/Release/locales/en-US.pak out/Release/resources
	chmod 4755 out/Release/chrome-sandbox # suid sandbox
	sed -e s/@@PACKAGE@@/chromium/g -e s/@@MENUNAME@@/chromium/g \
	    < chrome/app/resources/manpage.1.in > out/Release/chromium.1

override_dh_auto_build-indep: out/Release/gn
	./out/Release/gn gen out/Release --args="$(defines)"
	ninja $(njobs) -C out/Release packed_resources
	rm -f out/Release/locales/en-US.pak

override_dh_auto_install-arch:
	dh_auto_install
	# create /etc/chromium.d README file
	echo "Any files placed in this directory will be sourced prior to executing chromium." \
	    > debian/chromium/etc/chromium.d/README
	# update launcher script with build information
	sed 's|@BUILD_DIST@|$(shell cat /etc/debian_version)|' \
	    < debian/scripts/chromium > debian/chromium/usr/bin/chromium
	# move icons into /usr/share
	./debian/scripts/icons

override_dh_fixperms:
	dh_fixperms --exclude chrome-sandbox

override_dh_strip:
	# skip dbgsym package for widevine to prevent duplication of the src package
	dh_strip -pchromium-widevine --no-automatic-dbgsym
	# execute dh_strip on all of the remaining packages
	dh_strip --remaining-packages

override_dh_gencontrol:
	dh_gencontrol -- -VBuilt-Using="$(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W libjs-jquery libjs-jquery-flot)"

override_dh_auto_clean:
	rm -rf out
	find . -name \*.pyc -execdir rm -f {} \;
	dh_auto_clean

###################### upstream source downloading ############################

url=https://gsdview.appspot.com/chromium-browser-official
version=$(shell dpkg-parsechangelog -S Version | sed s/-.*//)
extract=chromium-$(version)
tarball=$(extract).tar.xz
debian=chromium-browser_$(version)
output=$(debian).orig.tar.xz
removed=$(debian).files-removed

get-orig-source:
	wget -nv --show-progress -c $(url)/$(tarball) -O ../$(tarball)
	cp /usr/bin/mk-origtargz debian/scripts
	patch -p1 < debian/scripts/mk-origtargz.patch
	/usr/bin/time --portability ./debian/scripts/mk-origtargz ../$(tarball) > ../$(removed)
	test ! -e $(extract) || rm -rf $(extract)
	/usr/bin/time --portability tar xaf ../$(tarball)
	while read line; do rm -rf $$line; done < ../$(removed)
	test ! -e ../$(output) || rm -f ../$(output)
	/usr/bin/time --portability tar caf ../$(output) $(extract)
	/usr/bin/time --portability rm -rf $(extract)
