Automatically detect and build libsodium from source on systems with old or missing libsodium's.

Fix caching on Travis.
master
Didactic Drunk 2019-06-26 17:22:33 -07:00
parent 1f6446b6f5
commit a5d1d14297
7 changed files with 124 additions and 35 deletions

View File

@ -5,18 +5,14 @@ os:
# - osx # - osx
install: install:
- ./travis-install-lib-sodium.sh - COX_BUILD_DEBUG=1 LIBSODIUM_BUILD_DIR=$HOME/libsodium ./build/libsodium_install.sh
before_script: before_script:
- export C_INCLUDE_PATH=$LD_LIBRARY_PATH:`pwd`/sodium/include - export LIBSODIUM_BUILD_DIR=$HOME/libsodium
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`/sodium/lib after_failure:
- env | egrep PKG - "[ -f libsodium_install.out ] && cat libsodium_install.out"
- export PKG_CONFIG_PATH=`pwd`/sodium/lib/pkgconfig
- env | egrep LD_
- env | egrep PKG
- pwd
cache: cache:
directories: directories:
- sodium - "$HOME/libsodium"
#addons: #addons:
# apt: # apt:

34
build/env.sh Normal file
View File

@ -0,0 +1,34 @@
#!/bin/sh
# Overridable.
[ -z "$LIBSODIUM_BUILD_DIR" ] && LIBSODIUM_BUILD_DIR=`pwd`/build
# Upgraded from time to time.
export MIN_LIBSODIUM_VERSION=1.0.18
export LIBSODIUM_SHA256=6f504490b342a4f8a4c4a02fc9b866cbef8622d5df4e5452b46be121e46636c1
[ ! -z "$COX_BUILD_DEBUG" ] && export COX_BUILD_VERBOSE=1
function version { echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; }
if `pkg-config libsodium --exists`; then
PKG_VER=`pkg-config libsodium --modversion`
if [ $(version "$PKG_VER") -ge $(version "$MIN_LIBSODIUM_VERSION") ]; then
[ ! -z "$COX_BUILD_VERBOSE" ] && echo "Using packaged libsodium."
else
[ ! -z "$COX_BUILD_VERBOSE" ] echo "System packaged libsodium is too old."
export LIBSODIUM_INSTALL=1
fi
else
[ ! -z "$COX_BUILD_VERBOSE" ] && echo "Missing libsodium system package."
export LIBSODIUM_INSTALL=1
fi
if [ "$LIBSODIUM_INSTALL" = "1" ]; then
export LIBSODIUM_INSTALL_PATH="$LIBSODIUM_BUILD_DIR"/libsodium
export PKG_CONFIG_PATH="$LIBSODIUM_INSTALL_PATH"/lib/pkgconfig
fi

68
build/libsodium_install.sh Executable file
View File

@ -0,0 +1,68 @@
#!/bin/bash
# The purpose of this file is to install libsodium when:
# 1. No packaged version is found.
# 2. The packaged version is older than the version specified in this repository.
# pkg-config is used to find the library and determine it's version.
# You may replace the libsodium library with your own installed version by setting PKG_CONFIG_PATH before compiling.
set -e
# Always use bash. `dash` doesn't work properly with . includes. I'm not sure why.
. ./build/env.sh
#export LIBSODIUM_INSTALL=1
if [ "$LIBSODIUM_INSTALL" != "1" ]; then
[ ! -z "$COX_BUILD_VERBOSE" ] echo "Skipping libsodium build."
exit
fi
mkdir -p "$LIBSODIUM_BUILD_DIR"
cd "$LIBSODIUM_BUILD_DIR"
if [ ! -f "$LIBSODIUM_INSTALL_PATH/include/sodium.h" ]; then
[ ! -z "$COX_BUILD_DEBUG" ] && set -x
DIRNAME=libsodium-"$MIN_LIBSODIUM_VERSION"
TGZ_FILENAME="$DIRNAME".tar.gz
if [ ! -f "$TGZ_FILENAME" ]; then
wget https://download.libsodium.org/libsodium/releases/"$TGZ_FILENAME"
# wget https://download.libsodium.org/libsodium/releases/"$TGZ_FILENAME".minisign
fi
SHA=`openssl sha256 -hex < "$TGZ_FILENAME" | sed 's/^.* //'`
if [ "$SHA" != "$LIBSODIUM_SHA256" ]; then
echo "SHA256 sum doesn't match."
echo "$SHA" != "$LIBSODIUM_SHA256"
exit 1
fi
if [ ! -d "$DIRNAME" ]; then
tar xfz "$TGZ_FILENAME"
fi
cd "$DIRNAME"
if [ ! -f ".configure.done" ]; then
./configure --prefix="$LIBSODIUM_INSTALL_PATH" --disable-shared
touch .configure.done
fi
if [ ! -f ".make.done" ]; then
make
touch .make.done
fi
if [ ! -f ".make.install.done" ]; then
make install
touch .make.install.done
fi
[ ! -z "$COX_BUILD_VERBOSE" ] && echo "Finished building libsodium."
else
# find "$LIBSODIUM_INSTALL_PATH"
[ ! -z "$COX_BUILD_VERBOSE" ] && echo "Using already built libsodium."
fi
exit 0

12
build/pkg-libs.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/bash
set -e
# Shard directory passed as first argument when called from lib_sodium.cr
[ ! -z "$1" ] && cd "$1"
./build/libsodium_install.sh > libsodium_install.out 2>&1
. ./build/env.sh
pkg-config libsodium --libs

View File

@ -4,6 +4,10 @@ version: 0.1.0
authors: authors:
- Andrew Hamon <andrew@hamon.cc> - Andrew Hamon <andrew@hamon.cc>
development_dependencies:
ghshard:
github: bcardiff/ghshard
crystal: 0.24.1 crystal: 0.24.1
license: MIT license: MIT

View File

@ -1,5 +1,5 @@
module Cox module Cox
@[Link("sodium")] @[Link(ldflags: "`#{__DIR__}/../../build/pkg-libs.sh #{__DIR__}/../..`")]
lib LibSodium lib LibSodium
fun sodium_init() : LibC::Int fun sodium_init() : LibC::Int

View File

@ -1,25 +0,0 @@
#!/bin/sh
# The purpose of this file is to install libsodium in
# the Travis CI environment. We recommend using a
# package manager.
set -e
LIBSODIUM_INSTALL_PATH=`pwd`/sodium
if [ ! -f "sodium/include/sodium.h" ]; then
set -x
mkdir -p "$LIBSODIUM_INSTALL_PATH"
find "$LIBSODIUM_INSTALL_PATH"
wget https://download.libsodium.org/libsodium/releases/LATEST.tar.gz
tar xfz LATEST.tar.gz
cd libsodium-stable
./configure --prefix="$LIBSODIUM_INSTALL_PATH"
make
make install
else
echo "using cached libsodium build"
find "$LIBSODIUM_INSTALL_PATH"
fi