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

#  Set these variables if you install by hand.
#  DESTDIR and LIBNAME should contain no pipe character.
DESTDIR ?=
LIB_NAME ?= gmpada
SOVERSION ?= 1

# gprbuild can do parallel builds; we don't want make to interfere.
.NOTPARALLEL:
CPUS := $(shell getconf _NPROCESSORS_ONLN)

test: src/gmp-constants.ads
	gprbuild -j$(CPUS) --create-missing-dirs Makefile_test.gpr
	cd test-obj && ./demo
TO_CLEAN += test-obj

build: src/gmp-constants.ads $(LIB_NAME).gpr
	gprbuild -j$(CPUS) --create-missing-dirs Makefile_build.gpr \
          -XKIND=dynamic \
          -XNAME=$(LIB_NAME) \
          -XVERSION=lib$(LIB_NAME).so.$(SOVERSION)
	gprbuild -j$(CPUS) --create-missing-dirs Makefile_build.gpr \
          -XKIND=static \
          -XNAME=$(LIB_NAME)
TO_CLEAN += $(foreach dir,obj lib,$(foreach kind,dynamic static,build-$(dir)-$(kind)))

$(LIB_NAME).gpr: template_for_installed_project
	sed \
          -e "s/\$$(LIB_NAME)/$(LIB_NAME)/g" \
          -e "s|\$$(DESTDIR)|$(DESTDIR)|g" \
          $< > $@
TO_CLEAN += $(LIB_NAME).gpr

src/gmp-constants.ads: generate_constants
	./$< > $@
TO_CLEAN += src/gmp-constants.ads

generate_constants: CFLAGS += -g -Wall -Wextra
generate_constants: LDFLAGS += -lgmp
TO_CLEAN += generate_constants

clean:
	rm -rf $(TO_CLEAN)
	find -name "*~" -delete

install: build
	install -d $(DESTDIR)/usr/share/ada/adainclude/$(LIB_NAME)
	install --mode=644 src/* $(DESTDIR)/usr/share/ada/adainclude/$(LIB_NAME)
	install --mode=644 $(LIB_NAME).gpr $(DESTDIR)/usr/share/ada/adainclude
	install -d $(DESTDIR)/usr/lib/ada/adalib/$(LIB_NAME)
	install --mode=444 build-lib-dynamic/*.ali $(DESTDIR)/usr/lib/ada/adalib/$(LIB_NAME)
	install --mode=644 build-lib-static/lib$(LIB_NAME).a $(DESTDIR)/usr/lib
	install --mode=644 build-lib-dynamic/lib$(LIB_NAME).so.$(SOVERSION) $(DESTDIR)/usr/lib
	cd $(DESTDIR)/usr/lib && ln -f -s lib$(LIB_NAME).so.$(SOVERSION) lib$(LIB_NAME).so

uninstall:
	rm -rf \
          $(DESTDIR)/usr/share/ada/adainclude/$(LIB_NAME) \
          $(DESTDIR)/usr/share/ada/adainclude/$(LIB_NAME).gpr \
          $(DESTDIR)/usr/lib/ada/adalib/$(LIB_NAME) \
          $(DESTDIR)/usr/lib/lib$(LIB_NAME).a \
          $(DESTDIR)/usr/lib/lib$(LIB_NAME).so.$(SOVERSION) \
          $(DESTDIR)/usr/lib/lib$(LIB_NAME).so

.PHONY: build clean install uninstall test
