#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

DEB_VERSION_UPSTREAM_ORIG = $(firstword $(subst ~,$() ,$(DEB_VERSION_UPSTREAM)))

# files generated during build
SOURCE_MAP_MIN_JS = src/emscripten-source-map.min.js
NEON_H = system/include/neon/arm_neon.h

#  * test_dwarf requires unversioned llvm
#  * test_freetype requires Liberation font
#  * test_poppler reauires tests.poppler.paper.pdf
TEST_ERRORS += \
 test_dwarf \
 test_freetype \
 test_lua \
 test_poppler

# TODO: skip test_indirectbr_many only on armhf
TEST_FAILS_wasm0 += \
 test_autodebug_wasm_standalone \
 test_bullet_autoconf \
 test_bullet_cmake \
 test_cube2hash_standalone \
 test_dlfcn_longjmp \
 test_emscripten_log \
 test_fs_nodefs_home \
 test_fs_nodefs_rw \
 test_i64_invoke_bigint \
 test_indirectbr_many \
 test_longjmp \
 test_longjmp2 \
 test_longjmp2_main_module \
 test_longjmp3 \
 test_longjmp4 \
 test_longjmp_exc \
 test_longjmp_funcptr \
 test_longjmp_i64 \
 test_longjmp_repeat \
 test_longjmp_stacked \
 test_longjmp_standalone \
 test_longjmp_throw \
 test_longjmp_unwind \
 test_lua \
 test_maybe_wasm2js \
 test_modularize_closure_pre \
 test_posixtime_standalone \
 test_pthread_exceptions \
 test_setjmp_many \
 test_setjmp_many_2 \
 test_setjmp_noleak \
 test_wasm2js \
 test_wasm2js_fallback

# refresh upstream version hints from Debian package
override_dh_auto_configure: $(SOURCE_MAP_MIN_JS) $(NEON_H)
	cp -f emscripten-version.txt emscripten-version.txt.orig
	echo $(DEB_VERSION_UPSTREAM_ORIG) > emscripten-version.txt
	echo $(patsubst $(DEB_VERSION_UPSTREAM_ORIG)-%,%,$(DEB_VERSION_UPSTREAM_ORIG_REVISION)) \
		> emscripten-revision.txt
	tools/create_dom_pk_codes.py
	echo "LLVM_ROOT = '/usr/bin'" > .emscripten
	echo "BINARYEN_ROOT = '/usr'" >> .emscripten
	echo "NODE_JS = '/usr/bin/nodejs'" >> .emscripten
	echo "LLVM_ADD_VERSION = '11'" >> .emscripten
	echo "CLANG_ADD_VERSION = '11'" >> .emscripten

override_dh_auto_build-arch:
	./embuilder build SYSTEM
	./embuilder build SYSTEM --lto
	./embuilder build SYSTEM --pic
	./embuilder build SYSTEM --pic --lto
	echo "FROZEN_CACHE = True" >> .emscripten

override_dh_auto_build-indep:

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test-arch:
	./emcc -v 2>&1
	./emcc tests/hello_world.c
	eslint --no-eslintrc --quiet --env browser --format unix a.out.js
	./emcc -O3 tests/hello_world.c
	eslint --no-eslintrc --quiet --env browser --format unix a.out.js
	rm a.out.js a.out.wasm
	EMTEST_ALL_ENGINES=1 tests/runner.py wasm0 \
		$(addprefix skip:wasm0.,$(TEST_ERRORS) $(TEST_FAILS_wasm0))
	EMTEST_ALL_ENGINES=1 tests/runner.py \
		$(addprefix wasm0.,$(TEST_FAILS_wasm0)) || true
endif

override_dh_auto_test-indep:

# avoid license and Visual Studio files
override_dh_install-arch:
	dh_install $(addprefix --exclude=,\
		COPYING GPL LICENSE License.txt license.dox \
		.dsp .dsw .sln .vcproj)
	mkdir -p debian/emscripten/usr/bin
	set -e; for wrapper in $$(find * -maxdepth 1 -type f -regextype posix-egrep -regex 'em[^.]+'); do \
		printf '#!/bin/sh\nexec "$${EMSDK_PYTHON:-python3}" "/usr/share/emscripten/$$(basename "$$0").py" "$$@"\n' \
			> debian/emscripten/usr/bin/$$wrapper; \
		chmod +x debian/emscripten/usr/bin/$$wrapper; \
	done

override_dh_missing:
	dh_missing --fail-missing

# restore maybe-outdated upstream version hint
override_dh_auto_clean:
	[ ! -e emscripten-version.txt.orig ] \
		|| mv -f emscripten-version.txt.orig emscripten-version.txt
	rm -rf cache
	rm -f .emscripten $(SOURCE_MAP_MIN_JS) $(NEON_H)

# generate header file from SIMDe source
$(NEON_H): /usr/include/simde/arm/neon.h
	echo '#define SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES' > $@
	echo '#define SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES' >> $@
	cat $< >> $@
	echo '#undef SIMDE_ARM_NEON_A32V7_ENABLE_NATIVE_ALIASES' >> $@
	echo '#undef SIMDE_ARM_NEON_A64V8_ENABLE_NATIVE_ALIASES' >> $@

# join source-less and source-only parts of upstream source-map script
$(SOURCE_MAP_MIN_JS): \
 /usr/share/javascript/source-map/source-map.min.js \
 debian/missing-sources/source-map-footer_$(DEB_VERSION_UPSTREAM_ORIG).js
	cat $^ > $@

# custom rule: get source-only part of upstream source-map script
# (see README.source)
get-source-map-footer:
	wget -O- https://github.com/emscripten-core/emscripten/raw/$(DEB_VERSION_UPSTREAM_ORIG)/src/emscripten-source-map.min.js \
		| tail -n +2 \
		> debian/missing-sources/source-map-footer_$(DEB_VERSION_UPSTREAM_ORIG).js

%:
	dh $@
