Scripts to process nacl source directory.

This commit is contained in:
Daniel O'Connor 2012-02-24 17:11:08 +10:30
parent 6d3e720784
commit b053386b4f
3 changed files with 248 additions and 0 deletions

86
nacl/nacl-gcc-prep Executable file
View File

@ -0,0 +1,86 @@
#!/bin/sh
if [ ! -e nacl-version ]; then
echo "Can't find nacl-version file"
exit 1
fi
nacl_version=`cat nacl-version`
# See if we already have a library built
arlist=`find "${nacl_version}/build" -name libnacl.a`
arcount=`echo $arlist | wc -w`
if [ $arcount -eq 0 ]; then
echo "No library found, building (go get a coffee)"
(cd ${nacl_version} ; ./do )
# Regen list
arlist=`find "${nacl_version}/build" -name libnacl.a`
fi
# Test which lib works
cat <<EOF >test.c
#include <stdio.h>
#include "crypto_box.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
/* it's really stupid that there isn't a syscall for this */
static int fd = -1;
void randombytes(unsigned char *x,unsigned long long xlen)
{
int i;
if (fd == -1) {
for (;;) {
fd = open("/dev/urandom",O_RDONLY);
if (fd != -1) break;
sleep(1);
}
}
while (xlen > 0) {
if (xlen < 1048576) i = xlen; else i = 1048576;
i = read(fd,x,i);
if (i < 1) {
sleep(1);
continue;
}
x += i;
xlen -= i;
}
}
unsigned char pk[crypto_box_PUBLICKEYBYTES];
unsigned char sk[crypto_box_SECRETKEYBYTES];
int main(int argc,char **argv) {
crypto_box_keypair(pk,sk);
printf("The compilation worked.\n");
}
EOF
for ar in $arlist; do
arch=`basename \`dirname $ar\``
incpath=`dirname $ar`/../../include/$arch
rm -f test
gcc -o test test.c $ar -I$incpath >/dev/null 2>&1
if [ -e test ]; then
naclbuilddir=`echo $ar | sed -e s,/libnacl.a,,`
echo "${incpath}" >naclinc.txt
echo "${ar}" >nacllib.txt
break
fi
done
rm -f test test.c
# Create nacl.h for lazy programmers
(cd ${incpath} ; find . -name \*.h -a \! -name mphlr.h -a \! -name nacl.h | sed -Ee 's,\./(.*),#include <\1>,') >${incpath}/nacl.h

161
nacl/nacl-prepare-sources Executable file
View File

@ -0,0 +1,161 @@
#!/bin/sh
# Prepare NaCl source files and include files for compilation with Serval DNA,
# possibly using the Android NDK to produce a JNI compatible library.
# Android and JNI complicate matters substantially, and prevent us just building
# the .a the way that NaCl was intended. The cross compilation environment
# will play havoc for a start.
# Derived from:
# nacl/do
# D. J. Bernstein
# Public domain.
if [ ! -e OPERATIONS ]; then
echo "OPERATIONS file not found, are you in the right directory?"
exit 1
fi
project=nacl
work=build_android
# and work around bug in GNU sort
LANG=C
export LANG
rm -rf "$work"
mkdir -p "$work"
# PGS - Create a couple of missing files needed
cp randombytes/devurandom.h ${work}/randombytes.h
cat <<EOF > ${work}/crypto_uint32.h
#ifndef CRYPTO_UINT32
#define CRYPTO_UINT32
typedef unsigned int crypto_uint32;
#endif
EOF
# loop over operations
cat OPERATIONS \
| while read o
do
[ -d "$o" ] || continue
selected=''
[ -f "$o/selected" ] && selected=`cat "$o/selected"`
# for each operation, loop over primitives
ls "$o" \
| sort \
| while read p
do
[ -d "$o/$p" ] || continue
expectedchecksum=''
[ -f "$o/$p/checksum" ] && expectedchecksum=`cat "$o/$p/checksum"`
op="${o}_${p}"
startdate=`date +%Y%m%d`
# for each operation primitive abi, loop over implementations
find "$o/$p" -follow -name "api.h" \
| grep /ref/ \
| sort \
| while read doth
do
implementationdir=`dirname $doth`
opi=`echo "$implementationdir" | tr ./- ___`
echo "=== `date` === $abi $implementationdir"
cfiles=`ls "$implementationdir" | grep '\.c$' || :`
sfiles=`ls "$implementationdir" | grep '\.[sS]$' || :`
cppfiles=`ls "$o" | grep '\.cpp$' || :`
mkdir -p "${work}/${opi}"
cp -p "$o"/*.c "$work/${opi}"
cp -pr "$implementationdir"/* "$work/${opi}"
rm ${work}/${opi}/measure.c
rm ${work}/${opi}/try.c
cp -p MACROS "$work/MACROS"
cp -p PROTOTYPES.c "$work/PROTOTYPES.c"
cp -p PROTOTYPES.cpp "$work/PROTOTYPES.cpp"
cp $implementationdir/api.h "$work"
(
cd "$work"
(
echo "#ifndef ${o}_H"
echo "#define ${o}_H"
echo ""
echo "#include \"${op}.h\""
echo ""
egrep "${o}"'$|'"${o}"'\(|'"${o}"'_' < MACROS \
| sed "s/$o/$op/" | while read mop
do
echo "#define ${mop} ${mop}" | sed "s/$op/$o/"
echo "/* CHEESEBURGER ${mop} */"
done
echo "#define ${o}_PRIMITIVE \"${p}\""
echo "#define ${o}_IMPLEMENTATION ${op}_IMPLEMENTATION"
echo "#define ${o}_VERSION ${op}_VERSION"
echo ""
echo "#endif"
) > "${opi}/$o.h"
(
echo "#ifndef ${op}_H"
echo "#define ${op}_H"
echo ""
sed 's/[ ]CRYPTO_/ '"${opi}"'_/g' < api.h
echo '#ifdef __cplusplus'
echo '#include <string>'
egrep "${o}"'$|'"${o}"'\(|'"${o}"'_' < PROTOTYPES.cpp \
| sed "s/$o/$opi/"
echo 'extern "C" {'
echo '#endif'
egrep "${o}"'$|'"${o}"'\(|'"${o}"'_' < PROTOTYPES.c \
| sed "s/$o/$opi/"
echo '#ifdef __cplusplus'
echo '}'
echo '#endif'
echo ""
egrep "${o}"'$|'"${o}"'\(|'"${o}"'_' < MACROS \
| sed "s/$o/$opi/" | while read mopi
do
echo "#define ${mopi} ${mopi}" | sed "s/$opi/$op/"
echo "/* POTATO ${mopi} $opi $op */"
done
echo "#define ${op}_IMPLEMENTATION \"${implementationdir}\""
echo "#ifndef ${opi}_VERSION"
echo "#define ${opi}_VERSION \"-\""
echo "#endif"
echo "#define ${op}_VERSION ${opi}_VERSION"
echo ""
echo "#endif"
) > "$op.h"
cd ..
)
done
echo "=== `date` === $abi $o/$p measuring"
done
done
# PGS - Remove testing files that we don't care about.
rm -f ${work}/measure.c ${work}/try.c
# DOC - Create nacl.h for lazy programmers
(cd ${work} ; find . -name \*.h -a \! -name mphlr.h -a \! -name nacl.h | sed -Ee 's,\./(.*),#include <\1>,') >${work}/nacl.h
# DOC - Create source list suitable for inclusion into a makefile
echo "NACL_SOURCES := \\" >${work}/sources.mk
(cd ${work} ; find . -mindepth 2 -name \*.c | sed -Ee 's,\./(.*),${NACL_BASE}/\1,' | sort | xargs echo) >>${work}/sources.mk
echo "=== `date` === finishing"

1
nacl/nacl-version Normal file
View File

@ -0,0 +1 @@
nacl-20110221