# vim: noexpandtab tabstop=8 shiftwidth=8 syntax=make
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# WARNING: This may or may not work on your system. This Makefile is
# an example, rather than a ready-made universal solution.

PERL ?= yes
ENABLE_PERL_BINDINGS = $(PERL)
THREADING ?= yes
ifeq ($(THREADING),yes)
JAVA ?= yes
else
JAVA ?= no
endif
ENABLE_JAVA_BINDINGS = $(JAVA)
USE_APR_ICONV ?= no # set to yes to use APR iconv instead of GNU iconv
PARALLEL ?= 1
CLEANUP ?= 1
EXCLUSIVE_WC_LOCKS ?= 1
USE_HTTPV1 ?= no
USE_AUTHZ_SHORT_CIRCUIT ?= no
RAMDISK ?= /ramdisk
MAKE_JOBS ?= 4
# available backends: fsfs fsx bdb
SVN_CHECK_FS_BACKENDS ?= fsfs

PWD		= $(shell pwd)
UNAME		= $(shell uname)
RUBY		= $(shell which ruby 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby27 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby26 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby25 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby24 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby23 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby22 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby21 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby20 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby19 2>/dev/null)
ifeq ($(RUBY),)
RUBY		= $(shell which ruby18)
endif # 1.8
endif # 1.9
endif # 2.0
endif # 2.1
endif # 2.2
endif # 2.3
endif # 2.4
endif # 2.5
endif # 2.6
endif # 2.7

ifeq ($(UNAME),OpenBSD)
# Pick the correct base compiler (ie. clang rather than ancient gcc 4.2.1)
CC = cc
CXX = c++
endif

TAG		?= none
ifeq ($(TAG),none)
BRANCH		?= trunk
else
BRANCH		= $(TAG)
endif
WC		?= $(BRANCH)
BRANCH_MAJOR	:= $(shell echo $(BRANCH) | sed -E 's/([0-9]+)\.([0-9]+)\..*/\1.\2/')
ifeq ($(BRANCH_MAJOR), $(filter 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13, $(BRANCH_MAJOR)))
PYTHON = python2
else
PYTHON = python3
endif
SVN_REL_WC	= svn-$(WC)
SVN_WC		= $(PWD)/$(SVN_REL_WC)
PREFIX		= $(PWD)/prefix
SVN_PREFIX	= $(PREFIX)/svn-$(WC)
DISTDIR		= $(PWD)/distfiles
SRCDIR		= $(PWD)/src
OBJDIR		= $(PWD)/objdir

# Default dependency versions
BDB_MAJOR_VER	= 4.7
BDB_VER		= $(BDB_MAJOR_VER).25
APR_VER		= 1.7.6
APR_ICONV_VER	= 1.2.1
GNU_ICONV_VER	= 1.15
OPENLDAP_VER	= 2.6.10
APR_UTIL_VER	= 1.6.3
PCRE_VER	= 8.45
HTTPD_VER	= 2.4.65
NEON_VER	= 0.32.5
SERF_VER	= 1.3.10
CYRUS_SASL_VER	= 2.1.28
SQLITE_VER	= 3510100
LIBMAGIC_VER	= 5.46
RUBY_MAJOR_VER	= 3.4
RUBY_VER	= $(RUBY_MAJOR_VER).7
BZ2_VER	= 1.0.8
PYTHON_MAJOR_VER= 3.13
PYTHON_VER	= ${PYTHON_MAJOR_VER}.9
PYTHON2_VER	= 2.7.18
PY3C_VER	= 1.1
JUNIT_VER	= 4.10
GETTEXT_VER	= 0.23.1
LZ4_VER		= 1.10.0
ifeq ($(BRANCH_MAJOR), $(filter 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13, $(BRANCH_MAJOR)))
SWIG_VER	= 3.0.12
else
SWIG_VER	=
endif

# Get local dependency versions
include Makefile.local

BDB_DIST	= db-$(BDB_VER).tar.gz
OPENLDAP_DIST	= openldap-$(OPENLDAP_VER).tgz
APR_ICONV_DIST	= apr-iconv-$(APR_ICONV_VER).tar.gz
GNU_ICONV_DIST	= libiconv-$(GNU_ICONV_VER).tar.gz
NEON_DIST	= neon-$(NEON_VER).tar.gz
SQLITE_DIST	= sqlite-autoconf-$(SQLITE_VER).tar.gz
CYRUS_SASL_DIST	= cyrus-sasl-$(CYRUS_SASL_VER).tar.gz
PCRE_DIST	= pcre-$(PCRE_VER).tar.gz
HTTPD_DIST	= httpd-$(HTTPD_VER).tar.gz
LIBMAGIC_DIST	= file-$(LIBMAGIC_VER).tar.gz
RUBY_DIST	= ruby-$(RUBY_VER).tar.gz
BZ2_DIST	= bzip2-$(BZ2_VER).tar.gz
PYTHON_DIST	= Python-$(PYTHON_VER).tgz
PYTHON2_DIST	= Python-$(PYTHON2_VER).tgz
PY3C_DIST	= py3c-$(PY3C_VER).tar.gz
JUNIT_DIST	= junit-${JUNIT_VER}.jar
GETTEXT_DIST	= gettext-$(GETTEXT_VER).tar.gz
LZ4_DIST	= lz4-$(LZ4_VER).tar.gz
SWIG_DIST	= swig-$(SWIG_VER).tar.gz

SHA256_${BDB_DIST} = f14fd96dd38915a1d63dcb94a63fbb8092334ceba6b5060760427096f631263e
SHA256_${APR_ICONV_DIST} = 19381959d50c4a5f3b9c84d594a5f9ffb3809786919b3058281f4c87e1f4b245
SHA256_${GNU_ICONV_DIST} = ccf536620a45458d26ba83887a983b96827001e92a13847b45e4925cc8913178
SHA256_${OPENLDAP_DIST} = c065f04aad42737aebd60b2fe4939704ac844266bc0aeaa1609f0cad987be516
SHA256_${PCRE_DIST} = 4e6ce03e0336e8b4a3d6c2b70b1c5e18590a5673a98186da90d4f33c23defc09
SHA256_httpd-$(HTTPD_VER).tar.gz = 4f92861a50325c6d1046ebad5d814bff0d4169ada8cc265655f32b7f1ba4be1b
SHA256_${NEON_DIST} = 4872e12f802572dedd4b02f870065814b2d5141f7dbdaf708eedab826b51a58a
SHA256_${CYRUS_SASL_DIST} = 7ccfc6abd01ed67c1a0924b353e526f1b766b21f42d4562ee635a8ebfc5bb38c
SHA256_${SQLITE_DIST} = 4f2445cd70479724d32ad015ec7fd37fbb6f6130013bd4bfbc80c32beb42b7e0
SHA256_${LIBMAGIC_DIST} = c9cc77c7c560c543135edc555af609d5619dbef011997e988ce40a3d75d86088
SHA256_ruby-2.7.4.tar.gz = 3043099089608859fc8cce7f9fdccaa1f53a462457e3838ec3b25a7d609fbc5b
SHA256_ruby-2.7.7.tar.gz = e10127db691d7ff36402cfe88f418c8d025a3f1eea92044b162dd72f0b8c7b90
SHA256_ruby-2.7.8.tar.gz = c2dab63cbc8f2a05526108ad419efa63a67ed4074dbbcf9fc2b1ca664cb45ba0
SHA256_ruby-3.0.7.tar.gz = 2a3411977f2850431136b0fab8ad53af09fb74df2ee2f4fb7f11b378fe034388
SHA256_ruby-3.1.6.tar.gz = 0d0dafb859e76763432571a3109d1537d976266be3083445651dc68deed25c22
SHA256_ruby-${RUBY_VER}.tar.gz = 23815a6d095696f7919090fdc3e2f9459b2c83d57224b2e446ce1f5f7333ef36
SHA256_${BZ2_DIST} = ab5a03176ee106d3f0fa90e381da478ddae405918153cca248e682cd0c4a2269
SHA256_${PYTHON_DIST} = c4c066af19c98fb7835d473bebd7e23be84f6e9874d47db9e39a68ee5d0ce35c
SHA256_${PYTHON2_DIST} = da3080e3b488f648a3d7a4560ddee895284c3380b11d6de75edb986526b9a814
SHA256_${PY3C_DIST} = c7ffc22bc92dded0ca859db53ef3a0b466f89a9f8aad29359c9fe4ff18ebdd20
SHA256_${JUNIT_DIST} = 36a747ca1e0b86f6ea88055b8723bb87030d627766da6288bf077afdeeb0f75a
SHA256_${GETTEXT_DIST} = 52a578960fe308742367d75cd1dff8552c5797bd0beba7639e12bdcda28c0e49
SHA256_${LZ4_DIST} = 537512904744b35e232912055ccf8ec66d768639ff3abe5788d90d792ec5f48b
SHA256_swig-3.0.12.tar.gz = 7cf9f447ae7ed1c51722efc45e7f14418d15d7a1e143ac9f09a668999f4fc94d
SHA256_swig-4.2.1.tar.gz = fa045354e2d048b2cddc69579e4256245d4676894858fcf0bab2290ecf59b7d8
SHA256_swig-4.3.0.tar.gz = f7203ef796f61af986c70c05816236cbd0d31b7aa9631e5ab53020ab7804aa9e

define do_check_sha256
if [ -x /bin/sha256 ]; then \
	(cd $(DISTDIR) && \
		echo "SHA256 (${1}) = ${SHA256_${1}}" | /bin/sha256 -C /dev/stdin "${1}"); \
elif [ -x /usr/bin/sha256sum ]; then \
	(cd $(DISTDIR) && \
		echo "${SHA256_${1}} ${1}" | /usr/bin/sha256sum --quiet --check); \
elif [ -x /sbin/sha256sum ]; then \
	(cd $(DISTDIR) && \
		echo "${SHA256_${1}} ${1}" | /sbin/sha256sum --quiet --check /dev/stdin); \
elif [ -x /usr/bin/shasum ]; then \
        echo "${SHA256_${1}}  ${1}"; \
        (cd $(DISTDIR) && \
                echo "${SHA256_${1}}  ${1}" | /usr/bin/shasum -a 256 -c /dev/stdin ); \
else \
	echo "Error: No tool found to verify checksum"; \
	false; \
fi
endef

DISTFILES	= $(DISTDIR)/$(NEON_DIST) \
		$(DISTDIR)/$(SERF_DIST) \
		$(DISTDIR)/$(SQLITE_DIST) \
		$(DISTDIR)/$(OPENLDAP_DIST) \
		$(DISTDIR)/$(PCRE_DIST) \
		$(DISTDIR)/$(HTTPD_DIST) \
		$(DISTDIR)/$(APR_ICONV_DIST) \
		$(DISTDIR)/$(GNU_ICONV_DIST) \
		$(DISTDIR)/$(CYRUS_SASL_DIST) \
		$(DISTDIR)/$(LIBMAGIC_DIST) \
		$(DISTDIR)/$(RUBY_DIST) \
		$(DISTDIR)/$(BZ2_DIST) \
		$(DISTDIR)/$(PYTHON_DIST) \
		$(DISTDIR)/$(PYTHON2_DIST) \
		$(DISTDIR)/$(PY3C_DIST) \
		$(DISTDIR)/$(JUNIT_DIST) \
		$(DISTDIR)/$(GETTEXT_DIST) \
		$(DISTDIR)/$(SWIG_DIST)

FETCH_CMD	= wget -c

SUBVERSION_REPOS_URL = https://svn.apache.org/repos/asf/subversion
BDB_URL		= http://download.oracle.com/berkeley-db/$(BDB_DIST)
APR_URL		= https://svn.apache.org/repos/asf/apr/apr
APR_ICONV_URL	= https://archive.apache.org/dist/apr/$(APR_ICONV_DIST)
GNU_ICONV_URL	= https://ftp.gnu.org/pub/gnu/libiconv/$(GNU_ICONV_DIST)
OPENLDAP_URL	= https://openldap.org/software/download/OpenLDAP/openldap-release/${OPENLDAP_DIST}
APR_UTIL_URL	= https://svn.apache.org/repos/asf/apr/apr-util
PCRE_URL	= https://downloads.sourceforge.net/project/pcre/pcre/$(PCRE_VER)/$(PCRE_DIST)
HTTPD_URL	= https://archive.apache.org/dist/httpd/$(HTTPD_DIST)
NEON_URL	= https://notroj.github.io/neon/$(NEON_DIST)
SERF_URL	= https://svn.apache.org/repos/asf/serf/tags/$(SERF_VER)
SQLITE_URL	= https://www.sqlite.org/2025/$(SQLITE_DIST)
CYRUS_SASL_URL	= https://github.com/cyrusimap/cyrus-sasl/releases/download/cyrus-sasl-${CYRUS_SASL_VER}/$(CYRUS_SASL_DIST)
LIBMAGIC_URL	= ftp://ftp.astron.com/pub/file/$(LIBMAGIC_DIST)
RUBY_URL	= https://cache.ruby-lang.org/pub/ruby/$(RUBY_MAJOR_VER)/$(RUBY_DIST)
BZ2_URL		= https://sourceware.org/pub/bzip2/$(BZ2_DIST)
PYTHON_URL	= https://python.org/ftp/python/$(PYTHON_VER)/$(PYTHON_DIST)
PYTHON2_URL	= https://python.org/ftp/python/$(PYTHON2_VER)/$(PYTHON2_DIST)
PY3C_URL	= https://stsp.name/distfiles/py3c-${PY3C_VER}.tar.gz
JUNIT_URL	= https://stsp.name/distfiles/$(JUNIT_DIST)
GETTEXT_URL	= https://ftp.gnu.org/pub/gnu/gettext/$(GETTEXT_DIST)
LZ4_URL		= https://github.com/lz4/lz4/archive/v$(LZ4_VER).tar.gz
SWIG_URL	= https://downloads.sourceforge.net/sourceforge/swig/swig-$(SWIG_VER).tar.gz


BDB_SRCDIR	= $(SRCDIR)/db-$(BDB_VER)
APR_SRCDIR	= $(SRCDIR)/apr-$(APR_VER)
APR_ICONV_SRCDIR	= $(SRCDIR)/apr-iconv-$(APR_ICONV_VER)
GNU_ICONV_SRCDIR	= $(SRCDIR)/libiconv-$(GNU_ICONV_VER)
OPENLDAP_SRCDIR	= ${SRCDIR}/openldap-$(OPENLDAP_VER)
APR_UTIL_SRCDIR	= $(SRCDIR)/apr-util-$(APR_UTIL_VER)
PCRE_SRCDIR	= $(SRCDIR)/pcre-$(PCRE_VER)
HTTPD_SRCDIR	= $(SRCDIR)/httpd-$(HTTPD_VER)
NEON_SRCDIR	= $(SRCDIR)/neon-$(NEON_VER)
SERF_SRCDIR	= $(SRCDIR)/serf-$(SERF_VER)
SQLITE_SRCDIR	= $(SRCDIR)/sqlite-autoconf-$(SQLITE_VER)
CYRUS_SASL_SRCDIR	= $(SRCDIR)/cyrus-sasl-$(CYRUS_SASL_VER)
LIBMAGIC_SRCDIR	= $(SRCDIR)/file-$(LIBMAGIC_VER)
RUBY_SRCDIR	= $(SRCDIR)/ruby-$(RUBY_VER)
BZ2_SRCDIR	= $(SRCDIR)/bzip2-$(BZ2_VER)
PYTHON_SRCDIR	= $(SRCDIR)/Python-$(PYTHON_VER)
PYTHON2_SRCDIR	= $(SRCDIR)/Python-$(PYTHON2_VER)
PY3C_SRCDIR	= $(SRCDIR)/py3c-$(PY3C_VER)
GETTEXT_SRCDIR	= $(SRCDIR)/gettext-$(GETTEXT_VER)
LZ4_SRCDIR	= ${SRCDIR}/lz4-$(LZ4_VER)
SWIG_SRCDIR	= ${SRCDIR}/swig-$(SWIG_VER)
SVN_SRCDIR	= $(SVN_WC)

BDB_OBJDIR	= $(OBJDIR)/db-$(BDB_VER)
APR_OBJDIR	= $(OBJDIR)/apr-$(APR_VER)
APR_ICONV_OBJDIR	= $(OBJDIR)/apr-iconv-$(APR_ICONV_VER)
GNU_ICONV_OBJDIR	= $(OBJDIR)/libiconv-$(GNU_ICONV_VER)
OPENLDAP_OBJDIR	= ${OBJDIR}/openldap-$(OPENLDAP_VER)
APR_UTIL_OBJDIR	= $(OBJDIR)/apr-util-$(APR_UTIL_VER)
PCRE_OBJDIR	= $(OBJDIR)/pcre-$(PCRE_VER)
HTTPD_OBJDIR	= $(OBJDIR)/httpd-$(HTTPD_VER)
NEON_OBJDIR	= $(OBJDIR)/neon-$(NEON_VER)
SERF_OBJDIR	= $(OBJDIR)/serf-$(SERF_VER)
SQLITE_OBJDIR	= $(OBJDIR)/sqlite-$(SQLITE_VER)
CYRUS_SASL_OBJDIR	= $(OBJDIR)/cyrus-sasl-$(CYRUS_SASL_VER)
LIBMAGIC_OBJDIR	= $(OBJDIR)/file-$(LIBMAGIC_VER)
RUBY_OBJDIR	= $(OBJDIR)/ruby-$(RUBY_VER)
BZ2_OBJDIR	= $(OBJDIR)/bzip2-$(BZ2_VER)
PYTHON_OBJDIR	= $(OBJDIR)/python-$(PYTHON_VER)
PYTHON2_OBJDIR	= $(OBJDIR)/python-$(PYTHON2_VER)
PY3C_OBJDIR	= $(OBJDIR)/py3c-$(PY3C_VER)
GETTEXT_OBJDIR	= $(OBJDIR)/gettext-$(GETTEXT_VER)
LZ4_OBJDIR	= ${OBJDIR}/lz4-$(LZ4_VER)
SWIG_OBJDIR	= ${OBJDIR}/swig-$(SWIG_VER)
SVN_OBJDIR	= $(OBJDIR)/$(SVN_REL_WC)

# Tweak this for out-of-tree builds. Note that running individual
# tests in the test suite won't work conveniently with out-of-tree
# builds!
svn_builddir	?=$(SVN_WC)

ifdef PROFILE
PROFILE_CFLAGS=-pg
endif

# We need this to make sure some targets below pick up the right libraries
LD_LIBRARY_PATH=$(PREFIX)/lz4/lib:$(PREFIX)/openldap/lib:$(PREFIX)/apr/lib:$(PREFIX)/gettext/lib:$(PREFIX)/iconv/lib:$(PREFIX)/bdb/lib:$(PREFIX)/neon/lib:$(PREFIX)/serf/lib:$(PREFIX)/sqlite/lib:$(PREFIX)/cyrus-sasl/lib:$(PREFIX)/iconv/lib:$(PREFIX)/libmagic/lib:$(PREFIX)/ruby/lib:$(PREFIX)/svn-$(WC)/lib

# We need this to make sure some targets below pick up the right pkg-config files
PKG_CONFIG_PATH=$(PREFIX)/openldap/lib/pkgconfig:$(PREFIX)/apr/lib/pkgconfig:$(PREFIX)/neon/lib/pkgconfig:$(PREFIX)/serf/lib/pkgconfig:$(PREFIX)/sqlite/lib/pkgconfig:$(PREFIX)/ruby/lib/pkgconfig:$(PREFIX)/lz4/lib/pkgconfig


#######################################################################
# Main targets.
#######################################################################

.PHONY: all reset clean nuke fetch

all: dirs-create bdb-install sqlite-install cyrus-sasl-install \
	openldap-install apr-install iconv-install apr-util-install \
	pcre-install httpd-install neon-install serf-install libmagic-install \
	ruby-install bz2-install python-install python2-install py3c-retrieve \
	gettext-install lz4-install swig-install svn-install svn-bindings-install

# Use these to start a build from the beginning.
reset: dirs-reset bdb-reset apr-reset iconv-reset apr-util-reset \
	pcre-reset httpd-reset neon-reset serf-reset \
	sqlite-reset cyrus-sasl-reset libmagic-reset ruby-reset python-reset \
	python2-reset bz2-reset gettext-reset lz4-reset swig-reset \
	openldap-reset svn-reset

# Use to save disk space.
clean: bdb-clean apr-clean iconv-clean apr-util-clean pcre-clean httpd-clean \
	neon-clean serf-clean sqlite-clean cyrus-sasl-clean \
	libmagic-clean ruby-clean bz2-clean python-clean python2-clean \
	gettext-clean lz4-clean swig-clean openldap-clean svn-clean

# Nukes everything (including installed binaries!)
# Use this to start ALL OVER AGAIN! Use with caution!
nuke:
	@echo
	@echo "I will now remove the following directories PERMANENTLY:"
	@echo
	@echo "  $(SRCDIR)"
	@echo "  $(OBJDIR)"
	@echo "  $(PREFIX)"
	@echo
	@echo -n 'Do you want me to continue? ([no]/yes): '
	@read ANSWER ; \
		case $$ANSWER in \
			yes)    echo "You said $$ANSWER. I will continue."; \
				echo rm -rf $(SRCDIR) $(OBJDIR) $(PREFIX); \
				rm -rf $(SRCDIR) $(OBJDIR) $(PREFIX); \
				$(MAKE) reset; \
				;; \
			"")     echo "You said no."; \
				;; \
			*)      echo "You said $$ANSWER."; \
				;; \
		esac

fetch: $(DISTFILES)

#######################################################################
# Local version override file
#######################################################################
Makefile.local:
	cp unix-build/Makefile.local .

#######################################################################
# directories
#######################################################################

dirs-create: $(PWD)/.dirs-created
dirs-reset:
	rm -f $(PWD)/.dirs-created

$(PWD)/.dirs-created: 
	$(foreach d, $(PREFIX) $(DISTDIR) $(SRCDIR) $(OBJDIR), \
		[ -d $(d) ] || mkdir -p $(d);)
		touch $@

#######################################################################
# bdb
#######################################################################

bdb-retrieve:	$(BDB_OBJDIR)/.retrieved
bdb-configure:	$(BDB_OBJDIR)/.configured
bdb-compile:	$(BDB_OBJDIR)/.compiled
bdb-install:	$(BDB_OBJDIR)/.installed
bdb-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(BDB_OBJDIR)/$(f);)

bdb-clean:
	-(cd $(BDB_SRCDIR)/build_unix/ && env MAKEFLAGS= make clean)

# fetch distfile for bdb
$(DISTDIR)/$(BDB_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(BDB_URL)

# retrieve bdb
$(BDB_OBJDIR)/.retrieved: $(DISTDIR)/$(BDB_DIST)
	$(call do_check_sha256,$(BDB_DIST))
	[ -d $(BDB_OBJDIR) ] || mkdir -p $(BDB_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(BDB_DIST)
	touch $@

# configure bdb
$(BDB_OBJDIR)/.configured: $(BDB_OBJDIR)/.retrieved
	cd $(BDB_SRCDIR)/build_unix \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`" \
		CC=$(CC) CXX=$(CXX) \
		../dist/configure \
		--prefix=$(PREFIX)/bdb \
		--enable-debug
	touch $@

# compile bdb
$(BDB_OBJDIR)/.compiled: $(BDB_OBJDIR)/.configured
	(cd $(BDB_SRCDIR)/build_unix && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install bdb
$(BDB_OBJDIR)/.installed: $(BDB_OBJDIR)/.compiled
	(cd $(BDB_SRCDIR)/build_unix && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# apr
#######################################################################

apr-retrieve:	$(APR_OBJDIR)/.retrieved
apr-configure:	$(APR_OBJDIR)/.configured
apr-compile:	$(APR_OBJDIR)/.compiled
apr-install:	$(APR_OBJDIR)/.installed
apr-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(APR_OBJDIR)/$(f);)

apr-clean:
	-(cd $(APR_OBJDIR) && env MAKEFLAGS= make clean)

# retrieve apr if not present yet
$(APR_OBJDIR)/.retrieved:
	[ -d $(APR_OBJDIR) ] || mkdir -p $(APR_OBJDIR)
	if [ ! -d $(APR_SRCDIR) ]; then \
		svn export $(APR_URL)/tags/$(APR_VER)/ $(APR_SRCDIR); \
	fi
	touch $@

ifeq ($(THREADING),yes)
THREADS_FLAG=--enable-threads
else
THREADS_FLAG=--disable-threads
endif

ifdef POOL_DEBUG
POOL_DEBUG_FLAG=--enable-pool-debug=all
else
# Map apr_palloc()/apr_pool_{clear,destroy}() to malloc()/free().
# This also puts poison bytes into freed memory to help detect use after free.
POOL_DEBUG_FLAG=--enable-pool-debug=yes
endif

# configure apr
$(APR_OBJDIR)/.configured: $(APR_OBJDIR)/.retrieved $(BDB_OBJDIR)/.installed
	cd $(APR_SRCDIR) && ./buildconf
	cd $(APR_OBJDIR) \
		&& env CFLAGS="-O0 -g $(PROFILE_CFLAGS)" GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		CC=$(CC) CXX=$(CXX) \
		$(APR_SRCDIR)/configure \
		--prefix=$(PREFIX)/apr \
		--enable-maintainer-mode \
		$(THREADS_FLAG) \
		$(POOL_DEBUG_FLAG)
	touch $@

# compile apr
$(APR_OBJDIR)/.compiled: $(APR_OBJDIR)/.configured
	(cd $(APR_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install apr
$(APR_OBJDIR)/.installed: $(APR_OBJDIR)/.compiled
	(cd $(APR_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# apr-iconv
#######################################################################

apr-iconv-retrieve:	$(APR_ICONV_OBJDIR)/.retrieved
apr-iconv-configure:	$(APR_ICONV_OBJDIR)/.configured
apr-iconv-compile:	$(APR_ICONV_OBJDIR)/.compiled
apr-iconv-install:	$(APR_ICONV_OBJDIR)/.installed
apr-iconv-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(APR_ICONV_OBJDIR)/$(f);)

apr-iconv-clean:
	-(cd $(APR_ICONV_OBJDIR) && env MAKEFLAGS= make clean)

# fetch distfile for apr-iconv
$(DISTDIR)/$(APR_ICONV_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(APR_ICONV_URL)

# retrieve apr-iconv
$(APR_ICONV_OBJDIR)/.retrieved: $(DISTDIR)/$(APR_ICONV_DIST)
	$(call do_check_sha256,$(APR_ICONV_DIST))
	[ -d $(APR_ICONV_OBJDIR) ] || mkdir -p $(APR_ICONV_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(APR_ICONV_DIST)
	touch $@

# configure apr-iconv
$(APR_ICONV_OBJDIR)/.configured: $(APR_ICONV_OBJDIR)/.retrieved \
	$(APR_OBJDIR)/.installed
	cd $(APR_ICONV_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS) -DAPR_POOL_DEBUG" \
		CC=$(CC) CXX=$(CXX) \
		GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(APR_ICONV_SRCDIR)/configure \
		--prefix=$(PREFIX)/apr \
		--with-apr=$(PREFIX)/apr
	touch $@

# compile apr-iconv
$(APR_ICONV_OBJDIR)/.compiled: $(APR_ICONV_OBJDIR)/.configured
	(cd $(APR_ICONV_OBJDIR) \
		&& env MAKEFLAGS= make CPPFLAGS="-D_OSD_POSIX" CFLAGS="-g -O0 $(PROFILE_CFLAGS)" -j${MAKE_JOBS})
	touch $@

# install apr-iconv
$(APR_ICONV_OBJDIR)/.installed: $(APR_ICONV_OBJDIR)/.compiled
	(cd $(APR_ICONV_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# gnu-iconv
#######################################################################

gnu-iconv-retrieve:	$(GNU_ICONV_OBJDIR)/.retrieved
gnu-iconv-configure:	$(GNU_ICONV_OBJDIR)/.configured
gnu-iconv-compile:	$(GNU_ICONV_OBJDIR)/.compiled
gnu-iconv-install:	$(GNU_ICONV_OBJDIR)/.installed
gnu-iconv-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(GNU_ICONV_OBJDIR)/$(f);)

gnu-iconv-clean:
	-(cd $(GNU_ICONV_OBJDIR) && env MAKEFLAGS= make clean)
	rm -f $(GNU_ICONV_OBJDIR)/lib_encodings.def.diff
	rm -f $(GNU_ICONV_OBJDIR)/lib_aliases.gperf.diff

# fetch distfile for gnu-iconv
$(DISTDIR)/$(GNU_ICONV_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(GNU_ICONV_URL)

$(GNU_ICONV_OBJDIR)/lib_encodings.def.diff:
	mkdir -p $(dir $@)
	echo > $@.tmp '--- lib/encodings.def.orig	Wed Oct 24 23:41:41 2007'
	echo >>$@.tmp '+++ lib/encodings.def	Wed Oct 24 23:43:47 2007'
	echo >>$@.tmp '@@ -37,6 +37,7 @@'
	echo >>$@.tmp ' '
	echo >>$@.tmp ' '
	echo >>$@.tmp ' DEFENCODING(( "US-ASCII",               /* IANA */'
	echo >>$@.tmp '+              "646",'
	echo >>$@.tmp '               "ASCII",                  /* IANA, JDK 1.1 */'
	echo >>$@.tmp '               "ISO646-US",              /* IANA */'
	echo >>$@.tmp '               "ISO_646.IRV:1991",       /* IANA */'
	mv -f $@.tmp $@

$(GNU_ICONV_OBJDIR)/lib_aliases.gperf.diff:
	mkdir -p $(dir $@)
	echo > $@.tmp '--- lib/aliases.gperf.orig	Wed Oct 24 23:41:32 2007'
	echo >>$@.tmp '+++ lib/aliases.gperf	Wed Oct 24 23:47:38 2007'
	echo >>$@.tmp '@@ -10,6 +10,7 @@ struct alias { int name; unsigned int encoding_index; '
	echo >>$@.tmp ' %pic'
	echo >>$@.tmp ' %%'
	echo >>$@.tmp ' US-ASCII, ei_ascii'
	echo >>$@.tmp '+646, ei_ascii'
	echo >>$@.tmp ' ASCII, ei_ascii'
	echo >>$@.tmp ' ISO646-US, ei_ascii'
	echo >>$@.tmp ' ISO_646.IRV:1991, ei_ascii'
	mv -f $@.tmp $@

# retrieve gnu-iconv
# Add 646 as an alias for ASCII to fix prop_test 22 on OpenBSD
$(GNU_ICONV_OBJDIR)/.retrieved: $(DISTDIR)/$(GNU_ICONV_DIST) \
		$(GNU_ICONV_OBJDIR)/lib_encodings.def.diff \
		$(GNU_ICONV_OBJDIR)/lib_aliases.gperf.diff
	$(call do_check_sha256,$(GNU_ICONV_DIST))
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(GNU_ICONV_DIST)
	cd $(SRCDIR)/libiconv-$(GNU_ICONV_VER) && \
		patch -p0 < $(GNU_ICONV_OBJDIR)/lib_encodings.def.diff && \
		patch -p0 < $(GNU_ICONV_OBJDIR)/lib_aliases.gperf.diff
	cd $(SRCDIR)/libiconv-${GNU_ICONV_VER} && \
		sed -i 's/gcc/${CC}/' Makefile.devel
	touch $@

# configure gnu-iconv
$(GNU_ICONV_OBJDIR)/.configured: $(GNU_ICONV_OBJDIR)/.retrieved
	cd $(SRCDIR)/libiconv-${GNU_ICONV_VER} && \
		${MAKE} -f Makefile.devel lib/aliases.h
	cd $(GNU_ICONV_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`"\
		CC=$(CC) CXX=$(CXX) \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(GNU_ICONV_SRCDIR)/configure \
		--prefix=$(PREFIX)/iconv \
		--enable-extra-encodings
	touch $@

# compile gnu-iconv
$(GNU_ICONV_OBJDIR)/.compiled: $(GNU_ICONV_OBJDIR)/.configured
	(cd $(GNU_ICONV_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install gnu-iconv
$(GNU_ICONV_OBJDIR)/.installed: $(GNU_ICONV_OBJDIR)/.compiled
	(cd $(GNU_ICONV_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# iconv
#######################################################################

.PHONY: iconv-install iconv-reset iconv-clean

ifeq ($(USE_APR_ICONV),yes)
iconv-install: apr-iconv-install
iconv-reset: apr-iconv-reset
iconv-clean: apr-iconv-clean
else
iconv-install: gnu-iconv-install
iconv-reset: gnu-iconv-reset
iconv-clean: gnu-iconv-clean
endif

#######################################################################
# openldap
#######################################################################

openldap-retrieve:	$(OPENLDAP_OBJDIR)/.retrieved
openldap-configure:	$(OPENLDAP_OBJDIR)/.configured
openldap-compile:	$(OPENLDAP_OBJDIR)/.compiled
openldap-install:	$(OPENLDAP_OBJDIR)/.installed
openldap-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(OPENLDAP_OBJDIR)/$(f);)

openldap-clean:
	-(cd $(OPENLDAP_OBJDIR) && env MAKEFLAGS= make clean)


# fetch distfile for openldap
$(DISTDIR)/$(OPENLDAP_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(OPENLDAP_URL)

# retrieve pcre
$(OPENLDAP_OBJDIR)/.retrieved: $(DISTDIR)/$(OPENLDAP_DIST)
	$(call do_check_sha256,$(OPENLDAP_DIST))
	[ -d $(OPENLDAP_OBJDIR) ] || mkdir -p $(OPENLDAP_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(OPENLDAP_DIST)
	touch $@

# configure openldap
$(OPENLDAP_OBJDIR)/.configured: $(OPENLDAP_OBJDIR)/.retrieved \
	$(CYRUS_SASL_OBJDIR)/.installed
	cd $(OPENLDAP_OBJDIR) \
		&& env PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
			CFLAGS="-O0 -g $(PROFILE_CFLAGS)" \
			CPPFLAGS="-I$(PREFIX)/cyrus-sasl/include" \
			LDFLAGS="-L$(PREFIX)/cyrus-sasl/lib" \
			CC=$(CC) CXX=$(CXX) \
			GREP="`which grep`" \
			ac_cv_lib_ssl_SSL_export_keying_material_early=yes \
			$(OPENLDAP_SRCDIR)/configure \
			--enable-dynamic \
			--enable-local \
			--enable-ipv6 \
			--with-tls=openssl \
			--disable-balancer \
			--disable-slapd \
			--with-cyrus-sasl=yes \
		--prefix=$(PREFIX)/openldap
	touch $@

# compile openldap
$(OPENLDAP_OBJDIR)/.compiled: $(OPENLDAP_OBJDIR)/.configured
	(cd $(OPENLDAP_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install openldap
$(OPENLDAP_OBJDIR)/.installed: $(OPENLDAP_OBJDIR)/.compiled
	(cd $(OPENLDAP_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# apr-util
#######################################################################

apr-util-retrieve:	$(APR_UTIL_OBJDIR)/.retrieved
apr-util-configure:	$(APR_UTIL_OBJDIR)/.configured
apr-util-compile:	$(APR_UTIL_OBJDIR)/.compiled
apr-util-install:	$(APR_UTIL_OBJDIR)/.installed
apr-util-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(APR_UTIL_OBJDIR)/$(f);)

apr-util-clean:
	-(cd $(APR_UTIL_OBJDIR) && env MAKEFLAGS= make clean)


# retrieve apr-util if not present yet
$(APR_UTIL_OBJDIR)/.retrieved:
	[ -d $(APR_UTIL_OBJDIR) ] || mkdir -p $(APR_UTIL_OBJDIR)
	if [ ! -d $(APR_UTIL_SRCDIR) ]; then \
		svn export $(APR_UTIL_URL)/tags/$(APR_UTIL_VER)/ \
			$(APR_UTIL_SRCDIR); \
	fi
	touch $@

ifeq ($(USE_APR_ICONV),yes)
ICONV_FLAG=--with-iconv=$(PREFIX)/apr
ICONV_OBJDIR=$(APR_ICONV_OBJDIR)
else
ICONV_FLAG=--with-iconv=$(PREFIX)/iconv
ICONV_OBJDIR=$(GNU_ICONV_OBJDIR)
endif

# configure apr-util
$(APR_UTIL_OBJDIR)/.configured: $(APR_UTIL_OBJDIR)/.retrieved \
	$(APR_OBJDIR)/.installed $(ICONV_OBJDIR)/.installed
	cd $(APR_UTIL_SRCDIR) && ./buildconf --with-apr=$(APR_SRCDIR)
	cd $(APR_UTIL_OBJDIR) \
		&& env LD_LIBRARY_PATH=$(PREFIX)/bdb/lib \
			PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
			CFLAGS="-O0 -g $(PROFILE_CFLAGS) -DAPR_POOL_DEBUG" \
			CC=$(CC) CXX=$(CXX) \
			GREP="`which grep`" \
			$(APR_UTIL_SRCDIR)/configure \
		--prefix=$(PREFIX)/apr \
		--enable-maintainer-mode \
		--with-apr=$(PREFIX)/apr \
		--with-berkeley-db=$(PREFIX)/bdb \
		--with-ldap-includes=$(PREFIX)/openldap/include/ \
		--with-ldap-lib=$(PREFIX)/openldap/lib/ \
		--with-ldap=yes \
		$(ICONV_FLAG)
	touch $@

# compile apr-util
$(APR_UTIL_OBJDIR)/.compiled: $(APR_UTIL_OBJDIR)/.configured
	(cd $(APR_UTIL_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install apr-util
$(APR_UTIL_OBJDIR)/.installed: $(APR_UTIL_OBJDIR)/.compiled
	(cd $(APR_UTIL_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# pcre
#######################################################################

pcre-retrieve:	$(PCRE_OBJDIR)/.retrieved
pcre-configure:	$(PCRE_OBJDIR)/.configured
pcre-compile:	$(PCRE_OBJDIR)/.compiled
pcre-install:	$(PCRE_OBJDIR)/.installed
pcre-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(PCRE_OBJDIR)/$(f);)

pcre-clean:
	-(cd $(PCRE_OBJDIR) && env MAKEFLAGS= make clean)

# fetch distfile for pcre
$(DISTDIR)/$(PCRE_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(PCRE_URL)

# retrieve pcre
$(PCRE_OBJDIR)/.retrieved: $(DISTDIR)/$(PCRE_DIST)
	$(call do_check_sha256,$(PCRE_DIST))
	[ -d $(PCRE_OBJDIR) ] || mkdir -p $(PCRE_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(PCRE_DIST)
	touch $@

# configure pcre
$(PCRE_OBJDIR)/.configured: $(PCRE_OBJDIR)/.retrieved
	cd $(PCRE_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" \
		CC=$(CC) CXX=$(CXX) \
		GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(PCRE_SRCDIR)/configure \
		--prefix=$(PREFIX)/pcre
	touch $@

# compile pcre
$(PCRE_OBJDIR)/.compiled: $(PCRE_OBJDIR)/.configured
	(cd $(PCRE_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install pcre
$(PCRE_OBJDIR)/.installed: $(PCRE_OBJDIR)/.compiled
	(cd $(PCRE_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# httpd
#######################################################################

HTTPD_CONF=	$(PREFIX)/httpd/conf/httpd-$(SVN_REL_WC).conf
httpd-retrieve:	$(HTTPD_OBJDIR)/.retrieved
httpd-configure:	$(HTTPD_OBJDIR)/.configured
httpd-compile:	$(HTTPD_OBJDIR)/.compiled
httpd-install:	$(HTTPD_OBJDIR)/.installed $(HTTPD_CONF)
httpd-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(HTTPD_OBJDIR)/$(f);)

httpd-clean:
	-(cd $(HTTPD_OBJDIR) && env MAKEFLAGS= make clean)
	rm -f $(HTTPD_OBJDIR)/mod-proxy-no-threads-2.4.diff
	rm -f $(HTTPD_OBJDIR)/ssl-init-proxy-certs.diff

# fetch distfile for httpd
$(DISTDIR)/$(HTTPD_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(HTTPD_URL)

$(HTTPD_OBJDIR)/ssl-init-proxy-certs.diff:
	mkdir -p $(dir $@)
	echo > $@.tmp '--- modules/ssl/ssl_engine_init.c.orig	Sun Dec 16 13:34:14 2018'
	echo >> $@.tmp '+++ modules/ssl/ssl_engine_init.c	Sun Dec 16 13:34:59 2018'
	echo >> $@.tmp '@@ -1492,7 +1492,7 @@'
	echo >> $@.tmp '     X509_STORE_CTX *sctx;'
	echo >> $@.tmp '     X509_STORE *store = SSL_CTX_get_cert_store(mctx->ssl_ctx);'
	echo >> $@.tmp ' '
	echo >> $@.tmp '-#if OPENSSL_VERSION_NUMBER >= 0x1010100fL'
	echo >> $@.tmp '+#if OPENSSL_VERSION_NUMBER >= 0x1010100fL && !defined(LIBRESSL_VERSION_NUMBER)'
	echo >> $@.tmp '     /* For OpenSSL >=1.1.1, turn on client cert support which is'
	echo >> $@.tmp '      * otherwise turned off by default (by design).'
	echo >> $@.tmp '      * https://github.com/openssl/openssl/issues/6933 */'
	mv -f $@.tmp $@

$(HTTPD_OBJDIR)/mod-proxy-no-threads-2.4.diff:
	cd $(HTTPD_OBJDIR) && $(FETCH_CMD) https://stsp.name/mod-proxy-no-threads-2.4.diff

# retrieve httpd
$(HTTPD_OBJDIR)/.retrieved: $(DISTDIR)/$(HTTPD_DIST) \
	$(HTTPD_OBJDIR)/ssl-init-proxy-certs.diff \
	$(HTTPD_OBJDIR)/mod-proxy-no-threads-2.4.diff
	$(call do_check_sha256,$(HTTPD_DIST))
	[ -d $(HTTPD_OBJDIR) ] || mkdir -p $(HTTPD_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(HTTPD_DIST)
ifeq ($(shell test $(HTTPD_VER) \< 2.4.42 && echo 1), 1)
	(cd $(HTTPD_SRCDIR) && patch -p0 < $(HTTPD_OBJDIR)/ssl-init-proxy-certs.diff)
	(cd $(HTTPD_SRCDIR) && patch -p0 < $(HTTPD_OBJDIR)/mod-proxy-no-threads-2.4.diff)
endif
ifeq ($(shell test $(HTTPD_VER) \< 2.4.49 && echo 1), 1)
	(cd $(HTTPD_SRCDIR) && svn diff -cr1849590 https://svn.apache.org/repos/asf/httpd/httpd/trunk | patch -p0)
endif
	(cd $(HTTPD_SRCDIR) && svn diff -cr1663375 https://svn.apache.org/repos/asf/httpd/httpd/trunk | patch -p0)
	touch $@

# configure httpd
$(HTTPD_OBJDIR)/.configured: $(HTTPD_OBJDIR)/.retrieved \
	$(APR_UTIL_OBJDIR)/.installed $(PCRE_OBJDIR)/.installed
	cd $(HTTPD_SRCDIR) && ./buildconf \
		--with-apr="$(PREFIX)/apr/bin/apr-1-config" \
		--with-apr-util="$(PREFIX)/apr/bin/apu-1-config"
	cd $(HTTPD_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS) -DAPR_POOL_DEBUG" \
		CC=$(CC) CXX=$(CXX) \
		GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(HTTPD_SRCDIR)/configure \
		--prefix=$(PREFIX)/httpd \
		--with-mpm=prefork \
		--enable-ssl \
		--enable-dav \
		--enable-proxy \
		--disable-md \
		--disable-http2 \
		--disable-brotli \
		--with-apr="$(PREFIX)/apr/bin/apr-1-config" \
		--with-apr-util="$(PREFIX)/apr/bin/apu-1-config" \
		--with-pcre="$(PREFIX)/pcre/bin/pcre-config"
	touch $@

# compile httpd
$(HTTPD_OBJDIR)/.compiled: $(HTTPD_OBJDIR)/.configured
	(cd $(HTTPD_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install httpd
$(HTTPD_OBJDIR)/.installed: $(HTTPD_OBJDIR)/.compiled
	(cd $(HTTPD_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

# create a httpd.conf for mod_dav_svn
$(HTTPD_CONF):
	mkdir -p $(dir $@)
	echo > $@.tmp '# httpd config for use with mod_dav_svn'
	echo >>$@.tmp 'ServerRoot "$(PREFIX)/httpd"'
	echo >>$@.tmp 'Listen localhost:8080'
	echo >>$@.tmp 'LoadModule unixd_module modules/mod_unixd.so'
	echo >>$@.tmp 'LoadModule alias_module modules/mod_alias.so'
	echo >>$@.tmp 'LoadModule access_compat_module modules/mod_access_compat.so'
	echo >>$@.tmp 'LoadModule authn_core_module modules/mod_authn_core.so'
	echo >>$@.tmp 'LoadModule authn_file_module modules/mod_authn_file.so'
	echo >>$@.tmp 'LoadModule authz_core_module modules/mod_authz_core.so'
	echo >>$@.tmp 'LoadModule authz_user_module modules/mod_authz_user.so'
	echo >>$@.tmp 'LoadModule authz_groupfile_module modules/mod_authz_groupfile.so'
	echo >>$@.tmp 'LoadModule auth_basic_module modules/mod_auth_basic.so'
	echo >>$@.tmp 'LoadModule dav_module modules/mod_dav.so'
	echo >>$@.tmp 'LoadModule dav_svn_module modules/svn-$(WC)/mod_dav_svn.so'
	echo >>$@.tmp 'LoadModule authz_svn_module modules/svn-$(WC)/mod_authz_svn.so'
	echo >>$@.tmp 'DocumentRoot "$(PREFIX)/httpd/htdocs"'
	echo >>$@.tmp '<Directory />'
	echo >>$@.tmp '    Options FollowSymLinks'
	echo >>$@.tmp '    AllowOverride None'
	echo >>$@.tmp '    Require all denied'
	echo >>$@.tmp '</Directory>'
	echo >>$@.tmp '<Location /repos>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNPath /tmp/svn-sandbox/repos'
	echo >>$@.tmp '    Require ip localhost'
	echo >>$@.tmp '</Location>'
	mv -f $@.tmp $@

#######################################################################
# neon
#######################################################################

neon-retrieve:	$(NEON_OBJDIR)/.retrieved
neon-configure:	$(NEON_OBJDIR)/.configured
neon-compile:	$(NEON_OBJDIR)/.compiled
neon-install:	$(NEON_OBJDIR)/.installed
neon-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(NEON_OBJDIR)/$(f);)

neon-clean:
	-(cd $(NEON_OBJDIR) && env MAKEFLAGS= make clean)

# fetch distfile for neon
$(DISTDIR)/$(NEON_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(NEON_URL)

# retrieve neon
$(NEON_OBJDIR)/.retrieved: $(DISTDIR)/$(NEON_DIST)
	$(call do_check_sha256,$(NEON_DIST))
	[ -d $(NEON_OBJDIR) ] || mkdir -p $(NEON_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(NEON_DIST)
	touch $@

# OpenBSD does not have krb5-config in PATH, but the neon port has
# a suitable replacement.
ifeq ($(UNAME),OpenBSD)
KRB5_CONFIG_PATH=/usr/ports/net/neon/files
endif

# configure neon
$(NEON_OBJDIR)/.configured: $(NEON_OBJDIR)/.retrieved
	cd $(NEON_SRCDIR) && ./autogen.sh
	if [ -n "$(KRB5_CONFIG_PATH)" ] && [ -d "$(KRB5_CONFIG_PATH)" ]; then \
		cp $(KRB5_CONFIG_PATH)/krb5-config $(NEON_OBJDIR); \
		chmod +x $(NEON_OBJDIR)/krb5-config; \
	fi
	cd $(NEON_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`" \
		CC=$(CC) CXX=$(CXX) \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(NEON_SRCDIR)/configure \
		PATH=$(NEON_OBJDIR):$$PATH \
		--prefix=$(PREFIX)/neon \
		--with-ssl \
		--enable-shared \
		--without-libproxy
	touch $@

# compile neon
$(NEON_OBJDIR)/.compiled: $(NEON_OBJDIR)/.configured
	(cd $(NEON_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	(cd $(NEON_OBJDIR) && env MAKEFLAGS= make docs)
	touch $@

# install neon
$(NEON_OBJDIR)/.installed: $(NEON_OBJDIR)/.compiled
	(cd $(NEON_OBJDIR) && env MAKEFLAGS= make install)
	touch $@


#######################################################################
# serf
#######################################################################

serf-retrieve:	$(SERF_OBJDIR)/.retrieved
serf-configure:	$(SERF_OBJDIR)/.configured
serf-compile:	$(SERF_OBJDIR)/.compiled
serf-install:	$(SERF_OBJDIR)/.installed
serf-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(SERF_OBJDIR)/$(f);)

serf-clean:
	-(cd $(SERF_SRCDIR) && scons -c)


# fetch distfile for serf
#$(DISTDIR)/$(SERF_DIST):
#	cd $(DISTDIR) && $(FETCH_CMD) $(SERF_URL)
#
# retrieve serf
#$(SERF_OBJDIR)/.retrieved: $(DISTDIR)/$(SERF_DIST)
#	[ -d $(SERF_OBJDIR) ] || mkdir -p $(SERF_OBJDIR)
#	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(SERF_DIST)
#	touch $@

# retrieve serf if not present yet
$(SERF_OBJDIR)/.retrieved:
	[ -d $(SERF_OBJDIR) ] || mkdir -p $(SERF_OBJDIR)
	if [ ! -d $(SERF_SRCDIR) ]; then \
		svn co $(SERF_URL) $(SERF_SRCDIR); \
		svn merge ^/serf/branches/1.3.x-sslbuild@1910116 $(SERF_SRCDIR); \
		(cd $(SERF_SRCDIR) && svn merge -c r1833229 \
			^/serf/trunk/buckets/ssl_buckets.c \
			buckets/ssl_buckets.c); \
	fi
	touch $@

# compile serf (serf won't compile outside its source tree)
$(SERF_OBJDIR)/.compiled: $(SERF_OBJDIR)/.retrieved \
	$(APR_UTIL_OBJDIR)/.installed
	cd $(SERF_SRCDIR) && \
		scons -j${MAKE_JOBS} DEBUG=1 \
			CFLAGS="-O0 -g $(PROFILE_CFLAGS) -fPIC -DAPR_POOL_DEBUG" \
			CC=$(CC) CXX=$(CXX) \
			APR=$(PREFIX)/apr \
			APU=$(PREFIX)/apr \
			PREFIX=$(PREFIX)/serf \
			PKG_CONFIG_PATH=$(PKG_CONFIG_PATH)
	touch $@

# install serf
$(SERF_OBJDIR)/.installed: $(SERF_OBJDIR)/.compiled
	rm -rf $(PREFIX)/serf # XXX scons cannot reinstall :(
	cd $(SERF_SRCDIR) && \
		scons install
	# work around unportable scons shared lib support
	-ln -s libserf-1.so.$(shell echo $(SERF_VER) | sed -e 's/[0-9]$$/0/') \
		$(PREFIX)/serf/lib/libserf-1.so
	-ln -s libserf-1.so.$(shell echo $(SERF_VER) | sed -e 's/[0-9]$$/0/') \
		$(PREFIX)/serf/lib/libserf-1.so.$(shell echo $(SERF_VER) | sed -e 's/\.[0-9]$$//')
	touch $@

#######################################################################
# sqlite
#######################################################################

sqlite-retrieve:	$(SQLITE_OBJDIR)/.retrieved
sqlite-configure:	$(SQLITE_OBJDIR)/.configured
sqlite-compile:	$(SQLITE_OBJDIR)/.compiled
sqlite-install:	$(SQLITE_OBJDIR)/.installed
sqlite-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(SQLITE_OBJDIR)/$(f);)

sqlite-clean:
	-cd $(SQLITE_OBJDIR) && env MAKEFLAGS= make clean

# fetch distfile for sqlite
$(DISTDIR)/$(SQLITE_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(SQLITE_URL)

# retrieve sqlite
$(SQLITE_OBJDIR)/.retrieved: $(DISTDIR)/$(SQLITE_DIST)
	$(call do_check_sha256,$(SQLITE_DIST))
	[ -d $(SQLITE_OBJDIR) ] || mkdir -p $(SQLITE_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(SQLITE_DIST)
	touch $@

ifeq ($(THREADING),yes)
SQLITE_THREADSAFE_FLAG=--enable-threadsafe
else
SQLITE_THREADSAFE_FLAG=--disable-threadsafe
endif

# configure sqlite
$(SQLITE_OBJDIR)/.configured: $(SQLITE_OBJDIR)/.retrieved
	cd $(SQLITE_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		CC=$(CC) CXX=$(CXX) \
		$(SQLITE_SRCDIR)/configure \
		--prefix=$(PREFIX)/sqlite \
		$(SQLITE_THREADSAFE_FLAG)
	touch $@

# compile sqlite
$(SQLITE_OBJDIR)/.compiled: $(SQLITE_OBJDIR)/.configured
	(cd $(SQLITE_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install sqlite
$(SQLITE_OBJDIR)/.installed: $(SQLITE_OBJDIR)/.compiled
	(cd $(SQLITE_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# cyrus-sasl
#######################################################################

cyrus-sasl-retrieve:	$(CYRUS_SASL_OBJDIR)/.retrieved
cyrus-sasl-configure:	$(CYRUS_SASL_OBJDIR)/.configured
cyrus-sasl-compile:	$(CYRUS_SASL_OBJDIR)/.compiled
cyrus-sasl-install:	$(CYRUS_SASL_OBJDIR)/.installed
cyrus-sasl-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(CYRUS_SASL_OBJDIR)/$(f);)

cyrus-sasl-clean:
	-(cd $(CYRUS_SASL_OBJDIR) && env MAKEFLAGS= make distclean)

# fetch distfile for cyrus-sasl
$(DISTDIR)/$(CYRUS_SASL_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(CYRUS_SASL_URL)

# retrieve cyrus-sasl
$(CYRUS_SASL_OBJDIR)/.retrieved: $(DISTDIR)/$(CYRUS_SASL_DIST)
	$(call do_check_sha256,$(CYRUS_SASL_DIST))
	[ -d $(CYRUS_SASL_OBJDIR) ] || mkdir -p $(CYRUS_SASL_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(CYRUS_SASL_DIST)
	# fixes build on Debian:
	sed 's/#elif WITH_DES/#elif defined(WITH_DES)/' \
		< $(CYRUS_SASL_SRCDIR)/plugins/digestmd5.c \
		> $(CYRUS_SASL_SRCDIR)/plugins/digestmd5.c.patched
	mv $(CYRUS_SASL_SRCDIR)/plugins/digestmd5.c.patched \
		$(CYRUS_SASL_SRCDIR)/plugins/digestmd5.c
ifeq ($(UNAME),OpenBSD)
	# Fixes GSSAPI support on OpenBSD, which hasn't got libroken:
	for f in `grep -l -R -- -lroken $(CYRUS_SASL_SRCDIR)`; do \
		sed -e 's/-lroken//g' < $$f > $$f.tmp && \
		mv $$f.tmp $$f; \
	done
	chmod +x $(CYRUS_SASL_SRCDIR)/configure
endif
	# Fixes excessive auth log spam from sasl if broken .la files exist
	sed 's/SASL_LOG_WARN/SASL_LOG_DEBUG/' \
		< $(CYRUS_SASL_SRCDIR)/lib/dlopen.c \
		> $(CYRUS_SASL_SRCDIR)/lib/dlopen.c.patched
	mv $(CYRUS_SASL_SRCDIR)/lib/dlopen.c.patched \
		$(CYRUS_SASL_SRCDIR)/lib/dlopen.c
	# Fix a weird autotools error about missing cmulocal dir
	(cd $(CYRUS_SASL_SRCDIR)/saslauthd/ && ln -sf ../cmulocal)
	# Fix 64-bit time_t format-string issues
	sed -i 's/t%020ld"/t%020lld"/' $(CYRUS_SASL_SRCDIR)/plugins/otp.c
	touch $@

# configure cyrus-sasl
$(CYRUS_SASL_OBJDIR)/.configured: $(CYRUS_SASL_OBJDIR)/.retrieved \
	$(BDB_OBJDIR)/.installed $(SQLITE_OBJDIR)/.installed
	cd $(CYRUS_SASL_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" \
		CC=$(CC) CXX=$(CXX) MAKE=$(MAKE) \
		GREP="`which grep`" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(CYRUS_SASL_SRCDIR)/configure \
		--with-dbpath=$(PREFIX)/cyrus-sasl/etc/sasldb2 \
		--with-plugindir=$(PREFIX)/cyrus-sasl/lib/sasl2 \
		--with-configdir=$(PREFIX)/cyrus-sasl/lib/sasl2 \
		--with-bdb-libdir=$(PREFIX)/bdb/lib \
		--with-bdb-incdir=$(PREFIX)/bdb/include \
		--with-dblib=berkeley \
		--with-sqlite=$(PREFIX)/sqlite \
		--prefix=$(PREFIX)/cyrus-sasl
	touch $@

# compile cyrus-sasl (ignore MAKE_JOBS; multiple jobs cause random build failures)
$(CYRUS_SASL_OBJDIR)/.compiled: $(CYRUS_SASL_OBJDIR)/.configured
	(cd $(CYRUS_SASL_OBJDIR) && env MAKE=$(MAKE) MAKEFLAGS= $(MAKE))
	touch $@

# install cyrus-sasl
$(CYRUS_SASL_OBJDIR)/.installed: $(CYRUS_SASL_OBJDIR)/.compiled
	(cd $(CYRUS_SASL_OBJDIR) && env MAKE=$(MAKE) MAKEFLAGS= $(MAKE) install)
	touch $@

#######################################################################
# libmagic
#######################################################################

libmagic-retrieve:	$(LIBMAGIC_OBJDIR)/.retrieved
libmagic-configure:	$(LIBMAGIC_OBJDIR)/.configured
libmagic-compile:	$(LIBMAGIC_OBJDIR)/.compiled
libmagic-install:	$(LIBMAGIC_OBJDIR)/.installed
libmagic-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(LIBMAGIC_OBJDIR)/$(f);)

libmagic-clean:
	-(cd $(LIBMAGIC_OBJDIR) && env MAKEFLAGS= make distclean)

# fetch distfile for libmagic
$(DISTDIR)/$(LIBMAGIC_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(LIBMAGIC_URL)

# retrieve libmagic
$(LIBMAGIC_OBJDIR)/.retrieved: $(DISTDIR)/$(LIBMAGIC_DIST)
	$(call do_check_sha256,$(LIBMAGIC_DIST))
	[ -d $(LIBMAGIC_OBJDIR) ] || mkdir -p $(LIBMAGIC_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(LIBMAGIC_DIST)
	touch $@

# configure libmagic
$(LIBMAGIC_OBJDIR)/.configured: $(LIBMAGIC_OBJDIR)/.retrieved
	cd $(LIBMAGIC_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`"\
		CC=$(CC) CXX=$(CXX) \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(LIBMAGIC_SRCDIR)/configure \
		--enable-fsect-man5 \
		--prefix=$(PREFIX)/libmagic
	touch $@

# compile libmagic
$(LIBMAGIC_OBJDIR)/.compiled: $(LIBMAGIC_OBJDIR)/.configured
	(cd $(LIBMAGIC_OBJDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install libmagic
$(LIBMAGIC_OBJDIR)/.installed: $(LIBMAGIC_OBJDIR)/.compiled
	(cd $(LIBMAGIC_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# ruby
#######################################################################

ruby-retrieve:	$(RUBY_OBJDIR)/.retrieved
ruby-configure:	$(RUBY_OBJDIR)/.configured
ruby-compile:	$(RUBY_OBJDIR)/.compiled
ruby-install:	$(RUBY_OBJDIR)/.installed
ruby-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(RUBY_OBJDIR)/$(f);)

ruby-clean:
	-(cd $(RUBY_OBJDIR) && env MAKEFLAGS= make distclean)

# fetch distfile for ruby
$(DISTDIR)/$(RUBY_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(RUBY_URL)

$(RUBY_OBJDIR)/openssl_missing.patch:
	mkdir -p $(dir $@)
ifeq ($(shell test $(RUBY_MAJOR_VER) \< 3.0 && echo 1), 1)
	echo > $@.tmp 'Index: ext/openssl/openssl_missing.h'
	echo >> $@.tmp '--- ext/openssl/openssl_missing.h.orig'
	echo >> $@.tmp '+++ ext/openssl/openssl_missing.h'
	echo >> $@.tmp '@@ -119,6 +119,9 @@ void ossl_HMAC_CTX_free(HMAC_CTX *);'
	echo >> $@.tmp ' #if !defined(HAVE_X509_STORE_SET_EX_DATA)'
	echo >> $@.tmp ' #  define X509_STORE_set_ex_data(x, idx, data) \'
	echo >> $@.tmp ' 	CRYPTO_set_ex_data(&(x)->ex_data, (idx), (data))'
	echo >> $@.tmp '+#endif'
	echo >> $@.tmp '+'
	echo >> $@.tmp '+#if !defined(HAVE_X509_STORE_GET_EX_NEW_INDEX)'
	echo >> $@.tmp ' #  define X509_STORE_get_ex_new_index(l, p, newf, dupf, freef) \'
	echo >> $@.tmp ' 	CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE, (l), (p), \'
	echo >> $@.tmp ' 				(newf), (dupf), (freef))'
	echo >> $@.tmp '@@ -192,6 +195,7 @@ void ossl_X509_REQ_get0_signature(const X509_REQ *, co'
	echo >> $@.tmp ' #endif'
	echo >> $@.tmp ' '
	echo >> $@.tmp ' #if !defined(HAVE_OPAQUE_OPENSSL)'
	echo >> $@.tmp '+#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL'
	echo >> $@.tmp ' #define IMPL_PKEY_GETTER(_type, _name) \'
	echo >> $@.tmp ' static inline _type *EVP_PKEY_get0_##_type(EVP_PKEY *pkey) { \'
	echo >> $@.tmp ' 	return pkey->pkey._name; }'
	echo >> $@.tmp '@@ -243,6 +247,7 @@ IMPL_PKEY_GETTER(EC_KEY, ec)'
	echo >> $@.tmp ' #undef IMPL_PKEY_GETTER'
	echo >> $@.tmp ' #undef IMPL_KEY_ACCESSOR2'
	echo >> $@.tmp ' #undef IMPL_KEY_ACCESSOR3'
	echo >> $@.tmp '+#endif'
	echo >> $@.tmp ' #endif /* HAVE_OPAQUE_OPENSSL */'
	echo >> $@.tmp ' '
	echo >> $@.tmp ' #if defined(HAVE_AUTHENTICATED_ENCRYPTION) && !defined(EVP_CTRL_AEAD_GET_TAG)'
	mv -f $@.tmp $@
else
	touch $@
endif

$(RUBY_OBJDIR)/sparc64_buserror.patch:
	mkdir -p $(dir $@)
ifeq ($(shell test $(RUBY_MAJOR_VER) \< 3.0 && echo 1), 1)
	echo > $@.tmp '--- compile.c.orig	Thu Mar 12 12:58:26 2020'
	echo >> $@.tmp '+++ compile.c	Thu Mar 12 16:36:55 2020'
	echo >> $@.tmp '@@ -751,6 +751,16 @@'
	echo >> $@.tmp '   #define STRICT_ALIGNMENT'
	echo >> $@.tmp ' #endif'
	echo >> $@.tmp ' '
	echo >> $@.tmp '+/*'
	echo >> $@.tmp '+ * Some OpenBSD platforms (including sparc64) require strict alignment.'
	echo >> $@.tmp '+ */'
	echo >> $@.tmp '+#if defined(__OpenBSD__)'
	echo >> $@.tmp '+  #include <sys/endian.h>'
	echo >> $@.tmp '+  #ifdef __STRICT_ALIGNMENT'
	echo >> $@.tmp '+    #define STRICT_ALIGNMENT'
	echo >> $@.tmp '+  #endif'
	echo >> $@.tmp '+#endif'
	echo >> $@.tmp '+'
	echo >> $@.tmp ' #ifdef STRICT_ALIGNMENT'
	echo >> $@.tmp '   #if defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG > SIZEOF_VALUE'
	echo >> $@.tmp '     #define ALIGNMENT_SIZE SIZEOF_LONG_LONG'
	mv -f $@.tmp $@
else
	touch $@
endif

ifeq ($(UNAME),OpenBSD)
RUBY_SSL_EX_NEW_DATA_PATCH = sed -i -e '/^have_func("X509_STORE_set_ex_data")$$/ { p; s/^.*$$/\have_func("X509_STORE_get_ex_new_index")/; }'
else
RUBY_SSL_EX_NEW_DATA_PATCH = true
endif

# retrieve ruby
#
$(RUBY_OBJDIR)/.retrieved: $(DISTDIR)/$(RUBY_DIST) $(RUBY_OBJDIR)/openssl_missing.patch $(RUBY_OBJDIR)/sparc64_buserror.patch
	$(call do_check_sha256,$(RUBY_DIST))
	[ -d $(RUBY_OBJDIR) ] || mkdir -p $(RUBY_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(RUBY_DIST)
	-which ghead && sed -i -e "s/head -c/ghead -c/" $(RUBY_SRCDIR)/configure
	$(RUBY_SSL_EX_NEW_DATA_PATCH) $(RUBY_SRCDIR)/ext/openssl/extconf.rb
ifeq ($(shell test $(RUBY_MAJOR_VER) \< 3.0 && echo 1), 1)
	cd $(RUBY_SRCDIR) && patch -p0 < $(RUBY_OBJDIR)/openssl_missing.patch
	cd $(RUBY_SRCDIR) && patch -p0 < $(RUBY_OBJDIR)/sparc64_buserror.patch
endif
	sed -i 's/X509_num(bs->certs)/X509_num(OCSP_resp_get0_certs(bs))/' \
		$(RUBY_SRCDIR)/ext/openssl/ossl_ocsp.c
	touch $@

ifeq ($(THREADING),yes)
RUBY_PTHREAD_FLAG=--enable-pthread
else
RUBY_PTHREAD_FLAG=--disable-pthread
endif

# configure ruby
$(RUBY_OBJDIR)/.configured: $(RUBY_OBJDIR)/.retrieved
	cd $(RUBY_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`"\
		CC=$(CC) CXX=$(CXX) \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(RUBY_SRCDIR)/configure \
		--prefix=$(PREFIX)/ruby \
		--enable-shared \
		--with-baseruby="$(RUBY)" \
		$(RUBY_PTHREAD_FLAG) \
		--disable-install-doc \
		--without-valgrind \
		--without-gmp
	touch $@

# compile ruby (ignore MAKE_JOBS; multiple jobs cause random build failures)
$(RUBY_OBJDIR)/.compiled: $(RUBY_OBJDIR)/.configured
	(cd $(RUBY_OBJDIR) && env MAKEFLAGS= make)
	touch $@

# install ruby
$(RUBY_OBJDIR)/.installed: $(RUBY_OBJDIR)/.compiled
	(cd $(RUBY_OBJDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# bz2
#######################################################################

bz2-retrieve:	$(BZ2_OBJDIR)/.retrieved
bz2-compile:	$(BZ2_OBJDIR)/.compiled
bz2-install:	$(BZ2_OBJDIR)/.installed
bz2-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(BZ2_OBJDIR)/$(f);)

bz2-clean:
	-(cd $(BZ2_SRCDIR) && env MAKEFLAGS= make distclean)

# fetch distfile for bz2
$(DISTDIR)/$(BZ2_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(BZ2_URL)

# retrieve bz2
$(BZ2_OBJDIR)/.retrieved: $(DISTDIR)/$(BZ2_DIST)
	$(call do_check_sha256,$(BZ2_DIST))
	[ -d $(BZ2_OBJDIR) ] || mkdir -p $(BZ2_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(BZ2_DIST)
	touch $@

# compile bz2
$(BZ2_OBJDIR)/.compiled: $(BZ2_OBJDIR)/.retrieved
	(cd $(BZ2_SRCDIR) && env MAKEFLAGS= make CC=$(CC) CXX=$(CXX) \
		CFLAGS="-g $(PROFILE_CFLAGS) -fPIC" -j${MAKE_JOBS})
	touch $@

# install bz2
$(BZ2_OBJDIR)/.installed: $(BZ2_OBJDIR)/.compiled
	(cd $(BZ2_SRCDIR) && env MAKEFLAGS= make install PREFIX=$(PREFIX)/bz2)
	touch $@


#######################################################################
# python
#######################################################################

python-retrieve:	$(PYTHON_OBJDIR)/.retrieved
python-configure:	$(PYTHON_OBJDIR)/.configured
python-compile:	$(PYTHON_OBJDIR)/.compiled
python-install:	$(PYTHON_OBJDIR)/.installed
python-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(PYTHON_OBJDIR)/$(f);)

python-clean:
	-(cd $(PYTHON_OBJDIR) && env MAKEFLAGS= make distclean)

# fetch distfile for python
$(DISTDIR)/$(PYTHON_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(PYTHON_URL)

$(PYTHON_OBJDIR)/openbsd-march.diff:
	mkdir -p $(dir $@)
	echo >$@.tmp '--- configure.ac.orig	Mon Nov 21 13:32:19 2022'
	echo >>$@.tmp '+++ configure.ac	Mon Nov 21 13:32:40 2022'
	echo >>$@.tmp '@@ -878,2 +878,3 @@'
	echo >>$@.tmp '   [FreeBSD*], [MULTIARCH=""],'
	echo >>$@.tmp '+  [OpenBSD*], [MULTIARCH=""],'
	echo >>$@.tmp '   [MULTIARCH=$$($$CC --print-multiarch 2>/dev/null)]'
	mv -f $@.tmp $@

$(PYTHON_OBJDIR)/openbsd-make.diff:
	mkdir -p $(dir $@)
	echo >$@.tmp 'Index: Makefile.pre.in'
	echo >>$@.tmp '--- Makefile.pre.in.orig'
	echo >>$@.tmp '+++ Makefile.pre.in'
	echo >>$@.tmp '@@ -534,8 +534,6 @@ LIBMPDEC_OBJS= \'
	echo >>$@.tmp ' 		Modules/_decimal/libmpdec/numbertheory.o \'
	echo >>$@.tmp ' 		Modules/_decimal/libmpdec/sixstep.o \'
	echo >>$@.tmp ' 		Modules/_decimal/libmpdec/transpose.o'
	echo >>$@.tmp '-		# _decimal does not use signaling API'
	echo >>$@.tmp '-		# Modules/_decimal/libmpdec/mpsignal.o'
	echo >>$@.tmp ' '
	echo >>$@.tmp ' LIBMPDEC_HEADERS= \'
	echo >>$@.tmp ' 		$(srcdir)/Modules/_decimal/libmpdec/basearith.h \'
	mv -f $@.tmp $@

# retrieve python
#
$(PYTHON_OBJDIR)/.retrieved: $(DISTDIR)/$(PYTHON_DIST) $(PYTHON_OBJDIR)/openbsd-march.diff $(PYTHON_OBJDIR)/openbsd-make.diff
	$(call do_check_sha256,$(PYTHON_DIST))
	[ -d $(PYTHON_OBJDIR) ] || mkdir -p $(PYTHON_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(PYTHON_DIST)
	# Make setup.py use our own dependencies instead of system ones
	if [ -f $(PYTHON_SRCDIR)/setup.py ]; then \
	sed -e "s#sqlite_inc_paths = \[ '/usr/include',#sqlite_inc_paths = [ '$(PREFIX)/sqlite/include',#" \
		-e "s#'/usr/include/db4'#'$(PREFIX)/bdb/include'#" \
		-e "s|\(add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')\)|pass #\1|" \
		-e "s|\(add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')\)|pass #\1|" \
		-e "s#find_library_file(lib_dirs, 'bz2'#find_library_file(['$(PREFIX)/bz2/lib'] + lib_dirs, 'bz2'#" \
		< $(PYTHON_SRCDIR)/setup.py \
		> $(PYTHON_SRCDIR)/setup.py.patched && \
	mv $(PYTHON_SRCDIR)/setup.py.patched $(PYTHON_SRCDIR)/setup.py && \
	chmod +x $(PYTHON_SRCDIR)/setup.py; \
	fi
	cd $(PYTHON_SRCDIR) && patch -p0 < $(PYTHON_OBJDIR)/openbsd-march.diff
	cd $(PYTHON_SRCDIR) && patch -p0 < $(PYTHON_OBJDIR)/openbsd-make.diff
	touch $@

# configure python
ifdef PROFILE
PYTHON_PROFILING=--enable-profiling
endif
$(PYTHON_OBJDIR)/.configured: $(PYTHON_OBJDIR)/.retrieved \
	$(BZ2_OBJDIR)/.installed
	if [ "`uname`" = "OpenBSD" ]; then cd $(PYTHON_SRCDIR) && autoconf; fi
	cd $(PYTHON_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`" \
		CC=$(CC) CXX=$(CXX) \
		CPPFLAGS="-I$(PREFIX)/bz2/include" \
		LDFLAGS="-Wl,-rpath=$(PREFIX)/python/lib -L$(PREFIX)/bz2/lib" \
		LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$(PREFIX)/python/lib:$$LD_LIBRARY_PATH" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(PYTHON_SRCDIR)/configure \
		--prefix=$(PREFIX)/python \
		--enable-shared \
		--with-system-expat \
		--with-dbmliborder=bdb \
		--without-pymalloc \
		$(PYTHON_PROFILING)
	touch $@

# compile python
$(PYTHON_OBJDIR)/.compiled: $(PYTHON_OBJDIR)/.configured
	(cd $(PYTHON_OBJDIR) && \
		env MAKEFLAGS= \
		LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$(PREFIX)/python/lib:$$LD_LIBRARY_PATH" \
		make -j${MAKE_JOBS})
	touch $@

# install python
$(PYTHON_OBJDIR)/.installed: $(PYTHON_OBJDIR)/.compiled
	(cd $(PYTHON_OBJDIR) && \
		env MAKEFLAGS= \
		LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$(PREFIX)/python/lib:$$LD_LIBRARY_PATH" \
		make install)
	ln -sf $(PREFIX)/python/bin/python3 $(PREFIX)/python/bin/python
	touch $@

#######################################################################
# python 2
#######################################################################

python2-retrieve:	$(PYTHON2_OBJDIR)/.retrieved
python2-configure:	$(PYTHON2_OBJDIR)/.configured
python2-compile:	$(PYTHON2_OBJDIR)/.compiled
python2-install:	$(PYTHON2_OBJDIR)/.installed
python2-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(PYTHON2_OBJDIR)/$(f);)

python2-clean:
	-(cd $(PYTHON2_OBJDIR) && env MAKEFLAGS= make distclean)

# fetch distfile for python 2
$(DISTDIR)/$(PYTHON2_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(PYTHON2_URL)

# https://bugs.python.org/issue12560
$(DISTDIR)/python-issue12560.patch:
	cd $(DISTDIR) && $(FETCH_CMD) -O "$@" \
		https://bugs.python.org/file22654/python-2.7.2-configure.diff

# retrieve python 2
#
$(PYTHON2_OBJDIR)/.retrieved: $(DISTDIR)/$(PYTHON2_DIST) $(DISTDIR)/python-issue12560.patch
	$(call do_check_sha256,$(PYTHON2_DIST))
	[ -d $(PYTHON2_OBJDIR) ] || mkdir -p $(PYTHON2_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(PYTHON2_DIST)
	# Make setup.py use our own dependencies instead of system ones
	sed -e "s#sqlite_inc_paths = \[ '/usr/include',#sqlite_inc_paths = [ '$(PREFIX)/sqlite/include',#" \
		-e "s#'/usr/include/db4'#'$(PREFIX)/bdb/include'#" \
		-e "s|\(add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')\)|pass #\1|" \
		-e "s|\(add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')\)|pass #\1|" \
		-e "s#find_library_file(lib_dirs, 'bz2'#find_library_file(['$(PREFIX)/bz2/lib'] + lib_dirs, 'bz2'#" \
		< $(PYTHON2_SRCDIR)/setup.py \
		> $(PYTHON2_SRCDIR)/setup.py.patched
	mv $(PYTHON2_SRCDIR)/setup.py.patched $(PYTHON2_SRCDIR)/setup.py
	chmod +x $(PYTHON2_SRCDIR)/setup.py
	cd $(PYTHON2_SRCDIR) && patch -p1 < $(DISTDIR)/python-issue12560.patch
	mkdir -p $(PYTHON2_OBJDIR)/bin
	ln -sf "`which python2`" $(PYTHON2_OBJDIR)/bin/python
	touch $@

# configure python 2
ifdef PROFILE
PYTHON2_PROFILING=--enable-profiling
endif
$(PYTHON2_OBJDIR)/.configured: $(PYTHON2_OBJDIR)/.retrieved \
	$(BZ2_OBJDIR)/.installed
	cd $(PYTHON2_OBJDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`" \
		CC=$(CC) CXX=$(CXX) \
		CPPFLAGS="-I$(PREFIX)/bz2/include" \
		LDFLAGS="-Wl,-rpath=$(PREFIX)/python2/lib -L$(PREFIX)/bz2/lib" \
		LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$(PREFIX)/python2/lib:$$LD_LIBRARY_PATH" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		PATH=$(PYTHON2_OBJDIR)/bin:$$PATH \
		$(PYTHON2_SRCDIR)/configure \
		--prefix=$(PREFIX)/python2 \
		--enable-shared \
		--with-system-expat \
		--with-dbmliborder=bdb \
		--without-pymalloc \
		$(PYTHON2_PROFILING)
	touch $@

# compile python 2
$(PYTHON2_OBJDIR)/.compiled: $(PYTHON2_OBJDIR)/.configured
	(cd $(PYTHON2_OBJDIR) && \
		env MAKEFLAGS= \
		LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$(PREFIX)/python2/lib:$$LD_LIBRARY_PATH" \
		PATH=$(PYTHON2_OBJDIR)/bin:$$PATH \
		make -j${MAKE_JOBS})
	touch $@

# install python 2
$(PYTHON2_OBJDIR)/.installed: $(PYTHON2_OBJDIR)/.compiled
	(cd $(PYTHON2_OBJDIR) && \
		env MAKEFLAGS= \
		LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$(PREFIX)/python2/lib:$$LD_LIBRARY_PATH" \
		PATH=$(PYTHON2_OBJDIR)/bin:$$PATH \
		make install)
	ln -sf $(PREFIX)/python2/bin/python2 $(PREFIX)/python2/bin/python
	touch $@

#######################################################################
# py3c
#######################################################################

py3c-retrieve:	$(PY3C_OBJDIR)/.retrieved
py3c-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(PY3C_OBJDIR)/$(f);)

# fetch distfile for py3c
$(DISTDIR)/$(PY3C_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(PY3C_URL)

# retrieve py3c
$(PY3C_OBJDIR)/.retrieved: $(DISTDIR)/$(PY3C_DIST)
	$(call do_check_sha256,$(PY3C_DIST))
	[ -d $(PY3C_OBJDIR) ] || mkdir -p $(PY3C_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(PY3C_DIST)
	touch $@

#######################################################################
# junit
#######################################################################

# fetch distfile for junit
$(DISTDIR)/$(JUNIT_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(JUNIT_URL)
	$(call do_check_sha256,$(JUNIT_DIST))


#######################################################################
# gettext
#######################################################################

gettext-retrieve:	$(GETTEXT_OBJDIR)/.retrieved
gettext-configure:	$(GETTEXT_OBJDIR)/.configured
gettext-compile:	$(GETTEXT_OBJDIR)/.compiled
gettext-install:	$(GETTEXT_OBJDIR)/.installed
gettext-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(GETTEXT_OBJDIR)/$(f);)

gettext-clean:
	-(cd $(GETTEXT_SRCDIR) && env MAKEFLAGS= make clean)

# fetch distfile for gettext
$(DISTDIR)/$(GETTEXT_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) $(GETTEXT_URL)

# retrieve gettext
$(GETTEXT_OBJDIR)/.retrieved: $(DISTDIR)/$(GETTEXT_DIST)
	$(call do_check_sha256,$(GETTEXT_DIST))
	[ -d $(GETTEXT_OBJDIR) ] || mkdir -p $(GETTEXT_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(GETTEXT_DIST)
	touch $@

# (gettext won't compile outside its source tree)
# configure gettext
$(GETTEXT_OBJDIR)/.configured: $(GETTEXT_OBJDIR)/.retrieved
	cd $(GETTEXT_SRCDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`"\
		CC=$(CC) CXX=$(CXX) \
		LDFLAGS="-L$(PREFIX)/iconv/lib" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(GETTEXT_SRCDIR)/configure \
		--prefix=$(PREFIX)/gettext \
		--with-libiconv-prefix=$(PREFIX)/iconv \
		--disable-c++ \
		--disable-java \
		--disable-csharp \
		$(THREADS_FLAG)
	-which gsed && \
		sed -e 's/sed /gsed /g' < $(GETTEXT_SRCDIR)/build-aux/moopp \
		> $(GETTEXT_SRCDIR)/build-aux/moopp.fixed && \
		mv $(GETTEXT_SRCDIR)/build-aux/moopp.fixed \
		$(GETTEXT_SRCDIR)/build-aux/moopp && \
		chmod +x $(GETTEXT_SRCDIR)/build-aux/moopp
	touch $@

# compile gettext
$(GETTEXT_OBJDIR)/.compiled: $(GETTEXT_OBJDIR)/.configured
	(cd $(GETTEXT_SRCDIR) && env MAKEFLAGS= make -j${MAKE_JOBS})
	touch $@

# install gettext
$(GETTEXT_OBJDIR)/.installed: $(GETTEXT_OBJDIR)/.compiled
	(cd $(GETTEXT_SRCDIR) && env MAKEFLAGS= make install)
	touch $@

#######################################################################
# lz4
#######################################################################

lz4-retrieve:	$(LZ4_OBJDIR)/.retrieved
lz4-configure:	$(LZ4_OBJDIR)/.configured
lz4-compile:	$(LZ4_OBJDIR)/.compiled
lz4-install:	$(LZ4_OBJDIR)/.installed
lz4-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(LZ4_OBJDIR)/$(f);)

lz4-clean:
	-(cd $(LZ4_SRCDIR) && env MAKEFLAGS= $(MAKE) clean)

# fetch distfile for lz4
$(DISTDIR)/$(LZ4_DIST):
	cd $(DISTDIR) && $(FETCH_CMD) -O $(LZ4_DIST) $(LZ4_URL)

# retrieve lz4
$(LZ4_OBJDIR)/.retrieved: $(DISTDIR)/$(LZ4_DIST)
	$(call do_check_sha256,$(LZ4_DIST))
	[ -d $(LZ4_OBJDIR) ] || mkdir -p $(LZ4_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(LZ4_DIST)
	touch $@

# configure lz4
$(LZ4_OBJDIR)/.configured: $(LZ4_OBJDIR)/.retrieved
	touch $@

# compile lz4
$(LZ4_OBJDIR)/.compiled: $(LZ4_OBJDIR)/.configured
	(cd $(LZ4_SRCDIR)/lib && \
		env MAKEFLAGS= $(MAKE) CC=$(CC) CXX=$(CXX) \
			-j${MAKE_JOBS} PREFIX=$(PREFIX)/lz4)
	touch $@

# install lz4
$(LZ4_OBJDIR)/.installed: $(LZ4_OBJDIR)/.compiled
	mkdir -p $(PREFIX)/lz4/lib
	(cd $(LZ4_SRCDIR)/lib && \
		env MAKEFLAGS= $(MAKE) PREFIX=$(PREFIX)/lz4 install)
	touch $@

#######################################################################
# swig
#######################################################################

swig-retrieve:	$(SWIG_OBJDIR)/.retrieved
swig-configure:	$(SWIG_OBJDIR)/.configured
swig-compile:	$(SWIG_OBJDIR)/.compiled
swig-install:	$(SWIG_OBJDIR)/.installed
swig-reset:
	$(foreach f, .retrieved .configured .compiled .installed, \
		rm -f $(SWIG_OBJDIR)/$(f);)

swig-clean:
	-(cd $(SWIG_SRCDIR) && env MAKEFLAGS= $(MAKE) clean)

# fetch distfile for swig
$(DISTDIR)/$(SWIG_DIST):
ifneq ($(SWIG_VER),)
	cd $(DISTDIR) && $(FETCH_CMD) -O $(SWIG_DIST) $(SWIG_URL)
endif

# retrieve swig
$(SWIG_OBJDIR)/.retrieved: $(DISTDIR)/$(SWIG_DIST)
ifneq ($(SWIG_VER),)
	$(call do_check_sha256,$(SWIG_DIST))
	[ -d $(SWIG_OBJDIR) ] || mkdir -p $(SWIG_OBJDIR)
	tar -C $(SRCDIR) -zxf $(DISTDIR)/$(SWIG_DIST)
	touch $@
endif

# configure swig
$(SWIG_OBJDIR)/.configured: $(SWIG_OBJDIR)/.retrieved
ifneq ($(SWIG_VER),)
	cd $(SWIG_SRCDIR) \
		&& env CFLAGS="-g $(PROFILE_CFLAGS)" GREP="`which grep`"\
		CC=$(CC) CXX=$(CXX) \
		LDFLAGS="-L$(PREFIX)/iconv/lib" \
		PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) \
		$(SWIG_SRCDIR)/configure \
		--prefix=$(PREFIX)/swig
	touch $@
endif

# compile swig
$(SWIG_OBJDIR)/.compiled: $(SWIG_OBJDIR)/.configured
ifneq ($(SWIG_VER),)
	(cd $(SWIG_SRCDIR) && \
		env MAKEFLAGS= $(MAKE) CC=$(CC) CXX=$(CXX) \
			-j${MAKE_JOBS} PREFIX=$(PREFIX)/swig)
	touch $@
endif

# install swig
$(SWIG_OBJDIR)/.installed: $(SWIG_OBJDIR)/.compiled
ifneq ($(SWIG_VER),)
	(cd $(SWIG_SRCDIR) && \
		env MAKEFLAGS= $(MAKE) PREFIX=$(PREFIX)/swig install)
	touch $@
endif

#######################################################################
# svn
#######################################################################

.PHONY: svn-configure svn-compile svn-install svn-bindings-compile \
	svn-bindings-install svn-bindings-reset svn-clean

svn-install-all: svn-install svn-bindings-install

svn-retrieve:	$(SVN_OBJDIR)/.retrieved
svn-configure:	$(SVN_OBJDIR)/.configured
svn-compile:	$(SVN_OBJDIR)/.compiled
svn-bindings-compile:	$(SVN_OBJDIR)/.bindings-compiled
svn-install:	$(SVN_OBJDIR)/.installed
svn-bindings-install:	$(SVN_OBJDIR)/.bindings-installed
svn-bindings-reset:
	$(foreach f, .bindings-compiled .bindings-installed, \
		rm -f $(SVN_OBJDIR)/$(f);)
svn-reset: svn-bindings-reset
	$(foreach f, .retrieved .configured .compiled .installed \
	        .bindings-compiled .bindings-installed, \
		rm -f $(SVN_OBJDIR)/$(f);)

svn-clean:
	-(cd $(svn_builddir) && env MAKEFLAGS= make distclean)

# retrieve svn if not present yet
$(SVN_OBJDIR)/.retrieved:
	[ -d $(SVN_OBJDIR) ] || mkdir -p $(SVN_OBJDIR)
	if [ "$(TAG)" != "none" ]; then \
		branchdir="tags/$(TAG)"; \
		co="export"; \
	elif [ $(BRANCH) != trunk ]; then \
		branchdir="branches/$(BRANCH)"; \
		co="co"; \
	else \
		branchdir="$(BRANCH)"; \
		co="co"; \
	fi; \
	if [ ! -d $(SVN_WC) ] && [ ! -h $(SVN_WC) ]; then \
		svn $${co} $(SUBVERSION_REPOS_URL)/$${branchdir} \
			$(SVN_WC); \
	fi
	touch $@

ifeq ($(BRANCH_MAJOR),1.7)
BDB_FLAG=db.h:$(PREFIX)/bdb/include:$(PREFIX)/bdb/lib:db-$(BDB_MAJOR_VER)
SERF_LDFLAG=-Wl,-rpath,$(PREFIX)/serf/lib -Wl,-rpath,$(PREFIX)/bdb/lib
MOD_DAV_SVN=modules/svn-$(WC)/mod_dav_svn.so
MOD_AUTHZ_SVN=modules/svn-$(WC)/mod_authz_svn.so
MOD_DONTDOTHAT=modules/svn-$(WC)/mod_dontdothat.so
LIBMAGIC_FLAG=--with-libmagic=$(PREFIX)/libmagic
NEON_FLAG=--with-neon="$(PREFIX)/neon"
JAVAHL_CHECK_TARGET=check-javahl
SWIG_FLAG=--with-swig=$(PREFIX)/swig/bin/swig
PYTHON_BIN_PATH=$(PREFIX)/python2/bin
else ifeq ($(BRANCH_MAJOR),1.6)
BDB_FLAG=db.h:$(PREFIX)/bdb/include:$(PREFIX)/bdb/lib:db-$(BDB_MAJOR_VER)
SERF_LDFLAG=-Wl,-rpath,$(PREFIX)/serf/lib -Wl,-rpath,$(PREFIX)/bdb/lib
MOD_DAV_SVN=modules/svn-$(WC)/mod_dav_svn.so
MOD_AUTHZ_SVN=modules/svn-$(WC)/mod_authz_svn.so
MOD_DONTDOTHAT=modules/svn-$(WC)/mod_dontdothat.so
W_NO_SYSTEM_HEADERS=-Wno-system-headers
NEON_FLAG=--with-neon="$(PREFIX)/neon"
JAVAHL_CHECK_TARGET=check-javahl
SWIG_FLAG=--with-swig=$(PREFIX)/swig/bin/swig
PYTHON_BIN_PATH=$(PREFIX)/python2/bin
else ifeq ($(BRANCH_MAJOR), $(filter 1.8 1.9, $(BRANCH_MAJOR)))
BDB_FLAG=db.h:$(PREFIX)/bdb/include:$(PREFIX)/bdb/lib:db-$(BDB_MAJOR_VER)
# serf >= 1.3.0 is built with scons and no longer sets up rpath linker flags,
# so we have to do that ourselves :(
SERF_LDFLAG=-Wl,-rpath,$(PREFIX)/serf/lib -Wl,-rpath,$(PREFIX)/bdb/lib
MOD_DAV_SVN=modules/svn-$(WC)/mod_dav_svn.so
MOD_AUTHZ_SVN=modules/svn-$(WC)/mod_authz_svn.so
MOD_DONTDOTHAT=modules/svn-$(WC)/mod_dontdothat.so
LIBMAGIC_FLAG=--with-libmagic=$(PREFIX)/libmagic
JAVAHL_CHECK_TARGET=check-all-javahl
SWIG_FLAG=--with-swig=$(PREFIX)/swig/bin/swig
PYTHON_BIN_PATH=$(PREFIX)/python2/bin
else ifeq ($(BRANCH_MAJOR), $(filter 1.10 1.11, 1.12, 1.13 $(BRANCH_MAJOR)))
BDB_FLAG=db.h:$(PREFIX)/bdb/include:$(PREFIX)/bdb/lib:db-$(BDB_MAJOR_VER)
# serf >= 1.3.0 is built with scons and no longer sets up rpath linker flags,
# so we have to do that ourselves :(
SERF_LDFLAG=-Wl,-rpath,$(PREFIX)/serf/lib -Wl,-rpath,$(PREFIX)/bdb/lib
LZ4_LDFLAG=-Wl,-rpath,$(PREFIX)/lz4/lib
MOD_DAV_SVN=modules/svn-$(WC)/mod_dav_svn.so
MOD_AUTHZ_SVN=modules/svn-$(WC)/mod_authz_svn.so
MOD_DONTDOTHAT=modules/svn-$(WC)/mod_dontdothat.so
LIBMAGIC_FLAG=--with-libmagic=$(PREFIX)/libmagic
JAVAHL_CHECK_TARGET=check-all-javahl
LZ4_FLAG=--with-lz4=$(PREFIX)/lz4
UTF8PROC_FLAG=--with-utf8proc=internal
SWIG_FLAG=--with-swig=$(PREFIX)/swig/bin/swig
PYTHON_BIN_PATH=$(PREFIX)/python2/bin
else # 1.14, trunk
BDB_FLAG=db.h:$(PREFIX)/bdb/include:$(PREFIX)/bdb/lib:db-$(BDB_MAJOR_VER)
# serf >= 1.3.0 is built with scons and no longer sets up rpath linker flags,
# so we have to do that ourselves :(
SERF_LDFLAG=-Wl,-rpath,$(PREFIX)/serf/lib -Wl,-rpath,$(PREFIX)/bdb/lib
LZ4_LDFLAG=-Wl,-rpath,$(PREFIX)/lz4/lib
MOD_DAV_SVN=modules/svn-$(WC)/mod_dav_svn.so
MOD_AUTHZ_SVN=modules/svn-$(WC)/mod_authz_svn.so
MOD_DONTDOTHAT=modules/svn-$(WC)/mod_dontdothat.so
LIBMAGIC_FLAG=--with-libmagic=$(PREFIX)/libmagic
JAVAHL_CHECK_TARGET=check-all-javahl
LZ4_FLAG=--with-lz4=$(PREFIX)/lz4
UTF8PROC_FLAG=--with-utf8proc=internal
ifneq ($(SWIG_VER),)
SWIG_FLAG=--with-swig=$(PREFIX)/swig/bin/swig
endif
PYTHON_BIN_PATH=$(PREFIX)/python/bin
endif

ifeq ($(ENABLE_JAVA_BINDINGS),yes)
	JAVAHL_FLAG=--enable-javahl=yes --with-jdk --with-jikes=no \
		--with-junit=$(DISTDIR)/$(JUNIT_DIST)
else
	JAVAHL_FLAG=--with-jdk=no
endif

ifdef PROFILE
SVN_STATIC_FLAG=--enable-all-static
else
SVN_STATIC_FLAG=--disable-static
SVN_WITH_HTTPD=--with-apxs="$(PREFIX)/httpd/bin/apxs" \
	--with-apache-libexecdir="$(PREFIX)/httpd/modules/svn-$(WC)"
SVN_WITH_SASL=--with-sasl="$(PREFIX)/cyrus-sasl"
endif

# On OpenBSD, MExtUtils -e ldopts outputs -L/usr/local/lib, which can
# cause us to link Perl bindings against the wrong set of SVN libraries.
# As a workaround, we patch the configure script after it has been generated.
ifeq ($(UNAME),OpenBSD)
SWIG_PL_INCLUDES_HACK= sed -i 's^\($$PERL -MExtUtils::Embed -e ccopts\)^\1 | sed -e s@-I/usr/local/include@@^' $(svn_builddir)/configure
SWIG_PL_LINK_HACK= sed -i 's^\($$PERL -MExtUtils::Embed -e ldopts\)^\1 | sed -e s@-L/usr/local/lib@@^' $(svn_builddir)/configure
else
SWIG_PL_INCLUDES_HACK=true
SWIG_PL_LINK_HACK=true
endif

$(SVN_OBJDIR)/.configured: $(SVN_OBJDIR)/.retrieved $(DISTDIR)/$(JUNIT_DIST) \
	$(APR_OBJDIR)/.installed $(APR_UTIL_OBJDIR)/.installed \
	$(BDB_OBJDIR)/.installed $(SQLITE_OBJDIR)/.installed \
	$(HTTPD_OBJDIR)/.installed $(CYRUS_SASL_OBJDIR)/.installed \
	$(LIBMAGIC_OBJDIR)/.installed $(NEON_OBJDIR)/.installed \
	$(SERF_OBJDIR)/.installed $(SWIG_OBJDIR)/.installed \
	$(RUBY_OBJDIR)/.installed $(PYTHON_OBJDIR)/.installed
ifeq ($(BRANCH_MAJOR), $(filter 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13, $(BRANCH_MAJOR)))
ifneq ($(shell test $(SWIG_VER) \< 4.0 && echo 1), 1)
	@echo "SWIG 3 is required for Subversion < 1.14"
	@exit 1
endif
endif
	cd $(SVN_SRCDIR) && ./autogen.sh
	$(SWIG_PL_INCLUDES_HACK)
	$(SWIG_PL_LINK_HACK)
	cd $(svn_builddir) && \
		env LDFLAGS="-L$(PREFIX)/neon/lib -L$(PREFIX)/apr/lib -L$(PREFIX)/serf/lib $(SERF_LDFLAG) -lcrypto -lssl $(LZ4_LDFLAG) -L$(PREFIX)/gettext/lib -L$(PREFIX)/iconv/lib" \
			CC=$(CC) CXX=$(CXX) \
			CFLAGS="-I$(PREFIX)/gettext/include -DAPR_POOL_DEBUG" \
			CXXFLAGS="-I$(PREFIX)/gettext/include -DAPR_POOL_DEBUG" \
			LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$(PREFIX)/$(PYTHON)/lib:$$LD_LIBRARY_PATH" \
			PKG_CONFIG_PATH=$(PKG_CONFIG_PATH):$(PREFIX)/$(PYTHON)/lib/pkgconfig \
			PYTHON=$(PREFIX)/$(PYTHON)/bin/python \
			GREP="`which grep`" \
			PATH=$(PREFIX)/ruby/bin:$(PREFIX)/$(PYTHON)/bin:$(PREFIX)/gettext/bin:$$PATH \
			$(SVN_SRCDIR)/configure \
			--enable-maintainer-mode \
			--prefix="$(SVN_PREFIX)" \
			--with-apr="$(PREFIX)/apr" \
			--with-apr-util="$(PREFIX)/apr" \
			$(NEON_FLAG) \
			$(SVN_WITH_HTTPD) \
			$(SVN_WITH_SASL) \
			--with-serf="$(PREFIX)/serf" \
			--with-sqlite="$(PREFIX)/sqlite" \
			--with-zlib="/usr" \
			--without-gnome-keyring \
			--with-berkeley-db="$(BDB_FLAG)" \
			--with-ruby-sitedir="$(SVN_PREFIX)/lib/ruby/site_ruby" \
			--with-py3c="$(SRCDIR)/py3c-${PY3C_VER}" \
			--disable-mod-activation \
			--with-boost=no \
			$(JAVAHL_FLAG) \
			$(LIBMAGIC_FLAG) \
			$(LZ4_FLAG) \
			$(UTF8PROC_FLAG) \
			$(SWIG_FLAG) \
			--with-swig-python=$(PYTHON_BIN_PATH)/python \
			$(SVN_STATIC_FLAG) \
			$(DISABLE_NEON_VERSION_CHECK)
	touch $@

# compile svn
$(SVN_OBJDIR)/.compiled: $(SVN_OBJDIR)/.configured
	cd $(svn_builddir) \
		&& env MAKEFLAGS= make -j${MAKE_JOBS} EXTRA_CFLAGS="$(PROFILE_CFLAGS) $(W_NO_SYSTEM_HEADERS)"
	touch $@

# install svn
$(SVN_OBJDIR)/.installed: $(SVN_OBJDIR)/.compiled
	cd $(svn_builddir) \
		&& env MAKEFLAGS= make install install-tools
	touch $@

# SWIG 1.x and 2.x are not compatible. If SWIG 2.x is used to generated .swg
# files and 1.x is used to build the bindings, the Python bindings fail to
# load with errors such as "undefined symbol 'SWIG_Python_str_AsChar'".
# So clean any pre-generated .swg files to make sure everything is done
# by the same version of SWIG.
$(SVN_OBJDIR)/.pre-generated-swig-cleaned:
	-cd $(svn_builddir) \
		&& env MAKEFLAGS= make clean-swig
	touch $@


# On OpenBSD, Perl's LDDLFLAGS include -L/usr/local/lib, which can cause
# us to link Perl bindings against the wrong set of SVN libraries.
# We manually fix up the generated Makefile.PL to work around this issue.
ifeq ($(UNAME),OpenBSD)
MAKEFILE_PL_LDDLFLAGS_HACK= sed -i 's@^WriteMakefile@$$config{LDDLFLAGS} =~ s+-L/usr/local/lib++; WriteMakefile@' \
			$(SVN_SRCDIR)/subversion/bindings/swig/perl/native/Makefile.PL
else
MAKEFILE_PL_LDDLFLAGS_HACK=true
endif

$(SVN_OBJDIR)/.bindings-compiled: $(SVN_OBJDIR)/.installed $(SVN_OBJDIR)/.pre-generated-swig-cleaned
	cd $(svn_builddir) \
		&& env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
			env MAKEFLAGS= make -j${MAKE_JOBS} swig-py
	cd $(svn_builddir) && \
		env PATH=$(PREFIX)/ruby/bin:$$PATH \
		LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) env MAKEFLAGS= make -j${MAKE_JOBS} swig-rb
	if [ $(ENABLE_PERL_BINDINGS) = yes ]; then \
		cd $(svn_builddir) && make $(SVN_SRCDIR)/subversion/bindings/swig/perl/native/Makefile.PL; \
		$(MAKEFILE_PL_LDDLFLAGS_HACK); \
		cd $(svn_builddir) \
			&& env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
				env MAKEFLAGS= make -j${MAKE_JOBS} swig-pl; \
	fi
	if [ $(ENABLE_JAVA_BINDINGS) = yes ]; then \
		cd $(svn_builddir) \
			&& env MAKEFLAGS= make javahl; \
	fi
	touch $@

$(SVN_OBJDIR)/.bindings-installed: $(SVN_OBJDIR)/.bindings-compiled
	cd $(svn_builddir) \
	  && env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
		env MAKEFLAGS= make install-swig-py
	cd $(svn_builddir) && \
		env PATH=$(PREFIX)/ruby/bin:$$PATH \
		LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) MAKEFLAGS= make install-swig-rb
	if [ $(ENABLE_PERL_BINDINGS) = yes ]; then \
		cd $(svn_builddir) \
			&& env MAKEFLAGS= LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
				make install-swig-pl-lib; \
		cd subversion/bindings/swig/perl/native \
			&& perl Makefile.PL PREFIX="$(SVN_PREFIX)" \
			&& env MAKEFLAGS= LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
				make install; \
	fi
	if [ $(ENABLE_JAVA_BINDINGS) = yes ]; then \
		cd $(svn_builddir) \
			&& env MAKEFLAGS= make install-javahl; \
	fi
	touch $@

# run svn regression tests
HTTPD_CHECK_CONF=$(PREFIX)/httpd/conf/httpd-svn-check-$(WC).conf
HTTPD_PROXY_CONF=$(PREFIX)/httpd/conf/httpd-svn-proxy-$(WC).conf
HTTPD_CHECK_USERS=$(PREFIX)/httpd/conf/httpd-svn-check-users
HTTPD_CHECK_GROUPS=$(PREFIX)/httpd/conf/httpd-svn-check-groups
HTTPD_CHECK_PORT=8081
HTTPD_PROXY_PORT=8082
MOD_DONTDOTHAT_CONF=$(PREFIX)/httpd/conf/dontdothat

$(MOD_DONTDOTHAT_CONF):
	mkdir -p $(dir $@)
	echo > $@.tmp '[recursive-actions]'
	echo >>$@.tmp '/ = deny'
	mv -f $@.tmp $@

$(HTTPD_CHECK_GROUPS):
	mkdir -p $(dir $@)
	printf "random: jrandom\nconstant: jconstant\n" > $@

$(HTTPD_CHECK_CONF): $(MOD_DONTDOTHAT_CONF) $(HTTPD_CHECK_GROUPS)
	mkdir -p $(dir $@)
	env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) $(PREFIX)/httpd/bin/htpasswd -bc $(HTTPD_CHECK_USERS).tmp jrandom rayjandom
	env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) $(PREFIX)/httpd/bin/htpasswd -b $(HTTPD_CHECK_USERS).tmp jconstant rayjandom
	env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) $(PREFIX)/httpd/bin/htpasswd -b $(HTTPD_CHECK_USERS).tmp __dumpster__ __loadster__
	env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) $(PREFIX)/httpd/bin/htpasswd -b $(HTTPD_CHECK_USERS).tmp JRANDOM rayjandom
	env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) $(PREFIX)/httpd/bin/htpasswd -b $(HTTPD_CHECK_USERS).tmp JCONSTANT rayjandom
	mv -f $(HTTPD_CHECK_USERS).tmp $(HTTPD_CHECK_USERS)
	echo > $@.tmp '# httpd config for make check'
	echo >>$@.tmp 'ServerRoot "$(PREFIX)/httpd"'
	echo >>$@.tmp 'Listen localhost:$(HTTPD_CHECK_PORT)'
	echo >>$@.tmp 'LogLevel warn'
	echo >>$@.tmp 'LoadModule unixd_module modules/mod_unixd.so'
	echo >>$@.tmp 'LoadModule alias_module modules/mod_alias.so'
	echo >>$@.tmp 'LoadModule access_compat_module modules/mod_access_compat.so'
	echo >>$@.tmp 'LoadModule authn_core_module modules/mod_authn_core.so'
	echo >>$@.tmp 'LoadModule authn_file_module modules/mod_authn_file.so'
	echo >>$@.tmp 'LoadModule authz_core_module modules/mod_authz_core.so'
	echo >>$@.tmp 'LoadModule authz_user_module modules/mod_authz_user.so'
	echo >>$@.tmp 'LoadModule authz_groupfile_module modules/mod_authz_groupfile.so'
	echo >>$@.tmp 'LoadModule auth_basic_module modules/mod_auth_basic.so'
	echo >>$@.tmp 'LoadModule dav_module modules/mod_dav.so'
	echo >>$@.tmp 'LoadModule dav_svn_module $(MOD_DAV_SVN)'
	echo >>$@.tmp 'LoadModule authz_svn_module $(MOD_AUTHZ_SVN)'
	echo >>$@.tmp 'LoadModule dontdothat_module $(MOD_DONTDOTHAT)'
	echo >>$@.tmp 'DocumentRoot "$(PREFIX)/httpd/htdocs"'
	echo >>$@.tmp '# These two Locations are used for "make check"'
	echo >>$@.tmp '<Directory />'
	echo >>$@.tmp '    Options FollowSymLinks'
	echo >>$@.tmp '    AllowOverride None'
	echo >>$@.tmp '    Require all granted'
	echo >>$@.tmp '</Directory>'
	echo >>$@.tmp '<Location /svn-test-work/repositories>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNParentPath $(SVN_WC)/subversion/tests/cmdline/svn-test-work/repositories'
	echo >>$@.tmp '    AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
	echo >>$@.tmp '    AuthType Basic'
	echo >>$@.tmp '    AuthName "Subversion Repository"'
	echo >>$@.tmp '    AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    Require valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /svn-test-work/local_tmp/repos>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNPath $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp/repos'
	echo >>$@.tmp '    AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
	echo >>$@.tmp '    AuthType Basic'
	echo >>$@.tmp '    AuthName "Subversion Repository"'
	echo >>$@.tmp '    AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    Require valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /svn-test-work/local_tmp/trojan>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNPath $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp/trojan'
	echo >>$@.tmp '    AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
	echo >>$@.tmp '    AuthType Basic'
	echo >>$@.tmp '    AuthName "Subversion Repository"'
	echo >>$@.tmp '    AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    Require valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '# This Location lets you access repositories dropped in /tmp/'
	echo >>$@.tmp '<Location /svn>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNParentPath /tmp'
	echo >>$@.tmp '    Require all granted'
	echo >>$@.tmp '    #AuthType Basic'
	echo >>$@.tmp '    #AuthName "Subversion Repository"'
	echo >>$@.tmp '    #AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    #Require valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >> $@.tmp '   SVNAdvertiseV2Protocol off'
endif
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >> $@.tmp '   SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '# Location for tests using mod_dontdothat'
	echo >>$@.tmp '<Location /ddt-test-work/repositories>'
	echo >> $@.tmp 'DAV               svn'
	echo >> $@.tmp 'SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/repositories'
	echo >> $@.tmp 'AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
	echo >> $@.tmp 'AuthType          Basic'
	echo >> $@.tmp 'AuthName          "Subversion Repository"'
	echo >> $@.tmp 'AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
	echo >> $@.tmp 'AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >> $@.tmp 'Require           valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >> $@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >> $@.tmp 'DontDoThatConfigFile "$(MOD_DONTDOTHAT_CONF)"'
	echo >> $@.tmp '</Location>'

	echo >>$@.tmp '# Several locations for mod_authz_svn test follow'
	echo >>$@.tmp '<Location /authz-test-work/anon>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
       echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  <IfModule mod_authz_core.c>'
	echo >>$@.tmp '    Require all granted'
	echo >>$@.tmp '  </IfModule>'
	echo >>$@.tmp '  <IfModule !mod_authz_core.c>'
	echo >>$@.tmp '    Requite all granted'
	echo >>$@.tmp '  </IfModule>'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/in-repos-authz>'
	echo >>$@.tmp '  DAV svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/repositories'
	echo >>$@.tmp '  AuthzSVNReposRelativeAccessFile "^/authz"'
	echo >>$@.tmp '  AuthType Basic'
	echo >>$@.tmp '  AuthName "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  Require valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '  SVNAdvertiseV2Protocol off'
endif
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/mixed>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  Require           valid-user'
	echo >>$@.tmp '  Satisfy           Any'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/mixed-noauthwhenanon>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  Require           valid-user'
	echo >>$@.tmp '  AuthzSVNNoAuthWhenAnonymousAllowed On'
	echo >>$@.tmp '  SVNPathAuthz On'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/authn>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  Require           valid-user'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/authn-anonoff>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  Require           valid-user'
	echo >>$@.tmp '  AuthzSVNAnonymous Off'
	echo >>$@.tmp '  SVNPathAuthz On'
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/authn-lcuser>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  Require           valid-user'
	echo >>$@.tmp '  AuthzForceUsernameCase Lower'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<Location /authz-test-work/authn-group>'
	echo >>$@.tmp '  DAV               svn'
	echo >>$@.tmp '  SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '  AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '  SVNListParentPath On'
	echo >>$@.tmp '  AuthType          Basic'
	echo >>$@.tmp '  AuthName          "Subversion Repository"'
	echo >>$@.tmp '  AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '  AuthGroupFile     $(HTTPD_CHECK_GROUPS)'
	echo >>$@.tmp '  Require           group random'
	echo >>$@.tmp '  AuthzSVNAuthoritative Off'
	echo >>$@.tmp '  SVNPathAuthz On'
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '<IfModule mod_authz_core.c>'
	echo >>$@.tmp '  <Location /authz-test-work/sallrany>'
	echo >>$@.tmp '    DAV               svn'
	echo >>$@.tmp '    SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '    AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '    SVNListParentPath On'
	echo >>$@.tmp '    AuthType          Basic'
	echo >>$@.tmp '    AuthName          "Subversion Repository"'
	echo >>$@.tmp '    AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    AuthzSendForbiddenOnFailure On'
	echo >>$@.tmp '    Satisfy All'
	echo >>$@.tmp '    <RequireAny>'
	echo >>$@.tmp '      Require valid-user'
	echo >>$@.tmp '      Require expr req("ALLOW") == "1"'
	echo >>$@.tmp '    </RequireAny>'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '  </Location>'
	echo >>$@.tmp '  <Location /authz-test-work/sallrall>'
	echo >>$@.tmp '    DAV               svn'
	echo >>$@.tmp '    SVNParentPath     $(SVN_WC)/subversion/tests/cmdline/svn-test-work/local_tmp'
	echo >>$@.tmp '    AuthzSVNAccessFile $(SVN_WC)/subversion/tests/cmdline/svn-test-work/authz'
ifeq ($(USE_HTTPV1),yes)
	echo >>$@.tmp '    SVNAdvertiseV2Protocol off'
endif
	echo >>$@.tmp '    SVNListParentPath On'
	echo >>$@.tmp '    AuthType          Basic'
	echo >>$@.tmp '    AuthName          "Subversion Repository"'
	echo >>$@.tmp '    AuthUserFile      $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    AuthzSendForbiddenOnFailure On'
	echo >>$@.tmp '    Satisfy All'
	echo >>$@.tmp '    <RequireAll>'
	echo >>$@.tmp '      Require valid-user'
	echo >>$@.tmp '      Require expr req("ALLOW") == "1"'
	echo >>$@.tmp '    </RequireAll>'
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >>$@.tmp '    SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '  </Location>'
	echo >>$@.tmp '</IfModule>'
	echo >>$@.tmp 'RedirectMatch permanent ^/svn-test-work/repositories/REDIRECT-PERM-(.*)$$ /svn-test-work/repositories/$$1'
	echo >>$@.tmp 'RedirectMatch ^/svn-test-work/repositories/REDIRECT-TEMP-(.*)$$ /svn-test-work/repositories/$$1'
	echo >>$@.tmp 'IncludeOptional "conf/$(SVN_REL_WC)*-custom.conf"'
	echo >> $@.tmp '#SVNInMemoryCacheSize 0'
	echo >> $@.tmp '#SVNCacheTextDeltas Off'
	echo >> $@.tmp '#SVNCacheRevProps Off'
	mv -f $@.tmp $@

$(HTTPD_PROXY_CONF): $(HTTPD_CHECK_CONF)
	mkdir -p $(dir $@)
	echo > $@.tmp '# httpd config for a write-through proxy'
	echo >>$@.tmp 'ServerRoot "$(PREFIX)/httpd"'
	echo >>$@.tmp 'Listen localhost:$(HTTPD_PROXY_PORT)'
	echo >>$@.tmp 'LoadModule unixd_module modules/mod_unixd.so'
	echo >>$@.tmp 'LoadModule alias_module modules/mod_alias.so'
	echo >>$@.tmp 'LoadModule access_compat_module modules/mod_access_compat.so'
	echo >>$@.tmp 'LoadModule authn_core_module modules/mod_authn_core.so'
	echo >>$@.tmp 'LoadModule authn_file_module modules/mod_authn_file.so'
	echo >>$@.tmp 'LoadModule authz_core_module modules/mod_authz_core.so'
	echo >>$@.tmp 'LoadModule authz_user_module modules/mod_authz_user.so'
	echo >>$@.tmp 'LoadModule authz_groupfile_module modules/mod_authz_groupfile.so'
	echo >>$@.tmp 'LoadModule auth_basic_module modules/mod_auth_basic.so'
	echo >>$@.tmp 'LoadModule dav_module modules/mod_dav.so'
	echo >>$@.tmp 'LoadModule dav_svn_module $(MOD_DAV_SVN)'
	echo >>$@.tmp 'LoadModule authz_svn_module $(MOD_AUTHZ_SVN)'
	echo >>$@.tmp 'LoadModule dontdothat_module $(MOD_DONTDOTHAT)'
	echo >>$@.tmp 'DocumentRoot "$(PREFIX)/httpd/htdocs"'
	echo >>$@.tmp '# This Location lets you access repositories dropped in /tmp/svn-$(BRANCH)-proxy'
	echo >>$@.tmp '<Location /svn>'
	echo >>$@.tmp '    DAV svn'
	echo >>$@.tmp '    SVNParentPath /tmp/svn-$(BRANCH)-proxy'
	echo >>$@.tmp '    SVNMasterURI http://localhost:$(HTTPD_CHECK_PORT)/svn/'
	echo >>$@.tmp '    Requite all granted'
	echo >>$@.tmp '    #AuthType Basic'
	echo >>$@.tmp '    #AuthName "Subversion Repository"'
	echo >>$@.tmp '    #AuthUserFile $(HTTPD_CHECK_USERS)'
	echo >>$@.tmp '    #Require valid-user'
ifeq ($(USE_HTTPV1),yes)
	echo >> $@.tmp '   SVNAdvertiseV2Protocol off'
endif
ifeq ($(USE_AUTHZ_SHORT_CIRCUIT),yes)
	echo >> $@.tmp '   SVNPathAuthz short_circuit'
endif
	echo >>$@.tmp '</Location>'
	echo >>$@.tmp '# This Location allows repositories to be synced'
	echo >>$@.tmp '<Location /svn-proxy-sync>'
	echo >>$@.tmp 'DAV svn'
	echo >>$@.tmp 'SVNParentPath /tmp/svn-$(BRANCH)-proxy'
	echo >>$@.tmp 'Require all granted'
	echo >>$@.tmp '</Location>'
	mv -f $@.tmp $@

.PHONY: libpath
libpath:
	@echo export LD_LIBRARY_PATH="$(LD_LIBRARY_PATH):$$LD_LIBRARY_PATH" \
		"PYTHONPATH=$(SVN_PREFIX)/lib/svn-python"
#
# OpenBSD requires an LD_PRELOAD hack to dlopen() libraries linked to
# libpthread (e.g. libsvn_auth_gnome_keyring.so) into executables that
# aren't linked to libpthread.
ifeq ($(UNAME),OpenBSD)
LIB_PTHREAD_HACK=LD_PRELOAD=libpthread.so
endif

.PHONY: start-svnserve stop-svnserve start-httpd stop-httpd

HTTPD_CMD = env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) $(LIB_PTHREAD_HACK) \
		$(PREFIX)/httpd/bin/apachectl
HTTPD_LOG_ROTATE = mv $(PREFIX)/httpd/logs/error_log $(PREFIX)/httpd/logs/error_log.old
HTTPD_START_CMD = $(HTTPD_CMD) -f $(HTTPD_CHECK_CONF) -k start
HTTPD_START_CMD_PROXY = $(HTTPD_CMD) -f $(HTTPD_PROXY_CONF)
HTTPD_START_CMD_DEBUG = $(HTTPD_START_CMD) -X
HTTPD_STOP_CMD = $(HTTPD_CMD) -f $(HTTPD_CHECK_CONF) -k stop; sleep 3

SVNSERVE_START_CMD = (test -e $(PWD)/svnserve-*.pid && \
			ls $(PWD)/svnserve-*.pid | while read pidfile; do \
				kill `cat "$$pidfile"`; sleep 3; \
				rm -f $$pidfile; \
			done); \
			$(SVN_PREFIX)/bin/svnserve \
			--listen-host 127.0.0.1 \
			--pid-file $(PWD)/svnserve-$(WC).pid \
			-d -r $(svn_builddir)/subversion/tests/cmdline
SVNSERVE_STOP_CMD = kill `cat $(PWD)/svnserve-$(WC).pid`; sleep 3; \
			rm -f $(PWD)/svnserve-$(WC).pid

start-httpd: $(HTTPD_CHECK_CONF)
	-$(HTTPD_LOG_ROTATE)
	$(HTTPD_START_CMD)
	@echo "To run tests over http, run:"
	@echo "    make check BASE_URL=http://localhost:$(HTTPD_CHECK_PORT)"
	@echo "The URL http://localhost:$(HTTPD_CHECK_PORT)/svn/"
	@echo "lets you access repositories dropped into /tmp"

start-httpd-debug: $(HTTPD_CHECK_CONF)
	$(HTTPD_START_CMD_DEBUG) &
	@echo "To run tests over http, run:"
	@echo "    make check BASE_URL=http://localhost:$(HTTPD_CHECK_PORT)"
	@echo "The URL http://localhost:$(HTTPD_CHECK_PORT)/svn/"
	@echo "lets you access repositories dropped into /tmp"
	@echo "Trying to attach gdb to httpd..."
	@sleep 1
	gdb $(PREFIX)/httpd/bin/httpd `cat $(PREFIX)/httpd/logs/httpd.pid`

start-httpd-proxy: $(HTTPD_PROXY_CONF)
	$(HTTPD_START_CMD_PROXY)
	@echo "The URL http://localhost:$(HTTPD_PROXY_PORT)/svn/"
	@echo "lets you access repositories dropped into /tmp/svn-$(BRANCH)-proxy"

stop-httpd: $(HTTPD_CHECK_CONF)
	$(HTTPD_STOP_CMD)

stop-httpd-proxy: $(HTTPD_PROXY_CONF)
	pkill -f '$(PREFIX)/httpd/bin/httpd -f $(HTTPD_PROXY_CONF)'

start-svnserve: $(SVN_OBJDIR)/.compiled
	$(SVNSERVE_START_CMD)

stop-svnserve:
	$(SVNSERVE_STOP_CMD)

define do_check
-cd $(svn_builddir) && for fs in $(SVN_CHECK_FS_BACKENDS); do \
    echo "Begin test: $(subst svn-check-,,$@) x $$fs"; \
    test -d "$(RAMDISK)/tmp" && export TMPDIR="$(RAMDISK)/tmp"; \
    env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) $(LIB_PTHREAD_HACK) \
	  PATH=$(PYTHON_BIN_PATH):$(PREFIX)/ruby/bin:$$PATH \
	  MAKEFLAGS= make check PARALLEL=$(PARALLEL) CLEANUP=$(CLEANUP) \
	  EXCLUSIVE_WC_LOCKS=$(EXCLUSIVE_WC_LOCKS) \
	  THREADED=$(THREADED) \
	  SVN_BIN_DIR=$(SVN_PREFIX)/bin \
	  MEMCACHED_SERVER=$(MEMCACHED_SERVER) $1 FS_TYPE=$$fs; \
    for log in tests.log fails.log; do \
        test -f $$log && mv -f $$log $$log.$@-$$fs; \
    done; \
done
endef

TEST_WORK=$(svn_builddir)/subversion/tests/cmdline/svn-test-work
svn-check-prepare-ramdisk:
	-rm -rf "$(TEST_WORK)"; \
	if [ -d "$(RAMDISK)" ] && \
		touch "$(RAMDISK)/$(SVN_REL_WC).writetest" && \
		mkdir -p "$(RAMDISK)/$(SVN_REL_WC)"; then \
			rm -f "$(RAMDISK)/$(SVN_REL_WC).writetest"; \
			ln -s "$(RAMDISK)/$(SVN_REL_WC)" "$(TEST_WORK)"; \
			mkdir -p "$(RAMDISK)/tmp"; \
	fi

ifndef NEON_FLAG
svn-check-neon:
	@echo Neon is not supported by this build of Subversion, skipping tests
	@true
else
svn-check-neon: $(HTTPD_CHECK_CONF) svn-check-prepare-ramdisk
	$(HTTPD_START_CMD)
	$(call do_check,BASE_URL=http://localhost:$(HTTPD_CHECK_PORT) HTTP_LIBRARY=neon)
	$(HTTPD_STOP_CMD)
endif

svn-check-serf: $(HTTPD_CHECK_CONF) svn-check-prepare-ramdisk
	$(HTTPD_START_CMD)
	$(call do_check,BASE_URL=http://localhost:$(HTTPD_CHECK_PORT) HTTP_LIBRARY=serf)
	$(HTTPD_STOP_CMD)

svn-check-local: svn-check-prepare-ramdisk
	$(call do_check)

svn-check-svn: svn-check-prepare-ramdisk
	$(SVNSERVE_START_CMD)
	$(call do_check,BASE_URL=svn://127.0.0.1)
	$(SVNSERVE_STOP_CMD)

.PHONY: svn-check-swig-pl svn-check-swig-py svn-check-swig-rb svn-check-javahl
svn-check-bindings: svn-check-swig-pl svn-check-swig-py svn-check-swig-rb \
	svn-check-javahl

RUBYLIB=$(SVN_PREFIX)/lib/ruby/site_ruby$(shell grep \
	^svn_cv_ruby_sitedir_archsuffix $(svn_builddir)/config.log | \
	cut -d'=' -f2):$(SVN_PREFIX)/lib/ruby/site_ruby$(shell \
	grep ^svn_cv_ruby_sitedir_libsuffix $(svn_builddir)/config.log | \
	cut -d'=' -f2)
svn-check-swig-pl:
	-if [ $(ENABLE_PERL_BINDINGS) = yes ]; then \
		(cd $(svn_builddir) && \
			env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
			$(LIB_PTHREAD_HACK) \
			env MAKEFLAGS= make check-swig-pl 2>&1) | \
				tee $(svn_builddir)/tests.log.bindings.pl; \
	fi

svn-check-swig-py:
	-(cd $(svn_builddir) && \
		env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
		PATH=$(PYTHON_BIN_PATH):$$PATH \
		MAKEFLAGS= make check-swig-py 2>&1) | \
			tee $(svn_builddir)/tests.log.bindings.py

# We add the svn prefix to PATH here because the ruby tests
# attempt to start an svnserve binary found in PATH.
svn-check-swig-rb:
	(cd $(svn_builddir) && \
		env RUBYLIB=$(RUBYLIB) \
		LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
		PATH=$(SVN_PREFIX)/bin:$$PATH \
		$(LIB_PTHREAD_HACK) \
			env MAKEFLAGS= make check-swig-rb 2>&1) | \
			tee $(svn_builddir)/tests.log.bindings.rb

svn-check-javahl:
	-if [ $(ENABLE_JAVA_BINDINGS) = yes ]; then \
		ln -s libsvnjavahl-1.so.0.0 $(PREFIX)/svn-$(WC)/lib/libsvnjavahl-1.so; \
		(cd $(svn_builddir) && \
			env LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) \
			MAKEFLAGS= make $(JAVAHL_CHECK_TARGET) 2>&1) | \
				tee $(svn_builddir)/tests.log.bindings.javahl; \
	fi

.PHONY: svn-check-neon svn-check-serf svn-check-local svn-check-svn
.PHONY: svn-check-prepare-ramdisk svn-check-bindings svn-check
svn-check: svn-check-prepare-ramdisk svn-check-local svn-check-svn \
	svn-check-neon svn-check-serf svn-check-bindings

.PHONY: sign-email
ifdef NEON_FLAG
NEON_STR=ra_neon | 
NEON_VER_LINE=@echo "neon:       $(NEON_VER)"
endif
sign-email:
	@echo "Summary: +1 to release"
	@echo ""
	@echo "Tested: [bdb | fsfs] x [ra_local | ra_svn | $(NEON_STR)ra_serf]"
	@echo "        swig bindings"
ifeq ($(ENABLE_JAVA_BINDINGS),yes)
	@echo "        javahl bindings"
endif
	@echo ""
	@echo "Test results: All passed."
	@echo ""
	@echo "Platform: `uname -r -s -m`"
	@echo ""
	@echo "Dependencies:"
	@echo "bdb:        $(BDB_VER)"
ifeq ($(USE_APR_ICONV),yes)
	@echo "apr-iconv:  $(APR_ICONV_VER)"
else
	@echo "GNU-iconv:  $(GNU_ICONV_VER)"
endif
	@echo "apr:        $(APR_VER)"
	@echo "apr-util:   $(APR_UTIL_VER)"
	@echo "OpenLDAP:   $(OPENLDAP_VER)"
	@echo "httpd:      $(HTTPD_VER)"
	$(NEON_VER_LINE)
	@echo "serf:       $(SERF_VER)"
	@echo "cyrus-sasl: $(CYRUS_SASL_VER)"
	@echo "sqlite:     $(SQLITE_VER)"
ifdef LZ4_FLAG
	@echo "lz4:        $(LZ4_VER)"
endif
	@echo "libssl:     `openssl version`"
ifdef SWIG_FLAG
	@echo "swig:       `$(PREFIX)/swig/bin/swig -version | grep Version | cut -d' ' -f3`"
else
	@echo "swig:       `swig -version | grep Version | cut -d' ' -f3`"
endif
	@echo "python:     $(PYTHON_VER)"
	@echo "perl:       `eval \`perl -V:version\`; echo $$version`"
	@echo "ruby:       $(RUBY_VER)"
ifeq ($(ENABLE_JAVA_BINDINGS),yes)
	@echo "java:       `java -version 2>&1 | grep version | cut -d' ' -f3  | sed -e 's/\"//g'`"
endif
	@echo ""
	@echo "Signatures:"
	@echo
	@echo "subversion-$(TAG).tar.gz"
	@echo "`cat subversion-$(TAG).tar.gz.asc`" 
	@echo
	@echo "subversion-$(TAG).tar.bz2"
	@echo "`cat subversion-$(TAG).tar.bz2.asc`" 
