# whether to link dynamically
#   if static=0 then link dynamically
#   if static=2 then link statically
#   if static=1 then link libkondo statically but other libraries dynamically
STATIC=1

VERSION=0.3.1

# products of the compilation
PROJECT_BINS= root_newton integral_gauss-legendre

# debug and optimization flags
#DB= -ggdb
OPT= -O3

# warning flags
WARNINGS= -Wall -Wextra -Wno-strict-overflow -std=c99 -pedantic -Wno-unused-parameter

# compiler
CC=/usr/bin/gcc
LD=$(CC)

# directories
INCLUDE =
LIB =
#INCLUDE = -I../../include
#LIB = -L../../build

# flags
override LDFLAGS +=$(LIB)
override CFLAGS +=$(INCLUDE) $(DB) $(OPT) $(WARNINGS)

# build directories
BUILDDIR=./build
SRCDIR=./src
OBJDIR=./objs

# objects
OBJS = $(addprefix $(OBJDIR)/, root_newton.o integral_gauss-legendre.o)

# flags which depend on whether to link statically or dynamically
# lib flag
LIBINUM_FLAG=-linum
# additional library required for static linking
XTRA_LIBS=

ifeq ($(STATIC),1)
  # libinum is linked against libm, libmpfr, libgmp and libpthread
  XTRA_LIBS=-lm -lmpfr -lgmp -lpthread
  # link binaries using the static library
  LIBINUM_FLAG=-l:libinum.a
else ifeq ($(STATIC),2)
  # libinum is linked against libm, libmpfr, libgmp and libpthread
  XTRA_LIBS=-lm -lmpfr -lgmp -lpthread
  # link binaries statically
  override LDFLAGS += -static
else
  # required flag for subsequent dynamic linking
  override CFLAGS += -fPIC
endif


all:	init $(PROJECT_BINS)

# create dirs
init:
	@[ -d $(OBJDIR) ] || /bin/mkdir $(OBJDIR)
	@[ -d $(BUILDDIR) ] || /bin/mkdir $(BUILDDIR)

root_newton:
	$(CC) -c $(CFLAGS) src/$@.c -o objs/$@.o
	$(LD) $(LDFLAGS) -o $(BUILDDIR)/$@ objs/$@.o $(LIBINUM_FLAG) $(XTRA_LIBS)

integral_gauss-legendre:
	$(CC) -c $(CFLAGS) src/$@.c -o objs/$@.o
	$(LD) $(LDFLAGS) -o $(BUILDDIR)/$@ objs/$@.o $(LIBINUM_FLAG) $(XTRA_LIBS)

clean:
	@rm -rf $(OBJDIR)
	@rm -rf $(BUILDDIR)