#!/bin/sh
#
# Copyright (c) 2013 John Cunningham Bowler
#
# Last changed in libpng 1.6.0 [February 14, 2013]
#
# This code is released under the libpng license.
# For conditions of distribution and use, see the disclaimer
# and license in png.h
#
# Generate a set of PNG test images.  The images are generated in a
# sub-directory called 'tests' by default, however a command line argument will
# change that name.  The generation requires a built version of makepng in the
# current directory.
#
usage(){
   exec >&2
   echo "$0 [<directory>]"
   echo '  Generate a set of PNG test files in "directory" ("tests" by default)'
   exit 1
}

mp="$PWD/makepng"
test -x "$mp" || {
   exec >&2
   echo "$0: the 'makepng' program must exist"
   echo "  in the directory within which this program:"
   echo "    $mp"
   echo "  is executed"
   usage
}

# Just one argument: the directory
testdir="tests"
test $# -gt 1 && {
   testdir="$1"
   shift
}
test $# -eq 0 || usage

# Take care not to clobber something
if test -e "$testdir"
then
   test -d "$testdir" || usage
else
   # mkdir -p isn't portable, so do the following
   mkdir "$testdir" 2>/dev/null || mkdir -p "$testdir" || usage
fi

# This fails in a very satisfactory way if it's not accessible
cd "$testdir"
:>"test$$.png" || {
   exec >&2
   echo "$testdir: directory not writable"
   usage
}
rm "test$$.png" || {
   exec >&2
   echo "$testdir: you have create but not write privileges here."
   echo "  This is unexpected.  You have a spurion; "'"'"test$$.png"'"'"."
   echo "  You need to remove this yourself.  Try a different directory."
   exit 1
}

# Now call makepng ($mp) to create every file we can think of with a
# reasonable name
doit(){
   for gamma in "" --sRGB --linear --1.8
   do
      case "$gamma" in
         "")
            gname=;;
         --sRGB)
            gname="-srgb";;
         --linear)
            gname="-lin";;
         --1.8)
            gname="-18";;
         *)
            gname="-$gamma";;
      esac
      "$mp" $gamma "$1" "$2" "test-$1-$2$gname.png"
   done
}
#
for ct in gray palette
do
   for bd in 1 2 4 8
   do
      doit "$ct" "$bd"
   done
done
#
doit "gray" "16"
#
for ct in gray-alpha rgb rgb-alpha
do
   for bd in 8 16
   do
      doit "$ct" "$bd"
   done
done


#!/bin/sh
# -- grayscale
./png2pnm -noraw ../pngsuite/basn0g01.png basn0g01.pgm
./png2pnm -noraw ../pngsuite/basn0g02.png basn0g02.pgm
./png2pnm -noraw ../pngsuite/basn0g04.png basn0g04.pgm
./png2pnm -noraw ../pngsuite/basn0g08.png basn0g08.pgm
./png2pnm -noraw ../pngsuite/basn0g16.png basn0g16.pgm
# -- full-color
./png2pnm -noraw ../pngsuite/basn2c08.png basn2c08.ppm
./png2pnm -noraw ../pngsuite/basn2c16.png basn2c16.ppm
# -- palletted
./png2pnm -noraw ../pngsuite/basn3p01.png basn3p01.ppm
./png2pnm -noraw ../pngsuite/basn3p02.png basn3p02.ppm
./png2pnm -noraw ../pngsuite/basn3p04.png basn3p04.ppm
./png2pnm -noraw ../pngsuite/basn3p08.png basn3p08.ppm
# -- gray with alpha-channel
./png2pnm -noraw ../pngsuite/basn4a08.png basn4a08.pgm
./png2pnm -noraw ../pngsuite/basn4a16.png basn4a16.pgm
# -- color with alpha-channel
./png2pnm -noraw -alpha basn6a08.pgm ../pngsuite/basn6a08.png basn6a08.ppm
./png2pnm -noraw -alpha basn6a16.pgm ../pngsuite/basn6a16.png basn6a16.ppm
# -- grayscale
./png2pnm -raw ../pngsuite/basn0g01.png rawn0g01.pgm
./png2pnm -raw ../pngsuite/basn0g02.png rawn0g02.pgm
./png2pnm -raw ../pngsuite/basn0g04.png rawn0g04.pgm
./png2pnm -raw ../pngsuite/basn0g08.png rawn0g08.pgm
./png2pnm -raw ../pngsuite/basn0g16.png rawn0g16.pgm
# -- full-color
./png2pnm -raw ../pngsuite/basn2c08.png rawn2c08.ppm
./png2pnm -raw ../pngsuite/basn2c16.png rawn2c16.ppm
# -- palletted
./png2pnm -raw ../pngsuite/basn3p01.png rawn3p01.ppm
./png2pnm -raw ../pngsuite/basn3p02.png rawn3p02.ppm
./png2pnm -raw ../pngsuite/basn3p04.png rawn3p04.ppm
./png2pnm -raw ../pngsuite/basn3p08.png rawn3p08.ppm
# -- gray with alpha-channel
./png2pnm -raw ../pngsuite/basn4a08.png rawn4a08.pgm
./png2pnm -raw ../pngsuite/basn4a16.png rawn4a16.pgm
# -- color with alpha-channel
./png2pnm -noraw -alpha rawn6a08.pgm ../pngsuite/basn6a08.png rawn6a08.ppm
./png2pnm -noraw -alpha rawn6a16.pgm ../pngsuite/basn6a16.png rawn6a16.ppm


#!/bin/sh
make -f makefile.std
sh png2pnm.sh
sh pnm2png.sh


#!/bin/sh
# -- grayscale
./pnm2png basn0g01.pgm basn0g01.png
./pnm2png basn0g02.pgm basn0g02.png
./pnm2png basn0g04.pgm basn0g04.png
./pnm2png basn0g08.pgm basn0g08.png
./pnm2png basn0g16.pgm basn0g16.png
# -- full-color
./pnm2png basn2c08.ppm basn2c08.png
./pnm2png basn2c16.ppm basn2c16.png
# -- palletted
./pnm2png basn3p01.ppm basn3p01.png
./pnm2png basn3p02.ppm basn3p02.png
./pnm2png basn3p04.ppm basn3p04.png
./pnm2png basn3p08.ppm basn3p08.png
# -- gray with alpha-channel
./pnm2png -alpha basn6a08.pgm basn4a08.pgm basn4a08.png
./pnm2png -alpha basn6a16.pgm basn4a16.pgm basn4a16.png
# -- color with alpha-channel
./pnm2png -alpha basn6a08.pgm basn6a08.ppm basn6a08.png
./pnm2png -alpha basn6a16.pgm basn6a16.ppm basn6a16.png
# -- grayscale
./pnm2png rawn0g01.pgm rawn0g01.png
./pnm2png rawn0g02.pgm rawn0g02.png
./pnm2png rawn0g04.pgm rawn0g04.png
./pnm2png rawn0g08.pgm rawn0g08.png
./pnm2png rawn0g16.pgm rawn0g16.png
# -- full-color
./pnm2png rawn2c08.ppm rawn2c08.png
./pnm2png rawn2c16.ppm rawn2c16.png
# -- palletted
./pnm2png rawn3p01.ppm rawn3p01.png
./pnm2png rawn3p02.ppm rawn3p02.png
./pnm2png rawn3p04.ppm rawn3p04.png
./pnm2png rawn3p08.ppm rawn3p08.png
# -- gray with alpha-channel
./pnm2png -alpha rawn6a08.pgm rawn4a08.pgm rawn4a08.png
./pnm2png -alpha rawn6a16.pgm rawn4a16.pgm rawn4a16.png
# -- color with alpha-channel
./pnm2png -alpha rawn6a08.pgm rawn6a08.ppm rawn6a08.png
./pnm2png -alpha rawn6a16.pgm rawn6a16.ppm rawn6a16.png


#!/bin/sh
#
# intgamma.sh
#
# Last changed in libpng 1.6.0 [February 14, 2013]
#
# COPYRIGHT: Written by John Cunningham Bowler, 2013.
# To the extent possible under law, the author has waived all copyright and
# related or neighboring rights to this work.  This work is published from:
# United States.
#
# Shell script to generate png.c 8-bit and 16-bit log tables (see the code in
# png.c for details).
#
# This script uses the "bc" arbitrary precision calculator to calculate 32-bit
# fixed point values of logarithms appropriate to finding the log of an 8-bit
# (0..255) value and a similar table for the exponent calculation.
#
# "bc" must be on the path when the script is executed, and the math library
# (-lm) must be available
#
# function to print out a list of numbers as integers; the function truncates
# the integers which must be one-per-line
function print(){
   awk 'BEGIN{
      str = ""
   }
   {
      sub("\\.[0-9]*$", "")
      if ($0 == "")
         $0 = "0"

      if (str == "")
         t = "   " $0 "U"
      else
         t = str ", " $0 "U"

      if (length(t) >= 80) {
         print str ","
         str = "   " $0 "U"
      } else
         str = t
   }
   END{
      print str
   }'
}
#
# The logarithm table.
cat <<END
/* 8-bit log table: png_8bit_l2[128]
 * This is a table of -log(value/255)/log(2) for 'value' in the range 128 to
 * 255, so it's the base 2 logarithm of a normalized 8-bit floating point
 * mantissa.  The numbers are 32-bit fractions.
 */
static const png_uint_32
png_8bit_l2[128] =
{
END
#
bc -lqws <<END | print
f=65536*65536/l(2)
for (i=128;i<256;++i) { .5 - l(i/255)*f; }
END
echo '};'
echo
#
# The exponent table.
cat <<END
/* The 'exp()' case must invert the above, taking a 20-bit fixed point
 * logarithmic value and returning a 16 or 8-bit number as appropriate.  In
 * each case only the low 16 bits are relevant - the fraction - since the
 * integer bits (the top 4) simply determine a shift.
 *
 * The worst case is the 16-bit distinction between 65535 and 65534; this
 * requires perhaps spurious accuracy in the decoding of the logarithm to
 * distinguish log2(65535/65534.5) - 10^-5 or 17 bits.  There is little chance
 * of getting this accuracy in practice.
 *
 * To deal with this the following exp() function works out the exponent of the
 * frational part of the logarithm by using an accurate 32-bit value from the
 * top four fractional bits then multiplying in the remaining bits.
 */
static const png_uint_32
png_32bit_exp[16] =
{
END
#
bc -lqws <<END | print
f=l(2)/16
for (i=0;i<16;++i) {
   x = .5 + e(-i*f)*2^32;
   if (x >= 2^32) x = 2^32-1;
   x;
}
END
echo '};'
echo
#
# And the table of adjustment values.
cat <<END
/* Adjustment table; provided to explain the numbers in the code below. */
#if 0
END
bc -lqws <<END | awk '{ printf "%5d %s\n", 12-NR, $0 }'
for (i=11;i>=0;--i){
   (1 - e(-(2^i)/65536*l(2))) * 2^(32-i)
}
END
echo '#endif'


#!/bin/bash
BINARY=$1
INSTALLPATH="$2"
WORKDIR="$3"

if [ -z "$DESTDIR" ]; then
	OUTDIR="$INSTALLPATH"
else
	if [ -n $(echo ${INSTALLPATH} | grep "^[A-Z]:") ]; then
		INSTALLPATH="${INSTALLPATH:3}"
	fi
	OUTDIR="$WORKDIR/$DESTDIR/$INSTALLPATH"
fi

IFS=$'\n'
if [ -n $(which ntldd 2>&1 | grep /ntldd) ]; then
	DLLS=$(ntldd -R "$BINARY" | grep -i mingw | cut -d">" -f2 | sed -e 's/(0x[0-9a-f]\+)//' -e 's/^ \+//' -e 's/ \+$//' -e 's,\\,/,g')
elif [ -n $(which gdb 2>&1 | grep /gdb) ]; then
	DLLS=$(gdb "$BINARY" --command=$(dirname $0)/dlls.gdb | grep -i mingw | cut -d" " -f7- | sed -e 's/^ \+//' -e 's/ \+$//' -e 's,\\,/,g')
else
	echo "Please install gdb or ntldd for deploying DLLs"
fi
cp -vu $DLLS "$OUTDIR"
if [ -n $(which windeployqt 2>&1 | grep /windeployqt) ]; then
	windeployqt --no-opengl-sw --no-svg --release --dir "$OUTDIR" "$BINARY"
fi


#!/bin/sh
# Clean up the caveats that CPack leaves behind
BINARY=mgba

rmdep () {
    local DEP=$1
    echo Removing dependency $DEP
    sed -i~ "s/[^, ]*$DEP[^,]*//g" deb-temp/DEBIAN/control
}

adddep() {
    local DEP=$1
    echo Adding dependency $DEP
    sed -i~ "s/^Depends: /&$DEP,/" deb-temp/DEBIAN/control
}

while [ $# -gt 0 ]; do
    DEB=$1
    dpkg-deb -R $DEB deb-temp
    mv $DEB $DEB~
    sed -i~ s/mgba-// deb-temp/DEBIAN/control
    PKG=`head -n1 deb-temp/DEBIAN/control | cut -f2 -d ' '`
    echo Found pacakge $PKG

    case $PKG in
    lib$BINARY)
        rmdep sdl
        rmdep qt
    ;;
    $BINARY-qt)
        rmdep libav
        rmdep libedit
        rmdep libpng
        rmdep libzip
        rmdep libmagickwand
        rmdep libswscale
        rmdep zlib
        adddep lib$BINARY
    ;;
    $BINARY-sdl)
        rmdep libav
        rmdep libedit
        rmdep libpng
        rmdep qt
        rmdep libzip
        rmdep libmagickwand
        rmdep libswscale
        rmdep zlib
        adddep lib$BINARY
    ;;
    *)
        echo Unknown package!
    esac

    sed -i~ "s/,,*/,/g" deb-temp/DEBIAN/control
    sed -i~ "s/,$//g" deb-temp/DEBIAN/control
    sed -i~ "/^[^:]*: $/d" deb-temp/DEBIAN/control
    rm deb-temp/DEBIAN/control~
    chmod 644 deb-temp/DEBIAN/md5sums
    chown -R root:root deb-temp
    dpkg-deb -b deb-temp $DEB
    rm -rf deb-temp
    shift
done


#!/bin/sh
rm -rf genplus.opk
mksquashfs gen_gcw0 opk-data/* genplus.opk -all-root -noappend -no-exports -no-xattrs


g++-4.5 -std=gnu++0x -I../../../.. -o generate generate.cpp


#!/bin/sh

cd "$(dirname $0)/../"
./docker/linux/build_scripts/prefetch.sh OPENSSL CURL
docker build . --tag openai/retro-build:linux-v2 --file docker/linux/Dockerfile
docker push openai/retro-build:linux-v2
docker build . --tag openai/retro-build:win64-v2 --file docker/windows/Dockerfile
docker push openai/retro-build:win64-v2


#!/bin/bash
# Top-level build script called from Dockerfile

# Stop at any error, show all commands
set -ex

# Set build environment variables
MY_DIR=$(dirname "${BASH_SOURCE[0]}")
. $MY_DIR/build_env.sh

# Dependencies for compiling Python that we want to remove from
# the final image after compiling Python
# GPG installed to verify signatures on Python source tarballs.
PYTHON_COMPILE_DEPS="zlib-devel bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel gpg libffi-devel"

# Libraries that are allowed as part of the manylinux1 profile
MANYLINUX1_DEPS="glibc-devel libstdc++-devel glib2-devel libX11-devel libXext-devel libXrender-devel  mesa-libGL-devel libICE-devel libSM-devel ncurses-devel"

# Centos 5 is EOL and is no longer available from the usual mirrors, so switch
# to http://vault.centos.org
# From: https://github.com/rust-lang/rust/pull/41045
# The location for version 5 was also removed, so now only the specific release
# (5.11) can be referenced.
sed -i 's/enabled=1/enabled=0/' /etc/yum/pluginconf.d/fastestmirror.conf
sed -i 's/mirrorlist/#mirrorlist/' /etc/yum.repos.d/*.repo
sed -i 's/#\(baseurl.*\)mirror.centos.org\/centos\/$releasever/\1vault.centos.org\/5.11/' /etc/yum.repos.d/*.repo

# Get build utilities
source $MY_DIR/build_utils.sh

# See https://unix.stackexchange.com/questions/41784/can-yum-express-a-preference-for-x86-64-over-i386-packages
echo "multilib_policy=best" >> /etc/yum.conf

# https://hub.docker.com/_/centos/
# "Additionally, images with minor version tags that correspond to install
# media are also offered. These images DO NOT recieve updates as they are
# intended to match installation iso contents. If you choose to use these
# images it is highly recommended that you include RUN yum -y update && yum
# clean all in your Dockerfile, or otherwise address any potential security
# concerns."
# Decided not to clean at this point: https://github.com/pypa/manylinux/pull/129
yum -y update

# EPEL support
yum -y install wget
wget -q http://archives.fedoraproject.org/pub/archive/epel/5/x86_64/epel-release-5-4.noarch.rpm
check_sha256sum epel-release-5-4.noarch.rpm $EPEL_RPM_HASH

# Dev toolset (for LLVM and other projects requiring C++11 support)
wget -q http://people.centos.org/tru/devtools-2/devtools-2.repo
check_sha256sum devtools-2.repo $DEVTOOLS_HASH
mv devtools-2.repo /etc/yum.repos.d/devtools-2.repo
rpm -Uvh --replacepkgs epel-release-5*.rpm
rm -f epel-release-5*.rpm

# from now on, we shall only use curl to retrieve files
yum -y erase wget

# Development tools and libraries
yum -y install \
    automake \
    bison \
    bzip2 \
    cmake28 \
    devtoolset-2-binutils \
    devtoolset-2-gcc \
    devtoolset-2-gcc-c++ \
    devtoolset-2-gcc-gfortran \
    diffutils \
    expat-devel \
    gettext \
    kernel-devel-`uname -r` \
    file \
    make \
    patch \
    unzip \
    which \
    yasm \
    ${PYTHON_COMPILE_DEPS}

# Build an OpenSSL for both curl and the Pythons. We'll delete this at the end.
build_openssl $OPENSSL_ROOT $OPENSSL_HASH

# Install curl so we can have TLS 1.2 in this ancient container.
build_curl $CURL_ROOT $CURL_HASH
hash -r
curl --version
curl-config --features

# Install a git we link against OpenSSL so that we can use TLS 1.2
build_git $GIT_ROOT $GIT_HASH
git version

# Install newest autoconf
build_autoconf $AUTOCONF_ROOT $AUTOCONF_HASH
autoconf --version

# Install newest automake
build_automake $AUTOMAKE_ROOT $AUTOMAKE_HASH
automake --version

# Install newest libtool
build_libtool $LIBTOOL_ROOT $LIBTOOL_HASH
libtool --version

# Install a more recent SQLite3
curl -fsSLO $SQLITE_AUTOCONF_DOWNLOAD_URL/$SQLITE_AUTOCONF_VERSION.tar.gz
check_sha256sum $SQLITE_AUTOCONF_VERSION.tar.gz $SQLITE_AUTOCONF_HASH
tar xfz $SQLITE_AUTOCONF_VERSION.tar.gz
cd $SQLITE_AUTOCONF_VERSION
do_standard_install
cd ..
rm -rf $SQLITE_AUTOCONF_VERSION*

# Compile the latest Python releases.
# (In order to have a proper SSL module, Python is compiled
# against a recent openssl [see env vars above], which is linked
# statically.
mkdir -p /opt/python
build_cpythons $CPYTHON_VERSIONS

PY36_BIN=/opt/python/cp36-cp36m/bin

# Install certifi and auditwheel
$PY36_BIN/pip install --require-hashes -r $MY_DIR/py36-requirements.txt

# Our openssl doesn't know how to find the system CA trust store
#   (https://github.com/pypa/manylinux/issues/53)
# And it's not clear how up-to-date that is anyway
# So let's just use the same one pip and everyone uses
ln -s $($PY36_BIN/python -c 'import certifi; print(certifi.where())') \
      /opt/_internal/certs.pem
# If you modify this line you also have to modify the versions in the
# Dockerfiles:
export SSL_CERT_FILE=/opt/_internal/certs.pem

# Now we can delete our built OpenSSL headers/static libs since we've linked everything we need
rm -rf /usr/local/ssl

# Install patchelf (latest with unreleased bug fixes)
curl -fsSL -o patchelf.tar.gz https://github.com/NixOS/patchelf/archive/$PATCHELF_VERSION.tar.gz
check_sha256sum patchelf.tar.gz $PATCHELF_HASH
tar -xzf patchelf.tar.gz
(cd patchelf-$PATCHELF_VERSION && ./bootstrap.sh && do_standard_install)
rm -rf patchelf.tar.gz patchelf-$PATCHELF_VERSION

ln -s $PY36_BIN/auditwheel /usr/local/bin/auditwheel

# Clean up development headers and other unnecessary stuff for
# final image
yum -y erase \
    avahi \
    bitstream-vera-fonts \
    freetype \
    gtk2 \
    hicolor-icon-theme \
    libX11 \
    wireless-tools \
    ${PYTHON_COMPILE_DEPS}  > /dev/null 2>&1
yum -y install ${MANYLINUX1_DEPS}
yum -y clean all > /dev/null 2>&1
yum list installed

# we don't need libpython*.a, and they're many megabytes
find /opt/_internal -name '*.a' -print0 | xargs -0 rm -f

# Strip what we can -- and ignore errors, because this just attempts to strip
# *everything*, including non-ELF files:
find /opt/_internal -type f -print0 \
    | xargs -0 -n1 strip --strip-unneeded 2>/dev/null || true

# We do not need the Python test suites, or indeed the precompiled .pyc and
# .pyo files. Partially cribbed from:
#    https://github.com/docker-library/python/blob/master/3.4/slim/Dockerfile
find /opt/_internal -depth \
     \( -type d -a -name test -o -name tests \) \
  -o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) | xargs rm -rf

for PYTHON in /opt/python/*/bin/python; do
    # Smoke test to make sure that our Pythons work, and do indeed detect as
    # being manylinux compatible:
    $PYTHON $MY_DIR/manylinux1-check.py
    # Make sure that SSL cert checking works
    $PYTHON $MY_DIR/ssl-check.py
done

# Fix libc headers to remain compatible with C99 compilers.
find /usr/include/ -type f -exec sed -i 's/\bextern _*inline_*\b/extern __inline __attribute__ ((__gnu_inline__))/g' {} +


# source me

PYTHON_DOWNLOAD_URL=https://www.python.org/ftp/python
CPYTHON_VERSIONS="3.5.6 3.6.7 3.7.1 3.8.2"

# openssl version to build, with expected sha256 hash of .tar.gz
# archive.
OPENSSL_ROOT=openssl-1.0.2p
OPENSSL_HASH=50a98e07b1a89eb8f6a99477f262df71c6fa7bef77df4dc83025a2845c827d00
OPENSSL_DOWNLOAD_URL=https://www.openssl.org/source/old/1.0.2

# Update to slightly newer, verified Git commit:
# https://github.com/NixOS/patchelf/commit/2a9cefd7d637d160d12dc7946393778fa8abbc58
PATCHELF_VERSION=2a9cefd7d637d160d12dc7946393778fa8abbc58
PATCHELF_HASH=12da4727f09be42ae0b54878e1b8e86d85cb7a5b595731cdc1a0a170c4873c6d

CURL_ROOT=curl-7.60.0
CURL_HASH=e9c37986337743f37fd14fe8737f246e97aec94b39d1b71e8a5973f72a9fc4f5
CURL_DOWNLOAD_URL=https://curl.haxx.se/download

AUTOCONF_ROOT=autoconf-2.69
AUTOCONF_HASH=954bd69b391edc12d6a4a51a2dd1476543da5c6bbf05a95b59dc0dd6fd4c2969
AUTOCONF_DOWNLOAD_URL=http://ftp.gnu.org/gnu/autoconf
AUTOMAKE_ROOT=automake-1.16.1
AUTOMAKE_HASH=608a97523f97db32f1f5d5615c98ca69326ced2054c9f82e65bade7fc4c9dea8
AUTOMAKE_DOWNLOAD_URL=http://ftp.gnu.org/gnu/automake
LIBTOOL_ROOT=libtool-2.4.6
LIBTOOL_HASH=e3bd4d5d3d025a36c21dd6af7ea818a2afcd4dfc1ea5a17b39d7854bcd0c06e3
LIBTOOL_DOWNLOAD_URL=http://ftp.gnu.org/gnu/libtool

SQLITE_AUTOCONF_VERSION=sqlite-autoconf-3230100
SQLITE_AUTOCONF_HASH=92842b283e5e744eff5da29ed3c69391de7368fccc4d0ee6bf62490ce555ef25
SQLITE_AUTOCONF_DOWNLOAD_URL=https://www.sqlite.org/2018

GIT_ROOT=2.17.0
GIT_HASH=46f3a29c250313276ac747a3a221ca69dbc3218754f8db188841322cf7ab58d3
GIT_DOWNLOAD_URL=https://github.com/git/git/archive

GET_PIP_URL=https://bootstrap.pypa.io/get-pip.py
EPEL_RPM_HASH=0dcc89f9bf67a2a515bad64569b7a9615edc5e018f676a578d5fd0f17d3c81d4
DEVTOOLS_HASH=a8ebeb4bed624700f727179e6ef771dafe47651131a00a78b342251415646acc


#!/bin/bash
# Helper utilities for build

function check_var {
    if [ -z "$1" ]; then
        echo "required variable not defined"
        exit 1
    fi
}

function lex_pyver {
    # Echoes Python version string padded with zeros
    # Thus:
    # 3.2.1 -> 003002001
    # 3     -> 003000000
    echo $1 | awk -F "." '{printf "%03d%03d%03d", $1, $2, $3}'
}

function pyver_dist_dir {
    # Echoes the dist directory name of given pyver, removing alpha/beta prerelease
    # Thus:
    # 3.2.1   -> 3.2.1
    # 3.7.0b4 -> 3.7.0
    echo $1 | awk -F "." '{printf "%d.%d.%d", $1, $2, $3}'
}

function do_cpython_build {
    local py_ver=$1
    check_var $py_ver
    local ucs_setting=$2
    check_var $ucs_setting
    tar -xzf Python-$py_ver.tgz
    pushd Python-$py_ver
    if [ "$ucs_setting" = "none" ]; then
        unicode_flags=""
        dir_suffix=""
    else
        local unicode_flags="--enable-unicode=$ucs_setting"
        local dir_suffix="-$ucs_setting"
    fi
    local prefix="/opt/_internal/cpython-${py_ver}${dir_suffix}"
    mkdir -p ${prefix}/lib
    ./configure --prefix=${prefix} --disable-shared $unicode_flags > /dev/null
    make -j2 > /dev/null
    make install > /dev/null
    popd
    rm -rf Python-$py_ver
    # Some python's install as bin/python3. Make them available as
    # bin/python.
    if [ -e ${prefix}/bin/python3 ]; then
        ln -s python3 ${prefix}/bin/python
    fi
    # --force-reinstall is to work around:
    #   https://github.com/pypa/pip/issues/5220
    #   https://github.com/pypa/get-pip/issues/19
    ${prefix}/bin/python get-pip.py --force-reinstall
    if [ -e ${prefix}/bin/pip3 ] && [ ! -e ${prefix}/bin/pip ]; then
        ln -s pip3 ${prefix}/bin/pip
    fi
    # Since we fall back on a canned copy of get-pip.py, we might not have
    # the latest pip and friends. Upgrade them to make sure.
    ${prefix}/bin/pip install -U --require-hashes -r ${MY_DIR}/requirements.txt
    local abi_tag=$(${prefix}/bin/python ${MY_DIR}/python-tag-abi-tag.py)
    ln -s ${prefix} /opt/python/${abi_tag}
}

function build_cpython {
    local py_ver=$1
    check_var $py_ver
    check_var $PYTHON_DOWNLOAD_URL
    local py_dist_dir=$(pyver_dist_dir $py_ver)
    curl -fsSLO $PYTHON_DOWNLOAD_URL/$py_dist_dir/Python-$py_ver.tgz
    curl -fsSLO $PYTHON_DOWNLOAD_URL/$py_dist_dir/Python-$py_ver.tgz.asc
    gpg --verify Python-$py_ver.tgz.asc
    if [ $(lex_pyver $py_ver) -lt $(lex_pyver 3.3) ]; then
        do_cpython_build $py_ver ucs2
        do_cpython_build $py_ver ucs4
    else
        do_cpython_build $py_ver none
    fi
    rm -f Python-$py_ver.tgz
    rm -f Python-$py_ver.tgz.asc
}

function build_cpythons {
    check_var $GET_PIP_URL
    curl -fsSLO $GET_PIP_URL
    # Import public keys used to verify downloaded Python source tarballs.
    curl -fsSLO https://www.python.org/static/files/pubkeys.txt
    gpg --import pubkeys.txt
    for py_ver in $@; do
        build_cpython $py_ver
    done
    # Remove GPG hidden directory.
    rm -rf /root/.gnupg/
    rm -f get-pip.py
}

function do_openssl_build {
    ./config no-ssl2 no-shared -fPIC --prefix=/usr/local/ssl > /dev/null
    make > /dev/null
    make install_sw > /dev/null
}

function check_required_source {
    local file=$1
    check_var ${file}
    if [ ! -f $file ]; then
        echo "Required source archive must be prefetched to docker/sources/ with prefetch.sh: $file"
        return 1
    fi
}

function fetch_source {
    # This is called both inside and outside the build context (e.g. in Travis) to prefetch
    # source tarballs, where curl exists (and works)
    local file=$1
    check_var ${file}
    local url=$2
    check_var ${url}
    if [ -f ${file} ]; then
        echo "${file} exists, skipping fetch"
    else
        curl -fsSL -o ${file} ${url}/${file}
    fi
}

function check_sha256sum {
    local fname=$1
    check_var ${fname}
    local sha256=$2
    check_var ${sha256}

    echo "${sha256}  ${fname}" > ${fname}.sha256
    sha256sum -c ${fname}.sha256
    rm -f ${fname}.sha256
}

function build_openssl {
    local openssl_fname=$1
    check_var ${openssl_fname}
    local openssl_sha256=$2
    check_var ${openssl_sha256}
    # Can't use curl here because we don't have it yet, OpenSSL must be prefetched
    check_required_source ${openssl_fname}.tar.gz
    check_sha256sum ${openssl_fname}.tar.gz ${openssl_sha256}
    tar -xzf ${openssl_fname}.tar.gz
    (cd ${openssl_fname} && do_openssl_build)
    rm -rf ${openssl_fname} ${openssl_fname}.tar.gz
}

function build_git {
    local git_fname=$1
    check_var ${git_fname}
    local git_sha256=$2
    check_var ${git_sha256}
    check_var ${GIT_DOWNLOAD_URL}
    fetch_source v${git_fname}.tar.gz ${GIT_DOWNLOAD_URL}
    check_sha256sum v${git_fname}.tar.gz ${git_sha256}
    tar -xzf v${git_fname}.tar.gz
    (cd git-${git_fname} && make install prefix=/usr/local LDFLAGS="-L/usr/local/ssl/lib -ldl" CFLAGS="-I/usr/local/ssl/include" > /dev/null)
    rm -rf git-${git_fname} v${git_fname}.tar.gz
}

function do_curl_build {
    # We do this shared to avoid obnoxious linker issues where git couldn't
    # link properly. If anyone wants to make this build statically go for it.
    LIBS=-ldl CFLAGS=-Wl,--exclude-libs,ALL ./configure --with-ssl --disable-static > /dev/null
    make > /dev/null
    make install > /dev/null
}

function build_curl {
    local curl_fname=$1
    check_var ${curl_fname}
    local curl_sha256=$2
    check_var ${curl_sha256}
    check_var ${CURL_DOWNLOAD_URL}
    # Can't use curl here because we don't have it yet...we are building it. It must be prefetched
    check_required_source ${curl_fname}.tar.gz
    check_sha256sum ${curl_fname}.tar.gz ${curl_sha256}
    tar -zxf ${curl_fname}.tar.gz
    (cd curl-* && do_curl_build)
    rm -rf curl-*
}

function do_standard_install {
    ./configure > /dev/null
    make > /dev/null
    make install > /dev/null
}

function build_autoconf {
    local autoconf_fname=$1
    check_var ${autoconf_fname}
    local autoconf_sha256=$2
    check_var ${autoconf_sha256}
    check_var ${AUTOCONF_DOWNLOAD_URL}
    fetch_source ${autoconf_fname}.tar.gz ${AUTOCONF_DOWNLOAD_URL}
    check_sha256sum ${autoconf_fname}.tar.gz ${autoconf_sha256}
    tar -zxf ${autoconf_fname}.tar.gz
    (cd ${autoconf_fname} && do_standard_install)
    rm -rf ${autoconf_fname} ${autoconf_fname}.tar.gz
}

function build_automake {
    local automake_fname=$1
    check_var ${automake_fname}
    local automake_sha256=$2
    check_var ${automake_sha256}
    check_var ${AUTOMAKE_DOWNLOAD_URL}
    fetch_source ${automake_fname}.tar.gz ${AUTOMAKE_DOWNLOAD_URL}
    check_sha256sum ${automake_fname}.tar.gz ${automake_sha256}
    tar -zxf ${automake_fname}.tar.gz
    (cd ${automake_fname} && do_standard_install)
    rm -rf ${automake_fname} ${automake_fname}.tar.gz
}

function build_libtool {
    local libtool_fname=$1
    check_var ${libtool_fname}
    local libtool_sha256=$2
    check_var ${libtool_sha256}
    check_var ${LIBTOOL_DOWNLOAD_URL}
    fetch_source ${libtool_fname}.tar.gz ${LIBTOOL_DOWNLOAD_URL}
    check_sha256sum ${libtool_fname}.tar.gz ${libtool_sha256}
    tar -zxf ${libtool_fname}.tar.gz
    (cd ${libtool_fname} && do_standard_install)
    rm -rf ${libtool_fname} ${libtool_fname}.tar.gz
}


#!/bin/bash
# Prefetch tarballs so they don't need to be fetched in the container (which has
# very old tools).
#
# usage: prefetch.sh [name ...]
set -ex

MY_DIR=$(dirname "${BASH_SOURCE[0]}")
. $MY_DIR/build_env.sh
. $MY_DIR/build_utils.sh

SOURCES=$MY_DIR/../sources

[ -d "$SOURCES" ] || mkdir "$SOURCES"
cd "$SOURCES"

for name in "$@"; do
    name_upper=$(echo "${name}" | tr [:lower:] [:upper:])
    root=${name_upper}_ROOT
    ext=${name_upper}_EXTENSION
    url=${name_upper}_DOWNLOAD_URL
    file=${!root}${!ext:-.tar.gz}
    fetch_source $file ${!url}
done


#!/bin/bash

if [ -n "$CROSS" ]; then
	TOOLCHAIN=-DCMAKE_TOOLCHAIN_FILE=../../cmake/$CROSS.cmake
else
	CROSS=native
fi

cd capnproto/c++
mkdir build-$CROSS
cd build-$CROSS
cmake .. $TOOLCHAIN \
         -DCMAKE_INSTALL_PREFIX=$ROOT \
         -DBUILD_SHARED_LIBS=OFF \
         -DBUILD_STATIC_LIBS=ON \
         -DBUILD_TESTING=OFF
sed -i -E -e 's,\$<TARGET_FILE:CapnProto::capnp_tool>,/usr/bin/capnp,' -e 's,\$<TARGET_FILE:CapnProto::capnpc_cpp>,/usr/bin/capnpc-c++,' cmake/CapnProtoConfig.cmake
make -j2 install
cd ..
rm -rf build-$CROSS


#!/bin/bash

curl -LO https://www.samba.org/ftp/ccache/ccache-3.4.2.tar.bz2
tar xjf ccache-*.tar.bz2
rm ccache-*.tar.bz2
pushd ccache*
./configure --prefix=/usr/local
make install
popd
rm -rf ccache

mkdir -p /usr/local/libexec/ccache
pushd /usr/local/libexec/ccache
ln -s /usr/local/bin/ccache cc
ln -s /usr/local/bin/ccache c++
popd


#!/bin/bash

if [ -n "$CROSS" ]; then
	TOOLCHAIN=-DCMAKE_TOOLCHAIN_FILE=../../cmake/$CROSS.cmake
else
	CROSS=native
fi

cd libzip
sed -i -Ee "/ADD_SUBDIRECTORY\\((examples|regress|src)\\)/d" CMakeLists.txt
mkdir build-$CROSS
cd build-$CROSS
rm -f $ROOT/lib/libz.dll.a # Prevent shared builds
cmake .. $TOOLCHAIN \
         -DCMAKE_INSTALL_PREFIX=$ROOT \
         -DBUILD_SHARED_LIBS=OFF \
         -DBUILD_STATIC_LIBS=ON
make -j2 install
cd ..
rm -rf build-$CROSS


#!/bin/bash

case $CROSS in
win32)
	CROSS_COMPILE=i686-w64-mingw32-
	;;
win64)
	CROSS_COMPILE=x86_64-w64-mingw32-
	;;
esac

if [ -n "$CROSS_COMPILE" ]; then
	CROSS_OPTS="-xplatform win32-g++ -device-option CROSS_COMPILE=$CROSS_COMPILE"
fi

REMOVE_QT="
3d
activeqt
canvas3d
connectivity
declarative
doc
docgallery
enginio
feedback
graphicaleffects
imageformats
location
pim
purchasing
qa
quick1
quickcontrols
quickcontrols2
repotools
script
sensors
serialbus
serialport
svg
systems
wayland
webchannel
webengine
webkit
webkit-examples
websockets
webview
xmlpatterns
"

if [ -d qt5 ]; then
	cd qt5
	git clean -dfx
	git submodule foreach git clean -dfx
else
	git clone https://github.com/qt/qt5.git -b v5.6.3
	cd qt5
	git submodule init
	for dir in $REMOVE_QT; do
	    git submodule deinit -f qt$dir
	done
	git submodule update
fi

./configure \
	-prefix $ROOT \
	-opensource \
	-confirm-license \
	-platform linux-g++ \
	$CROSS_OPTS \
	-release \
	-I $ROOT/include \
	-L $ROOT/lib \
	-v \
	-static \
	-c++std c++14 \
	-opengl desktop \
	-no-pch \
	-no-avx2 \
	-nomake examples \
	-nomake tools \
	-nomake tests \
	-no-compile-examples \
	-no-freetype \
	-no-icu \
	-no-gif \
	-no-sql-odbc \
	-no-harfbuzz \
	-no-openssl \
	-no-dbus && make -j$(nproc) install


#!/bin/bash

case $CROSS in
win32)
	PLATFORM=win32
	;;
win64)
	PLATFORM=amd64
	;;
esac

function install_python {
	local MAJOR=$1
	local MINOR=$2
	local PATCH=$3

	curl -LO https://www.python.org/ftp/python/$MAJOR.$MINOR.$PATCH/$PLATFORM/dev.msi
	7z x dev.msi
	mkdir -p $ROOT/lib/python$MAJOR.$MINOR include include/cpython include/internal
	mv libs_python.lib $ROOT/lib/python$MAJOR.$MINOR/python$MAJOR$MINOR.lib
	mv libs_python_stable.lib $ROOT/lib/python$MAJOR.$MINOR/python$MAJOR.lib
	mv libs_libpython.a $ROOT/lib/python$MAJOR.$MINOR/libpython.a
	find . -maxdepth 1 -name include_cpython_\*.h -print0 | xargs -0 -n1 -I% bash -c 'mv % $(echo % | sed -e "s,_,/," | sed -e "s,_,/,")'
	find . -maxdepth 1 -name include_internal_\*.h -print0 | xargs -0 -n1 -I% bash -c 'mv % $(echo % | sed -e "s,_,/," | sed -e "s,_,/,")'
	find . -maxdepth 1 -name include_\*.h -print0 | xargs -0 -n1 -I% bash -c 'mv % $(echo % | sed -e "s,_,/,")'
	mv include $ROOT/include/python$MAJOR.$MINOR
	rm dev.msi

	# python 3.8 is missing this file, create it from the dll
	# https://github.com/openai/retro/pull/201#issuecomment-619326417
	if [[ ! -f $ROOT/lib/python$MAJOR.$MINOR/libpython.a ]]
	then
		curl -LO https://www.python.org/ftp/python/$MAJOR.$MINOR.$PATCH/$PLATFORM/core.msi
		7z x core.msi
		apt-get install --yes mingw-w64-tools
		gendef python.dll
		/usr/bin/x86_64-w64-mingw32-dlltool --as-flags=--64 -m i386:x86-64 -k --output-lib $ROOT/lib/python$MAJOR.$MINOR/libpython.a --input-def python.def
		rm *.dll *.def core.msi
	fi
}

install_python 3 5 4
install_python 3 6 6
install_python 3 7 0
install_python 3 8 2


#!/bin/bash

autopep8 -ir -j2 . &
find src \( -name \*.cpp -o -name \*.h \) -a \( -not -name libretro.h -a -not -name *.capnp.* -a -not -path \*CMake\* -a -not -path \*autogen\* \) -print0 | \
	xargs -0 -P2 clang-format -i &

wait


#! /usr/bin/env bash

set -euo pipefail

if [ "$1" != "package" ]; then
  if (grep -r KJ_DBG c++/src | egrep -v '/debug(-test)?[.]' | grep -v 'See KJ_DBG\.$'); then
    echo '*** Error:  There are instances of KJ_DBG in the code.' >&2
    exit 1
  fi

  if (egrep -r 'TODO\((now|soon)\)' *); then
    echo '*** Error:  There are release-blocking TODOs in the code.' >&2
    exit 1
  fi
fi

doit() {
  echo "@@@@ $@"
  "$@"
}

get_version() {
  local VERSION=$(grep AC_INIT c++/configure.ac | sed -e 's/^[^]]*],\[\([^]]*\)].*$/\1/g')
  if [[ ! "$VERSION" =~ $1 ]]; then
    echo "Couldn't parse version: $VERSION" >&2
    exit 1
  fi
  echo "$VERSION"
}

get_release_version() {
  get_version '^[0-9]+[.][0-9]+[.][0-9]+(-rc[0-9]+|[.][0-9]+)?$'
}

update_version() {
  local OLD=$1
  local NEW=$2
  local BRANCH_DESC=$3

  local OLD_REGEX=${OLD//./[.]}
  doit sed -i -e "s/$OLD_REGEX/$NEW/g" c++/configure.ac
  doit sed -i -e "s/set(VERSION.*)/set(VERSION $NEW)/g" c++/CMakeLists.txt

  local NEW_NOTAG=${NEW%%-*}
  declare -a NEW_ARR=(${NEW_NOTAG//./ })
  doit sed -i -re "
      s/^#define CAPNP_VERSION_MAJOR [0-9]+\$/#define CAPNP_VERSION_MAJOR ${NEW_ARR[0]}/g;
      s/^#define CAPNP_VERSION_MINOR [0-9]+\$/#define CAPNP_VERSION_MINOR ${NEW_ARR[1]}/g;
      s/^#define CAPNP_VERSION_MICRO [0-9]+\$/#define CAPNP_VERSION_MICRO ${NEW_ARR[2]:-0}/g" \
      c++/src/capnp/common.h

  local NEW_COMBINED=$(( ${NEW_ARR[0]} * 1000000 + ${NEW_ARR[1]} * 1000 + ${NEW_ARR[2]:-0 }))
  doit sed -i -re "s/^#if CAPNP_VERSION != [0-9]*\$/#if CAPNP_VERSION != $NEW_COMBINED/g" \
      c++/src/*/*.capnp.h c++/src/*/*/*.capnp.h

  doit git commit -a -m "Set $BRANCH_DESC version to $NEW."
}

build_packages() {
  local VERSION=$1
  local VERSION_BASE=${VERSION%%-*}

  echo "========================================================================="
  echo "Building C++ package..."
  echo "========================================================================="

  # make dist tarball and move into ..
  cd c++
  doit autoreconf -i
  doit ./configure
  doit make -j6 distcheck
  doit mv capnproto-c++-$VERSION.tar.gz ..
  doit make distclean

  # build windows executables
  doit ./configure --host=i686-w64-mingw32 --with-external-capnp \
      --disable-shared CXXFLAGS='-static-libgcc -static-libstdc++'
  doit make -j6 capnp.exe capnpc-c++.exe capnpc-capnp.exe
  doit i686-w64-mingw32-strip capnp.exe capnpc-c++.exe capnpc-capnp.exe
  doit mkdir capnproto-tools-win32-$VERSION
  doit mv capnp.exe capnpc-c++.exe capnpc-capnp.exe capnproto-tools-win32-$VERSION
  doit make maintainer-clean

  # repack dist tarball and win32 tools into win32 zip, with DOS line endings
  doit tar zxf ../capnproto-c++-$VERSION.tar.gz
  find capnproto-c++-$VERSION -name '*.c++' -o -name '*.h' -o -name '*.capnp' -o -name '*.md' -o -name '*.txt' | grep -v testdata | doit xargs unix2dos
  doit zip -r ../capnproto-c++-win32-$VERSION.zip capnproto-c++-$VERSION capnproto-tools-win32-$VERSION

  rm -rf capnproto-c++-$VERSION capnproto-tools-win32-$VERSION
  cd ..
}

cherry_pick() {
  shift
  if [ $# -gt 0 ]; then
    echo "========================================================================="
    echo "Cherry-picking fixes"
    echo "========================================================================="
    doit git cherry-pick "$@"
  fi
}

done_banner() {
  local VERSION=$1
  local PUSH=$2
  local FINAL=$3
  echo "========================================================================="
  echo "Done"
  echo "========================================================================="
  echo "Ready to release:"
  echo "  capnproto-c++-$VERSION.tar.gz"
  echo "  capnproto-c++-win32-$VERSION.zip"
  echo "Don't forget to push changes:"
  echo "  git push origin $PUSH"

  read -s -n 1 -p "Shall I push to git and upload to capnproto.org now? (y/N)" YESNO

  echo
  case "$YESNO" in
    y | Y )
      doit git push origin $PUSH
      doit gce-ss copy-files capnproto-c++-$VERSION.tar.gz capnproto-c++-win32-$VERSION.zip \
          fe:/var/www/capnproto.org

      if [ "$FINAL" = yes ]; then
        echo "========================================================================="
        echo "Publishing docs"
        echo "========================================================================="
        cd doc
        doit ./push-site.sh
        cd ..
        echo "========================================================================="
        echo "Really done"
        echo "========================================================================="
      fi

      echo "Release is available at:"
      echo "  http://capnproto.org/capnproto-c++-$VERSION.tar.gz"
      ;;
    * )
      echo "OK, do it yourself then."
      ;;
  esac
}

BRANCH=$(git rev-parse --abbrev-ref HEAD)

case "${1-}:$BRANCH" in
  # ======================================================================================
  candidate:master )
    echo "New major release."

    if [ $# -gt 1 ]; then
      echo "Cannot cherry-pick when starting from master.  Do it yourself." >&2
      exit 1
    fi

    HEAD_VERSION=$(get_version '^[0-9]+[.][0-9]+-dev$')
    RELEASE_VERSION=${HEAD_VERSION%%-dev}.0

    echo "Version: $RELEASE_VERSION"

    echo "========================================================================="
    echo "Creating release branch..."
    echo "========================================================================="
    doit git checkout -b release-$RELEASE_VERSION

    update_version $HEAD_VERSION $RELEASE_VERSION-rc1 "release branch"

    build_packages $RELEASE_VERSION-rc1

    echo "========================================================================="
    echo "Updating version in master branch..."
    echo "========================================================================="

    doit git checkout master
    declare -a VERSION_ARR=(${RELEASE_VERSION//./ })
    NEXT_VERSION=${VERSION_ARR[0]}.$((VERSION_ARR[1] + 1))

    update_version $HEAD_VERSION $NEXT_VERSION-dev "mainlaine"

    done_banner $RELEASE_VERSION-rc1 "master release-$RELEASE_VERSION" no
    ;;

  # ======================================================================================
  candidate:release-* )
    echo "New release candidate."
    OLD_VERSION=$(get_release_version)

    if [[ $OLD_VERSION == *-rc* ]]; then
      # New release candidate for existing release.

      RC=${OLD_VERSION##*-rc}
      BRANCH_VERSION=${OLD_VERSION%%-rc*}
      RC_VERSION=$BRANCH_VERSION-rc$(( RC + 1 ))

      echo "Version: $RC_VERSION"
    else
      # New micro release.

      declare -a VERSION_ARR=(${OLD_VERSION//./ })
      BRANCH_VERSION=${VERSION_ARR[0]}.${VERSION_ARR[1]}.$((VERSION_ARR[2] + 1))

      RC_VERSION=$BRANCH_VERSION-rc1
      echo "Version: $RC_VERSION"

      echo "========================================================================="
      echo "Creating new release branch..."
      echo "========================================================================="

      doit git checkout -b release-$BRANCH_VERSION
    fi

    echo "========================================================================="
    echo "Updating version number to $RC_VERSION..."
    echo "========================================================================="

    update_version $OLD_VERSION $RC_VERSION "release branch"

    cherry_pick "$@"

    build_packages $RC_VERSION

    done_banner $RC_VERSION release-$BRANCH_VERSION no
    ;;

  # ======================================================================================
  final:release-* )
    echo "Final release."
    OLD_VERSION=$(get_release_version)

    if [[ $OLD_VERSION != *-rc* ]]; then
      echo "Current version is already a final release.  You need to create a new candidate first." >&2
      exit 1
    fi

    if [ $# -gt 1 ]; then
      echo "Cannot cherry-pick into final release.  Make another candidate." >&2
      exit 1
    fi

    RC=${OLD_VERSION##*-rc}
    NEW_VERSION=${OLD_VERSION%%-rc*}

    echo "Version: $NEW_VERSION"

    echo "========================================================================="
    echo "Updating version number to $NEW_VERSION..."
    echo "========================================================================="

    doit sed -i -re "s/capnproto-c[+][+]-[0-9]+[.][0-9]+[.][0-9]+([.][0-9]+)?\>/capnproto-c++-$NEW_VERSION/g" doc/install.md
    doit sed -i -re "s/capnproto-c[+][+]-win32-[0-9]+[.][0-9]+[.][0-9]+([.][0-9]+)?\>/capnproto-c++-win32-$NEW_VERSION/g" doc/install.md
    doit sed -i -re "s/capnproto-tools-win32-[0-9]+[.][0-9]+[.][0-9]+([.][0-9]+)?\>/capnproto-tools-win32-$NEW_VERSION/g" doc/install.md
    update_version $OLD_VERSION $NEW_VERSION "release branch"

    doit git tag v$NEW_VERSION

    build_packages $NEW_VERSION

    done_banner $NEW_VERSION "v$NEW_VERSION release-$NEW_VERSION" yes
    ;;

  # ======================================================================================
  security:release-* )
    echo "Security release."
    OLD_VERSION=$(get_release_version)

    if [[ $OLD_VERSION == *-rc* ]]; then
      echo "Security releases don't have candidates." >&2
      exit 1
    fi

    declare -a VERSION_ARR=(${OLD_VERSION//./ } 0)
    NEW_VERSION=${VERSION_ARR[0]}.${VERSION_ARR[1]}.${VERSION_ARR[2]}.$((VERSION_ARR[3] + 1))

    echo "Version: $NEW_VERSION"

    echo "========================================================================="
    echo "Updating version number to $NEW_VERSION..."
    echo "========================================================================="

    doit sed -i -re "s/capnproto-c[+][+]-[0-9]+[.][0-9]+[.][0-9]+([.][0-9]+)?\>/capnproto-c++-$NEW_VERSION/g" doc/install.md
    doit sed -i -re "s/capnproto-c[+][+]-win32-[0-9]+[.][0-9]+[.][0-9]+([.][0-9]+)?\>/capnproto-c++-win32-$NEW_VERSION/g" doc/install.md
    doit sed -i -re "s/capnproto-tools-win32-[0-9]+[.][0-9]+[.][0-9]+([.][0-9]+)?\>/capnproto-tools-win32-$NEW_VERSION/g" doc/install.md
    update_version $OLD_VERSION $NEW_VERSION "release branch"

    cherry_pick "$@"

    doit git tag v$NEW_VERSION

    build_packages $NEW_VERSION

    done_banner $NEW_VERSION "v$NEW_VERSION release-$NEW_VERSION" yes
    ;;

  # ======================================================================================
  retry:release-* )
    echo "Retrying release."
    OLD_VERSION=$(get_release_version)
    echo "Version: $OLD_VERSION"

    if [[ $OLD_VERSION == *-rc* ]]; then
      # We can add more cherry-picks when retrying a candidate.
      cherry_pick "$@"
    else
      if [ $# -gt 1 ]; then
        echo "Cannot cherry-pick into final release.  Make another candidate." >&2
        exit 1
      fi
    fi

    OLD_VERSION=$(get_release_version)
    build_packages $OLD_VERSION

    if [[ $OLD_VERSION == *-rc* ]]; then
      BRANCH_VERSION=${OLD_VERSION%%-rc*}
      done_banner $OLD_VERSION release-$BRANCH_VERSION no
    else
      doit git tag v$OLD_VERSION
      done_banner $OLD_VERSION "v$OLD_VERSION release-$OLD_VERSION" no
    fi
    ;;

  # ======================================================================================
  package:* )
    echo "Just building a package."
    build_packages $(get_version '.*')
    ;;

  # ======================================================================================
  *:master )
    echo "Invalid command for mainline branch.  Only command is 'candidate'." >&2
    exit 1
    ;;

  *:release-* )
    echo "Invalid command for release branch.  Commands are 'candidate', 'final', and 'retry'." >&2
    exit 1
    ;;

  * )
    echo "Not a master or release branch." >&2
    exit 1
    ;;
esac


#! /usr/bin/env bash

set -euo pipefail

doit() {
  echo "@@@@ $@"
  "$@"
}

QUICK=

PARALLEL=$(nproc 2>/dev/null || echo 1)

while [ $# -gt 0 ]; do
  case "$1" in
    -j* )
      PARALLEL=${1#-j}
      ;;
    test )
      ;;  # nothing
    quick )
      QUICK=quick
      ;;
    caffeinate )
      # Re-run preventing sleep.
      shift
      exec caffeinate -ims $0 $@
      ;;
    tmpdir )
      # Clone to a temp directory.
      if [ "$#" -lt 2 ]; then
        echo "usage: $0 tmpdir NAME [COMMAND]" >&2
        exit 1
      fi
      DIR=/tmp/$2
      shift 2
      if [ -e $DIR ]; then
        if [ "${DIR/*..*}" = "" ]; then
          echo "NO DO NOT PUT .. IN THERE IT'S GOING TO GO IN /tmp AND I'M GONNA DELETE IT" >&2
          exit 1
        fi
        if [ ! -e "$DIR/super-test.sh" ]; then
          echo "$DIR exists and it doesn't look like one of mine." >&2
          exit 1
        fi
        # make distcheck leaves non-writable files when it fails, so we need to chmod to be safe.
        chmod -R +w $DIR
        rm -rf $DIR
      fi
      git clone . $DIR
      cd $DIR
      exec ./super-test.sh "$@"
      ;;
    remote )
      if [ "$#" -lt 2 ]; then
        echo "usage: $0 remote HOST [COMMAND]" >&2
        exit 1
      fi
      HOST=$2
      shift 2
      echo "========================================================================="
      echo "Pushing code to $HOST..."
      echo "========================================================================="
      BRANCH=$(git rev-parse --abbrev-ref HEAD)
      ssh $HOST '(chmod -fR +w tmp-test-capnp || true) && rm -rf tmp-test-capnp && mkdir tmp-test-capnp && git init tmp-test-capnp'
      git push ssh://$HOST/~/tmp-test-capnp "$BRANCH:test"
      ssh $HOST "cd tmp-test-capnp && git checkout test"
      exec ssh $HOST "cd tmp-test-capnp && ./super-test.sh $@ && cd .. && rm -rf tmp-test-capnp"
      ;;
    compiler )
      if [ "$#" -lt 2 ]; then
        echo "usage: $0 compiler CXX_NAME" >&2
        exit 1
      fi
      export CXX="$2"
      shift
      ;;
    clang )
      export CXX=clang++
      ;;
    gcc-4.9 )
      export CXX=g++-4.9
      ;;
    gcc-4.8 )
      export CXX=g++-4.8
      ;;
    gcc-4.7 )
      export CXX=g++-4.7
      ;;
    mingw )
      if [ "$#" -ne 2 ]; then
        echo "usage: $0 mingw CROSS_HOST" >&2
        exit 1
      fi
      CROSS_HOST=$2

      cd c++
      test -e configure || doit autoreconf -i
      test ! -e Makefile || (echo "ERROR: Directory unclean!" >&2 && false)

      export WINEPATH='Z:\usr\'"$CROSS_HOST"'\lib;Z:\usr\lib\gcc\'"$CROSS_HOST"'\6.3-win32;Z:'"$PWD"'\.libs'

      doit ./configure --host="$CROSS_HOST" --disable-shared CXXFLAGS='-static-libgcc -static-libstdc++'

      doit make -j$PARALLEL check
      doit make distclean
      rm -f *-mingw.exe
      exit 0
      ;;
    android )
      # To install Android SDK:
      # - Download command-line tools: https://developer.android.com/studio/index.html#command-tools
      # - Run $SDK_HOME/tools/bin/sdkmanager platform-tools 'platforms;android-25' 'system-images;android-25;google_apis;armeabi-v7a' emulator 'build-tools;25.0.2' ndk-bundle
      # - Run $SDK_HOME/tools/bin/avdmanager create avd -n capnp -k 'system-images;android-25;google_apis;armeabi-v7a' -b google_apis/armeabi-v7a
      # - Run $SDK_HOME/ndk-bundle/build/tools/make_standalone_toolchain.py --arch arm --api 24 --install-dir $TOOLCHAIN_HOME
      if [ "$#" -ne 4 ]; then
        echo "usage: $0 android SDK_HOME TOOLCHAIN_HOME CROSS_HOST" >&2
        exit 1
      fi
      SDK_HOME=$2
      TOOLCHAIN_HOME=$3
      CROSS_HOST=$4

      cd c++
      test -e configure || doit autoreconf -i
      test ! -e Makefile || (echo "ERROR: Directory unclean!" >&2 && false)
      doit ./configure --disable-shared
      doit make -j$PARALLEL capnp capnpc-c++

      cp capnp capnp-host
      cp capnpc-c++ capnpc-c++-host

      export PATH="$TOOLCHAIN_HOME/bin:$PATH"
      doit make distclean
      doit ./configure --host="$CROSS_HOST" --with-external-capnp --disable-shared CXXFLAGS='-pie -fPIE' CAPNP=./capnp-host CAPNPC_CXX=./capnpc-c++-host

      doit make -j$PARALLEL
      doit make -j$PARALLEL capnp-test

      echo "Starting emulator..."
      trap 'kill $(jobs -p)' EXIT
      # TODO(someday): Speed up with KVM? Then maybe we won't have to skip fuzz tests?
      $SDK_HOME/emulator/emulator -avd capnp -no-window &
      $SDK_HOME/platform-tools/adb 'wait-for-device'
      echo "Waiting for localhost to be resolvable..."
      doit $SDK_HOME/platform-tools/adb shell 'while ! ping -c 1 localhost > /dev/null 2>&1; do sleep 1; done'
      # TODO(cleanup): With 'adb shell' I find I cannot put files anywhere, so I'm using 'su' a
      #   lot here. There is probably a better way.
      doit $SDK_HOME/platform-tools/adb shell 'su 0 tee /data/capnp-test > /dev/null' < capnp-test
      doit $SDK_HOME/platform-tools/adb shell 'su 0 chmod a+rx /data/capnp-test'
      doit $SDK_HOME/platform-tools/adb shell 'cd /data && CAPNP_SKIP_FUZZ_TEST=1 su 0 /data/capnp-test && echo ANDROID_""TESTS_PASSED' | tee android-test.log
      grep -q ANDROID_TESTS_PASSED android-test.log

      doit make distclean
      rm -f capnp-host capnpc-c++-host
      exit 0
      ;;
    cmake )
      cd c++
      rm -rf cmake-build
      mkdir cmake-build
      cd cmake-build
      doit cmake -G "Unix Makefiles" ..
      doit make -j$PARALLEL check
      exit 0
      ;;
    exotic )
      echo "========================================================================="
      echo "MinGW 64-bit"
      echo "========================================================================="
      "$0" mingw x86_64-w64-mingw32
      echo "========================================================================="
      echo "MinGW 32-bit"
      echo "========================================================================="
      "$0" mingw i686-w64-mingw32
      echo "========================================================================="
      echo "Android"
      echo "========================================================================="
      "$0" android /home/kenton/android-sdk-linux /home/kenton/android-24 arm-linux-androideabi
      echo "========================================================================="
      echo "CMake"
      echo "========================================================================="
      "$0" cmake
      exit 0
      ;;
    clean )
      rm -rf tmp-staging
      cd c++
      if [ -e Makefile ]; then
        doit make maintainer-clean
      fi
      rm -f capnproto-*.tar.gz samples/addressbook samples/addressbook.capnp.c++ \
            samples/addressbook.capnp.h
      exit 0
      ;;
    help )
      echo "usage: $0 [COMMAND]"
      echo "commands:"
      echo "  test          Runs tests (the default)."
      echo "  clang         Runs tests using Clang compiler."
      echo "  gcc-4.7       Runs tests using gcc-4.7."
      echo "  gcc-4.8       Runs tests using gcc-4.8."
      echo "  gcc-4.9       Runs tests using gcc-4.9."
      echo "  remote HOST   Runs tests on HOST via SSH."
      echo "  mingw         Cross-compiles to MinGW and runs tests using WINE."
      echo "  android       Cross-compiles to Android and runs tests using emulator."
      echo "  clean         Delete temporary files that may be left after failure."
      echo "  help          Prints this help text."
      exit 0
      ;;
    * )
      echo "unknown command: $1" >&2
      echo "try: $0 help" >&2
      exit 1
      ;;
  esac
  shift
done

# Build optimized builds because they catch more problems, but also enable debugging macros.
# Enable lots of warnings and make sure the build breaks if they fire.  Disable strict-aliasing
# because GCC warns about code that I know is OK.  Disable sign-compare because I've fixed more
# sign-compare warnings than probably all other warnings combined and I've never seen it flag a
# real problem. Disable unused parameters because it's stupidly noisy and never a real problem.
# Enable expensive release-gating tests.
export CXXFLAGS="-O2 -DDEBUG -Wall -Wextra -Werror -Wno-strict-aliasing -Wno-sign-compare -Wno-unused-parameter -DCAPNP_EXPENSIVE_TESTS=1"

STAGING=$PWD/tmp-staging

rm -rf "$STAGING"
mkdir "$STAGING"
mkdir "$STAGING/bin"
mkdir "$STAGING/lib"
export PATH=$STAGING/bin:$PATH
export LD_LIBRARY_PATH=$STAGING/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export PKG_CONFIG_PATH=$STAGING/lib/pkgconfig

if [ "$QUICK" = quick ]; then
  echo "************************** QUICK TEST ***********************************"
fi

echo "========================================================================="
echo "Building c++"
echo "========================================================================="

# Apple now aliases gcc to clang, so probe to find out what compiler we're really using.
if (${CXX:-g++} -dM -E -x c++ /dev/null 2>&1 | grep -q '__clang__'); then
  IS_CLANG=yes
  DISABLE_OPTIMIZATION_IF_GCC=
else
  IS_CLANG=no
  DISABLE_OPTIMIZATION_IF_GCC=-O0
fi

if [ $IS_CLANG = yes ]; then
  # Don't fail out on this ridiculous "argument unused during compilation" warning.
  export CXXFLAGS="$CXXFLAGS -Wno-error=unused-command-line-argument"
else
  # GCC emits uninitialized warnings all over and they seem bogus. We use valgrind to test for
  # uninitialized memory usage later on. GCC 4 also emits strange bogus warnings with
  # -Wstrict-overflow, so we disable it.
  CXXFLAGS="$CXXFLAGS -Wno-maybe-uninitialized -Wno-strict-overflow"
fi

cd c++
doit autoreconf -i
doit ./configure --prefix="$STAGING"
doit make -j$PARALLEL check

if [ $IS_CLANG = no ]; then
  # Verify that generated code compiles with pedantic warnings.  Make sure to treat capnp headers
  # as system headers so warnings in them are ignored.
  doit ${CXX:-g++} -isystem src -std=c++11 -fno-permissive -pedantic -Wall -Wextra -Werror \
      -c src/capnp/test.capnp.c++ -o /dev/null
fi

echo "========================================================================="
echo "Testing c++ install"
echo "========================================================================="

doit make install

test "x$(which capnp)" = "x$STAGING/bin/capnp"
test "x$(which capnpc-c++)" = "x$STAGING/bin/capnpc-c++"

cd samples

doit capnp compile -oc++ addressbook.capnp -I"$STAGING"/include --no-standard-import
doit ${CXX:-g++} -std=c++11 addressbook.c++ addressbook.capnp.c++ -o addressbook \
    $CXXFLAGS $(pkg-config --cflags --libs capnp)
echo "@@@@ ./addressbook (in various configurations)"
./addressbook write | ./addressbook read
./addressbook dwrite | ./addressbook dread
rm addressbook addressbook.capnp.c++ addressbook.capnp.h

doit capnp compile -oc++ calculator.capnp -I"$STAGING"/include --no-standard-import
doit ${CXX:-g++} -std=c++11 calculator-client.c++ calculator.capnp.c++ -o calculator-client \
    $CXXFLAGS $(pkg-config --cflags --libs capnp-rpc)
doit ${CXX:-g++} -std=c++11 calculator-server.c++ calculator.capnp.c++ -o calculator-server \
    $CXXFLAGS $(pkg-config --cflags --libs capnp-rpc)
rm -f /tmp/capnp-calculator-example-$$
./calculator-server unix:/tmp/capnp-calculator-example-$$ &
sleep 0.1
./calculator-client unix:/tmp/capnp-calculator-example-$$
kill %+
wait %+ || true
rm calculator-client calculator-server calculator.capnp.c++ calculator.capnp.h /tmp/capnp-calculator-example-$$

cd ..

if [ "$QUICK" = quick ]; then
  doit make maintainer-clean
  rm -rf "$STAGING"
  exit 0
fi

echo "========================================================================="
echo "Testing --with-external-capnp"
echo "========================================================================="

doit make distclean
doit ./configure --prefix="$STAGING" --disable-shared \
    --with-external-capnp CAPNP=$STAGING/bin/capnp
doit make -j$PARALLEL check

echo "========================================================================="
echo "Testing --disable-reflection"
echo "========================================================================="

doit make distclean
doit ./configure --prefix="$STAGING" --disable-shared --disable-reflection \
    --with-external-capnp CAPNP=$STAGING/bin/capnp
doit make -j$PARALLEL check
doit make distclean

# Test 32-bit build now while we have $STAGING available for cross-compiling.
if [ "x`uname -m`" = "xx86_64" ]; then
  echo "========================================================================="
  echo "Testing 32-bit build"
  echo "========================================================================="

  if [[ "`uname`" =~ CYGWIN ]]; then
    # It's just not possible to run cygwin32 binaries from within cygwin64.

    # Build as if we are cross-compiling, using the capnp we installed to $STAGING.
    doit ./configure --prefix="$STAGING" --disable-shared --host=i686-pc-cygwin \
        --with-external-capnp CAPNP=$STAGING/bin/capnp
    doit make -j$PARALLEL
    doit make -j$PARALLEL capnp-test.exe

    # Expect a cygwin32 sshd to be listening at localhost port 2222, and use it
    # to run the tests.
    doit scp -P 2222 capnp-test.exe localhost:~/tmp-capnp-test.exe
    doit ssh -p 2222 localhost './tmp-capnp-test.exe && rm tmp-capnp-test.exe'

    doit make distclean

  elif [ "x${CXX:-g++}" != "xg++-4.8" ]; then
    doit ./configure CXX="${CXX:-g++} -m32" --disable-shared
    doit make -j$PARALLEL check
    doit make distclean
  fi
fi

echo "========================================================================="
echo "Testing c++ uninstall"
echo "========================================================================="

doit ./configure --prefix="$STAGING"
doit make uninstall

echo "========================================================================="
echo "Testing c++ dist"
echo "========================================================================="

doit make -j$PARALLEL distcheck
doit make distclean
rm capnproto-*.tar.gz

if [ "x`uname`" = xLinux ]; then
  echo "========================================================================="
  echo "Testing generic Unix (no Linux-specific features)"
  echo "========================================================================="

  doit ./configure --disable-shared CXXFLAGS="$CXXFLAGS -DKJ_USE_FUTEX=0 -DKJ_USE_EPOLL=0"
  doit make -j$PARALLEL check
  doit make distclean
fi

echo "========================================================================="
echo "Testing with -fno-rtti and -fno-exceptions"
echo "========================================================================="

# GCC miscompiles capnpc-c++ when -fno-exceptions and -O2 are specified together. The
# miscompilation happens in one specific inlined call site of Array::dispose(), but this method
# is inlined in hundreds of other places without issue, so I have no idea how to narrow down the
# bug. Clang works fine. So, for now, we disable optimizations on GCC for -fno-exceptions tests.

doit ./configure --disable-shared CXXFLAGS="$CXXFLAGS -fno-rtti"
doit make -j$PARALLEL check
doit make distclean
doit ./configure --disable-shared CXXFLAGS="$CXXFLAGS -fno-exceptions $DISABLE_OPTIMIZATION_IF_GCC"
doit make -j$PARALLEL check
doit make distclean
doit ./configure --disable-shared CXXFLAGS="$CXXFLAGS -fno-rtti -fno-exceptions $DISABLE_OPTIMIZATION_IF_GCC"
doit make -j$PARALLEL check

# Valgrind is currently "experimental and mostly broken" on OSX and fails to run the full test
# suite, but I have it installed because it did manage to help me track down a bug or two.  Anyway,
# skip it on OSX for now.
if [ "x`uname`" != xDarwin ] && which valgrind > /dev/null; then
  doit make distclean

  echo "========================================================================="
  echo "Testing with valgrind"
  echo "========================================================================="

  doit ./configure --disable-shared CXXFLAGS="-g"
  doit make -j$PARALLEL
  doit make -j$PARALLEL capnp-test
  # Running the fuzz tests under Valgrind is a great thing to do -- but it takes
  # some 40 minutes. So, it needs to be done as a separate step of the release
  # process, perhaps along with the AFL tests.
  CAPNP_SKIP_FUZZ_TEST=1 doit valgrind --leak-check=full --track-fds=yes --error-exitcode=1 ./capnp-test
fi

doit make maintainer-clean

rm -rf "$STAGING"


#! /bin/bash

set -euo pipefail

echo "Choose test case:"
echo "1) TestAllTypes parsing"
echo "2) TestLists parsing"
echo "3) Canonicalization"

read -p "choice: " -n 1 TESTCASE
echo

case "$TESTCASE" in
  1 )
    TESTDATA=binary
    FLAGS=
    TESTNAME=default
    ;;
  2 )
    TESTDATA=lists.binary
    FLAGS=--lists
    TESTNAME=lists
    ;;
  3 )
    TESTDATA=binary
    FLAGS=--canonicalize
    TESTNAME=canonicalize
    ;;
  * )
    echo "Invalid choice: $TESTCASE" >&2
    exit 1
esac

echo "Choose compiler:"
echo "1) GCC"
echo "2) Clang"

read -p "choice: " -n 1 TESTCASE
echo

case "$TESTCASE" in
  1 )
    export CXX=afl-g++
    ;;
  2 )
    export CXX=afl-clang++
    ;;
  * )
    echo "Invalid choice: $TESTCASE" >&2
    exit 1
esac

if [ -e Makefile ]; then
  if ! grep -q '^CXX *= *'"$CXX" Makefile; then
    # Wrong compiler used.
    make distclean
    $(dirname $0)/configure --disable-shared
  fi
else
  $(dirname $0)/configure --disable-shared
fi

make -j$(nproc)
make -j$(nproc) capnp-afl-testcase

NOW=$(date +%Y-%m-%d.%H-%M-%S).$TESTNAME.$CXX

mkdir afl.$NOW.inputs afl.$NOW.findings

cp $(dirname $0)/src/capnp/testdata/$TESTDATA afl.$NOW.inputs

afl-fuzz -i afl.$NOW.inputs -o afl.$NOW.findings -- ./capnp-afl-testcase $FLAGS


#! /usr/bin/env bash

set -euo pipefail

export PATH=$PWD/bin:$PWD:$PATH

capnp compile -Isrc --no-standard-import --src-prefix=src -oc++:src \
    src/capnp/c++.capnp src/capnp/schema.capnp \
    src/capnp/compiler/lexer.capnp src/capnp/compiler/grammar.capnp \
    src/capnp/rpc.capnp src/capnp/rpc-twoparty.capnp src/capnp/persistent.capnp \
    src/capnp/compat/json.capnp


#! /usr/bin/env bash

set -euo pipefail

echo "This script is no longer needed. Go ahead and run autoreconf. For example:"
echo "  autoreconf -i && ./configure && make -j6 check && sudo make install"


#! /usr/bin/env bash
#
# Quick script that compiles and runs the samples, then cleans up.
# Used for release testing.

set -exuo pipefail

capnpc -oc++ addressbook.capnp
c++ -std=c++11 -Wall addressbook.c++ addressbook.capnp.c++ \
    $(pkg-config --cflags --libs capnp) -o addressbook
./addressbook write | ./addressbook read
./addressbook dwrite | ./addressbook dread
rm addressbook addressbook.capnp.c++ addressbook.capnp.h

capnpc -oc++ calculator.capnp
c++ -std=c++11 -Wall calculator-client.c++ calculator.capnp.c++ \
    $(pkg-config --cflags --libs capnp-rpc) -o calculator-client
c++ -std=c++11 -Wall calculator-server.c++ calculator.capnp.c++ \
    $(pkg-config --cflags --libs capnp-rpc) -o calculator-server
rm -f /tmp/capnp-calculator-example-$$
./calculator-server unix:/tmp/capnp-calculator-example-$$ &
sleep 0.1
./calculator-client unix:/tmp/capnp-calculator-example-$$
kill %+
wait %+ || true
rm calculator-client calculator-server calculator.capnp.c++ calculator.capnp.h /tmp/capnp-calculator-example-$$


#! /bin/sh
# Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
# Licensed under the MIT License:
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Tests the `capnp` tool's various commands, other than `compile`.

set -eu

fail() {
  echo "FAILED: $@" >&2
  exit 1
}

if test -f ./capnp; then
  CAPNP=${CAPNP:-./capnp}
elif test -f ./capnp.exe; then
  CAPNP=${CAPNP:-./capnp.exe}
else
  CAPNP=${CAPNP:-capnp}
fi

SCHEMA=`dirname "$0"`/../test.capnp
TESTDATA=`dirname "$0"`/../testdata

$CAPNP encode $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/binary - || fail encode
$CAPNP encode --flat $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/flat - || fail encode flat
$CAPNP encode --packed $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/packed - || fail encode packed
$CAPNP encode --packed --flat $SCHEMA TestAllTypes < $TESTDATA/short.txt | cmp $TESTDATA/packedflat - || fail encode packedflat
$CAPNP encode $SCHEMA TestAllTypes < $TESTDATA/pretty.txt | cmp $TESTDATA/binary - || fail parse pretty

$CAPNP decode $SCHEMA TestAllTypes < $TESTDATA/binary | cmp $TESTDATA/pretty.txt - || fail decode
$CAPNP decode --flat $SCHEMA TestAllTypes < $TESTDATA/flat | cmp $TESTDATA/pretty.txt - || fail decode flat
$CAPNP decode --packed $SCHEMA TestAllTypes < $TESTDATA/packed | cmp $TESTDATA/pretty.txt - || fail decode packed
$CAPNP decode --packed --flat $SCHEMA TestAllTypes < $TESTDATA/packedflat | cmp $TESTDATA/pretty.txt - || fail decode packedflat
$CAPNP decode --short $SCHEMA TestAllTypes < $TESTDATA/binary | cmp $TESTDATA/short.txt - || fail decode short

$CAPNP decode $SCHEMA TestAllTypes < $TESTDATA/segmented | cmp $TESTDATA/pretty.txt - || fail decode segmented
$CAPNP decode --packed $SCHEMA TestAllTypes < $TESTDATA/segmented-packed | cmp $TESTDATA/pretty.txt - || fail decode segmented-packed

test_eval() {
  test "x`$CAPNP eval $SCHEMA $1 | tr -d '\r'`" = "x$2" || fail eval "$1 == $2"
}

test_eval TestDefaults.uInt32Field 3456789012
test_eval TestDefaults.structField.textField '"baz"'
test_eval TestDefaults.int8List "[111, -111]"
test_eval 'TestDefaults.structList[1].textField' '"structlist 2"'
test_eval globalPrintableStruct '(someText = "foo")'
test_eval TestConstants.enumConst corge
test_eval 'TestListDefaults.lists.int32ListList[2][0]' 12341234

$CAPNP compile -ofoo $TESTDATA/errors.capnp.nobuild 2>&1 | sed -e "s,^.*/errors[.]capnp[.]nobuild,file,g" | tr -d '\r' |
    cmp $TESTDATA/errors.txt - || fail error output


#! /usr/bin/env bash

set -eu
shopt -s extglob

if grep 'localhost:4000' *.md _posts/*.md; then
  echo "ERROR: Your content has links to localhost:4000!" >&2
  exit 1
fi

if [ "x$(git status --porcelain)" != "x" ]; then
  echo -n "git repo has uncommited changes.  Continue anyway? (y/N) " >&2
  read -n 1 YESNO
  echo >&2
  if [ "x$YESNO" != xy ]; then
    exit 1
  fi
fi

case $(git rev-parse --abbrev-ref HEAD) in
  master )
    echo "On master branch.  Will generate to /next."
    CONFIG=_config_next.yml
    PREFIX=/next
    LABEL="preview site"
    FUTURE=--future
    ;;

  release-* )
    echo "On release branch.  Will generate to /."
    CONFIG=_config.yml
    PREFIX=
    LABEL="site"
    FUTURE=
    ;;

  * )
    echo "Unrecognized branch." >&2
    exit 1
    ;;
esac

echo "Regenerating site..."

rm -rf _site _site.tar.gz

jekyll build --safe $FUTURE --config $CONFIG

echo -n "Push now? (y/N)"
read -n 1 YESNO
echo

if [ "x$YESNO" == "xy" ]; then
  echo "Pushing..."
  tar cz --xform='s,_site/,,' _site/* | gce-ss ssh fe --command "cd /var/www/capnproto.org$PREFIX && tar xz"
else
  echo "Push CANCELED"
fi


#!/usr/bin/env sh
set -evx
env | sort

mkdir build || true
mkdir build/$GTEST_TARGET || true
cd build/$GTEST_TARGET
cmake -Dgtest_build_samples=ON \
      -Dgmock_build_samples=ON \
      -Dgtest_build_tests=ON \
      -Dgmock_build_tests=ON \
      -DCMAKE_CXX_FLAGS=$CXX_FLAGS \
      ../../$GTEST_TARGET
make
CTEST_OUTPUT_ON_FAILURE=1 make test


#!/bin/bash
#
# Copyright 2008, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Executes the samples and tests for the Google Test Framework.

# Help the dynamic linker find the path to the libraries.
export DYLD_FRAMEWORK_PATH=$BUILT_PRODUCTS_DIR
export DYLD_LIBRARY_PATH=$BUILT_PRODUCTS_DIR

# Create some executables.
test_executables=$@

# Now execute each one in turn keeping track of how many succeeded and failed.
succeeded=0
failed=0
failed_list=()
for test in ${test_executables[*]}; do
  "$test"
  result=$?
  if [ $result -eq 0 ]; then
    succeeded=$(( $succeeded + 1 ))
  else
    failed=$(( failed + 1 ))
    failed_list="$failed_list $test"
  fi
done

# Report the successes and failures to the console.
echo "Tests complete with $succeeded successes and $failed failures."
if [ $failed -ne 0 ]; then
  echo "The following tests failed:"
  echo $failed_list
fi
exit $failed


#!/bin/bash
#
# Copyright 2008, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Executes the samples and tests for the Google Test Framework.

# Help the dynamic linker find the path to the libraries.
export DYLD_FRAMEWORK_PATH=$BUILT_PRODUCTS_DIR
export DYLD_LIBRARY_PATH=$BUILT_PRODUCTS_DIR

# Create some executables.
test_executables=("$BUILT_PRODUCTS_DIR/gtest_unittest-framework"
                  "$BUILT_PRODUCTS_DIR/gtest_unittest"
                  "$BUILT_PRODUCTS_DIR/sample1_unittest-framework"
                  "$BUILT_PRODUCTS_DIR/sample1_unittest-static")

# Now execute each one in turn keeping track of how many succeeded and failed.
succeeded=0
failed=0
failed_list=()
for test in ${test_executables[*]}; do
  "$test"
  result=$?
  if [ $result -eq 0 ]; then
    succeeded=$(( $succeeded + 1 ))
  else
    failed=$(( failed + 1 ))
    failed_list="$failed_list $test"
  fi
done

# Report the successes and failures to the console.
echo "Tests complete with $succeeded successes and $failed failures."
if [ $failed -ne 0 ]; then
  echo "The following tests failed:"
  echo $failed_list
fi
exit $failed


#!/bin/sh

#  make_zipconf.sh: create platform specific include file zipconf.h
#  Copyright (C) 1999-2011 Dieter Baron and Thomas Klausner
#
#  This file is part of libzip, a library to manipulate ZIP archives.
#  The authors can be contacted at <libzip@nih.at>
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#  3. The names of the authors may not be used to endorse or promote
#     products derived from this software without specific prior
#     written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
#  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
#  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
#  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
#  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set -e

define_type()
{
    short=$1
    long=$2
    bytes=$3
    infile="$4"
    outfile="$5"

    bits=`expr $bytes '*' 8`
    type="${short}int${bits}"
    TYPE=`echo $type | tr '[a-z]' '[A-Z]'`
    if grep "define HAVE_${TYPE}_T" "$infile" > /dev/null
    then
	echo "typedef ${type}_t zip_${type}_t;" >> "$outfile"
	LTYPE="$TYPE"
    else
	SHORT=`echo $short | tr '[a-z]' '[A-Z]'`
	if [ -z "$SHORT" ]
	then
	    SHORT='S'
	fi
	if [ "$bytes" -eq 1 ]
	then
	    if [ -z "$long" ]
	    then
		long='signed'
	    fi
	    echo "typedef $long char ${type}_t;" >> $outfile
	    LTYPE="${SHORT}CHAR"
	else
	    ctype=`sed -n "s/.define SIZEOF_\([A-Z_]*\) $bytes/\1/p" "$infile" \
		| head -1 | tr '[A-Z_]' '[a-z ]'`
	    if [ -z "$ctype" ]
	    then
		echo "$0: no $bits bit type found" >&2
		exit 1
	    fi
	    echo "typedef $long $ctype ${type}_t;" >> "$outfile"
	    case "$ctype" in
		short) LTYPE=${SHORT}SHRT;;
		int) LTYPE=${SHORT}INT;;
                long) LTYPE=${SHORT}LONG;;
		"long long") LTYPE=${SHORT}LLONG;;
            esac
	fi
    fi

    if [ -z "$long" ]
    then
	echo "#define ZIP_${TYPE}_MIN ${LTYPE}_MIN" >> "$outfile"
    fi
    echo "#define ZIP_${TYPE}_MAX ${LTYPE}_MAX" >> "$outfile"
    echo >> "$outfile"
}

if [ "$#" -ne 2 ]
then
    echo "Usage: $0 config_h_file out_file" >&2
    echo "       e.g. $0 ../config.h zip_err_str.c" >&2
    exit 1
fi

if [ "$1" = "$2" ]
then
    echo "$0: error: output file = input file" >&2
    exit 1
fi

cat <<EOF > "$2.$$"
#ifndef _HAD_ZIPCONF_H
#define _HAD_ZIPCONF_H

/*
   zipconf.h -- platform specific include file

   This file was generated automatically by $0
   based on $1.
 */

EOF

version=`sed -n 's/^#define VERSION "\(.*\)"/\1/p' "$1"`

version_major=`expr "$version" : '^\([0-9]*\)' || true`
version_minor=`expr "$version" : '^[0-9]*\.\([0-9]*\)' || true`
version_micro=`expr "$version" : '^[0-9]*\.[0-9]\.\([0-9]*\)' || true`

if [ -z "$version_major" ]
then
    version_major=0
fi
if [ -z "$version_minor" ]
then
    version_minor=0
fi
if [ -z "$version_micro" ]
then
    version_micro=0
fi

cat <<EOF >> "$2.$$"
#define LIBZIP_VERSION "$version"
#define LIBZIP_VERSION_MAJOR $version_major
#define LIBZIP_VERSION_MINOR $version_minor
#define LIBZIP_VERSION_MICRO $version_micro

EOF

if grep 'define HAVE_INTTYPES_H' "$1" > /dev/null
then
    echo '#include <inttypes.h>' >> "$2.$$"
else
    echo '#include <limits.h>' >> "$2.$$"
fi
echo >> "$2.$$"

for size in 1 2 4 8
do
    define_type '' '' $size "$1" "$2.$$"
    define_type u unsigned $size "$1" "$2.$$"
done

echo >> "$2.$$"
echo '#endif /* zipconf.h */' >> "$2.$$"

mv "$2.$$" "$2"


#!/bin/sh

#  make_zip_err_str.sh: create zip_err_str.c from zip.h
#  Copyright (C) 1999-2014 Dieter Baron and Thomas Klausner
#
#  This file is part of libzip, a library to manipulate ZIP archives.
#  The authors can be contacted at <libzip@nih.at>
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#  3. The names of the authors may not be used to endorse or promote
#     products derived from this software without specific prior
#     written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
#  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
#  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
#  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
#  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if [ "$#" -ne 2 ]
then
    echo "Usage: $0 in_file out_file" >&2
    echo "       e.g. $0 zip.h zip_err_str.c" >&2
    exit 1
fi

if [ "$1" = "$2" ]
then
    echo "$0: error: output file = input file" >&2
    exit 1
fi

cat <<EOF >> "$2.$$" || exit 1
/*
   This file was generated automatically by $0
   from $1; make changes there.
 */

#include "zipint.h"

const char * const _zip_err_str[] = {
EOF

sed -n  '/^#define ZIP_ER_/ s/.*\/\* . \([^*]*\) \*\//    "\1",/p' "$1" \
    >> "$2.$$" || exit 1

cat <<EOF >> "$2.$$" || exit 1
};

const int _zip_nerr_str = sizeof(_zip_err_str)/sizeof(_zip_err_str[0]);

#define N ZIP_ET_NONE
#define S ZIP_ET_SYS
#define Z ZIP_ET_ZLIB

const int _zip_err_type[] = {
EOF

sed -n  '/^#define ZIP_ER_/ s/.*\/\* \(.\) \([^*]*\) \*\//    \1,/p' "$1" \
    >> "$2.$$" || exit 1

echo '};' >> "$2.$$" || exit 1

mv "$2.$$" "$2" || exit 1


#!/bin/sh

#
LINKBASE='http://pubs.opengroup.org/onlinepubs/9699919799/functions/'

sed -E -e 's,(),\1'"$LINKBASE"'\2\3,g' -e "s,$LINKBASE"'(libzip|zip),\1,g'


#!/bin/sh

#  make_zip_errrors.sh: create zip_errors.mdoc from zip.h
#  Copyright (C) 1999-2013 Dieter Baron and Thomas Klausner
#
#  This file is part of libzip, a library to manipulate ZIP archives.
#  The authors can be contacted at <libzip@nih.at>
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#  3. The names of the authors may not be used to endorse or promote
#     products derived from this software without specific prior
#     written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
#  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
#  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
#  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
#  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if [ "$#" -ne 2 ]
then
    echo "Usage: $0 in_file out_file" >&2
    echo "       e.g. $0 zip.h zip_errors.mdoc" >&2
    exit 1
fi

if [ "$1" = "$2" ]
then
    echo "$0: error: output file = input file" >&2
    exit 1
fi

date=`LC_TIME=en_US date '+%B %e, %Y' | sed 's/  / /'`

cat <<EOF >> "$2.$$" || exit 1
.\" zip_errors.mdoc -- list of all libzip error codes
.\" Copyright (C) 1999-2013 Dieter Baron and Thomas Klausner
.\"
.\" This file is part of libzip, a library to manipulate ZIP archives.
.\" The authors can be contacted at <libzip@nih.at>
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in
.\"    the documentation and/or other materials provided with the
.\"    distribution.
.\" 3. The names of the authors may not be used to endorse or promote
.\"    products derived from this software without specific prior
.\"    written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS \`\`AS IS'' AND ANY EXPRESS
.\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\"   This file was generated automatically by $0
.\"   from $1; make changes there.
.\"
.Dd $date
.Dt ZIP_ERRORS 3
.Os
.Sh NAME
.Nm zip_errors
.Nd list of all libzip error codes
.Sh LIBRARY
libzip (-lzip)
.Sh SYNOPSIS
.In zip.h
.Sh DESCRIPTION
The following error codes are used by libzip:
.Bl -tag -width XZIPXERXCOMPNOTSUPPXX
EOF

sed -n  's/^#define \(ZIP_ER_[A-Z_0-9]*\).*\/\* \(.\) \([^*]*\) \*\//.It Bq Er \1@\3./p' "$1" \
    | sort\
    | tr @ '\012' \
    >> "$2.$$" || exit 1

cat <<EOF >> "$2.$$" || exit 1
.El
.Sh AUTHORS
.An -nosplit
.An Dieter Baron Aq Mt dillo@nih.at
and
.An Thomas Klausner Aq Mt tk@giga.or.at
EOF

mv "$2.$$" "$2" || exit 1


#!/bin/sh

# Replace the value for PLIST_KEY with the resolved definition from the header file that was passed in.

SOURCE_HEADER_FILE_PATH=$1
SOURCE_PLIST_PATH=$2

PLIST_KEY="CFBundleShortVersionString"

VERSION_KEY=`/usr/libexec/PlistBuddy -c "Print :${PLIST_KEY}" "${SOURCE_PLIST_PATH}"`

#echo "Key: ${VERSION_KEY}"

VERSION_NUM=`cat "${SOURCE_HEADER_FILE_PATH}" | sed -n "s|#define ${VERSION_KEY} \"\(.*\)\".*|\1|p"`

#echo "Value: ${VERSION_NUM}"

TARGET_PLIST_PATH="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"

/usr/libexec/PlistBuddy -c "Set :${PLIST_KEY} ${VERSION_NUM}" "${TARGET_PLIST_PATH}"


#!/bin/sh

DIR=tmp.$$

mkdir -p $DIR/32 $DIR/64

(cd $DIR/32; ../../../configure CFLAGS=-m32)
(cd $DIR/64; ../../../configure CFLAGS=-m64)

diff -D __LP64__ $DIR/32/config.h $DIR/64/config.h > config.h

rm -r $DIR


#!/bin/bash
#
# Script to check include/test code for common pybind11 code style errors.
#
# This script currently checks for
#
# 1. use of tabs instead of spaces
# 2. MSDOS-style CRLF endings
# 3. trailing spaces
# 4. missing space between keyword and parenthesis, e.g.: for(, if(, while(
# 5. Missing space between right parenthesis and brace, e.g. 'for (...){'
# 6. opening brace on its own line. It should always be on the same line as the
#    if/while/for/do statement.
#
# Invoke as: tools/check-style.sh
#

check_style_errors=0
IFS=$'\n'

found="$( GREP_COLORS='mt=41' GREP_COLOR='41' grep $'\t' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )"
if [ -n "$found" ]; then
    # The mt=41 sets a red background for matched tabs:
    echo -e '\033[31;01mError: found tab characters in the following files:\033[0m'
    check_style_errors=1
    echo "$found" | sed -e 's/^/    /'
fi

found="$( grep -IUlr $'\r' include tests/*.{cpp,py,h} docs/*.rst --color=always )"
if [ -n "$found" ]; then
    echo -e '\033[31;01mError: found CRLF characters in the following files:\033[0m'
    check_style_errors=1
    echo "$found" | sed -e 's/^/    /'
fi

found="$(GREP_COLORS='mt=41' GREP_COLOR='41' grep '[[:blank:]]\+$' include tests/*.{cpp,py,h} docs/*.rst -rn --color=always )"
if [ -n "$found" ]; then
    # The mt=41 sets a red background for matched trailing spaces
    echo -e '\033[31;01mError: found trailing spaces in the following files:\033[0m'
    check_style_errors=1
    echo "$found" | sed -e 's/^/    /'
fi

found="$(grep '\<\(if\|for\|while\|catch\)(\|){' include tests/*.{cpp,h} -rn --color=always)"
if [ -n "$found" ]; then
    echo -e '\033[31;01mError: found the following coding style problems:\033[0m'
    check_style_errors=1
    echo "$found" | sed -e 's/^/    /'
fi

found="$(awk '
function prefix(filename, lineno) {
    return "    \033[35m" filename "\033[36m:\033[32m" lineno "\033[36m:\033[0m"
}
function mark(pattern, string) { sub(pattern, "\033[01;31m&\033[0m", string); return string }
last && /^\s*{/ {
    print prefix(FILENAME, FNR-1) mark("\\)\\s*$", last)
    print prefix(FILENAME, FNR)   mark("^\\s*{", $0)
    last=""
}
{ last = /(if|for|while|catch|switch)\s*\(.*\)\s*$/ ? $0 : "" }
' $(find include -type f) tests/*.{cpp,h} docs/*.rst)"
if [ -n "$found" ]; then
    check_style_errors=1
    echo -e '\033[31;01mError: braces should occur on the same line as the if/while/.. statement. Found issues in the following files:\033[0m'
    echo "$found"
fi

exit $check_style_errors


#!/bin/bash

export NVIDIA_DOCKER=${NVIDIA_DOCKER:-"nvidia-docker"}
export COMPO_RESULTS=$(realpath $(dirname -- $0))/results

mkdir -p $COMPO_RESULTS

alias \
    docker-retro-contest-agent="\$NVIDIA_DOCKER run --rm -v compo-tmp-vol:/root/compo/tmp agent retro-contest-agent" \
    docker-retro-contest-remote="docker run --rm -v compo-tmp-vol:/root/compo/tmp -v \$COMPO_RESULTS:/root/compo/results remote-env retro-contest-remote"


#!/bin/bash

DATA_PATH=$(python -c 'import retro; print(retro.data.path(retro.data.DATA_PATH))')
echo "Importing games from $DATA_PATH..."
CONTAINER_ID=$(docker run -v "$DATA_PATH":/root/roms:ro -d remote-env 'python /tmp/gym-retro/scripts/import.py /root/roms')
docker attach $CONTAINER_ID
docker commit $CONTAINER_ID remote-env:full
docker rm $CONTAINER_ID


#!/bin/sh

cd `dirname $0`

try_build () {
	docker build -t $@ || exit $?
}

echo - Pulling base images
docker pull -a openai/retro-agent
docker pull -a openai/retro-env
if [ "$1" = "rebuild" ]; then
	echo - Building base CPU image
	try_build openai/retro-agent:bare           --build-arg BASE=ubuntu --build-arg TAG= --pull -f agent.docker ..
	echo - Building base CUDA images
	try_build openai/retro-agent:bare-cuda8     --build-arg CUDA=8.0 --build-arg CUDNN=6 --pull -f agent.docker ..
	try_build openai/retro-agent:bare-cuda9     --build-arg CUDA=9.0 --build-arg CUDNN=7 --pull -f agent.docker ..
	echo - Building base TensorFlow images
	try_build openai/retro-agent:tensorflow-1.4 --build-arg CUDA=8 --build-arg TF=1.4.1        - < agent-tf.docker
	try_build openai/retro-agent:tensorflow-1.5 --build-arg CUDA=9 --build-arg TF=1.5.1        - < agent-tf.docker
	try_build openai/retro-agent:tensorflow-1.7 --build-arg CUDA=9 --build-arg TF=1.7.1        - < agent-tf.docker
	try_build openai/retro-agent:tensorflow-1.8 --build-arg CUDA=9 --build-arg TF=1.8.0        - < agent-tf.docker
	echo - Building base PyTorch images
	try_build openai/retro-agent:pytorch-0.3    --build-arg PYTORCH=0.3.1                      - < agent-pytorch.docker
	try_build openai/retro-agent:pytorch-0.4    --build-arg PYTORCH=0.4.0                      - < agent-pytorch.docker
	echo - Building remote image
	try_build openai/retro-env -f remote-env.docker ..
fi
if [ -n "$(ls ../roms)" ]; then
	echo - Building remote image with ROMs
	docker tag openai/retro-env openai/retro-env:bare
	try_build openai/retro-env -f remote-env-roms.docker ..
fi
echo - Tagging images
docker tag openai/retro-agent:tensorflow-1.8     openai/retro-agent:tensorflow-latest
docker tag openai/retro-agent:tensorflow-latest  openai/retro-agent:tensorflow
docker tag openai/retro-agent:pytorch-0.4        openai/retro-agent:pytorch
docker tag openai/retro-agent:tensorflow         openai/retro-agent:latest
docker tag openai/retro-agent agent

echo - Installing Python library
pip3 install -e '../support[docker,rest]'


#!/bin/bash
set -ex

if [ $(uname) == 'Linux' ]; then
    docker run -v $PWD:/io python:${MB_PYTHON_VERSION} /io/docker_build_wheel.sh
fi

if [ $(uname) == 'Darwin' ]; then
    ./osx_build_wheel.sh
fi


#!/bin/bash
function pre_build {
    echo "Hello world (this should not be run)"
}

function run_tests {
    echo "Hello world (this should not be run)"
}


# !/bin/bash
set -ex
# ** HACK - rename linux wheels to manylinux1 wheels
if [ $(uname) == 'Linux' ]; then
    cd wheelhouse
    for whl in $(ls roboschool-*linux*.whl); do
        echo "HACK!!! Renaming $whl --> ${whl%linux_x86_64.whl}manylinux1_x86_64.whl"
        sudo mv $whl ${whl%linux_x86_64.whl}manylinux1_x86_64.whl
    done
    cd ..
    pip install --user bleach==3.0.2
    pip install --user twine==1.12.1
fi
# ** END HACK

if [ $(uname) == 'Darwin' ]; then
    export PATH=$PWD/venv/bin:$PATH
    pip install twine
fi

if [[ ! -z "$TRAVIS_TAG" ]]; then
    python -m twine upload wheelhouse/*

    if [[ ! -z "$DEPLOY_SDIST" ]]; then
        python setup.py sdist
        python -m twine upload dist/*
    fi
fi


#!/bin/bash
set -ex
apt-get update && apt-get install -y libgl1-mesa-dev qtbase5-dev libqt5opengl5-dev libassimp-dev patchelf cmake > /dev/null

cd $(dirname "$0")
. ./exports.sh

./install_boost.sh
./install_bullet.sh
./roboschool_compile_and_graft.sh

pip wheel --no-deps -w /io/wheelhouse .


#!/bin/bash
set -ex
apt-get update && apt-get install -y libgl1-mesa-dev > /dev/null
pip install /io/wheelhouse/roboschool*.whl

python -c "import roboschool, gym; gym.make('RoboschoolAnt-v1')"


export ROBOSCHOOL_PATH=$( cd "$(dirname "$BASH_SOURCE")" ; pwd -P )
export CPP_HOUSEHOLD=$ROBOSCHOOL_PATH/roboschool/cpp-household
export BULLET_PATH=$HOME/.forked_bullet

if [[ $(uname) == 'Linux' ]]; then
    PYTHON_BIN=$(readlink -f $(which python))
    PYTHON_ROOT=${PYTHON_BIN%/bin/python*}
    PYTHON_VER=${PYTHON_BIN#$PYTHON_ROOT/bin/python}
fi
if [[ $(uname) == 'Darwin' ]]; then
    brew install coreutils
    PYTHON_BIN=$(greadlink -f $(which python))
    PYTHON_ROOT=${PYTHON_BIN%/bin/python*}
    PYTHON_VER=${PYTHON_BIN#$PYTHON_ROOT/bin/python}
    export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opt/qt/lib/pkgconfig:/Library/Frameworks/Python.framework/Versions/${PYTHON_VER}/lib/pkgconfig/
fi

export PKG_CONFIG_PATH=${PYTHON_ROOT}/lib/pkgconfig:$BULLET_PATH/lib/pkgconfig/:$PKG_CONFIG_PATH
export CPATH=$CPATH:${PYTHON_ROOT}/include/python${PYTHON_VER}m:$HOME/.boost/include
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$BULLET_PATH/lib:$HOME/.boost/lib


#!/bin/bash
if [ $(uname) == 'Linux' ]; then
    source ./exports.sh
    BOOST_SRCDIR=$HOME/.boost_src
    mkdir -p $BOOST_SRCDIR && cd $BOOST_SRCDIR
    # curl -OL https://storage.googleapis.com/games-src/boost/boost_1_58_0.tar.bz2
    curl -OL https://dl.bintray.com/boostorg/release/1.69.0/source/boost_1_69_0.tar.gz
    tar -xf boost_1_69_0.tar.gz
    cd boost_1_69_0

     ./bootstrap.sh --with-python=$(which python) --with-libraries=python --prefix=$HOME/.boost --with-python-root=$PYTHON_ROOT
     ./b2 install > $BOOST_SRCDIR/boost_make.log || (tail -100 $BOOST_SRCDIR/boost_make.log; exit 1)
fi


#!/bin/bash

BULLET_SRCDIR=$(mktemp -d)/bullet3
BULLET_PATH=$HOME/.forked_bullet

rm -rf $BULLET_SRCDIR
mkdir -p $BULLET_SRCDIR && cd $BULLET_SRCDIR
git clone https://github.com/olegklimov/bullet3 -b roboschool_self_collision .

mkdir build && cd build
cmake -DBUILD_SHARED_LIBS=ON -DUSE_DOUBLE_PRECISION=1 -DCMAKE_INSTALL_PREFIX:PATH=$BULLET_PATH -DBUILD_CPU_DEMOS=OFF -DBUILD_BULLET2_DEMOS=OFF -DBUILD_EXTRAS=OFF  -DBUILD_UNIT_TESTS=OFF -DBUILD_CLSOCKET=OFF -DBUILD_ENET=OFF -DBUILD_OPENGL3_DEMOS=OFF ..

make -j4 > /tmp/bullet_make.log || (tail -100 /tmp/bullet_make.log; exit 1)
make install > /tmp/bullet_install.log || (tail -100 /tmp/bullet_install.log; exit 1)

if [ $(uname) == 'Darwin' ]; then

    for lib in $(find $BULLET_PATH/lib -name "*.dylib"); do
        install_name_tool -id $lib $lib
        for dep in $(otool -L $lib | grep "@rpath" | awk '{print $1}'); do
            install_name_tool -change $dep "$BULLET_PATH/lib/${dep##@rpath/}" $lib
        done
    done
fi
if [ $(uname) == 'Linux' ]; then

    for lib in $(find $BULLET_PATH/lib -name "*.so.2.87"); do
        patchelf --set-rpath $BULLET_PATH/lib $lib
    done
fi


#!/bin/bash

git clone https://github.com/matthew-brett/multibuild && cd multibuild && git checkout 254ad28 && cd ..
. multibuild/common_utils.sh
. multibuild/travis_steps.sh
before_install

brew install qt assimp boost-python3 || echo "warning - brew returned non-zero exit code. \nThis may be a sign of error, but may also be because brew wants to point something out. Please inspect logs above"

. ./exports.sh
./install_bullet.sh
./roboschool_compile_and_graft.sh
pip wheel --no-deps -w wheelhouse .


#!/bin/bash
set -ex
. multibuild/common_utils.sh
. multibuild/travis_steps.sh
before_install

pip install wheelhouse/roboschool*.whl

python -c "import roboschool, gym; gym.make('RoboschoolAnt-v1')"


#!/bin/bash
set -e

function graft_libs {
    local libfile=$1
    local libdir=$(dirname $libfile)
    if [ $(uname) == 'Darwin' ]; then
        local relink="osx_relink"
        local origin="@loader_path"
        local deps=$(otool -L $libfile | awk 'FNR>2 {print $1}')
    fi
    if [ $(uname) == 'Linux' ]; then
        local relink="linux_relink"
        local library_lister="ldd"
        local origin="\$ORIGIN"
        local deps=$(ldd $libfile | awk '{print $3}')
    fi

    local patterns=${@:3}

    graft_dir=$2
    mkdir -p $graft_dir

    for dep in $deps; do
        # echo $dep
        for deppattern in $patterns; do
            if [[ "$dep" =~ $deppattern ]]; then
                new_depname=${dep##*/}
                new_deppath="$graft_dir/$new_depname"
                rel_path=$(realpath --relative-to="$libdir" $new_deppath)
                new_dep="$origin/$rel_path"
                echo "$libfile depends on $dep, relinking to $new_deppath ($new_dep)"
                $relink $dep $new_dep $libfile
                if [ ! -f $new_deppath ]; then
                    echo "$new_deppath not found, copying and calling self"
                    cp $dep $new_deppath
                    chmod 777 $new_deppath
                    graft_libs $new_deppath $graft_dir $patterns
                else
                    echo "grafted library $new_deppath already exists"
                fi
            fi
        done
    done
}

function osx_relink {
    install_name_tool -change $1 $2 $3
}

function linux_relink {
    # local depname=${2#*/}
    local dep_dir=${2%/*}
    patchelf --set-rpath $dep_dir $3
}

cd $(dirname "$0")

cd roboschool/cpp-household
# make clean
make -j4
cd ..

graft_libs cpp_household.so .libs \
    ^/.+/libboost_python.+ \
    ^/.+Qt.+ \
    ^/.+/libassimp.+ \
    ^/.+/libLinearMath.+ \
    ^/.+/libBullet.+ \
    ^/.+/libPhysicsClientC_API.+ \
    ^/.+/libjpeg.+ \
    ^/.+/libpng.+ \
    ^/.+/libicu.+ \
    ^/.+/libpcre.+ \
    ^/.+/libdouble-conversion.+ \
    ^/.+/libminizip.+

if [ $(uname) == 'Darwin' ]; then
    # HACK - this should auto-detect plugins dir
    cp -r /usr/local/Cellar/qt/5.10.1/plugins .qt_plugins
    lib_pattern="*.dylib"
fi
if [ $(uname) == 'Linux' ]; then
    cp -r /usr/lib/x86_64-linux-gnu/qt5/plugins .qt_plugins
    lib_pattern="*.so*"
fi
for lib in $(find .qt_plugins -name "$lib_pattern"); do
     graft_libs $lib .libs ^/.+Qt.+ \
                ^/.+/libjpeg.+ \
                ^/.+/libpng.+ \
                ^/.+/libicu.+ \
                ^/.+/libpcre.+ \
                ^/.+/libdouble-conversion.+ \
                ^/.+/libminizip.+ \
                ^/.+/libxcb.+ \
                ^/.+/libxkb.+
done


#!/bin/bash
set -ex

if [ $(uname) == 'Linux' ]; then
    docker run -v $PWD:/io python:${MB_PYTHON_VERSION} /io/docker_test_wheel.sh
fi

if [ $(uname) == 'Darwin' ]; then
    ./osx_test_wheel.sh
fi


#!/usr/bin/env bash

# Copyright 2014 The Kubernetes Authors.
#
# Licensed 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.

# This script is convenience to download and install etcd in third_party.
# Mostly just used by CI.
# Usage: `hack/install-etcd.sh`.

set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${SCRIPT_ROOT}/hack/lib/init.sh"

kube::etcd::install