#
# 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.6
TRUETYPE=../../truetype-1.14
# yudit_sedy-0.7.2 had a bug on arm64
YUDIT_SEDY=../../yudit_sedy-0.7.5


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

.PHONY: copy syntax

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


CPPFLAGS:=$(CPPFLAGS) -DNO_MAIN

all: $(TARGET) syntax

syntax:
ifneq (,$(wildcard  syntax/*))
	$(MAKE) -C syntax all
endif

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

# In case you have WARNING: 'aclocal-1.13' is missing on your system.
# in syntax, you should touch these files in syntax-1.2/src/hunspell-1.3.0-cvs/
# touch configure.ac aclocal.m4 configure Makefile.am Makefile.in
# https://stackoverflow.com/questions/18769770/user-of-autotools-generated-tarball-gets-error-message-aclocal-1-13-command-no
# We hope original timestamps are correc, so we copy with -pR
copy:
ifneq (,$(wildcard  $(YUDIT_SEDY)/*))
	cp -pR $(YUDIT_SEDY) yudit_sedy
	rm -rf yudit_sedy/*.obj yudit_sedy/*.o
else
	@echo WARNING yudit_sedy plugin skipped.
endif
ifneq (,$(wildcard  $(SYNTAX)/*))
	cp -pR $(SYNTAX) syntax
else
	@echo WARNING syntax plugin skipped.
endif
ifneq (,$(wildcard  $(TRUETYPE)/*))
	cp -pR $(TRUETYPE) truetype
	make -C truetype all
	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
