#
# This is a simple Makefile for one level c++ addons.
#
# Gaspar Sinai <gaspar@yudit.org>
# 2020-05-04, Tokyo
#
# If you see broken links here this is beta version.
#
include ../Makefile.conf

SOURCES=$(wildcard */*.cpp)

#
# In case you have those addons.
#
SYNTAX=../../syntax-1.2
TRUETYPE=../../truetype-1.12
# yudit_sedy-0.7.2 had a bug on arm64
YUDIT_SEDY=../../yudit_sedy-0.7.4


ifeq ($(SPLATFORM),WINDOWS)
OBJS=$(subst .cpp,.obj,$(SOURCES))
TARGET=addon.lib
else
OBJS=$(subst .cpp,.o,$(SOURCES))
TARGET=libaddon.a
endif

.PHONY: copy

SUBDIRS=$(sort $(dir $(SOURCES)))
SUBDIR_CPP_FLAGS=$(patsubst %,-I$(TOPDIR)/addon/%,$(SUBDIRS))


CPPFLAGS:=$(CPPFLAGS) -DNO_MAIN

all: $(TARGET)

addon.lib: $(OBJS)
	$(AR)$@ $(OBJS)

libaddon.a: $(OBJS)
	$(AR) $@ $(OBJS)
	$(RANLIB) $@      

%.o:%.cpp
	$(CXX) -c -o $@ $(SUBDIR_CPP_FLAGS) $(CPPFLAGS) $(patsubst %.o,%.cpp,$@)

#Windows will create it in local, we need to move it
%.obj:%.cpp
	$(CXX) -c $(SUBDIR_CPP_FLAGS) $(CPPFLAGS) $(patsubst %.obj,%.cpp,$@)
	mv $(@F) $(@D)

depend:
	$(CXX) -M $(SUBDIR_CPP_FLAGS) $(CPPFLAGS)  $(patsubst %.o,%.cpp,$(OBJS)) > .depend

clean: 
	rm -f $(wildcard */*.o)   $(wildcard */*.obj) $(TARGET) .depend

distclean: clean
	rm -rf syntax truetype yudit_sedy

copy:
ifneq (,$(wildcard  $(YUDIT_SEDY)/*))
	cp -R $(YUDIT_SEDY) yudit_sedy
	rm -rf yudit_sedy/*.obj yudit_sedy/*.o
else
	@echo WARNING yudit_sedy plugin skipped.
endif
ifneq (,$(wildcard  $(SYNTAX)/*))
	cp -R $(SYNTAX) syntax
	rm -rf syntax/src
else
	@echo WARNING syntax plugin skipped.
endif
ifneq (,$(wildcard  $(TRUETYPE)/*))
	cp -R $(TRUETYPE) truetype
	rm -rf truetype/reduced
	rm -rf truetype/candidate
else
	@echo WARNING truetype plugin skipped.
endif


install:

# This is called by bin/wininst on windows.
wininst_fonts:
ifneq (,$(wildcard  truetype/target/*))
	cp -R truetype/target/* $(INSTALLDIR)/fonts 
else
	@echo WARNING truetype plugin skipped.
endif

wininst_syntax:
ifneq (,$(wildcard  syntax/target/*))
	cp -R syntax/target/* $(INSTALLDIR)/syntax 
else
	@echo WARNING syntax plugin skipped
endif

ifeq (.depend, $(wildcard .depend))
include .depend
endif
