# compiled qubx_lib

# put the boost headers in the main project dir, e.g. $ ln -s /usr/include/boost boost
EMFLAGS_MAIN := -s TOTAL_MEMORY=128000000

EMFLAGS := -I. -s DISABLE_EXCEPTION_CATCHING=0 -s NO_EXIT_RUNTIME=1 -std=c++11 -s MODULARIZE=1
# --closure 1
EMFLAGS_OPT := -O3
EMFLAGS_DBG := -s ASSERTIONS=1 -s DEMANGLE_SUPPORT=1 

SOURCES := $(shell find lib -mindepth 1 -maxdepth 1 -name "*.cpp")
HEADERS := $(shell find lib -mindepth 1 -maxdepth 3 -name "*.h")
JSFILES := $(shell find . -mindepth 1 -maxdepth 3 -name "*.js")
HTMLFILES := $(shell find . -mindepth 1 -maxdepth 3 -name "*.html")
CSSFILES := $(shell find . -mindepth 1 -maxdepth 3 -name "*.css")
IMGFILES := $(shell find . -mindepth 1 -maxdepth 3 -name "*.png") \
            $(shell find . -mindepth 1 -maxdepth 3 -name "*.svg")
AUXFILES := $(shell find . -mindepth 1 -maxdepth 3 -name "*.sql") \
            $(shell find . -mindepth 1 -maxdepth 3 -name "*.json") \
            $(shell find . -mindepth 1 -maxdepth 3 -name "*.opencl") \
            $(shell find . -mindepth 1 -maxdepth 3 -name "*.LICENSE") \
            $(shell find . -mindepth 1 -maxdepth 3 -path "./init.d/*") \
            $(shell find . -mindepth 1 -maxdepth 3 -name "*.map") \
            $(shell find . -mindepth 1 -maxdepth 3 -name "Makefile") \
            LICENSE.txt README.txt
HJCFILES := $(shell find qubx_hjcfit -mindepth 1 -maxdepth 3)
ALLFILES := $(SOURCES) $(HEADERS) $(JSFILES) $(HTMLFILES) $(CSSFILES) $(IMGFILES) $(AUXFILES) $(HJCFILES)
OBJDIR := obj
OBJECTS := $(patsubst %.cpp,$(OBJDIR)/%.o,$(SOURCES))
EMOBJECTS := $(patsubst %.cpp,$(OBJDIR)/%.bc,$(SOURCES))
DEPFILES := $(patsubst %.cpp,$(OBJDIR)/%.d,$(SOURCES))
CFLAGS := -W -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align \
          -Wwrite-strings -Wredundant-decls -Wno-long-long -Wconversion \
          -g -fPIC -O3 -fopenmp -std=c++11
OPENCL_PATH := $(shell locate libOpenCL.so | grep lib)
#ifneq "$(wildcard $(OPENCL_PATH))" ""
#  OPENCL_FLAGS := -DQUBX_MAC_OPENCL -I$(CUDA_PATH)/include
#  OPENCL_LINK := -lOpenCL
#endif
LNFLAGS := $(OPENCL_LINK) -shared -fPIC -lm -g -lgomp

# only non-minified, non-asm js
JS_SOURCES := $(shell find static/js -mindepth 1 -maxdepth 1 -name "*.js" | perl -nle 'print if (/^((?!\.min\.).)*\.js/ && /^((?!_asm\.).)*\.js/ && /^((?!qubxhjc\.).)*\.js/)')

# minifying with uglify-js:
# $ npm install uglify-js -g

# JS_MINIFIED := $(patsubst %.js,%.min.js,$(JS_SOURCES))

default: all

-include $(DEPFILES)

all: asm lib install zip

clean:
	rm static/js/qubx_lib_asm.js*
	rm static/js/*.min.js
	rm libqubonline.so
	rm $(OBJDIR)/*/*.o $(OBJDIR)/*/*.bc $(OBJDIR)/*/*.d

release: static/version asm

VERSION  := $(shell cat static/version)
VNEXT    := $(shell echo $$(($(VERSION) + 1)))

static/version: asm
	echo $(VNEXT) > static/version
	#sed -i 's/const CACHE_VERSION[^"]*"v$(VERSION)/const CACHE_VERSION = "v$(VNEXT)/g' service-worker.js

#%.min.js: %.js Makefile
#	cd static/js; uglifyjs ../../$< -o ../../$@ --source-map $(*F).min.js.map -p 1 -c -m; cd -

asm: static/js/qubx_lib_asm.js

EMOBJECTS_D = $(patsubst %.bc,%.bcd,$(EMOBJECTS))
asmd: $(EMOBJECTS_D) Makefile
	-rm $(OBJDIR)/lib/*.bc
	rename 's/.bcd$$/.bc/' $(OBJDIR)/lib/*.bcd
	emcc $(EMFLAGS) $(EMFLAGS_DBG) -o $(OBJDIR)/qubx_lib_asm.js $(EMOBJECTS)
	cp $(OBJDIR)/qubx_lib_asm.js.mem static/
	cp $(OBJDIR)/qubx_lib_asm.js.mem static/js/
	echo 'define("qubx_lib_asm", [], function() {' > static/js/qubx_lib_asm.js
	cat $(OBJDIR)/qubx_lib_asm.js >> static/js/qubx_lib_asm.js
	echo '    return Module; });' >> static/js/qubx_lib_asm.js
	rm $(OBJDIR)/qubx_lib_asm.js

static/js/qubx_lib_asm.js: $(OBJDIR)/qubx_lib_asm.js Makefile
	echo 'define("qubx_lib_asm", [], function() {' > static/js/qubx_lib_asm.js
	cat $(OBJDIR)/qubx_lib_asm.js >> static/js/qubx_lib_asm.js
	echo '    return Module; });' >> static/js/qubx_lib_asm.js
$(OBJDIR)/qubx_lib_asm.js: $(EMOBJECTS) Makefile
	emcc $(EMFLAGS) $(EMFLAGS_OPT) -o $(OBJDIR)/qubx_lib_asm.js $(EMOBJECTS)
	cp $(OBJDIR)/qubx_lib_asm.js.mem static/
	cp $(OBJDIR)/qubx_lib_asm.js.mem static/js/


lib: libqubonline.so

libqubonline.so: $(OBJECTS) Makefile
	g++ -o libqubonline.so $(OBJECTS) $(LNFLAGS)


$(OBJDIR)/%.bc: %.cpp Makefile
	emcc $(EMFLAGS) $(EMFLAGS_OPT) $< -o $@

$(OBJDIR)/%.bcd: %.cpp Makefile
	emcc $(EMFLAGS) $(EMFLAGS_DBG) $< -o $@

$(OBJDIR)/%.o: %.cpp Makefile
	-@mkdir -p $(dir $@)
	@g++ $(CFLAGS) -MMD -MP -MT "$(OBJDIR)/$*.d $(OBJDIR)/$*.o" -c $< -o $@

$(OBJDIR)/lib/max_mac_ll.d $(OBJDIR)/lib/max_mac_ll.o: Makefile \
 lib/max_mac_ll.cpp \
 lib/qub_constraints.h lib/ublas_plus.h lib/qubfast.h \
 lib/max_mac_ll.h lib/qubfast.h \
 lib/max_ll_util.h lib/ublas_plus.h lib/ublas_matrixutil.h \
 lib/matrixutil.h lib/milutil.h lib/matrix.h \
 lib/callbk_reportstream.h lib/callbk_reportfun.h \
 lib/max_mac_ll.opencl
	-@mkdir -p $(OBJDIR)/lib
	@g++ $(CFLAGS) $(OPENCL_FLAGS) -MMD -MP -MT "$(OBJDIR)/lib/max_mac_ll.d $(OBJDIR)/lib/max_mac_ll.o" -c lib/max_mac_ll.cpp -o $(OBJDIR)/lib/max_mac_ll.o

$(OBJDIR)/lib/max_subi_ll.d $(OBJDIR)/lib/max_subi_ll.o: Makefile \
 lib/max_subi_ll.cpp \
 lib/max_subi_ll.h lib/qubfast.h lib/max_ll_util.h lib/ublas_plus.h \
 lib/ublas_matrixutil.h lib/matrixutil.h lib/milutil.h lib/matrix.h \
 lib/callbk_reportstream.h lib/callbk_reportfun.h lib/asmffi.h
	-@mkdir -p $(OBJDIR)/lib
	@g++ $(CFLAGS) $(OPENCL_FLAGS) -MMD -MP -MT "$(OBJDIR)/lib/max_subi_ll.d $(OBJDIR)/lib/max_subi_ll.o" -c lib/max_subi_ll.cpp -o $(OBJDIR)/lib/max_subi_ll.o

# server:

install: package.json
	npm install

serve:
	nodejs index.js

qubsite: install
	nodejs index-qubsite.js

debug:
	nodejs --debug-brk index.js

inspector:
	node-inspector

createdb:
	mysql -u root -p <createdb.sql

deldb:
	mysql -u root -p <deldb.sql

source: $(ALLFILES)
	-@rm -fr static/qub-online
	mkdir static/qub-online
	-@rsync -avR $(ALLFILES) --exclude="node_modules" static/qub-online/
	find static/qub-online -name node_modules -exec rm -fr {} \; -prune
	rm -fr static/qub-online/static/qub-online
	rm -fr static/qub-online/obj
	rm -fr static/qub-online/qubx_hjcfit/obj
	rm -fr static/qub-online/static/js/qubx_lib_asm.js
	rm -fr static/qub-online/static/js/*.min.js*
	cd static && ../indexdir.py qub-online && cd -

zip: source release
	-@rm static/qub-online.zip
	cd static && zip -r qub-online.zip qub-online
