# -*- sh -*-
#
# bob script for spigot.

module spigot

# Make up a version number.
set Version $(!builddate).$(vcsid)
set PythonVersion $(!builddate) # Python PEP 440 versioning hates git hashes

# Substitute it in to the various configuration and setup scripts, and
# into version.h.
in spigot do sed -i 's/NOVERSION/$(PythonVersion)/' setup.py python-doc/conf.py
in spigot do echo '/* Generated by automated build script */' > version.h
in spigot do echo '$#define VER "version $(Version)"' >> version.h

# use perl to avoid inconsistent behaviour of echo '\v'
in spigot do perl -e 'print "\n\\versionid spigot version $$ARGV[0]\n"' $(Version) >> manual.but

# Build the tarball.
in . do ln -s spigot spigot-$(Version)
in . do tar chzvf spigot-$(Version).tar.gz spigot-$(Version)

# By default, we run 'make test' with both bigint implementations, to
# ensure that we don't accidentally ship a build that doesn't at least
# basically work.
#
# By defining SKIP_TESTS=yes on the bob command line, you can skip
# this step. (I only expect to use this option for manual test builds
# if I'm doing a lot of them and don't want to keep waiting for two
# test runs.) But we still have to run one of the 'configure' stages,
# so that the following 'make dist' will work.
in . do mkdir build-gmp build-internal
in build-internal do cmake ../spigot -DUSE_GMP=OFF
in build-internal do make -j$(nproc) VERBOSE=1
ifneq "$(SKIP_TESTS)" "yes" in build-internal do ../spigot/test.sh ./spigot
in build-gmp do cmake ../spigot -DUSE_GMP=ON
in build-gmp do make -j$(nproc) VERBOSE=1
ifneq "$(SKIP_TESTS)" "yes" in build-gmp do ../spigot/test.sh ./spigot

# Build the Python module's source distribution.
in spigot do ./setup.py sdist

# Unpack it again, so we can test it from its clean unpack directory.
# It will have generated a file with a version number in the name,
# which we don't really want to hard-code in here.
in spigot/dist do tar xzvf spigot-*.tar.gz
in spigot/dist do ln -s $$(dirname spigot-*/setup.py) spigot

# Test the Python module, and build the Python docs.
delegate -
  in spigot/dist/spigot do python3 setup.py build
  in spigot/dist/spigot do python3 ../../python-module/testenv --build-base=python-build python-module/testsuite
  in spigot/dist/spigot do python3 ../../python-module/testenv --build-base=python-build --exec make -C python-doc doctest coverage html SPHINXBUILD=/usr/share/sphinx/scripts/python3/sphinx-build SPHINXOPTS=-W
  in spigot/dist/spigot do python3 ../../python-module/coverage-check python-doc/_build/coverage/undoc.pickle
  return spigot/dist/spigot/python-doc/_build/html/*
  return spigot/dist/spigot/python-doc/_build/html/_static/*
enddelegate

# Build the Windows binary, using clang-cl.
#
# If GMP_INSTALL_DIR was defined on the bob command line, expect it to
# contain include and lib subdirs from a 'make install' of GMP, and
# use that to link the Windows binary against GMP.
in . do mkdir build-windows
set gmpopts
ifneq "$(GMP_INSTALL_DIR)" "" set gmpopts -DGMP_DIR="$(GMP_INSTALL_DIR)"
in build-windows do cmake ../spigot -DCMAKE_TOOLCHAIN_FILE=$(cmake_toolchain_clangcl64) -DCMAKE_BUILD_TYPE=Release -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded -DCMAKE_C_FLAGS_RELEASE="/MT /O2" $(gmpopts)
in build-windows do make -j$(nproc) VERBOSE=1
# Code-sign the Windows binary, if the local bob config provides a
# script to do so. We assume here that the script accepts an -i option
# to provide a 'more info' URL, and that it signs the file in place.
ifneq "$(cross_winsigncode)" "" in build-windows do $(cross_winsigncode) -i https://www.chiark.greenend.org.uk/~sgtatham/spigot/ spigot.exe

# Deliver output.
deliver spigot-*.tar.gz $@
deliver build-internal/spigot.html $@
deliver build-windows/spigot.exe $@
deliver spigot/dist/spigot-py-*.tar.gz $@
deliver spigot/dist/spigot/python-doc/_build/html/* spigot-py-doc/$@
deliver spigot/dist/spigot/python-doc/_build/html/_static/* spigot-py-doc/_static/$@
