libsodium/dist-build/emscripten.sh

185 lines
30 KiB
Bash
Raw Normal View History

#! /bin/sh
export MAKE_FLAGS='-j4'
2018-12-25 06:50:13 -05:00
export EXPORTED_FUNCTIONS_STANDARD='["_malloc","_free","_crypto_aead_chacha20poly1305_abytes","_crypto_aead_chacha20poly1305_decrypt","_crypto_aead_chacha20poly1305_decrypt_detached","_crypto_aead_chacha20poly1305_encrypt","_crypto_aead_chacha20poly1305_encrypt_detached","_crypto_aead_chacha20poly1305_ietf_abytes","_crypto_aead_chacha20poly1305_ietf_decrypt","_crypto_aead_chacha20poly1305_ietf_decrypt_detached","_crypto_aead_chacha20poly1305_ietf_encrypt","_crypto_aead_chacha20poly1305_ietf_encrypt_detached","_crypto_aead_chacha20poly1305_ietf_keybytes","_crypto_aead_chacha20poly1305_ietf_keygen","_crypto_aead_chacha20poly1305_ietf_messagebytes_max","_crypto_aead_chacha20poly1305_ietf_npubbytes","_crypto_aead_chacha20poly1305_ietf_nsecbytes","_crypto_aead_chacha20poly1305_keybytes","_crypto_aead_chacha20poly1305_keygen","_crypto_aead_chacha20poly1305_messagebytes_max","_crypto_aead_chacha20poly1305_npubbytes","_crypto_aead_chacha20poly1305_nsecbytes","_crypto_aead_xchacha20poly1305_ietf_abytes","_crypto_aead_xchacha20poly1305_ietf_decrypt","_crypto_aead_xchacha20poly1305_ietf_decrypt_detached","_crypto_aead_xchacha20poly1305_ietf_encrypt","_crypto_aead_xchacha20poly1305_ietf_encrypt_detached","_crypto_aead_xchacha20poly1305_ietf_keybytes","_crypto_aead_xchacha20poly1305_ietf_keygen","_crypto_aead_xchacha20poly1305_ietf_messagebytes_max","_crypto_aead_xchacha20poly1305_ietf_npubbytes","_crypto_aead_xchacha20poly1305_ietf_nsecbytes","_crypto_auth","_crypto_auth_bytes","_crypto_auth_keybytes","_crypto_auth_keygen","_crypto_auth_verify","_crypto_box_beforenm","_crypto_box_beforenmbytes","_crypto_box_curve25519xchacha20poly1305_messagebytes_max","_crypto_box_detached","_crypto_box_detached_afternm","_crypto_box_easy","_crypto_box_easy_afternm","_crypto_box_keypair","_crypto_box_macbytes","_crypto_box_messagebytes_max","_crypto_box_noncebytes","_crypto_box_open_detached","_crypto_box_open_detached_afternm","_crypto_box_open_easy","_crypto_box_open_easy_afternm","_crypto_box_publickeybytes","_crypto_box_seal","_crypto_box_seal_open","_crypto_box_sealbytes","_crypto_box_secretkeybytes","_crypto_box_seed_keypair","_crypto_box_seedbytes","_crypto_generichash","_crypto_generichash_bytes","_crypto_generichash_bytes_max","_crypto_generichash_bytes_min","_crypto_generichash_final","_crypto_generichash_init","_crypto_generichash_keybytes","_crypto_generichash_keybytes_max","_crypto_generichash_keybytes_min","_crypto_generichash_keygen","_crypto_generichash_statebytes","_crypto_generichash_update","_crypto_hash","_crypto_hash_bytes","_crypto_kdf_bytes_max","_crypto_kdf_bytes_min","_crypto_kdf_contextbytes","_crypto_kdf_derive_from_key","_crypto_kdf_keybytes","_crypto_kdf_keygen","_crypto_kx_client_session_keys","_crypto_kx_keypair","_crypto_kx_publickeybytes","_crypto_kx_secretkeybytes","_crypto_kx_seed_keypair","_crypto_kx_seedbytes","_crypto_kx_server_session_keys","_crypto_kx_sessionkeybytes","_crypto_pwhash","_crypto_pwhash_alg_argon2i13","_crypto_pwhash_alg_argon2id13","_crypto_pwhash_alg_default","_crypto_pwhash_bytes_max","_crypto_pwhash_bytes_min","_crypto_pwhash_memlimit_interactive","_crypto_pwhash_memlimit_max","_crypto_pwhash_memlimit_min","_crypto_pwhash_memlimit_moderate","_crypto_pwhash_memlimit_sensitive","_crypto_pwhash_opslimit_interactive","_crypto_pwhash_opslimit_max","_crypto_pwhash_opslimit_min","_crypto_pwhash_opslimit_moderate","_crypto_pwhash_opslimit_sensitive","_crypto_pwhash_passwd_max","_crypto_pwhash_passwd_min","_crypto_pwhash_saltbytes","_crypto_pwhash_str","_crypto_pwhash_str_alg","_crypto_pwhash_str_needs_rehash","_crypto_pwhash_str_verify","_crypto_pwhash_strbytes","_crypto_pwhash_strprefix","_crypto_scalarmult","_crypto_scalarmult_base","_crypto_scalarmult_bytes","_crypto_scalarmult_scalarbytes","_crypto_secretbox_detached","_crypto_secretbox_easy","_crypto_secretbox_keybytes","_crypto_secretbox_keygen","_crypto_secretbox_macbytes","_crypto_secretbox_messagebytes_max","_crypto_secretbox_noncebytes","_crypto_secretbox_open_detached","_crypto_secretbox_open_easy","_crypto_secretstream_xchacha20poly1305
export EXPORTED_FUNCTIONS_SUMO='["_malloc","_free","_crypto_aead_chacha20poly1305_abytes","_crypto_aead_chacha20poly1305_decrypt","_crypto_aead_chacha20poly1305_decrypt_detached","_crypto_aead_chacha20poly1305_encrypt","_crypto_aead_chacha20poly1305_encrypt_detached","_crypto_aead_chacha20poly1305_ietf_abytes","_crypto_aead_chacha20poly1305_ietf_decrypt","_crypto_aead_chacha20poly1305_ietf_decrypt_detached","_crypto_aead_chacha20poly1305_ietf_encrypt","_crypto_aead_chacha20poly1305_ietf_encrypt_detached","_crypto_aead_chacha20poly1305_ietf_keybytes","_crypto_aead_chacha20poly1305_ietf_keygen","_crypto_aead_chacha20poly1305_ietf_messagebytes_max","_crypto_aead_chacha20poly1305_ietf_npubbytes","_crypto_aead_chacha20poly1305_ietf_nsecbytes","_crypto_aead_chacha20poly1305_keybytes","_crypto_aead_chacha20poly1305_keygen","_crypto_aead_chacha20poly1305_messagebytes_max","_crypto_aead_chacha20poly1305_npubbytes","_crypto_aead_chacha20poly1305_nsecbytes","_crypto_aead_xchacha20poly1305_ietf_abytes","_crypto_aead_xchacha20poly1305_ietf_decrypt","_crypto_aead_xchacha20poly1305_ietf_decrypt_detached","_crypto_aead_xchacha20poly1305_ietf_encrypt","_crypto_aead_xchacha20poly1305_ietf_encrypt_detached","_crypto_aead_xchacha20poly1305_ietf_keybytes","_crypto_aead_xchacha20poly1305_ietf_keygen","_crypto_aead_xchacha20poly1305_ietf_messagebytes_max","_crypto_aead_xchacha20poly1305_ietf_npubbytes","_crypto_aead_xchacha20poly1305_ietf_nsecbytes","_crypto_auth","_crypto_auth_bytes","_crypto_auth_hmacsha256","_crypto_auth_hmacsha256_bytes","_crypto_auth_hmacsha256_final","_crypto_auth_hmacsha256_init","_crypto_auth_hmacsha256_keybytes","_crypto_auth_hmacsha256_keygen","_crypto_auth_hmacsha256_statebytes","_crypto_auth_hmacsha256_update","_crypto_auth_hmacsha256_verify","_crypto_auth_hmacsha512","_crypto_auth_hmacsha512256","_crypto_auth_hmacsha512256_bytes","_crypto_auth_hmacsha512256_final","_crypto_auth_hmacsha512256_init","_crypto_auth_hmacsha512256_keybytes","_crypto_auth_hmacsha512256_keygen","_crypto_auth_hmacsha512256_statebytes","_crypto_auth_hmacsha512256_update","_crypto_auth_hmacsha512256_verify","_crypto_auth_hmacsha512_bytes","_crypto_auth_hmacsha512_final","_crypto_auth_hmacsha512_init","_crypto_auth_hmacsha512_keybytes","_crypto_auth_hmacsha512_keygen","_crypto_auth_hmacsha512_statebytes","_crypto_auth_hmacsha512_update","_crypto_auth_hmacsha512_verify","_crypto_auth_keybytes","_crypto_auth_keygen","_crypto_auth_primitive","_crypto_auth_verify","_crypto_box","_crypto_box_afternm","_crypto_box_beforenm","_crypto_box_beforenmbytes","_crypto_box_boxzerobytes","_crypto_box_curve25519xchacha20poly1305_beforenm","_crypto_box_curve25519xchacha20poly1305_beforenmbytes","_crypto_box_curve25519xchacha20poly1305_detached","_crypto_box_curve25519xchacha20poly1305_detached_afternm","_crypto_box_curve25519xchacha20poly1305_easy","_crypto_box_curve25519xchacha20poly1305_easy_afternm","_crypto_box_curve25519xchacha20poly1305_keypair","_crypto_box_curve25519xchacha20poly1305_macbytes","_crypto_box_curve25519xchacha20poly1305_messagebytes_max","_crypto_box_curve25519xchacha20poly1305_noncebytes","_crypto_box_curve25519xchacha20poly1305_open_detached","_crypto_box_curve25519xchacha20poly1305_open_detached_afternm","_crypto_box_curve25519xchacha20poly1305_open_easy","_crypto_box_curve25519xchacha20poly1305_open_easy_afternm","_crypto_box_curve25519xchacha20poly1305_publickeybytes","_crypto_box_curve25519xchacha20poly1305_seal","_crypto_box_curve25519xchacha20poly1305_seal_open","_crypto_box_curve25519xchacha20poly1305_sealbytes","_crypto_box_curve25519xchacha20poly1305_secretkeybytes","_crypto_box_curve25519xchacha20poly1305_seed_keypair","_crypto_box_curve25519xchacha20poly1305_seedbytes","_crypto_box_curve25519xsalsa20poly1305","_crypto_box_curve25519xsalsa20poly1305_afternm","_crypto_box_curve25519xsalsa20poly1305_beforenm","_crypto_box_curve25519xsalsa20poly1305_beforenmbytes","_crypto_box_curve25519xsalsa20poly1305_boxzerobytes","_crypto_box_curve25519xsalsa20poly1305_keypair","_crypto_box_curve25519xsalsa20poly1305_macbytes","_crypto_box_c
export EXPORTED_RUNTIME_METHODS='["Pointer_stringify","getValue","setValue"]'
export TOTAL_MEMORY=16777216
export TOTAL_MEMORY_SUMO=83886080
export TOTAL_MEMORY_TESTS=167772160
2016-04-17 16:45:37 -04:00
export LDFLAGS="-s RESERVED_FUNCTION_POINTERS=8"
export LDFLAGS="${LDFLAGS} -s SINGLE_FILE=1"
2017-12-26 22:05:08 -05:00
export LDFLAGS="${LDFLAGS} -s ASSERTIONS=0"
export LDFLAGS="${LDFLAGS} -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s ALIASING_FUNCTION_POINTERS=1"
export LDFLAGS="${LDFLAGS} -s DISABLE_EXCEPTION_CATCHING=1"
export LDFLAGS="${LDFLAGS} -s ELIMINATE_DUPLICATE_FUNCTIONS=1"
export CFLAGS="-Os"
2016-04-17 16:45:37 -04:00
echo
if [ "x$1" = "x--standard" ]; then
export EXPORTED_FUNCTIONS="$EXPORTED_FUNCTIONS_STANDARD"
export LDFLAGS="${LDFLAGS} ${LDFLAGS_DIST} -s TOTAL_MEMORY=${TOTAL_MEMORY}"
export PREFIX="$(pwd)/libsodium-js"
export DONE_FILE="$(pwd)/js.done"
export CONFIG_EXTRA="--enable-minimal"
2016-04-17 16:45:37 -04:00
export DIST='yes'
echo "Building a standard distribution in [${PREFIX}]"
2016-04-17 16:45:37 -04:00
elif [ "x$1" = "x--sumo" ]; then
export EXPORTED_FUNCTIONS="$EXPORTED_FUNCTIONS_SUMO"
2016-04-17 16:45:37 -04:00
export LDFLAGS="${LDFLAGS} ${LDFLAGS_DIST} -s TOTAL_MEMORY=${TOTAL_MEMORY_SUMO}"
export PREFIX="$(pwd)/libsodium-js-sumo"
2016-04-17 16:45:37 -04:00
export DONE_FILE="$(pwd)/js-sumo.done"
export DIST='yes'
echo "Building a sumo distribution in [${PREFIX}]"
2016-04-17 16:45:37 -04:00
elif [ "x$1" = "x--browser-tests" ]; then
export EXPORTED_FUNCTIONS="$EXPORTED_FUNCTIONS_SUMO"
export LDFLAGS="${LDFLAGS} -s TOTAL_MEMORY=${TOTAL_MEMORY_TESTS}"
2016-04-17 16:45:37 -04:00
export PREFIX="$(pwd)/libsodium-js-tests"
export DONE_FILE="$(pwd)/js-tests-browser.done"
export BROWSER_TESTS='yes'
export DIST='no'
echo "Building tests for web browsers in [${PREFIX}]"
2016-04-17 16:45:37 -04:00
elif [ "x$1" = "x--tests" ]; then
echo "Building for testing"
export EXPORTED_FUNCTIONS="$EXPORTED_FUNCTIONS_SUMO"
export CPPFLAGS="${CPPFLAGS} -DBENCHMARKS -DITERATIONS=10"
export LDFLAGS="${LDFLAGS} -s TOTAL_MEMORY=${TOTAL_MEMORY_TESTS}"
2016-04-17 16:45:37 -04:00
export PREFIX="$(pwd)/libsodium-js-tests"
export DONE_FILE="$(pwd)/js-tests.done"
export DIST='no'
echo "Building for testing in [${PREFIX}]"
else
2016-04-17 16:45:37 -04:00
echo "Usage: $0 <build_type>"
echo "<build_type> := --standard | --sumo | --browser-tests | --tests"
echo
exit 1
fi
export JS_EXPORTS_FLAGS="-s EXPORTED_FUNCTIONS=${EXPORTED_FUNCTIONS} -s EXTRA_EXPORTED_RUNTIME_METHODS=${EXPORTED_RUNTIME_METHODS}"
rm -f "$DONE_FILE"
2016-04-17 16:45:37 -04:00
echo
emconfigure ./configure $CONFIG_EXTRA --disable-shared --prefix="$PREFIX" \
2017-10-21 19:55:28 -04:00
--without-pthreads \
--disable-ssp --disable-asm --disable-pie \
CFLAGS="$CFLAGS" && \
2016-04-17 16:45:37 -04:00
emmake make clean
[ $? = 0 ] || exit 1
if [ "$DIST" = yes ]; then
emccLibsodium () {
outFile="${1}"
shift
2018-01-15 07:33:33 -05:00
emcc "$CFLAGS" --llvm-lto 1 $CPPFLAGS $LDFLAGS $JS_EXPORTS_FLAGS ${@} \
"${PREFIX}/lib/libsodium.a" -o "${outFile}" || exit 1
}
emmake make $MAKE_FLAGS install || exit 1
emccLibsodium "${PREFIX}/lib/libsodium.asm.tmp.js" -Oz -s WASM=0 -s RUNNING_JS_OPTS=1
emccLibsodium "${PREFIX}/lib/libsodium.wasm.tmp.js" -O3 -s WASM=1
cat > "${PREFIX}/lib/libsodium.js" <<- EOM
2017-10-05 02:12:05 -04:00
var Module;
if (typeof Module === 'undefined') {
2017-10-05 02:12:05 -04:00
Module = {};
}
var root = Module;
if (typeof root['sodium'] !== 'object') {
if (typeof global === 'object') {
root = global;
} else if (typeof window === 'object') {
root = window;
}
2017-10-05 02:12:05 -04:00
}
if (typeof root['sodium'] === 'object' && typeof root['sodium']['totalMemory'] === 'number') {
Module['TOTAL_MEMORY'] = root['sodium']['totalMemory'];
}
var _Module = Module;
2018-08-30 16:54:15 -04:00
Module.ready = new Promise(function(resolve, reject) {
var Module = _Module;
Module.onAbort = reject;
2018-08-30 16:54:15 -04:00
Module.onRuntimeInitialized = function() {
try {
/* Test arbitrary wasm function */
Module._crypto_secretbox_keybytes();
resolve();
} catch (err) {
reject(err);
}
};
2018-08-30 16:54:15 -04:00
Module.useBackupModule = function() {
2018-08-22 20:06:32 -04:00
var Module = _Module;
2018-08-30 16:54:29 -04:00
Object.keys(Module).forEach(function(k) {
2018-09-29 18:22:42 -04:00
if (k !== 'getRandomValue') {
delete Module[k];
}
2018-08-30 16:54:29 -04:00
});
2018-08-22 20:06:32 -04:00
$(cat "${PREFIX}/lib/libsodium.asm.tmp.js" | sed 's|use asm||g')
};
$(cat "${PREFIX}/lib/libsodium.wasm.tmp.js")
2018-08-30 16:54:15 -04:00
}).catch(function() {
2018-08-22 20:06:32 -04:00
_Module.useBackupModule();
});
EOM
rm "${PREFIX}/lib/libsodium.asm.tmp.js" "${PREFIX}/lib/libsodium.wasm.tmp.js"
2016-04-17 16:45:37 -04:00
touch -r "${PREFIX}/lib/libsodium.js" "$DONE_FILE"
ls -l "${PREFIX}/lib/libsodium.js"
exit 0
fi
if test "x$NODE" = x; then
for candidate in /usr/local/bin/node /usr/local/bin/nodejs /usr/bin/node /usr/bin/nodejs node nodejs; do
case $($candidate --version 2>&1) in #(
v*)
NODE=$candidate
break ;;
esac
done
fi
if [ "x$BROWSER_TESTS" != "x" ]; then
2015-12-06 20:01:39 -05:00
echo 'Compiling the test suite for web browsers...' && \
2016-04-17 16:45:37 -04:00
emmake make $MAKE_FLAGS CPPFLAGS="$CPPFLAGS -DBROWSER_TESTS=1" check > /dev/null 2>&1
else
2016-04-17 16:45:37 -04:00
if test "x$NODE" = x; then
echo 'node.js not found - test suite skipped' >&2
exit 1
fi
echo "Using [${NODE}] as a Javascript runtime"
echo 'Compiling the test suite...' && \
emmake make $MAKE_FLAGS check > /dev/null 2>&1
fi
if [ "x$BROWSER_TESTS" != "x" ]; then
2015-12-06 20:01:39 -05:00
echo 'Creating the test suite for web browsers'
(
cd test/default && \
mkdir -p browser && \
rm -f browser/tests.txt && \
for file in *.js; do
2016-04-17 16:45:37 -04:00
fgrep -v "#! /usr/bin/env ${NODE}" "$file" > "browser/${file}"
tname=$(echo "$file" | sed 's/.js$//')
cp -f "${tname}.exp" "browser/${tname}.exp"
sed "s/{{tname}}/${tname}/" index.html.tpl > "browser/${tname}.html"
echo "${tname}.html" >> "browser/tests.txt"
done
2016-04-17 16:45:37 -04:00
touch "$DONE_FILE"
)
else
echo 'Running the test suite'
(
cd test/default && \
for file in *.js; do
echo "#! /usr/bin/env ${NODE}" > "${file}.tmp"
2016-04-17 16:45:37 -04:00
fgrep -v "#! /usr/bin/env ${NODE}" "$file" >> "${file}.tmp"
chmod +x "${file}.tmp"
mv -f "${file}.tmp" "$file"
done
)
make $MAKE_FLAGS check || exit 1
2016-04-17 16:45:37 -04:00
touch "$DONE_FILE"
fi
echo 'Done.'