# ============================================================================
# Makefile for win31drv - Windows 3.x display driver library for DJGPP/DOS
#
# Builds libwindrv.a from: log.c neload.c thunk.c winstub.c windrv.c
# windrv.c is compiled with -fno-gcse (see WINDRV_CFLAGS below).
# ============================================================================

DJGPP_PREFIX ?= $(HOME)/djgpp/djgpp

CC      = $(DJGPP_PREFIX)/bin/i586-pc-msdosdjgpp-gcc
AR      = $(DJGPP_PREFIX)/bin/i586-pc-msdosdjgpp-ar
CFLAGS  = -Wall -Wextra -O2 -std=gnu99 -I.

# With -O2 GCSE enabled, GCC's code layout for windrv.c places stack
# locals and register spills at addresses that get corrupted during
# 16-bit driver calls via thunkCall16, causing wrong values in drawing
# parameters. Disabling GCSE changes the layout enough to avoid the
# issue. Only windrv.c is affected (it has the drawing functions that
# call thunkCall16 with interleaved parameter setup).
WINDRV_CFLAGS = $(CFLAGS) -fno-gcse

# DJGPP binutils need libfl.so.2 which may not be installed system-wide
export LD_LIBRARY_PATH := $(realpath ../tools/lib):$(LD_LIBRARY_PATH)

OBJDIR  = obj

SRCS    = log.c neload.c thunk.c winstub.c windrv.c
OBJS    = $(addprefix $(OBJDIR)/,$(SRCS:.c=.o))
LIB     = libwindrv.a

.PHONY: all clean

all: $(LIB)

$(LIB): $(OBJS)
	$(AR) rcs $@ $^

$(OBJDIR)/%.o: %.c | $(OBJDIR)
	$(CC) $(CFLAGS) -c -o $@ $<

$(OBJDIR):
	mkdir -p $(OBJDIR)

# Dependencies
$(OBJDIR)/log.o:     log.c log.h
$(OBJDIR)/neload.o:  neload.c neload.h neformat.h wintypes.h log.h
$(OBJDIR)/thunk.o:   thunk.c thunk.h wintypes.h log.h
$(OBJDIR)/winstub.o: winstub.c winstub.h thunk.h wintypes.h log.h
$(OBJDIR)/windrv.o:  windrv.c windrv.h wintypes.h winddi.h neformat.h neload.h thunk.h winstub.h log.h stb_truetype.h
	$(CC) $(WINDRV_CFLAGS) -c -o $@ $<

clean:
	-rm -rf $(OBJDIR) $(LIB)
