forked from bitcoinjs/tiny-secp256k1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
170 lines (135 loc) · 5.34 KB
/
Makefile
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
.PHONY: build
build: install-js-deps build-all-clean
.PHONY: build-all
build-all: build-js build-wasm
.PHONY: build-all-clean
build-all-clean: clean-built build-all
.PHONY: build-js
build-js:
npx tsc && \
sed -i -e 's/\.\/wasm_path\.js/.\/wasm_path_cjs.js/g' ./src_ts/wasm_loader.ts && \
npx tsc -p tsconfig-cjs.json && \
sed -i -e 's/\.\/wasm_path_cjs\.js/.\/wasm_path.js/g' ./src_ts/wasm_loader.ts && \
for f in ./lib/cjs/*.js; do mv -- "$$f" "$${f%.js}.cjs"; done && \
for f in ./lib/cjs/*.cjs; do sed -i -e 's/\(require(".*\)\.js");/\1.cjs");/g' -- "$$f"; done
.PHONY: build-wasm
build-wasm:
RUSTFLAGS="-C link-args=-zstack-size=655360" cargo build --target wasm32-unknown-unknown --release
mkdir -p lib && cp -f target/wasm32-unknown-unknown/release/secp256k1_wasm.wasm lib/secp256k1.wasm
wasm-opt -O4 --strip-debug --strip-producers --output lib/secp256k1.wasm lib/secp256k1.wasm
.PHONY: build-asmjs
build-asmjs: asmjs-build asmjs-fixes
.PHONY: build-asmjs-dev
build-asmjs-dev: asmjs-build-dev asmjs-fixes
.PHONY: asmjs-build
asmjs-build:
mkdir -p tiny-secp256k1-asmjs/lib
cp -R lib/ tiny-secp256k1-asmjs/
wasm2js -O4 --disable-gc -n tiny-secp256k1-asmjs/lib/secp256k1.wasm --output tiny-secp256k1-asmjs/lib/secp256k1.asm.js
.PHONY: asmjs-build-dev
asmjs-build-dev:
mkdir -p tiny-secp256k1-asmjs/lib
cp -R lib/ tiny-secp256k1-asmjs/
wasm2js tiny-secp256k1-asmjs/lib/secp256k1.wasm --output tiny-secp256k1-asmjs/lib/secp256k1.asm.js
.PHONY: asmjs-fixes
asmjs-fixes:
### Remove the copied wasm file and replace all imports
rm tiny-secp256k1-asmjs/lib/secp256k1.wasm
for FILE in $$(grep -lR secp256k1.wasm ./tiny-secp256k1-asmjs/lib); do \
sed -i 's/\.wasm/.asm.js/g' "$$FILE"; \
done
### Copy over package.json, README, LICENSE
cp package.json tiny-secp256k1-asmjs/
cp LICENSE tiny-secp256k1-asmjs/
cp README.md tiny-secp256k1-asmjs/
sed -i 's/"tiny-secp256k1"/"@bitcoin-js\/tiny-secp256k1-asmjs"/g' tiny-secp256k1-asmjs/package.json
grep -v wasm_loader tiny-secp256k1-asmjs/package.json > tiny-secp256k1-asmjs/package.json.new
mv tiny-secp256k1-asmjs/package.json.new tiny-secp256k1-asmjs/package.json
### 4 places where we want to replace the name tiny-secp256k1 in the README
sed -i 's/\(# \|install \|add \|v\/\|package\/\)tiny-secp256k1/\1@bitcoin-js\/tiny-secp256k1-asmjs/g' tiny-secp256k1-asmjs/README.md
### Copy the asm JS to cjs folder (needs modification)
cp tiny-secp256k1-asmjs/lib/secp256k1.asm.js tiny-secp256k1-asmjs/lib/cjs/secp256k1.asm.js
sed -i -e 's/import \* as \(.*\) from .\.\/\(.*\)\.js.;/const \1 = require('"'"'.\/\2.cjs'"'"');/g' tiny-secp256k1-asmjs/lib/cjs/secp256k1.asm.js
sed -i -e 's/export var /module.exports./g' tiny-secp256k1-asmjs/lib/cjs/secp256k1.asm.js
mv tiny-secp256k1-asmjs/lib/cjs/secp256k1.asm.js tiny-secp256k1-asmjs/lib/cjs/secp256k1.asm.cjs
### Modify imports for cjs only
for FILE in $$(grep -lR secp256k1.asm.js ./tiny-secp256k1-asmjs/lib/cjs); do \
sed -i 's/\.asm.js/.asm.cjs/g' "$$FILE"; \
done
### The NodeJS loader is not needed with ASM JS
mv tiny-secp256k1-asmjs/lib/cjs/wasm_loader.browser.cjs tiny-secp256k1-asmjs/lib/cjs/wasm_loader.cjs
mv tiny-secp256k1-asmjs/lib/wasm_loader.browser.js tiny-secp256k1-asmjs/lib/wasm_loader.js
rm tiny-secp256k1-asmjs/lib/wasm_loader.browser.d.ts
.PHONY: build-wasm-debug
build-wasm-debug:
RUSTFLAGS="-C link-args=-zstack-size=655360" cargo build --target wasm32-unknown-unknown
mkdir -p lib && cp -f target/wasm32-unknown-unknown/debug/secp256k1_wasm.wasm lib/secp256k1.wasm
.PHONY: clean
clean: clean-deps clean-built
.PHONY: clean-deps
clean-deps:
rm -rf \
benches/node_modules \
examples/random-in-node/node_modules \
examples/react-app/node_modules \
node_modules \
target
.PHONY: clean-built
clean-built:
rm -rf \
.nyc_output \
coverage \
examples/react-app/dist/*.js \
examples/react-app/dist/*.txt \
examples/react-app/dist/*.wasm \
lib \
tiny-secp256k1-asmjs \
*.tgz \
tests/browser
eslint_files = benches/*.{js,json} examples/**/*.{js,json} src_ts/*.ts tests/*.js *.json *.cjs
.PHONY: format
format:
cargo-fmt
npx eslint $(eslint_files) --fix
npx sort-package-json \
package.json \
benches/package.json \
examples/*/package.json
.PHONY: install-js-deps
install-js-deps:
npm ci
.PHONY: lint
lint:
cargo fmt -- --check
cargo clippy --target wasm32-unknown-unknown
npx eslint $(eslint_files)
.PHONY: test
test: test-browser test-node
.PHONY: test-browser-build-raw
test-browser-build-raw:
npx webpack build -c tests/browser.webpack.js
.PHONY: test-browser-build
test-browser-build: build-js build-wasm-debug build-asmjs test-browser-build-raw
test_browser_raw = node tests/browser-run.js | npx tap-summary
.PHONY: test-browser-raw
test-browser-raw:
$(test_browser_raw)
.PHONY: test-browser-raw-ci
test-browser-raw-ci:
$(test_browser_raw) --no-ansi --no-progress
.PHONY: test-browser
test-browser: test-browser-build test-browser-raw
test_node_raw = npx nyc --silent node --experimental-json-modules tests/index.js | npx tap-summary
.PHONY: test-node-raw
test-node-raw:
$(test_node_raw)
.PHONY: test-node-raw-ci
test-node-raw-ci:
$(test_node_raw) --no-ansi --no-progress
.PHONY: test-node
test-node: build-js build-wasm-debug build-asmjs test-node-raw
.PHONY: test-node-coverage-raw
test-node-coverage-raw:
npx nyc report --reporter=html --reporter=text
.PHONY: test-node-coverage
test-node-coverage: test-node test-node-coverage-raw