#    GMPAda, binding to the Ada Language for the GNU MultiPrecision library.
#    Copyright (C) 2007-2011 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 := 1

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

GPRBUILD_FLAGS :=

##############################
# 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: src/gmp-constants.ads
	gprbuild $(GPRBUILD_FLAGS) --create-missing-dirs Makefile_build.gpr \
          -XKIND=dynamic \
          -XNAME=$(LIB_NAME) \
          -XVERSION=lib$(LIB_NAME).so.$(SOVERSION)
	gprbuild $(GPRBUILD_FLAGS) --create-missing-dirs Makefile_build.gpr \
          -XKIND=static \
          -XNAME=$(LIB_NAME)
clean::
	rm -rf $(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: CFLAGS += -Wall -Wextra
generate_constants: LDLIBS += -lgmp
clean::
	rm -f generate_constants

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

test: src/gmp-constants.ads
	gprbuild $(GPRBUILD_FLAGS) --create-missing-dirs Makefile_test.gpr
	cd test-obj && ./demo
clean::
	rm -rf test-obj

install: build
	install --directory $(DESTDIR)/$(SRC_DIR)/$(LIB_NAME)
	install --mode=644 src/* $(DESTDIR)/$(SRC_DIR)/$(LIB_NAME)
	install --directory $(DESTDIR)/$(GPR_DIR)
	sed template_for_installed_project \
          -e "s|\$$(LIB_NAME)|$(LIB_NAME)|g" \
          -e "s|\$$(SOURCE_DIR)|/$(SRC_DIR)/$(LIB_NAME)|g" \
          -e "s|\$$(LIBRARY_ALI_DIR)|/$(ALI_DIR)/$(LIB_NAME)|g" \
          -e "s|\$$(LIBRARY_DIR)|/$(LIB_DIR)|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

.PHONY: build clean install test uninstall
