# Copyright 2026 Gabriel Bjørnager Jensen.
#
# This file is part of LOOKLOOK.
#
# LOOKLOOK is free software: you can redistribute
# it and/or modify it under the terms of the GNU
# Affero General Public License as published by
# the Free Software Foundation, either version 3
# of the License, or (at your option) any later
# version.
#
# LOOKLOOK 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
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Af-
# fero General Public License along with LOOKLOOK.
# If not, see <https://www.gnu.org/licenses/>.

AR      := m68k-elf-ar
AS      := m68k-elf-as
CC      := m68k-elf-gcc
OBJCOPY := m68k-elf-objcopy
RM      := rm -f

ARFLAGS := \
	-r \
	-c \
	-s

ASFLAGS := \
	-c \
	-mcpu=68000 \
	-noexecstack

CFLAGS := \
	-ffreestanding \
	-fshort-enums \
	-I include \
	-mcpu=68000 \
	-nostdlib \
	-Os \
	-std=c90 \
	-Wall \
	-Wextra \
	-Wpedantic \
	-z noexecstack

LDFLAGS := \
	-L . \
	-nostdlib \
	-T ld.ld \
	-Wl,--no-warn-rwx-segments

LDLIBS := \
	-l looklook

OBJCOPYFLAGS := \
	-O binary \
	-S

OBJS := \
	start.o \
	sys.o \
	string.o

DEMO_OBJS := \
	demo.o

HDRS := \
	include/looklook.h

LIB  := liblooklook.a
DEMO := demo.bin

.PHONY: all clean purge

all: $(LIB) $(DEMO)

clean:
	$(RM) $(OBJS) $(DEMO_OBJS) demo.elf

purge: clean
	$(RM) $(LIB) $(DEMO)

$(LIB): $(OBJS)
	$(RM) $(LIB)
	$(AR) $(ARFLAGS) $(LIB) $(OBJS)

$(DEMO): $(LIB) $(DEMO_OBJS) ld.ld
	$(CC) $(CFLAGS) $(LDFLAGS) -o demo.elf $(DEMO_OBJS) $(LDLIBS)
	$(OBJCOPY) $(OBJCOPYFLAGS) demo.elf demo.bin

start.o: start.s
	$(AS) $(ASFLAGS) -o $@ start.s

string.o: string.s
	$(AS) $(ASFLAGS) -o $@ string.s

sys.o: sys.s
	$(AS) $(ASFLAGS) -o $@ sys.s

demo.o: demo.c $(HDRS)
	$(CC) $(CFLAGS) -c -o $(@) demo.c
