#    GMPAda, binding to the Ada Language for the GNU MultiPrecision library.
#    Copyright (C) 2007-2012 Nicolas Boulenguez <nicolas.boulenguez@free.fr>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

########################
# Global configuration #
########################

LIB_NAME := gmpada

SOVERSION := $(shell sed -n 's/.*Soversion *:= *"\([[:digit:].]\+\)";.*/\1/p' $(LIB_NAME).gpr)

################################
# Build and test configuration #
################################

# Default values if these variables are not set in the environment.
# These flags are used in this Makefile as usual, in particular in the
# implicit rule compiling generate_constants.c. Prefer overriding them
# instead of cargs or largs..
CFLAGS  ?= -g -O2
LDFLAGS ?= -Wl,--as-needed
# -g: produce debugging information
# -O2: default optimization
# -Wl,--as-needed: link only needed libraries

# Gprbuild allows setting specific flags for each step (Compiler,
# Binder, Linker, Gprbuild) and language.
cargs     := $(CFLAGS)
cargs_Ada := -gnatn -gnatw.e -gnatwH
largs     := $(LDFLAGS)
gargs     := -R -s -j$(shell getconf _NPROCESSORS_ONLN)
# -gnatn: allow subprogram inlining
# -gnatw.e: activate all optional warnings
# -gnatwH: except hiding
# -R: no run path option
# -s: recompile if compilation switches have changed

# Here is the exhaustive flag list.
gpr_flag_names := \
  cargs cargs_Ada cargs_C \
  bargs bargs_Ada bargs_C \
  largs largs_Ada largs_C \
  gargs


# Note that -gargs must come last, or else following flags will be
# interpreted as linker flags.

# Build the actual command line flags. For readability, only append
# the non-empty variables.
define append_gprbuild_flag
  ifneq (,$$($(1)))
    GPRBUILD_FLAGS += -$(subst _,:,$(1)) $$($(1))
  endif
endef
$(foreach var,$(gpr_flag_names),$(eval $(call append_gprbuild_flag,$(var))))

##############################
# Installation configuration #
##############################

# Each of the following path should be prefixed with
DESTDIR :=

# The sources files are installed into a $(LIB_NAME) subdirectory of
SRC_DIR := usr/share/ada/adainclude

# A $(LIB_NAME).gpr project convenient for library usage is installed into
GPR_DIR := usr/share/ada/adainclude
# The content of this file ignores DESTDIR.

# The GNAT ALI files are installed into a $(LIB_NAME) subdirectory of
ALI_DIR := usr/lib/ada/adalib

# The static and dynamic library are installed into
LIB_DIR := usr/lib

#########
# Rules #
#########

build: build-dynamic build-static

build-dynamic build-static: build-%: src/gmp-constants.ads
	gprbuild $(GPRBUILD_FLAGS) $(LIB_NAME).gpr -XKIND=$*
clean::
	rm -f $(foreach dir,obj lib,$(foreach kind,dynamic static,build-$(dir)-$(kind)/*))

src/gmp-constants.ads: generate_constants
	./$< > $@
clean::
	rm -f src/gmp-constants.ads

generate_constants: LDLIBS := -lgmp
clean::
	rm -f generate_constants

clean::
	find -name "*~" -delete

test: build-static
	gprbuild $(GPRBUILD_FLAGS) demo/demo.gpr -XKIND=static
	demo/demo
clean::
	rm -f demo/demo demo/obj/*

install: build
	install --directory $(DESTDIR)/$(SRC_DIR)/$(LIB_NAME)
	install --mode=644 src/*.ad[sb] src/*.[ch] $(DESTDIR)/$(SRC_DIR)/$(LIB_NAME)
	install --directory $(DESTDIR)/$(GPR_DIR)
	sed template_for_installed_project \
          $(foreach var,LIB_NAME SRC_DIR ALI_DIR LIB_DIR,-e 's|$$($(var))|$($(var))|g') \
          > $(DESTDIR)/$(GPR_DIR)/$(LIB_NAME).gpr
	chmod 644 $(DESTDIR)/$(GPR_DIR)/$(LIB_NAME).gpr
	install --directory $(DESTDIR)/$(ALI_DIR)/$(LIB_NAME)
	install --mode=444 build-lib-dynamic/*.ali $(DESTDIR)/$(ALI_DIR)/$(LIB_NAME)
	install --directory $(DESTDIR)/$(LIB_DIR)
	install --mode=644 build-lib-static/lib$(LIB_NAME).a $(DESTDIR)/$(LIB_DIR)
	install --mode=644 build-lib-dynamic/lib$(LIB_NAME).so.$(SOVERSION) $(DESTDIR)/$(LIB_DIR)
	cd $(DESTDIR)/$(LIB_DIR) && ln --force --symbolic lib$(LIB_NAME).so.$(SOVERSION) lib$(LIB_NAME).so

uninstall:
	rm -rf $(DESTDIR)/$(SRC_DIR)/$(LIB_NAME)
	rm -f $(DESTDIR)/$(GPR_DIR)/$(LIB_NAME).gpr
	rm -rf $(DESTDIR)/$(ALI_DIR)/$(LIB_NAME)
	rm -f $(DESTDIR)/$(LIB_DIR)/lib$(LIB_NAME).a
	rm -f $(DESTDIR)/$(LIB_DIR)/lib$(LIB_NAME).so.$(SOVERSION)
	rm -f $(DESTDIR)/$(LIB_DIR)/lib$(LIB_NAME).so

# Paranoid flags for developpers.

# -g: debugging information
# -Wall: optional warnings
# -Wextra: extra warnings
# -fstack-check: stack overflow checking
# -gnatf: full errors
# -gnatq: dont’quit
# -gnatQ: generate ALI files for cross-ref
# -gnata: Assert and Debug pragmas
# -gnatVa: all validity checks
# -gnaty: all style checks
# -gnatyM159: max line length
# -gnato: runtime overflow checking
# -gnatE: checks for access-before-elaboration
# -E: exception backtraces
devel:
	$(MAKE) test CFLAGS="-g -O2 -Wall -Wextra -fstack-check" \
             cargs_Ada="-gnatn -gnatw.e -gnatwH -gnatf -gnatq -gnatQ -gnata -gnatVa -gnaty -gnatyM159 -gnato -gnatE" \
             bargs_Ada=-E

.PHONY: build build-dynamic build-static clean devel install test uninstall
