CC = gcc
CFLAGS = -Wall -Wextra -std=c11 -g -fwrapv -I.. -fPIC

ifeq ($(shell uname),Darwin)
	SO_EXT = dylib
	SO_FLAGS = -dynamiclib
else
	SO_EXT = so
	SO_FLAGS = -shared
endif
SO_FLAGS += -fPIC

TARGET = $(PLUGINNAME)/$(PLUGINNAME).$(SO_EXT)


.PHONY: all clean remake warning

all: warning $(TARGET)

clean: warning
	rm -rf $(TARGET) $(PLUGINNAME)/*.{o,dSYM}

remake: clean all

warning:
ifndef PLUGINNAME
	$(error "PLUGINNAME not set! Please run root Makefile instead.")
endif


.SECONDARY:

$(PLUGINNAME)/%.$(SO_EXT): $(patsubst %.c,%.o,$(wildcard $(PLUGINNAME)/*.c))
	$(CC) $(SO_FLAGS) -o $@ $(filter %.o,$^)

$(PLUGINNAME)/%.o: $(PLUGINNAME)/%.c $(wildcard $(PLUGINNAME)/*.h) $(wildcard ../*.h)
	$(CC) $(CFLAGS) $(SO_FLAGS) -o $@ $<