-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild-wasm
executable file
·46 lines (37 loc) · 1.43 KB
/
build-wasm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
set -e
build_dir=../../wasm
obj_dir=$build_dir/obj
compile_algorithm() {
id=$1
echo "Compiling $id"
variables="-DCRYPTO_NAMESPACE(x)=crypto_kem_${id}_##x -D_CRYPTO_NAMESPACE(x)=_crypto_kem_${id}_##x"
find kem/$id -type f -name '*.c' | while read source; do
target=$obj_dir/${source%.c}.o
echo " $source -> $target"
mkdir -p `dirname $target`
emcc -std=c11 -Oz -Ikem/$id/subroutines -fvisibility=default $variables -c $source -o $target
done
}
implementations=`ls -1 kem | while read id; do if [ -d "kem/$id" ]; then echo $id; fi; done`
rm -rf $obj_dir
for id in $implementations; do
compile_algorithm $id
done
all_exports=`(echo -n '["_malloc","_free"'; for impl in $implementations; do
echo -n ",\"_crypto_kem_${impl}_ref_keypair\""
echo -n ",\"_crypto_kem_${impl}_ref_enc\""
echo -n ",\"_crypto_kem_${impl}_ref_dec\""
done; echo -n ']') | sed 's/,\]/]/'`
echo "Linking"
all_objs=`find $obj_dir -type f -name '*.o'`
emcc -std=c11 -Oz $all_objs -o $build_dir/mceliece.wasm -s STACK_SIZE=5MB -s EXPORTED_FUNCTIONS="$all_exports" -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s WASM=1 --no-entry
echo "Converting header"
(
echo -e "'use strict';\n"
echo 'module.exports = {'
cat kem/mceliece*/crypto_kem_mceliece*.h | sort | grep -P '#define crypto_kem_mceliece([0-9f]+)_ref' | sed -e 's/#define / /g;' | sed -E 's/ ([0-9]+)/: \1,/g'
echo '};'
) > $build_dir/mceliece_constants.js
echo "Cleaning up"
rm -rf $obj_dir