#!/usr/bin/make -f
# Build script for Ada Web Server in Debian.
# Copyright (c) 2003-2013 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2014-2016 Nicolas Boulenguez <nicolas@debian.org>

# This build script 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/>.

# On Debian systems, the full text of the GPL is in the file
# /usr/share/common-licenses/GPL-3.

include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging.mk
DEB_DATE := $(shell dpkg-parsechangelog -S date)

SSL := $(shell sed debian/control -ne ' \
  /^[ B].*\blibgnutls[0-9.]*-dev\b/ { s/.*/gnutls/ ; p; q };\
  /^[ B].*\blibssl-dev\b/           { s/.*/openssl/; p; q };\
  /^Package: /                      { s/.*/dummy/  ; p; q }')
ASIS := $(shell sed debian/control -ne '\
  /^[ B].*\blibasis[0-9.]*-dev\b/ { s/.*/yes/; p; q};\
  /^Package: /                    { s/.*/no/ ; p; q}')
soversion := $(shell sed debian/control -ne ' \
  /^Package: $(DEB_SOURCE)\([^-]\+\)$$/ { s//\1/; p; q}')

ADAFLAGS += -gnatafno -gnatVa -gnatwa
ifeq ($(DEB_HOST_ARCH),i386)
  ADAFLAGS += -O1
endif
CFLAGS += `pkg-config --cflags $(filter-out dummy,$(SSL)) zlib`
LDLIBS := `pkg-config --libs   $(filter-out dummy,$(SSL)) zlib` -llber -lldap
soname := $(DEB_SOURCE).so.$(soversion)

# Circumvent #760211. Since LDFLAGS contains --as-needed, this setting
# only takes effect on architectures triggering the bug.
LDLIBS += -lpthread

.SUFFIXES:

######################################################################
POLICY_TARGETS := binary binary-arch binary-indep build build-arch \
  build-indep clean
.PHONY: $(POLICY_TARGETS)
$(POLICY_TARGETS):
	dh $@ --with ada-library --with sphinxdoc

# Ignore upstream makefile (and ./configure, if ever).
.PHONY: $(addprefix override_dh_auto_,configure build-arch build-indep test install clean)
# Configure test install exist and are empty so that dh_auto_* does
# nothing.  We will replace upstream makefiles with build-* and clean.

######################################################################
build-aws-dynamic build-aws-static: src/core/aws-os_lib.ads
src/core/aws-os_lib.ads: src/core/xoscons \
                         src/core/aws-os_lib-tmplt.i \
                         src/core/aws-os_lib-tmplt.s
	cd src/core; ./xoscons aws-os_lib
src/core/xoscons: config/setup/xoscons.adb
	gnatmake $(BUILDER_OPTIONS) $< -D src/core -o $@ -cargs $(ADAFLAGS) -largs $(LDFLAGS)
src/core/aws-os_lib-tmplt.s: %.s: %.i
	$(CC) -S $(CFLAGS) $< -o $@
src/core/aws-os_lib-tmplt.i: config/setup/aws-os_lib-tmplt.c
	$(CC) -C -E $(CPPFLAGS) -DTARGET=\"$(DEB_HOST_MULTIARCH)\" $< -o $@

override_dh_auto_clean::
	rm -f src/core/aws-os_lib* src/core/xoscons* src/core/xutil.*

######################################################################
ADA2WSDL_OPTIONS_ADB := tools/ada2wsdl-options.adb

ifeq ($(ASIS),yes)
  build-tools: $(ADA2WSDL_OPTIONS_ADB)
endif
$(ADA2WSDL_OPTIONS_ADB): tools/%: debian/%.sed
	sed \
          -e 's|@ADAINCLUDE_PATH@|/usr/share/ada/adainclude|' \
          -e 's|@ADALIB_PATH@|/usr/lib/$(DEB_HOST_MULTIARCH)/ada/adalib|' \
          $< > $@

override_dh_auto_clean::
	rm -f $(ADA2WSDL_OPTIONS_ADB)

######################################################################
# Compile C sources from config/ssl/*/ (currently, only
# wrappers.c) as described in build_aws.gpr.
# If SSL=dummy, no source will be detected.
AWS_C_SRC_DIR   := config/ssl/$(SSL)
AWS_C_OBJ_DIR_D := debian/build_aws_obj_dynamic
AWS_C_OBJ_DIR_S := debian/build_aws_obj_static
AWS_C_SRCS   := $(wildcard $(AWS_C_SRC_DIR)/*.c)
AWS_C_OBJS_D := $(patsubst $(AWS_C_SRC_DIR)/%.c,$(AWS_C_OBJ_DIR_D)/%.o,$(AWS_C_SRCS))
AWS_C_OBJS_S := $(patsubst $(AWS_C_SRC_DIR)/%.c,$(AWS_C_OBJ_DIR_S)/%.o,$(AWS_C_SRCS))

build-aws-dynamic: $(AWS_C_OBJS_D)
build-aws-static : $(AWS_C_OBJS_S)

$(AWS_C_OBJS_D): $(AWS_C_OBJ_DIR_D)/%.o: $(AWS_C_SRC_DIR)/%.c
	$(CC) -c -fPIC $(CFLAGS) $(CPPFLAGS) $< -o $@
$(AWS_C_OBJS_S): $(AWS_C_OBJ_DIR_S)/%.o: $(AWS_C_SRC_DIR)/%.c
	$(CC) -c       $(CFLAGS) $(CPPFLAGS) $< -o $@

# The objects will be cleaned with Ada ones.

######################################################################
# Build both versions of the library.

override_dh_auto_build-arch: build-aws-dynamic build-aws-static
.PHONY: build-aws-dynamic build-aws-static
build-aws-static:
	gnatmake $(BUILDER_OPTIONS) -Pdebian/build_aws.gpr -Xkind=static \
          $(foreach v,ADAFLAGS SSL,"-X$(v)=$($(v))")
build-aws-dynamic:
 # Ensure deterministic timestamps in ALI files.
	find . -depth \( -name "*.ad[bs]" -o -name "*.[hc]" \) \
	  -a -newermt '$(DEB_DATE)' -print0 | \
	  xargs -0r touch --no-dereference --date='$(DEB_DATE)'
	gnatmake $(BUILDER_OPTIONS) -Pdebian/build_aws.gpr -Xkind=dynamic \
          $(foreach v,ADAFLAGS LDFLAGS LDLIBS soname SSL,"-X$(v)=$($(v))")

override_dh_auto_clean::
	rm -f $(foreach d, lib obj,$(foreach k,dynamic static, \
                debian/build_aws_$(d)_$(k)/*))

# An override is necessary because LDLIBS contains a shell escape.
# The projet should import directly xmlada.gpr and
# templates-parser.gpr (both are used by specs).
.PHONY: override_dh_ada_library-arch
override_dh_ada_library-arch:
	dh_ada_library kind=dynamic \
          $(foreach v,LDLIBS soname SSL,"$(v)=$($(v))") \
          debian/build_aws.gpr

######################################################################
ifeq ($(ASIS),yes)
  TOOLS_GPR := debian/tools_with_asis.gpr
else
  TOOLS_GPR := debian/tools.gpr
endif

override_dh_auto_build-arch: build-tools
.PHONY: build-tools
build-tools: build-aws-dynamic
	gnatmake $(BUILDER_OPTIONS) -P$(TOOLS_GPR) -Xkind=dynamic \
          $(foreach v,ADAFLAGS LDFLAGS LDLIBS soname SSL,"-X$(v)=$($(v))")

override_dh_auto_clean::
	rm -f debian/tools_obj/*

######################################################################
.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --package=libaws-doc -X.adb -X.ads -X.thtml -X.wsdl
	dh_compress --remaining-packages

INSTALLED_EXAMPLES := debian/libaws-doc/usr/share/doc/libaws-doc/examples
.PHONY: override_dh_installexamples-indep
override_dh_installexamples-indep:
	dh_installexamples --indep
 # Fix permissions.
	find $(INSTALLED_EXAMPLES) -type f -print0 | xargs -0r chmod 644
 # Replace duplicate files with symlinks, then make the links relative.
	rdfind -outputname /dev/null -makesymlinks true $(INSTALLED_EXAMPLES)
	symlinks -crs $(INSTALLED_EXAMPLES)

######################################################################
# Documentation.
# ./makefile would recompute all makefile.conf variables, assume only
# HTML output format, and assume the existence of templates_parser/.

# Freeze the clock for deterministic PDF timestamps. The faketime API
# does not allow an explicit time zone, introducing an implicit
# dependency on its local value.
DEB_DATE_FAKETIME := $(shell date "+%F %T" -d "$(DEB_DATE)")
override_dh_auto_build-indep:
	faketime -f "$(DEB_DATE_FAKETIME)" \
	$(MAKE) -C docs html info latexpdf text style-guide.pdf \
          SPHINXOPTS=-j$(BUILDER_JOBS) $(foreach v,ADAFLAGS LDFLAGS,"$(v)=$($(v))")

override_dh_auto_clean::
	$(MAKE) -C docs clean
	rm -f docs/source/*.pyc
