# Press2P's XInput1_4.dll for xemu and Xenia (64-bit) and OpenBOR games (32-bit)
# Build with MSYS2 MinGW-w64 (MSYSTEM=MINGW64):  make  -> build/XInput1_4.dll, build/x86/XInput1_4.dll

ifeq ($(origin CC),default)
CC      := gcc
endif
BUILD   := build
TARGET  := $(BUILD)/XInput1_4.dll
TARGET32 := $(BUILD)/x86/XInput1_4.dll

CFLAGS  ?= -O2
CFLAGS  += -std=c11 -Wall -Wextra
# No timestamp in the DLL: the same source always builds the same bytes, so the release build can
# check that the copy in cores\ is this source's.
LDFLAGS += -shared -static -static-libgcc -s -Wl,--no-undefined -Wl,--no-insert-timestamp

SRCS    := src/proxy.c
HDRS    := src/pads.h

.PHONY: all clean

all: $(TARGET) $(TARGET32)

$(TARGET): $(SRCS) $(HDRS) XInput1_4.def | $(BUILD)
	$(CC) $(CFLAGS) -o $@ $(SRCS) XInput1_4.def $(LDFLAGS)

# The 32-bit one: the same source with the 64-bit GCC's -m32, and no C runtime (the proxy calls only
# kernel32, through an import library made here: MSYS2's 64-bit MinGW has no 32-bit libraries) and no
# entry point (it needs none).
$(BUILD)/x86/libkernel32.a: kernel32.x86.def | $(BUILD)/x86
	dlltool -m i386 --as-flags=--32 -k -d $< -l $@

$(TARGET32): $(SRCS) $(HDRS) XInput1_4.x86.def $(BUILD)/x86/libkernel32.a | $(BUILD)/x86
	$(CC) -m32 $(CFLAGS) -o $@ $(SRCS) XInput1_4.x86.def $(BUILD)/x86/libkernel32.a \
		-shared -nostdlib -nostartfiles -s -Wl,--kill-at,--no-insert-timestamp,--no-undefined,-e,0

$(BUILD):
	mkdir -p $(BUILD)

$(BUILD)/x86:
	mkdir -p $(BUILD)/x86

clean:
	rm -rf $(BUILD)
