diff --git a/.travis.yml b/.travis.yml index 379acf6..6be444c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,18 +5,14 @@ os: # - osx install: - - ./travis-install-lib-sodium.sh + - COX_BUILD_DEBUG=1 LIBSODIUM_BUILD_DIR=$HOME/libsodium ./build/libsodium_install.sh before_script: - - export C_INCLUDE_PATH=$LD_LIBRARY_PATH:`pwd`/sodium/include - - export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`/sodium/lib - - env | egrep PKG - - export PKG_CONFIG_PATH=`pwd`/sodium/lib/pkgconfig - - env | egrep LD_ - - env | egrep PKG - - pwd + - export LIBSODIUM_BUILD_DIR=$HOME/libsodium +after_failure: + - "[ -f libsodium_install.out ] && cat libsodium_install.out" cache: directories: - - sodium + - "$HOME/libsodium" #addons: # apt: diff --git a/build/env.sh b/build/env.sh new file mode 100644 index 0000000..65349ff --- /dev/null +++ b/build/env.sh @@ -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 diff --git a/build/libsodium_install.sh b/build/libsodium_install.sh new file mode 100755 index 0000000..32f0421 --- /dev/null +++ b/build/libsodium_install.sh @@ -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 diff --git a/build/pkg-libs.sh b/build/pkg-libs.sh new file mode 100755 index 0000000..6fd484c --- /dev/null +++ b/build/pkg-libs.sh @@ -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 diff --git a/shard.yml b/shard.yml index 565710d..7d0da18 100644 --- a/shard.yml +++ b/shard.yml @@ -4,6 +4,10 @@ version: 0.1.0 authors: - Andrew Hamon +development_dependencies: + ghshard: + github: bcardiff/ghshard + crystal: 0.24.1 license: MIT diff --git a/src/cox/lib_sodium.cr b/src/cox/lib_sodium.cr index 11d7312..97c3256 100644 --- a/src/cox/lib_sodium.cr +++ b/src/cox/lib_sodium.cr @@ -1,5 +1,5 @@ module Cox - @[Link("sodium")] + @[Link(ldflags: "`#{__DIR__}/../../build/pkg-libs.sh #{__DIR__}/../..`")] lib LibSodium fun sodium_init() : LibC::Int diff --git a/travis-install-lib-sodium.sh b/travis-install-lib-sodium.sh deleted file mode 100755 index 146b780..0000000 --- a/travis-install-lib-sodium.sh +++ /dev/null @@ -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