# Note: The official Hermes documentation includes zero guidance on embedding.=
# Tested against commit d070c749bcf6fb635579433421a976c29d7a2bb7 on darwin-x64
# History https://git.sheetjs.com/sheetjs/docs.sheetjs.com/commits/branch/master/docz/static/hermes/Makefile

MYCC=llvm-g++
POSTAMBLE=-framework CoreFoundation -Wl,-rpath .
UNAME__S := $(shell uname -s)
ifeq ($(UNAME__S),Linux)
MYCC=g++
POSTAMBLE=-pthread -licuuc -licudata -licuio -licutu -licui18n -Wl,-rpath=./
endif

.PHONY: doit
doit: sheetjs-hermes
	if [ ! -e xlsx.full.min.js ]; then curl -LO https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js; fi
	if [ ! -e pres.numbers ]; then curl -LO https://docs.sheetjs.com/pres.numbers; fi
	if [ -e ./build_release/API/hermes/libhermes.so ]; then cp ./build_release/API/hermes/libhermes.so .; fi
	if [ -e ./build_release/API/hermes/libhermes.dylib ]; then cp ./build_release/API/hermes/libhermes.dylib .; fi
	if [ -e ./build_release/jsi/libjsi.so ]; then cp ./build_release/jsi/libjsi.so .; fi
	if [ -e ./build_release/jsi/libjsi.dylib ]; then cp ./build_release/jsi/libjsi.dylib .; fi
	./sheetjs-hermes pres.numbers

.PHONY: clean
clean:
	rm -f sheetjs-hermes

.PHONY: clean-all
clean-all: clean
	rm -rf build_release hermes xlsx.full.min.js pres.numbers

# This sequence was cobbled together by linking against every artifact.
# Some of these lines are likely extraneous
sheetjs-hermes: sheetjs-hermes.cpp init
	$(MYCC) $< -o $@ -std=gnu++17  \
		-Ihermes/include/ -Ihermes/API/ -Ihermes/API/jsi -Ihermes/public \
		-Lbuild_release/API/hermes/  -lsynthTrace -lsynthTraceParser \
		-lhermes \
		-Lbuild_release/lib/VM/ -lhermesVMRuntime \
		-Lbuild_release/lib/BCGen/HBC/ -lhermesHBCBackend \
		-Lbuild_release/lib/BCGen/ -lhermesBackend \
		-Lbuild_release/lib/SourceMap/ -lhermesSourceMap \
		-Lbuild_release/lib/Parser/ -lhermesParser \
		-Lbuild_release/lib/Platform/Unicode/ -lhermesPlatformUnicode \
		-lcompileJS -ltimerStats -ltraceInterpreter \
		-Lbuild_release/external/llvh/lib/Demangle/ -lLLVHDemangle \
		-Lbuild_release/external/llvh/lib/Support/ -lLLVHSupport \
		-Lbuild_release/jsi/ -ljsi \
		-Lbuild_release/lib/ -lhermesFrontend \
		-Lbuild_release/lib/ -lhermesOptimizer \
		-Lbuild_release/lib/ADT -lhermesADT \
		-Lbuild_release/lib/AST/ -lhermesAST \
		-Lbuild_release/lib/AST2JS/ -lhermesAST2JS \
		-Lbuild_release/lib/CompilerDriver/ -lhermesCompilerDriver \
		-Lbuild_release/lib/ConsoleHost/ -lhermesConsoleHost \
		-Lbuild_release/lib/DependencyExtractor/ -lhermesDependencyExtractor \
		-Lbuild_release/lib/FlowParser/ -lhermesFlowParser \
		-Lbuild_release/lib/FrontEndDefs/ -lhermesFrontEndDefs \
		-Lbuild_release/lib/Inst/ -lhermesInst \
		-Lbuild_release/lib/InternalBytecode/ -lhermesInternalBytecode \
		-Lbuild_release/lib/Platform/ -lhermesPlatform \
		-Lbuild_release/lib/Platform/Intl/ -lhermesBCP47Parser \
		-Lbuild_release/lib/Regex/ -lhermesRegex \
		-Lbuild_release/lib/Support/ -lhermesSupport \
		-Lbuild_release/public/hermes/Public -lhermesPublic \
		-Lhermes/external/flowparser/ -lflowparser-mac \
		-Lbuild_release/external/dtoa/ -ldtoa \
		$(POSTAMBLE)

sheetjs-hermes.cpp:
	curl -LO https://docs.sheetjs.com/hermes/sheetjs-hermes.cpp

.PHONY: init
init:
	if [ ! -e hermes ]; then git clone https://github.com/facebook/hermes.git; cd hermes; git checkout d070c749bcf6fb635579433421a976c29d7a2bb7; cd ..; fi
	if [ ! -e build_release ]; then cmake -S hermes -B build_release -G Ninja -DCMAKE_BUILD_TYPE=Release -DHERMES_BUILD_APPLE_FRAMEWORK=OFF; cmake --build ./build_release; fi