# UPIWIN - Micro Pi Windowing Framework Kernel
# Copyright (C) 2019 Amy Bowersox/Erbosoft Metaverse Design Solutions
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# ------------------------------------------------------------------------
BUILDUTILS=../buildutils
RESOURCES=../resources
SPLASHSCREEN=splash-erbosoft.png

OBJS=main.o sysinput.o ep_init.o ep_upiwin.o ep_backlight.o ep_msg.o ep_graphics.o ep_devctxt.o ep_bitmap.o \
     ep_upiwin_tmp.o ep_util.o fbinit.o rect.o gfxobj.o devctxt.o dc_screen.o fontengine.o \
	 resources.o bitmap.o stockobj.o fbprimitive.o log.o gpio.o msg_queue.o time_func.o config.o \
	 i_freehand.o i_line.o i_rect.o i_fillrect.o i_undo.o i_clear.o sysresources.o
LIBS=-lpython3.7m -lcrypt -lfreetype -lbcm2835 -lzip -lpthread -ldl -lutil -lm
CFLAGS=-I/usr/include/python3.7m -I/usr/include/freetype2 -I/usr/include/libpng16 \
       -Wall -Werror -fstack-protector -fwrapv -fno-PIE -g -O3 -DDEBUG_ASSERT
LDFLAGS=-L/usr/lib/python3.7/config-3.7m-arm-linux-gnueabihf -Xlinker -export-dynamic -Wl,-O1 \
        -Wl,-Bsymbolic-functions

all: upiwin

upiwin: $(OBJS)
	gcc -o upiwin $(LDFLAGS) $(OBJS) $(LIBS)

.c.o:
	gcc -c $(CFLAGS) $<

sysresources.o: sysresources.zip
	objcopy -I binary -O elf32-littlearm -B arm --rename-section \
		.data=.rodata,alloc,load,readonly,data,contents sysresources.zip sysresources.o

sysresources.zip: splash.bin
	-rm sysresources.zip
	zip sysresources.zip splash.bin

%.o: %.bin
	objcopy -I binary -O elf32-littlearm -B arm --rename-section \
		.data=.rodata,alloc,load,readonly,data,contents $< $@

splash.bin: $(RESOURCES)/$(SPLASHSCREEN) $(BUILDUTILS)/mkgfx
	$(BUILDUTILS)/mkgfx $(RESOURCES)/$(SPLASHSCREEN) splash.bin

i_freehand.bin: $(RESOURCES)/i_freehand.png $(BUILDUTILS)/mkgfx
	$(BUILDUTILS)/mkgfx $(RESOURCES)/i_freehand.png i_freehand.bin

i_line.bin: $(RESOURCES)/i_line.png $(BUILDUTILS)/mkgfx
	$(BUILDUTILS)/mkgfx $(RESOURCES)/i_line.png i_line.bin

i_rect.bin: $(RESOURCES)/i_rect.png $(BUILDUTILS)/mkgfx
	$(BUILDUTILS)/mkgfx $(RESOURCES)/i_rect.png i_rect.bin

i_fillrect.bin: $(RESOURCES)/i_fillrect.png $(BUILDUTILS)/mkgfx
	$(BUILDUTILS)/mkgfx $(RESOURCES)/i_fillrect.png i_fillrect.bin

i_undo.bin: $(RESOURCES)/i_undo.png $(BUILDUTILS)/mkgfx
	$(BUILDUTILS)/mkgfx $(RESOURCES)/i_undo.png i_undo.bin

i_clear.bin: $(RESOURCES)/i_clear.png $(BUILDUTILS)/mkgfx
	$(BUILDUTILS)/mkgfx $(RESOURCES)/i_clear.png i_clear.bin

clean:
	rm -f upiwin *.o splash.bin *~
