Cleanup build scripts (reusability) (#2)

* Cleanup build scripts (reusability)

* Adjustments

* More cleanups
This commit is contained in:
Jaco Greeff
2019-03-17 16:42:48 +01:00
committed by GitHub
parent bff0dcdb09
commit efbabc9c5c
13 changed files with 166 additions and 180 deletions
+2 -2
View File
@@ -13,11 +13,11 @@
"typescript": "^3.3.3333"
},
"scripts": {
"build": "echo \"Build via ./test.sh\"",
"build": "./scripts/build.sh",
"check": "tslint --project . && tsc --noEmit --pretty",
"clean": "polkadot-dev-clean-build",
"postinstall": "polkadot-dev-yarn-only",
"test": "./test.sh"
"test": "echo \"Tests included in build\""
},
"devDependencies": {
"@polkadot/dev": "^0.27.1",
+11 -11
View File
@@ -407,6 +407,17 @@ name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "wasm"
version = "0.0.0"
dependencies = [
"hex-literal 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"schnorrkel 0.0.0 (git+https://github.com/w3f/schnorrkel?rev=d3289df76b8ae6dfb68e733204c5c009df5343a9)",
"wasm-bindgen 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)",
"wee_alloc 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.39"
@@ -455,17 +466,6 @@ name = "wasm-bindgen-shared"
version = "0.2.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "wasm-schnorrkel"
version = "0.1.0"
dependencies = [
"hex-literal 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"schnorrkel 0.0.0 (git+https://github.com/w3f/schnorrkel?rev=d3289df76b8ae6dfb68e733204c5c009df5343a9)",
"wasm-bindgen 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)",
"wee_alloc 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "wee_alloc"
version = "0.4.3"
+4 -2
View File
@@ -1,6 +1,8 @@
# name & version is only used internally, doesn't need to be adjusted. In the case of
# name keep it consistent at "wasm" since the scripts (for re-use purposes) expect that
[package]
name = "wasm-schnorrkel"
version = "0.1.0"
name = "wasm"
version = "0.0.0"
authors = ["Jaco Greeff <jacogr@gmail.com>", "kianenigma <Kian.peymani@gmail.com>"]
edition = "2018"
-6
View File
@@ -1,6 +0,0 @@
const fs = require('fs');
const buffer = fs.readFileSync('./build/wasm_schnorrkel_opt.wasm');
fs.writeFileSync('./build/wasm_schnorrkel_wasm.js', `
module.exports = Buffer.from('${buffer.toString('base64')}', 'base64');
`);
+7 -99
View File
@@ -2,51 +2,12 @@
set -e
# cleanup old
echo "*** Cleaning old builds"
rm -rf ./build ./pkg
# build new via nightly & wasm-pack
echo "*** Building WASM output"
rustup run nightly wasm-pack build --release --scope polkadot --target nodejs
mv pkg build
# optimise
echo "*** Optimising WASM output"
../../binaryen/bin/wasm-opt build/wasm_schnorrkel_bg.wasm -Os -o build/wasm_schnorrkel_opt.wasm
# build asmjs version from the input (optimised) WASM
# echo "*** Building asm.js version"
# ../../binaryen/bin/wasm2js --no-validation --output build/wasm_schnorrkel_temp.js build/wasm_schnorrkel_opt.wasm
# convert wasm to base64 structure
echo "*** Packing WASM into base64"
node ./build.js
# copy our package interfaces
echo "*** Copying package sources"
cp src/js/* build/
echo "*** Building package"
../../scripts/build-package.sh
# shortcuts for files
echo "*** Adjusting output"
BGJ=build/wasm_schnorrkel_bg.js
SRC=build/wasm_schnorrkel.js
DEF=build/wasm_schnorrkel.d.ts
TMP=build/wasm_schnorrkel_temp.js
ASM=build/wasm_schnorrkel_asm.js
PKG=build/package.json
# update the files (new addition)
# excluded: "wasm_schnorrkel_asm\.js",
# sed -i -e 's/wasm_schnorrkel_bg\.wasm/wasm_schnorrkel_wasm\.js", "index\.js", "index\.d\.ts/g' $PKG
# sed -i -e 's/"main": "wasm_schnorrkel\.js"/"main": "index\.js"/g' $PKG
# sed -i -e 's/"types": "wasm_schnorrkel\.d\.ts"/"types": "index\.d\.ts"/g' $PKG
# cleanup asm
# sed -i -e 's/import {/\/\/ import {/g' $TMP
# sed -i -e 's/function asmFunc/var schnorrkel = require('\''\.\/wasm_schnorrkel'\''); function asmFunc/g' $TMP
# sed -i -e 's/export const /module\.exports\./g' $TMP
# sed -i -e 's/{abort.*},memasmFunc/schnorrkel, memasmFunc/g' $TMP
SRC=build/wasm.js
# we do not want the __ imports (used by WASM) to clutter up
sed -i -e 's/var wasm;/const crypto = require('\''crypto'\''); let wasm; const requires = { crypto };/g' $SRC
@@ -58,64 +19,11 @@ sed -i -e 's/const TextDecoder = require('\''util'\'')\.TextDecoder;/const { u8a
sed -i -e 's/let cachedTextDecoder = new /\/\/ let cachedTextDecoder = new /g' $SRC
sed -i -e 's/cachedTextDecoder\.decode/u8aToString/g' $SRC
# we are swapping to a async interface for webpack support (wasm limits)
sed -i -e 's/wasm = require/\/\/ wasm = require/g' $SRC
# pull the requires from the imports and the `requires` object
sed -i -e 's/return addHeapObject(require(varg0));/return addHeapObject(requires[varg0]);/g' $SRC
# sed -i -e 's/return addHeapObject(require(varg0));/throw new Error(`Invalid require from WASM for ${varg0}`);/g' $SRC
# construct our promise and add ready helpers
echo "
module.exports.abort = function () { throw new Error('abort'); };
const createPromise = require('./wasm_schnorrkel_bg');
const wasmPromise = createPromise().catch(() => null);
module.exports.isReady = function () { return !!wasm; }
module.exports.waitReady = function () { return wasmPromise.then(() => !!wasm); }
wasmPromise.then((_wasm) => { wasm = _wasm });
" >> $SRC
# add extra methods to type definitions
echo "
export function isReady(): boolean;
export function waitReady(): Promise<boolean>;
" >> $DEF
# create the init promise handler
echo "
const asm = null; // require('./wasm_schnorrkel_asm');
const bytes = require('./wasm_schnorrkel_wasm');
const schnorrkel = require('./wasm_schnorrkel');
module.exports = async function createExportPromise () {
const imports = {
'./wasm_schnorrkel': schnorrkel
};
if (!WebAssembly) {
return asm;
}
try {
const { instance } = await WebAssembly.instantiate(bytes, imports);
return instance.exports;
} catch (error) {
return asm;
}
}
" > $BGJ
# cleanup in-place sed files
echo "*** Cleaning up in-place edits"
rm -rf build/*-e
# pass through uglify to make the actual output smaller
# echo "*** Optimising asm.js output"
# yarn run uglifyjs $TMP --compress --mangle --timings --output $ASM
# mv $TMP $ASM
ls -al build
echo "*** Testing package"
cargo test -- --nocapture
node ./test/wasm.js
yarn jest ./test/jest.spec.js
+7 -7
View File
@@ -5,18 +5,18 @@
"kianenigma <Kian.peymani@gmail.com>"
],
"files": [
"index.js",
"index.d.ts",
"crypto-polyfill.js",
"wasm_schnorrkel_wasm.js",
"wasm_schnorrkel.js",
"wasm_schnorrkel_bg.js",
"wasm_schnorrkel.d.ts"
"index.d.ts",
"index.js",
"wasm.d.ts",
"wasm.js",
"wasm_bg.js",
"wasm_wasm.js"
],
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "./test.sh"
"build": "./build.sh"
},
"version": "0.1.0-beta.2",
"peerDependencies": {
+1 -1
View File
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
// @ts-check
const stubbed = require('./wasm_schnorrkel');
const stubbed = require('./wasm');
module.exports.deriveKeypairHard = stubbed.derive_keypair_hard;
module.exports.deriveKeypairSoft = stubbed.derive_keypair_soft;
-14
View File
@@ -1,14 +0,0 @@
#!/usr/bin/env bash
set -e
./build.sh
echo "*** Running cargo tests"
cargo test --release -- --nocapture
echo "*** Running wasm tests"
node ./test/wasm.js
echo "*** Running jest tests"
yarn jest ./test/jest.spec.js
+95
View File
@@ -0,0 +1,95 @@
#!/usr/bin/env bash
set -e
# cleanup old
echo "*** Cleaning old builds"
rm -rf ./build ./pkg
# build new via nightly & wasm-pack
echo "*** Building WASM output"
rustup run nightly wasm-pack build --release --scope polkadot --target nodejs
mv pkg build
# optimise
echo "*** Optimising WASM output"
../../binaryen/bin/wasm-opt build/wasm_bg.wasm -Os -o build/wasm_opt.wasm
# build asmjs version from the input (optimised) WASM
# echo "*** Building asm.js version"
# ../../binaryen/bin/wasm2js --no-validation --output build/wasm_temp.js build/wasm_opt.wasm
# convert wasm to base64 structure
echo "*** Packing WASM into base64"
node ../../scripts/pack-wasm-base64.js
# copy our package interfaces
echo "*** Copying package sources"
cp src/js/* build/
# shortcuts for files
echo "*** Adjusting output"
BGJ=build/wasm_bg.js
SRC=build/wasm.js
DEF=build/wasm.d.ts
TMP=build/wasm_temp.js
ASM=build/wasm_asm.js
# update the files (new addition)
# excluded: "wasm_asm\.js",
# sed -i -e 's/wasm_bg\.wasm/wasm_wasm\.js", "index\.js", "index\.d\.ts/g' $PKG
# sed -i -e 's/"main": "wasm\.js"/"main": "index\.js"/g' $PKG
# sed -i -e 's/"types": "wasm\.d\.ts"/"types": "index\.d\.ts"/g' $PKG
# cleanup asm
# sed -i -e 's/import {/\/\/ import {/g' $TMP
# sed -i -e 's/function asmFunc/var schnorrkel = require('\''\.\/wasm'\''); function asmFunc/g' $TMP
# sed -i -e 's/export const /module\.exports\./g' $TMP
# sed -i -e 's/{abort.*},memasmFunc/schnorrkel, memasmFunc/g' $TMP
# we are swapping to a async interface for webpack support (wasm limits)
sed -i -e 's/wasm = require/\/\/ wasm = require/g' $SRC
# construct our promise and add ready helpers
echo "
module.exports.abort = function () { throw new Error('abort'); };
const createPromise = require('./wasm_bg');
const wasmPromise = createPromise().catch(() => null);
module.exports.isReady = function () { return !!wasm; }
module.exports.waitReady = function () { return wasmPromise.then(() => !!wasm); }
wasmPromise.then((_wasm) => { wasm = _wasm });
" >> $SRC
# add extra methods to type definitions
echo "
export function isReady(): boolean;
export function waitReady(): Promise<boolean>;
" >> $DEF
# create the init promise handler
echo "
const asm = null; // require('./wasm_asm');
const bytes = require('./wasm_wasm');
const imported = require('./wasm');
module.exports = async function createExportPromise () {
const imports = {
'./wasm': imported
};
if (!WebAssembly) {
return asm;
}
try {
const { instance } = await WebAssembly.instantiate(bytes, imports);
return instance.exports;
} catch (error) {
return asm;
}
}
" > $BGJ
+23
View File
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -e
./scripts/install-build-deps.sh
PACKAGES=( "schnorrkel" )
echo "*** Building packages"
cd packages
for PKG in "${PACKAGES[@]}"; do
echo "*** Building wasm-$PKG"
cd wasm-$PKG
./build.sh
rm -rf build/*-e build/package.json build/README.md
ls -al build
cd ..
done
cd ..
+10 -18
View File
@@ -2,7 +2,8 @@
set -e
PACKAGES=( "schnorrkel" )
# wasm2js for wasm -> asm.js
BINARYEN=( "wasm-opt" )
# install wasm-pack as required
if ! [ -x "$(command -v wasm-pack)" ]; then
@@ -17,20 +18,11 @@ if [ ! -d "binaryen" ]; then
rm -rf binaryen/test
fi
# build wasm2js as required
# if [ ! -f "binaryen/bin/wasm2js" ]; then
# echo "*** Building wasm2js"
# cd binaryen
# cmake .
# make wasm2js
# cd ..
# fi
# build wasm-opt as required
if [ ! -f "binaryen/bin/wasm-opt" ]; then
echo "*** Building wasm-opt"
cd binaryen
cmake .
make wasm-opt
cd ..
fi
for BIN in "${BINARYEN[@]}"; do
if [ ! -f "binaryen/bin/$BIN" ]; then
echo "*** Building $BIN"
cd binaryen
cmake . && make $BIN
cd ..
fi
done
+6
View File
@@ -0,0 +1,6 @@
const fs = require('fs');
const buffer = fs.readFileSync('./build/wasm_opt.wasm');
fs.writeFileSync('./build/wasm_wasm.js', `
module.exports = Buffer.from('${buffer.toString('base64')}', 'base64');
`);
-20
View File
@@ -1,20 +0,0 @@
#!/usr/bin/env bash
set -e
./deps.sh
PACKAGES=( "schnorrkel" )
echo "*** Testing packages"
cd packages
for PKG in "${PACKAGES[@]}"; do
cd wasm-$PKG
./test.sh
cd ..
done
cd ..