#!/usr/bin/make -f

# resolve DEB_VERSION_UPSTREAM DEB_DISTRIBUTION
include /usr/share/dpkg/pkg-info.mk

# resolve DEB_BUILD_OPTION_PARALLEL
-include /usr/share/dpkg/buildopts.mk

# resolve if release is experimental
EXP_RELEASE = $(filter experimental% UNRELEASED,$(DEB_DISTRIBUTION))

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

DOCS_SRC = README.md
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DOCS = $(DOCS_SRC:md=html) $(DOCS_SRC:md=txt)
CHANGELOGS = ChangeLog.html ChangeLog.txt
endif

_ENV = $(strip \
 PATH="$(CURDIR)/debian/tmpbin:$$PATH" \
 EM_CACHE="$(CURDIR)/debian/em_cache")

# generate manpage with help2man from --help option of python script
_mkman = $(_ENV) \
 help2man $(if $3,--name "$(strip $3)") --no-info --version-string $(DEB_VERSION_UPSTREAM) --output $2 $1 \
 || { $(_ENV) $1 --help; false; }
_mkman_sloppy = $(_ENV) \
 help2man $(if $3,--name "$(strip $3)") --no-info --version-string $(DEB_VERSION_UPSTREAM) --output $2 $1 \
 || true

ifneq (,$(DEB_BUILD_OPTION_PARALLEL))
export EMCC_CORES=$(DEB_BUILD_OPTION_PARALLEL)
endif

EMCC = $(_ENV) emcc
TESTS_RUNNER = $(_ENV) EMTEST_LACKS_CLOSURE_COMPILER=1 EMTEST_SKIP_V8=1 tests/runner.py --skip-slow

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

# libraries available for compilation (ports are unavailable)
LIBS_ALL = $(shell $(_ENV) embuilder --em-config .emscripten --help \
 | grep -Po '^        \K\S+')
LIBSTEMS_PORTS = $(shell find tools/ports -maxdepth 1 -type f \
 | grep -Po 'tools/ports/\K[a-z]+' | sort -u)
LIBS_MAIN = $(filter-out $(addsuffix %,$(LIBSTEMS_PORTS)),$(LIBS_ALL))

# requires third-party tests
#  * test_freetype also requires symlinked LiberationSansBold.ttf
#  * test_poppler also requires tests.poppler.paper.pdf
ERRORS_core += \
 test_freetype \
 test_lua \
 test_openjpeg \
 test_poppler

# requires Emscripten Ports
ERRORS_other += \
 test_boost_graph \
 test_bullet \
 test_bullet_autoconf \
 test_bullet_cmake \
 test_bzip2 \
 test_cmake_find_sdl2 \
 test_cmake_find_stuff \
 test_cmake_html \
 test_deps_info \
 test_freetype \
 test_giflib \
 test_libjpeg \
 test_libpng \
 test_sdl2_config \
 test_sdl2_gfx_linkable \
 test_sdl2_linkable \
 test_sdl2_mixer_wav \
 test_sdl2_ttf \
 test_vorbis

# attempts to download files from a remote server
ERRORS_other += \
 test_icu \
 test_pthread_icu

# avoid unimportant mode causing multiple failures: strict
CHECK_MODES_coreX = core0 core1 core2 core3 cores corez
CHECK_MODES_coreZ = corez wasm2jsz
CHECK_MODES_core23Z = core2 core3 corez
CHECK_MODES_coreJS = wasm2js1 wasm2js2 wasm2js3 wasm2jss wasm2jsz
CHECK_MODES_coreJS1S = wasm2js1 wasm2jss

# expects smaller output
FAILS_core1 += \
 test_emscripten_lazy_load_code_unconditional
FAILS_other += \
 test_function_exports_are_small \
 test_js_function_names_are_minified \
 test_metadce_minimal_pthreads \
 test_minimal_runtime_code_size_*

# needs node module wasm2c
FAILS_other += \
 test_wasm2c_multi_lib \

# requires bugfix for Error: FS error
FAILS_core += \
 test_fs_nodefs_home

# requires bugfix for RuntimeError: memory access out of bounds
FAILS_core += \
 test_pthread_dylink_exceptions

# unreliable; sometimes fails with AssertionError: Expected to find ... pthread sent an error! undefined:undefined: Aborted().
FAILS_core += \
 test_pthread_abort \

# likely requires newer LLVM
ERRORS_core += \
 test_dylink_syslibs_missing_assertions
FAILS_other += \
 test_lld_report_undefined_exceptions

# unknown cause
FAILS_coreX += \
 test_dlfcn_self \
 test_pthread_exit_process \
 test_pthread_proxying
FAILS_other += \
 test_cmake_stdproperty \
 test_embind_finalization \
 test_ld_library_path \
 test_metadce_hello_main_module_2 \
 test_minimal_dynamic \
 test_split_main_module

BIN_WRAPPER_SYMLINK = \
 em++ \
 emar \
 embuilder \
 emcc \
 emcmake \
 em-config \
 emconfigure \
 emmake \
 emranlib \
 emrun \
 emscons \
 emsize

BIN_WRAPPER_TOOLS_SYMLINK = \
 emdump \
 emdwp \
 emnm \
 emprofile \

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

SCRIPT_NOT_EXECUTABLE = \
 src/compiler.js \
 tests/embind/build_benchmark \
 tests/sourcemap2json.js \
 tools/preprocessor.js \
 tools/run_python_compiler.sh

override_dh_clean:
	dh_clean -- $(DOCS) $(CHANGELOGS)

override_dh_auto_configure: $(SOURCE_MAP_MIN_JS) $(NEON_H)
# refresh upstream version hints from Debian package
	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
	mkdir -p debian/em_cache
# setup binaries for use during build-time testing
	mkdir --parents debian/tmpbin
	sed "s,/usr/share/emscripten/,$$PWD/," < debian/bin/wrapper > debian/tmpbin/wrapper
	sed "s,/usr/share/emscripten/,$$PWD/," < debian/bin/wrapper-tools > debian/tmpbin/wrapper-tools
	chmod +x debian/tmpbin/wrapper debian/tmpbin/wrapper-tools
	$(foreach symlink,$(BIN_WRAPPER_SYMLINK),ln -fsT wrapper debian/tmpbin/$(symlink);)
	$(foreach symlink,$(BIN_WRAPPER_TOOLS_SYMLINK),ln -fsT wrapper-tools debian/tmpbin/$(symlink);)

execute_before_dh_auto_build:
	$(EMCC) --generate-config

# build frozen cache of non-port libraries unless noopt requested
override_dh_auto_build:
ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	$(_ENV) embuilder build $(LIBS_MAIN)
	$(_ENV) embuilder build $(LIBS_MAIN) --lto
	$(_ENV) embuilder build $(LIBS_MAIN) --pic
	$(_ENV) embuilder build $(LIBS_MAIN) --pic --lto
endif
	perl -p \
		-e "s/^# FROZEN_CACHE/FROZEN_CACHE/;" \
		-e "s,^EMSCRIPTEN_ROOT = '\K[^']+,/usr/share/emscripten,;" \
		.emscripten > debian/.emscripten

execute_after_dh_auto_build-arch: $(DOCS) $(CHANGELOGS)

# build documentation unless nodoc requested
execute_after_dh_auto_build-indep: $(DOCS) $(CHANGELOGS)
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	sphinx-build -b html site/source debian/doc/html
endif

# disable tests, too many where failing
ifeq (0,1)
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	$(EMCC) tests/hello_world.c
	eslint --no-eslintrc --quiet --env browser --parser-options=ecmaVersion:6 --format unix a.out.js
	$(EMCC) -O3 tests/hello_world.c
	eslint --no-eslintrc --quiet --env browser --parser-options=ecmaVersion:6 --format unix a.out.js
	rm a.out.js a.out.wasm
	ln -sT /usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf \
		tests/freetype/LiberationSansBold.ttf
	$(TESTS_RUNNER) test_zlib_configure test_zlib_cmake
	$(TESTS_RUNNER) \
		$(CHECK_MODES_coreX) $(CHECK_MODES_coreJS) \
		$(addprefix skip:core*.,$(ERRORS_core) $(FAILS_core)) \
		$(addprefix skip:wasm*.,$(ERRORS_core) $(FAILS_core)) \
		$(addprefix skip:core1.,$(FAILS_core1)) \
		$(addprefix skip:core2.,$(FAILS_core2)) \
		$(foreach mode,$(CHECK_MODES_coreX),\
			$(addprefix skip:$(mode).,$(FAILS_coreX))) \
		$(foreach mode,$(CHECK_MODES_coreZ),\
			$(addprefix skip:$(mode).,$(FAILS_coreZ))) \
		$(foreach mode,$(CHECK_MODES_core23Z),\
			$(addprefix skip:$(mode).,$(FAILS_core23Z))) \
		$(foreach mode,$(CHECK_MODES_coreJS),\
			$(addprefix skip:$(mode).,$(FAILS_coreJS))) \
		$(foreach mode,$(CHECK_MODES_coreJS1S),\
			$(addprefix skip:$(mode).,$(FAILS_coreJS1S))) \
		$(if $(EXP_RELEASE),|| true)
	$(TESTS_RUNNER) other \
		$(addprefix skip:other.,$(ERRORS_other) $(FAILS_other)) \
		$(if $(EXP_RELEASE),|| true)
ifneq (,$(EXP_RELEASE))
	$(TESTS_RUNNER) \
		$(addprefix core1.,$(FAILS_core1)) \
		$(addprefix core2.,$(FAILS_core2)) \
		$(foreach mode,$(CHECK_MODES_coreX) $(CHECK_MODES_coreJS),\
			$(addprefix $(mode).,$(FAILS_core))) \
		$(foreach mode,$(CHECK_MODES_coreX),\
			$(addprefix $(mode).,$(FAILS_coreX))) \
		$(foreach mode,$(CHECK_MODES_coreZ),\
			$(addprefix $(mode).,$(FAILS_coreZ))) \
		$(foreach mode,$(CHECK_MODES_core23Z),\
			$(addprefix $(mode).,$(FAILS_core23Z))) \
		$(foreach mode,$(CHECK_MODES_coreJS),\
			$(addprefix $(mode).,$(FAILS_coreJS))) \
		$(foreach mode,$(CHECK_MODES_coreJS1S),\
			$(addprefix $(mode).,$(FAILS_coreJS1S))) \
		$(addprefix other.,$(FAILS_other)) \
		$(addprefix skip:other.,$(ERRORS_other)) \
		|| true
endif
	rm -f tests/freetype/LiberationSansBold.ttf
endif
endif

execute_after_dh_auto_install:
	$(call _mkman, debian/tmpbin/em++, debian/em++.1,\
		emscripten compiler for WASM and JavaScript like g++ or clang++)
	$(call _mkman, debian/tmpbin/emar, debian/emar.1,\
		emscripten archiver for WASM and JavaScript like ar or llvm-ar)
	$(call _mkman, debian/tmpbin/embuilder, debian/embuilder.1,\
		Tool to manage building of Emscripten system libraries and ports)
	$(call _mkman, debian/tmpbin/emcc, debian/emcc.1,\
		emscripten compiler for WASM and JavaScript like gcc or clang)
	$(call _mkman_sloppy, debian/tmpbin/emcmake, debian/emcmake.1,\
		emscripten wrapper cmake)
	$(call _mkman_sloppy, debian/tmpbin/em-config, debian/em-config.1,\
		helper tool to read emscripten configuration variables)
	$(call _mkman_sloppy, debian/tmpbin/emconfigure, debian/emconfigure.1,\
		emscripten wrapper around ./configure scripts)
	$(call _mkman_sloppy, debian/tmpbin/emmake, debian/emmake.1,\
		emscripten wrapper around make)
	$(call _mkman, debian/tmpbin/emranlib, debian/emranlib.1,\
		emscripten wrapper around llvm-ranlib)
	$(call _mkman, debian/tmpbin/emrun, debian/emrun.1,\
		emscripten tool to compile as an HTML page)
#	$(call _mkman, debian/tmpbin/emscons, debian/emscons.1,\
#		emscripten wrapper around scons)
	$(call _mkman, debian/tmpbin/emsize, debian/emsize.1,\
		emscripten replacement for size)
	$(call _mkman, debian/tmpbin/emdump, debian/emdump.1,\
		emscripten tool to print out statistics about compiled code sizes)
	$(call _mkman, debian/tmpbin/emdwp, debian/emdwp.1,\
		emscripten wrapper around llvm-dwp)
	$(call _mkman, debian/tmpbin/emnm, debian/emnm.1,\
		emscripten wrapper around llvm-nm)
	$(call _mkman, debian/tmpbin/emprofile, debian/emprofile.1,\
		emscripten profiler tool)

# avoid cache sanity hint, license and Visual Studio files
ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
INSTALL_FROZEN_CACHE = debian/em_cache/* /usr/share/emscripten/cache
endif
override_dh_install:
	dh_install $(addprefix --exclude=,\
		sanity.txt \
		COPYING GPL LICENSE License.txt license.dox \
		.dsp .dsw .sln .vcproj) \
		$(INSTALL_FROZEN_CACHE)

override_dh_installdocs:
	dh_installdocs -- $(DOCS)

override_dh_installchangelogs:
	dh_installchangelogs -- $(CHANGELOGS)

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

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

# 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 debian/em_cache debian/tmpbin
	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

# TODO: drop when bug#1000136 is fixed
override_dh_sphinxdoc:
	dh_sphinxdoc -i || { \
	rm -rf debian/emscripten-doc/usr/share/doc/emscripten-doc/html/.doctrees;\
	find debian/emscripten-doc -type f -name .buildinfo -delete;\
	ln --symbolic --force \
		--target-directory=debian/emscripten-doc/usr/share/doc/emscripten-doc/html/_static/ \
		$(addprefix ../../../../javascript/sphinxdoc/1.0/,doctools.js jquery.js language_data.js searchtools.js underscore.js);\
	}

override_dh_gencontrol:
	dh_gencontrol -- -V"types:Version=$(shell jq --raw-output .version < Xtypes/package.json)~$(DEB_VERSION)"

%.html: %.md
	cmark-gfm $< > $@

%.txt: %.md
	cmark-gfm --to plaintext $< > $@

%:
	dh $@
