# If it exists includes Makefile.specific. In this Makefile, you should put
# specific Makefile code that you want to run before this. For example,
# build a particular environment.
-include Makefile.specific

# include parent_common.mk for buildsystem's defines
# It allows you to inherit an environment configuration from larger project
REPO_PARENT ?= ..
-include $(REPO_PARENT)/parent_common.mk

GIT_VERSION    := $(shell git describe --always --dirty --long --tags)
SO_VERSION_XYZ := $(shell git describe --tags --abbrev=0 | grep -o -E "[0-9]+\.[0-9]+\.[0-9]")

ifeq ($(SO_VERSION_XYZ),)
	SO_VERSION_XYZ := 0
endif

PREFIX ?= /usr/local
DESTLIBDIR ?= $(PREFIX)/lib

WRTD_DEP_TRTL ?= ../../dependencies/mock-turtle/

LIBSO     = libwrtd.so
LIBSO_XYZ = $(LIBSO).$(SO_VERSION_XYZ)
LIB       = libwrtd.a
OBJS     := libwrtd.o libwrtd-internal.o libwrtd-attributes.o libwrtd-reconfigure.o

INCFLAGS=-I. -I../include -I$(WRTD_DEP_TRTL)/software/include \
 -I$(WRTD_DEP_TRTL)/software/lib

CFLAGS += -fno-strict-aliasing $(INCFLAGS)
CFLAGS += -Wall -Werror -fPIC
CFLAGS += -DVERSION="\"$(GIT_VERSION)\""
CFLAGS += $(EXTRACFLAGS)
ARFLAGS = rcv

all: $(LIB) $(LIBSO)

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

$(LIBSO_XYZ): $(OBJS)
	$(CC) -shared $^ -L$(WRTD_DEP_TRTL)/lib -lmockturtle -o $@

$(LIBSO): $(LIBSO_XYZ)
	ln -sf $< $@

clean:
	rm -f $(LIB) $(LIBSO) $(LIBSO_XYZ) *.d *.o *~

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

install: $(LIBSO) $(LIBSO_XYZ)
	install -d $(DESTLIBDIR)
	install -m 0755 $(LIBSO_XYZ) $(DESTLIBDIR)
	ln -sf $(LIBSO_XYZ) $(DESTLIBDIR)/$(LIBSO)

uninstall:
	rm -f $(DESTLIBDIR)/$(LIBSO) $(DESTLIBDIR)/$(LIBSO_XYZ)
	rmdir --ignore-fail-on-non-empty $(DESTLIBDIR)

-include $(wildcard *.d)
