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

SRC_FILES = $(wildcard *.c)
HEADER_FILES = $(wildcard *.h)
OBJECT_FILES = $(patsubst %.c,%.o,$(SRC_FILES))

UNAME = $(shell uname)

ifeq ($(UNAME),Darwin)
	DYLIB_EXT = so  # should really be .dylib, but weechat seems to expect 'so'
	DYLIB_FLAGS = -dynamiclib
else
	DYLIB_EXT = so
	DYLIB_FLAGS = -shared
endif


# Don't remove intermediate files
.SECONDARY:


.PHONY: all clean remake dynamiclib

all: dynamiclib

clean:
	rm -f *.$(DYLIB_EXT) *.o

remake: clean
	$(MAKE) all

dynamiclib: tomsg.$(DYLIB_EXT)


%.o: %.c $(HEADER_FILES)
	$(CC) $(CFLAGS) -c -o $@ $<

%.$(DYLIB_EXT): $(OBJECT_FILES)
	$(CC) $(CFLAGS) $(DYLIB_FLAGS) -o $@ $^