Fix OS-X build with libsodium

Add a new 'build-libsodium.sh' script and instructions in
doc/Development.md.  Update the configure.in script to check for
libsodium headers and library, and print helpful messages if not
present.
This commit is contained in:
Andrew Bettison 2016-09-06 17:27:02 +09:30
parent 43bd0bd33f
commit c45e92b731
5 changed files with 318 additions and 32 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@
/win32/Release
*.user
*.ncb
/libsodium
/objs
/objs_lib
/objs_servald

View File

@ -9,7 +9,7 @@ These instructions will build [Serval DNA][] successfully for the following plat
* Debian Linux, ix86 and x86\_64, kernels 2.6.x and 3.x, using [gcc 4.4][] to
[gcc 4.8][]
* Mac OS X x86\_64, releases 10.7 “Lion” to 10.11 “El Capitan”, using [gcc
* Mac OS-X x86\_64, releases 10.7 “Lion” to 10.11 “El Capitan”, using [gcc
4.2][] available in [Xcode][] versions 3.2 to 7.2, and GNU tools available
from [homebrew][]
* Oracle SunOs 5.10 (Solaris), Sparc, using [gcc 4.4][] and GNU tools
@ -43,16 +43,21 @@ environment.
Mandatory dependencies:
* standard C library `libc` and standard headers
* standard math library `libm` and headers `<math.h>` `<float.h>`
* network services library `libnsl` and headers
* dynamic link library `libdl` and header `<dlfcn.h>`
* Native Posix Threads Library `libpthread` and header `<pthread.h>`
* elliptic curve encryption library `libsodium` and header <sodium.h>, 1.0.2
or greater
* on Solaris, the realtime library `librt` (for the `nanosleep()` function)
* Autoconf 2.67-2.69 (2.70 may work but has not been tested)
* Automake 1.15
* standard C library **libc** and standard headers
* standard math library **libm** and headers `<math.h>` `<float.h>`
* network services library **libnsl** and headers
* dynamic link library **libdl** and header `<dlfcn.h>`
* Native Posix Threads Library **libpthread** and header `<pthread.h>`
* elliptic curve encryption library **libsodium** and header `<sodium.h>`,
version 1.0.2 or greater
* on Solaris, the realtime library **librt** (for the `nanosleep()` function)
* **autoconf** 2.67-2.69 (2.70 may work but has not been tested)
* **automake** 1.15
The **libsodium** development files are available on Debian/Ubuntu systems in
the `libsodium-dev` package. On other systems, like Mac OS-X, it must be
compiled from source. The [Notes for Developers](./doc/Development.md) give
more details.
Optional:
@ -62,11 +67,11 @@ Optional:
Test dependencies:
* bash 3.2.48 or later
* GNU grep, sed and awk (on OSX and Solaris, as ggrep, gsed and gawk)
* GNU grep, sed and awk (on Mac OS-X and Solaris, as ggrep, gsed and gawk)
* jq 1.3 or later
* curl
The GNU grep, sed and awk programs can be installed on OSX using the
The GNU grep, sed and awk programs can be installed on Mac OS-X using the
[homebrew][] package manager. The [Notes for Developers](./doc/Development.md)
give more details.
@ -125,8 +130,11 @@ On Solaris, the system `make` command may not be GNU Make, and the system
$ gmake
$
In the event of a build failure, first consult the [Notes for
Developers](./doc/Development.md), then [contact the Serval Project][].
In the event of a build failure:
* ensure that all the [dependencies](#dependencies) are present
* consult the [Notes for Developers](./doc/Development.md)
* as a last resort, [contact the Serval Project][]
Built artifacts
---------------

219
build-libsodium.sh Executable file
View File

@ -0,0 +1,219 @@
#!/bin/bash
#
# Copyright (C) 2016 Serval Project, Inc.
#
# This script downloads the libsodium source code from GitHub, then compiles it
# and sets up the Serval DNA source code to build against the compiled
# libsodium.
#
# This script is useful for building Serval DNA on platforms such as Max OS X
# that do not provide the libsodium development package. Debian and Ubuntu
# provide the libsodium-dev package which can be installed instead of using this
# script.
#
# By default, the script creates a 'libsodium' directory in the same directory
# that contains the 'serval-dna' directory, ie, '../libsodium' relative to the
# script. This location can be overridden by giving an alternative directory
# path on the command line, which the script will create if it does not exist.
LIBSODIUM_URL_PATH="jedisct1/libsodium.git"
LIBSODIUM_GIT_URL_HTTPS="https://github.com/$LIBSODIUM_URL_PATH"
LIBSODIUM_GIT_URL_SSH="git@github.com:$LIBSODIUM_URL_PATH"
LIBSODIUM_GIT_URL="$LIBSODIUM_GIT_URL_HTTPS"
# Exit on error
set -e
usage() {
echo "Usage: ${0##*/} [--directory PATH] [--ssh] [--make-arg ARG]"
echo "Options: --directory PATH download and build in PATH [$LIBSODIUM_DIR]"
echo " --no-update do not update if already downloaded"
echo " --no-clean do not clean if already built"
echo " --ssh download from GitHub using SSH instead of HTTPS"
echo " --dist-build DIST build using libsodium's 'dist-build/DIST.sh' script"
echo " --make-arg ARG pass ARG to the 'make' command"
}
usage_error() {
echo "${0##*/}: $*" >&2
usage >&2
exit 1
}
fatal() {
echo "${0##*/}: $*" >&2
exit 1
}
# Work out the absolute path of the directory that contains this script.
case "$0" in
/*) SCRIPT_DIR="${0%/*}";;
./*) SCRIPT_DIR="$PWD";;
*/*) SCRIPT_DIR="$PWD/${0%/*}";;
*) SCRIPT_DIR="$PWD";;
esac
# The directory in which to install the built libsodium.
LIBSODIUM_DIR_NAME="libsodium"
LIBSODIUM_INSTALL_DIR="$SCRIPT_DIR/$LIBSODIUM_DIR_NAME"
# The default directory in which to download and build libsodium.
LIBSODIUM_DIR="$SCRIPT_DIR/../libsodium"
# Parse the command-line, preserving all the arguments for later reference.
PRESERVED_ARGS=()
OPT_UPDATE=true
OPT_CLEAN=true
DIST=
MAKE_ARGS=()
while [ $# -ne 0 ]; do
opt="$1"
shift
case "$opt" in
-h | --help | '-?' )
usage
exit 0
;;
--directory=*)
PRESERVED_ARGS+=("$opt")
LIBSODIUM_DIR="${opt#*=}"
;;
--directory)
[ $# -ge 1 ] || usage_error "missing argument after $opt"
PRESERVED_ARGS+=("$opt" "$1")
LIBSODIUM_DIR="$1"
shift
;;
--ssh)
PRESERVED_ARGS+=("$opt")
LIBSODIUM_GIT_URL="$LIBSODIUM_GIT_URL_SSH"
;;
--no-update)
OPT_UPDATE=false
;;
--no-clean)
OPT_CLEAN=false
;;
--dist-build=*)
PRESERVED_ARGS+=("$opt")
DIST="${opt#*=}"
;;
--dist-build)
[ $# -ge 1 ] || usage_error "missing argument after $opt"
PRESERVED_ARGS+=("$opt" "$1")
DIST="$1"
shift
;;
--make-arg=*)
PRESERVED_ARGS+=("$opt")
MAKE_ARGS+=("${opt#*=}")
;;
--make-arg)
[ $# -ge 1 ] || usage_error "missing argument after $opt"
PRESERVED_ARGS+=("$opt" "$1")
MAKE_ARGS+=("$1")
shift
;;
-*)
usage_error "unrecognised option: $1"
;;
*)
usage_error "spurious argument: $1"
;;
esac
done
if [ ! -d "$LIBSODIUM_DIR" ]; then
echo "Create $LIBSODIUM_DIR"
mkdir -p "$LIBSODIUM_DIR"
fi
is_libsodium_downloaded() {
[ -r "$1/src/libsodium/include/sodium.h" -a \
-r "$1/libsodium-uninstalled.pc.in" \
]
}
if ! is_libsodium_downloaded "$LIBSODIUM_DIR"; then
echo "Download libsodium from $LIBSODIUM_GIT_URL..."
git clone --branch stable "$LIBSODIUM_GIT_URL" "$LIBSODIUM_DIR"
cd "$LIBSODIUM_DIR" >/dev/null
is_libsodium_downloaded . || fatal "Download did not produce expected source files"
else
echo "Libsodium appears to already be downloaded"
cd "$LIBSODIUM_DIR" >/dev/null
if $OPT_UPDATE; then
echo "Update from" $(git remote get-url origin)
git pull --ff-only
fi
fi
if [ -d "$LIBSODIUM_INSTALL_DIR" ]; then
echo "Delete the previous installation"
rm -rf "$LIBSODIUM_INSTALL_DIR"
fi
if $OPT_CLEAN && [ -r Makefile ]; then
echo "Clean the previous build"
make distclean >/dev/null
fi
if [ -z "$DIST" ]; then
echo "Native build..."
[ -r Makefile ] || ./configure --prefix="$LIBSODIUM_INSTALL_DIR"
make -j3 "${MAKE_ARGS[@]}" check
make -j3 "${MAKE_ARGS[@]}" install
elif [ -x "dist-build/$DIST.sh" ]; then
installed="libsodium-$DIST"
case "$DIST" in
arm) installed="libsodium-armv6";;
esac
[ -e "$installed" ] && fatal "previous build remains in $installed"
echo "Build using 'dist-build/$DIST.sh'..."
"dist-build/$DIST.sh"
[ -d "$installed" ] || fatal "build did not produce $installed"
echo "Copy built installation into $LIBSODIUM_INSTALL_DIR"
cp -R -p "$installed" "$LIBSODIUM_INSTALL_DIR"
else
fatal "script "dist-build/$DIST_BUILD.sh" does not exist."
fi
# Create a shell script that will set up the environment to use the built and
# installed libsodium.
cat >"$LIBSODIUM_INSTALL_DIR/settings.sh" <<EOF
# libsodium development and run-time environment settings
#
# Source this file using the Bash "source" or Shell "." command to set up the
# environment so that compilation and execution will use the libsodium
# installed in this directory.
#
# NOTE: This file was generated by running the ${0##*/} script.
# If you edit this file, any changes will be overwritten the next time that
# script is run.
# Compiler settings:
export CPPFLAGS="\$CPPFLAGS -isystem $LIBSODIUM_INSTALL_DIR/include"
export LIBRARY_PATH="$LIBSODIUM_INSTALL_DIR/lib"
# Run-time settings:
export LD_LIBRARY_PATH="\${LD_LIBRARY_PATH:+\$LD_LIBRARY_PATH:}$LIBSODIUM_INSTALL_DIR/lib"
EOF
# Create a README.txt file.
cat >"$LIBSODIUM_INSTALL_DIR/README.txt" <<EOF
This directory is a local installation of the libsodium cryptographic library
downloaded from $LIBSODIUM_GIT_URL
It was downloaded and built locally using the command:
${0##*/} ${PRESERVED_ARGS[*]}
EOF
echo
echo "The libsodium run-time and development files have been installed in:"
echo "$LIBSODIUM_INSTALL_DIR"
echo
echo "To use this installation of libsodium, set up the environment using the"
echo "shell's \"dot\" command to source its settings.sh script, for example:"
echo
echo " . $LIBSODIUM_DIR_NAME/settings.sh ; ./configure"
echo

View File

@ -2,7 +2,7 @@ dnl Process this file with autoconf to produce a configure script.
AC_INIT(servald, 0.9)
AC_CONFIG_MACRO_DIR([m4])
CPPFLAGS=-D_GNU_SOURCE
CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"
dnl Set $host_os, which is needed by javac detection.
AC_CANONICAL_SYSTEM
@ -71,7 +71,7 @@ if test -n "$JAVAC"; then
pushdef([AC_MSG_ERROR], defn([AC_MSG_WARN]))
AC_JNI_INCLUDE_DIR
for JNI_INCLUDE_DIR in $JNI_INCLUDE_DIRS; do
CPPFLAGS="$CPPFLAGS -I$JNI_INCLUDE_DIR"
CPPFLAGS="$CPPFLAGS -I$JNI_INCLUDE_DIR"
done
popdef([AC_MSG_ERROR])
fi
@ -82,8 +82,6 @@ AC_CHECK_LIB(c,srandomdev)
AC_CHECK_LIB(m,sqrtf,[LDFLAGS="$LDFLAGS -lm"])
AC_CHECK_LIB(nsl,callrpc,[LDFLAGS="$LDFLAGS -lnsl"])
AC_CHECK_LIB(dl,dlopen,[LDFLAGS="$LDFLAGS -ldl"])
AC_CHECK_LIB(sodium,sodium_init,[LDFLAGS="$LDFLAGS -lsodium"],
[AC_MSG_ERROR([Missing libsodium])])
dnl Solaris hides nanosleep here
AC_CHECK_LIB(rt,nanosleep)
@ -138,6 +136,24 @@ AC_CHECK_HEADERS(
#endif
])
dnl libsodium
m4_define([LIBSODIUM_MESSAGE], [
*** If your platform provides a libsodium development package, then
*** install it and try again, eg, on Debian/Ubuntu:
*** sudo apt-get install libsodium-dev
*** If using a libsodium built locally from source, ensure that the
*** $1 environment variable is set, eg, with the command:
*** . libsodium/settings.sh
*** See INSTALL.md and doc/Development.md for more information.
])
AC_CHECK_HEADERS(
sodium.h,, [AC_MSG_ERROR([missing sodium.h LIBSODIUM_MESSAGE([C_INCLUDE_PATH])])]
)
AC_CHECK_LIB(sodium, sodium_init,, [AC_MSG_ERROR([missing libsodium LIBSODIUM_MESSAGE([LIBRARY_PATH])])])
dnl Lazy way of checking for Linux
AS_IF([test "x$ac_cv_header_linux_if_h" = xyes], [AC_DEFINE([USE_ABSTRACT_NAMESPACE])])

View File

@ -62,12 +62,12 @@ suppress most of these messages from the output of [aclocal][] and
Debian/Ubuntu
-------------
To install all mandatory and testing dependencies before building on [Debian][]
and [Ubuntu][] systems:
A single [apt-get][] command will install all mandatory and testing
dependencies before building on [Debian][] and [Ubuntu][] systems:
$ sudo apt-get --yes install libc6-dev libsodium-dev jq curl
Reading package lists... Done
Building dependency tree
Building dependency tree
Reading state information... Done
libc6-dev is already the newest version (2.23-5).
The following NEW packages will be installed:
@ -78,7 +78,7 @@ and [Ubuntu][] systems:
Get:1 http://ftp.us.debian.org/debian testing/main amd64 curl amd64 7.50.1-1 [218 kB]
Get:2 http://ftp.us.debian.org/debian testing/main amd64 jq amd64 1.5+dfsg-1 [156 kB]
Get:3 http://ftp.us.debian.org/debian testing/main amd64 libsodium-dev amd64 1.0.11-1 [170 kB]
Fetched 544 kB in 1s (304 kB/s)
Fetched 544 kB in 1s (304 kB/s)
Selecting previously unselected package curl.
(Reading database ... 205089 files and directories currently installed.)
Preparing to unpack .../0-curl_7.50.1-1_amd64.deb ...
@ -95,13 +95,51 @@ and [Ubuntu][] systems:
Processing triggers for man-db (2.7.5-1) ...
$
Apple OSX
---------
Apple Mac OS X
--------------
The [OSX grep(1)][] , [OSX sed(1)][] and [OSX awk(1)][] tools provided by Apple
OSX are the BSD variants. The test scripts require the GNU variants with the
names *ggrep*, *gsed* and *gawk*, which can be installed on OSX using the
[homebrew][] package manager:
### libsodium
Although the [libsodium][] package is available for Mac OS X using the
[homebrew][] package manager, that package only provides the run-time library,
not the development files; for example, it does not provide the `#include
<sodium.h>` header.
Before building Serval DNA on Mac OS X, the libsodium development files can be
locally installed by downloading the [libsodium source code][] and building it.
The [build-libsodium.sh](../build-libsodium.sh) script will do this:
$ ./build-libsodium.sh
...
The libsodium run-time and development files have been installed in:
/absolute/path/name/serval-dna/libsodium
To use this installation of libsodium, set up the environment using the
shell's "dot" command to source its settings.sh script, for example:
. libsodium/settings.sh ; ./configure
$
In the event of failure, check that:
* the [github.com][libsodium source code] web site can be reached
* there is at least 60 MB of available disk space
* there is no other libsodium development package already installed
For more information, refer to the [libsodium installation documentation][]
and the script's help message:
$ ./build-libsodium.sh --help
...
$
### Test utilities
The [OS X grep(1)][] , [OS X sed(1)][] and [OS X awk(1)][] tools provided by
Apple Mac OS X are the BSD variants. The test scripts require the GNU variants
with the names *ggrep*, *gsed* and *gawk*, which can be installed on Mac OS X
using the [homebrew][] package manager:
$ brew install grep
==> Installing grep from homebrew/dupes
@ -174,8 +212,12 @@ Available under the [Creative Commons Attribution 4.0 International licence][CC
[autoreconf]: https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf.html#autoreconf-Invocation
[Debian]: http://www.debian.org/
[Ubuntu]: http://www.ubuntu.com/
[OSX grep(1)]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/grep.1.html
[OSX sed(1)]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/sed.1.html
[OSX awk(1)]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/awk.1.html
[libsodium]: https://download.libsodium.org/doc/
[libsodium source code]: https://github.com/jedisct1/libsodium
[libsodium installation documentation]: https://download.libsodium.org/libsodium/content/installation/
[apt-get]: https://www.debian.org/doc/manuals/apt-guide/ch2.en.html
[OS X grep(1)]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/grep.1.html
[OS X sed(1)]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/sed.1.html
[OS X awk(1)]: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/awk.1.html
[homebrew]: http://brew.sh/
[Bourne shell]: http://en.wikipedia.org/wiki/Bourne_shell