Build packed soljson.js.

This commit is contained in:
Daniel Kirchner 2021-11-09 18:07:15 +01:00
parent 6895972d04
commit 3a201ec0f8
6 changed files with 74 additions and 6 deletions

View File

@ -982,10 +982,10 @@ jobs:
command: |
scripts/ci/build_emscripten.sh
- store_artifacts:
path: emscripten_build/libsolc/soljson.js
path: upload/soljson.js
destination: soljson.js
- run: mkdir -p workspace
- run: cp emscripten_build/libsolc/soljson.js workspace/soljson.js
- run: cp upload/soljson.js workspace/soljson.js
- run: scripts/get_version.sh > workspace/version.txt
- persist_to_workspace:
root: workspace

View File

@ -142,8 +142,6 @@ if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MA
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s WASM=1")
# Set webassembly build to synchronous loading.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s WASM_ASYNC_COMPILATION=0")
# Output a single js file with the wasm binary embedded as base64 string.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s SINGLE_FILE=1")
# Allow new functions to be added to the wasm module via addFunction.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s ALLOW_TABLE_GROWTH=1")
# Disable warnings about not being pure asm.js due to memory growth.

View File

@ -137,6 +137,36 @@ evmc:
See the License for the specific language governing permissions and
limitations under the License.
mini-lz4:
The file scripts/ci/mini-lz4.js is derived from the emscripten adaptation of
node-lz4 and licensed under the following terms:
Copyright (c) 2012 Pierre Curto
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.
base64:
The file scripts/ci/base64DecToArr.js is derived from a code example
in the MDN Web Docs, which permits use under CC0 terms:
Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/
All other code is licensed under GPL version 3:

View File

@ -40,6 +40,8 @@ else
BUILD_DIR="$1"
fi
apt-get update && apt-get install lz4
WORKSPACE=/root/project
cd $WORKSPACE
@ -71,8 +73,8 @@ make soljson
cd ..
mkdir -p upload
cp "$BUILD_DIR/libsolc/soljson.js" upload/
cp "$BUILD_DIR/libsolc/soljson.js" ./
scripts/ci/pack_soljson.sh "$BUILD_DIR/libsolc/soljson.js" "$BUILD_DIR/libsolc/soljson.wasm" upload/soljson.js
cp upload/soljson.js ./
OUTPUT_SIZE=$(ls -la soljson.js)

View File

@ -1,6 +1,7 @@
function uncompress(source, uncompressedSize) {
/*
based off https://github.com/emscripten-core/emscripten/blob/main/third_party/mini-lz4.js
The license only applies to the body of this function (``uncompress``).
====
MiniLZ4: Minimal LZ4 block decoding and encoding.

37
scripts/ci/pack_soljson.sh Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env bash
set -euo pipefail
script_dir="$(realpath "$(dirname "$0")")"
soljson_js="$1"
soljson_wasm="$2"
soljson_wasm_size=$(wc -c "${soljson_wasm}" | cut -d ' ' -f 1)
output="$3"
(( $# == 3 )) || { >&2 echo "Usage: $0 soljson.js soljson.wasm packed_soljson.js"; exit 1; }
# If this changes in an emscripten update, it's probably nothing to worry about,
# but we should double-check when it happens and adjust the tail command below.
[[ $(head -c 5 "${soljson_js}") == "null;" ]] || { >&2 echo 'Expected soljson.js to start with "null;"'; exit 1; }
echo "Packing $soljson_js and $soljson_wasm to $output."
(
echo -n 'var Module = Module || {}; Module["wasmBinary"] = '
echo -n '(function(source, uncompressedSize) {'
# Note that base64DecToArr assumes no trailing equals signs.
cpp "${script_dir}/base64DecToArr.js" | grep -v "^#.*"
# Note that mini-lz4.js assumes no file header and no frame crc checksums.
cpp "${script_dir}/mini-lz4.js" | grep -v "^#.*"
echo 'return uncompress(base64DecToArr(source), uncompressedSize);})('
echo -n '"'
# We fix lz4 format settings, remove the 8 bytes file header and remove the trailing equals signs of the base64 encoding.
lz4c --no-frame-crc --best --favor-decSpeed "${soljson_wasm}" - | tail -c +8 | base64 -w 0 | sed 's/[^A-Za-z0-9\+\/]//g'
echo '",'
echo -n "${soljson_wasm_size});"
# Remove "null;" from the js wrapper.
tail -c +6 "${soljson_js}"
) > "$output"
echo "Testing $output."
echo "process.stdout.write(require('$(realpath "${output}")').wasmBinary)" | node | cmp "${soljson_wasm}" && echo "Binaries match."
# Allow the wasm binary to be garbage collected after compilation.
echo 'Module["wasmBinary"] = undefined;' >> "${output}"