#!/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_bad_typeid \
 test_bullet_autoconf \
 test_bullet_cmake \
 test_cube2hash_standalone \
 test_dlfcn_alignment_and_zeroing \
 test_dlfcn_basic \
 test_dlfcn_data_and_fptr \
 test_dlfcn_funcs \
 test_dlfcn_handle_alloc \
 test_dlfcn_i64 \
 test_dlfcn_info \
 test_dlfcn_longjmp \
 test_dlfcn_mallocs \
 test_dlfcn_missing \
 test_dlfcn_qsort \
 test_dlfcn_self \
 test_dlfcn_stacks \
 test_dlfcn_unique_sig \
 test_dlfcn_varargs \
 test_dylink_basics \
 test_dylink_class \
 test_dylink_dot_a \
 test_dylink_dso_needed \
 test_dylink_dynamic_cast \
 test_dylink_floats \
 test_dylink_funcpointer \
 test_dylink_funcpointers_wrapper \
 test_dylink_function_pointer_equality \
 test_dylink_global_init \
 test_dylink_global_inits \
 test_dylink_global_var \
 test_dylink_global_var_modded \
 test_dylink_i64 \
 test_dylink_i64_b \
 test_dylink_i64_c \
 test_dylink_i64_invoke \
 test_dylink_iostream \
 test_dylink_jslib \
 test_dylink_many_postsets \
 test_dylink_memory_growth \
 test_dylink_no_export \
 test_dylink_postsets_chunking \
 test_dylink_printfs \
 test_dylink_raii_exceptions \
 test_dylink_rtti \
 test_dylink_safe_heap \
 test_dylink_spaghetti \
 test_dylink_static_funcpointer_float \
 test_dylink_static_funcpointers \
 test_dylink_stdlib \
 test_dylink_zlib \
 test_em_js_linked \
 test_emscripten_log \
 test_exceptions \
 test_exceptions_2 \
 test_exceptions_3 \
 test_exceptions_alias \
 test_exceptions_allowed \
 test_exceptions_allowed_2 \
 test_exceptions_allowed_uncaught \
 test_exceptions_convert \
 test_exceptions_custom \
 test_exceptions_destroy_virtual \
 test_exceptions_libcxx \
 test_exceptions_minimal_runtime \
 test_exceptions_multi \
 test_exceptions_multiple_inherit \
 test_exceptions_multiple_inherit_rethrow \
 test_exceptions_primary \
 test_exceptions_refcount \
 test_exceptions_resume \
 test_exceptions_rethrow \
 test_exceptions_simplify_cfg \
 test_exceptions_std \
 test_exceptions_typed \
 test_exceptions_uncaught_2 \
 test_exceptions_uncaught \
 test_exceptions_uncaught_count \
 test_exceptions_virtual_inheritance \
 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_main_module_static_align \
 test_maybe_wasm2js \
 test_modularize_closure_pre \
 test_posixtime_standalone \
 test_pthread_exceptions \
 test_relocatable_void_function \
 test_safe_stack_dylink \
 test_setjmp_many \
 test_setjmp_many_2 \
 test_setjmp_noleak \
 test_siglongjmp \
 test_stack_placement_pic \
 test_wasm2js \
 test_wasm2js_fallback

NONSCRIPT_EXECUTABLE = \
 system/include/libcxx/readme.txt \
 system/lib/libcxx/readme.txt \
 tests/other/wasm_sourcemap/foo.wasm \
 tests/other/wasm_sourcemap_dead/t.wasm \
 third_party/ply/test/testlex.py \
 tools/system_libs.py

SCRIPT_NOT_EXECUTABLE = \
 src/compiler.js \
 tests/embind/build_benchmark \
 third_party/uglify-js/test/beautify.js \
 third_party/uglify-js/test/testparser.js \
 third_party/uglify-js/tmp/test.js \
 tools/js-optimizer.js \
 tools/preprocessor.js \
 tools/source-maps/sourcemap2json.js \
 tools/source-maps/sourcemapper.js

# 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/node'" >> .emscripten
	echo "LLVM_ADD_VERSION = '11'" >> .emscripten
	echo "CLANG_ADD_VERSION = '11'" >> .emscripten

override_dh_auto_build:

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
# use ccache when available
ifneq (,$(wildcard /usr/bin/ccache))
override_dh_auto_test: export EM_COMPILER_WRAPPER=ccache
endif
override_dh_auto_test:
	PATH="$(CURDIR):$$PATH" emcc -v 2>&1
	PATH="$(CURDIR):$$PATH" emcc tests/hello_world.c
	eslint --no-eslintrc --quiet --env browser --format unix a.out.js
	PATH="$(CURDIR):$$PATH" emcc -O3 tests/hello_world.c
	eslint --no-eslintrc --quiet --env browser --format unix a.out.js
	rm a.out.js a.out.wasm
	PATH="$(CURDIR):$$PATH" EMTEST_ALL_ENGINES=1 tests/runner.py wasm0 \
		$(addprefix skip:wasm0.,$(TEST_ERRORS) $(TEST_FAILS_wasm0))
	PATH="$(CURDIR):$$PATH" EMTEST_ALL_ENGINES=1 tests/runner.py \
		$(addprefix wasm0.,$(TEST_FAILS_wasm0)) || true
endif

# avoid license and Visual Studio files
override_dh_install:
	dh_install $(addprefix --exclude=,\
		COPYING GPL LICENSE License.txt license.dox \
		.dsp .dsw .sln .vcproj)

# webassembly code is alien to dh_strip
override_dh_strip:
	dh_strip --exclude=/usr/share/emscripten/cache

override_dh_fixperms:
	dh_fixperms
	chmod -x $(addprefix debian/emscripten/usr/share/emscripten/,\
		$(NONSCRIPT_EXECUTABLE))
	chmod +x $(addprefix debian/emscripten/usr/share/emscripten/,\
		$(SCRIPT_NOT_EXECUTABLE))

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 $@
