#!/usr/bin/env bash
# Makefile.emEnv

LIBXML20_CFLAGS += `pkg-config libxml-2.0 --cflags`
LIBXML20_LDFLAGS += `pkg-config libxml-2.0 --libs`

JSONC_CFLAGS += `pkg-config json-c --cflags`
JSONC_LDFLAGS += `pkg-config json-c --libs`

ZLIB_CFLAGS += `pkg-config zlib --cflags`
ZLIB_LDFLAGS += `pkg-config zlib --libs`

all: fastXmlLint.js

fastXmlLint.js: fastXmlLint.c
	emcc -O3 -Wall -pedantic -Wextra $(JSONC_CFLAGS) $(ZLIB_CFLAGS) $(LIBXML20_CFLAGS) \
		-c fastXmlLint.c\
		-s AGGRESSIVE_VARIABLE_ELIMINATION=1 \
		--llvm-lto 3 \
		-o fastXmlLint.o 
	# https://github.com/kripken/emscripten/issues/2872
	# so -s EMULATE_FUNCTION_POINTER_CASTS=1
	emcc -O3 -Wall -pedantic -Wextra -std=gnu1x fastXmlLint.o  $(JSONC_LDFLAGS) $(ZLIB_LDFLAGS) $(LIBXML20_LDFLAGS)\
		-s EXPORTED_FUNCTIONS="['_validate', '_set_schema', '_set_schema_file', \
			'_set_relaxNG_schema', '_set_relaxNG_schema_file', '_validate_NG']" \
		-s ASSERTIONS=2 --emit-symbol-map  \
		-s EMULATE_FUNCTION_POINTER_CASTS=1 \
		--memory-init-file 0 \
		--pre-js pre.js \
		--memory-init-file 1 \
		-s TOTAL_MEMORY=234217728 \
		--llvm-lto 3 \
		--closure 0 \
		-o fastXmlLint.js
	rm -rf fastXmlLint.js.symbols fastXmlLint.o

# FIXME this test nearly works except we do not have a main function but right now this
#       test below assumes that and outputs nothing. (qk)
# test: fastXmlLint
# 	emcc -O3 -Wall -pedantic -Wextra -std=gnu1x fastXmlLint.o  $(JSONC_LDFLAGS) $(ZLIB_LDFLAGS) $(LIBXML20_LDFLAGS)\
# 		-s EXPORTED_FUNCTIONS="['_validate', '_set_schema', '_set_schema_file', \
# 			'_set_relaxNG_schema', '_set_relaxNG_schema_file', '_validate_NG']" \
# 		-s ASSERTIONS=2 --emit-symbol-map  \
# 		-s EMULATE_FUNCTION_POINTER_CASTS=1 \
# 		--memory-init-file 0 \
# 		--pre-js pre.js \
# 		--memory-init-file 1 \
# 		-s TOTAL_MEMORY=234217728 \
# 		--llvm-lto 3 \
# 		--closure 0 \
# 		--embed-file ./html5.xsd --embed-file ./success.html \
# 		-o fastXmlLint.test.js
# 		node ./fastXmlLint.test.js --noout --schema ./html5.xsd ./success.html
# 		rm -rf fastXmlLint.test.js
# 		rm -rf fastXmlLint.test.js.mem

clean:
	rm -rf fastXmlLint.js.mem fastXmlLint.js.symbols fastXmlLint.js fastXmlLint.o
