Skip to content

Commit 5d980bb

Browse files
authored
Merge pull request #915 from IntersectMBO/mgalazyn/doc/add-troubleshooting-to-wasm-readme
Add troubleshooting to cardano-wasm README
2 parents 30488e5 + fae0983 commit 5d980bb

2 files changed

Lines changed: 74 additions & 25 deletions

File tree

cardano-wasm/README.md

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ Enter the Nix shell by writing `nix develop .#wasm` on a shell in this folder an
1212

1313
For the installation we will need some dependencies. In Debian based distros we can install them using apt like this:
1414

15-
```console
15+
```bash
1616
sudo apt install happy pkgconf libtool git wget curl jq unzip zstd tar gzip
1717
```
1818

1919
#### Installing `ghc` for wasm
2020

2121
Then it is necessary to get `ghc` for wasm, and you see how to do that [here](https://gitlab.haskell.org/haskell-wasm/ghc-wasm-meta#getting-started-without-nix). At the moment, it is necessary to install a version of `ghc` that has `base <= 4.20`, so I would recommend installing `wasm32-wasi-9.10` like this:
2222

23-
```console
23+
```bash
2424
wget "https://gitlab.haskell.org/haskell-wasm/ghc-wasm-meta/-/archive/master/ghc-wasm-meta-master.tar.gz"
2525
tar -zxf ghc-wasm-meta-master.tar.gz
2626
cd ghc-wasm-meta-master
@@ -44,41 +44,41 @@ Then we need to compile three libraries to WASM: `libblst`, `libsodium`, and `li
4444

4545
In order to not interfere with the system library installation, we will create a folder to serve as our prefix:
4646

47-
```console
47+
```bash
4848
mkdir -p ~/prefix/{lib/pkgconfig,include}
4949
```
5050

5151
#### Installing `libblst`
5252

5353
We can obtain `libblst` from GitHub [here](https://github.com/supranational/blst). So we can use `git` to get its source code:
5454

55-
```console
55+
```bash
5656
git clone "https://github.com/supranational/blst.git"
5757
```
5858

5959
Then we get into the downloaded folder and we build it as follows:
6060

61-
```console
61+
```bash
6262
cd blst
6363
./build.sh
6464
```
6565

6666
And we copy the result and includes to our prefix as follows:
6767

68-
```console
68+
```bash
6969
cp libblst.a ~/prefix/lib/
7070
cp bindings/{blst.h,blst_aux.h} ~/prefix/include/
7171
```
7272

7373
We generate a dynamic version of the library:
7474

75-
```console
75+
```bash
7676
wasm32-wasi-clang -shared -Wl,--whole-archive ~/prefix/lib/libblst.a -o ~/prefix/lib/libblst.so
7777
```
7878

7979
And finally we write an entry for `pkgconfig`, so that later `ghc` can find our prefix:
8080

81-
```
81+
```bash
8282
cat <<EOF > $HOME/prefix/lib/pkgconfig/libblst.pc
8383
prefix=$HOME/prefix
8484
exec_prefix=\${prefix}
@@ -98,21 +98,21 @@ EOF
9898

9999
Finally we leave the folder:
100100

101-
```console
101+
```bash
102102
cd ..
103103
```
104104

105105
#### Installing `libsodium`
106106

107107
We can also obtain `libsodium` from its website [here](https://libsodium.org). We can use `wget` to get the source code for one of its releases. For example:
108108

109-
```console
109+
```bash
110110
wget "https://download.libsodium.org/libsodium/releases/libsodium-1.0.20-stable.tar.gz"
111111
```
112112

113113
Then we extract it, get into the created folder and compile it as follows:
114114

115-
```console
115+
```bash
116116
tar -zxf libsodium-1.0.20-stable.tar.gz
117117
cd libsodium-stable
118118
./configure --host=wasm32-wasi --prefix=$HOME/prefix
@@ -122,7 +122,7 @@ make install
122122

123123
Finally we generate a dynamic version of the library, and we leave the folder:
124124

125-
```console
125+
```bash
126126
wasm32-wasi-clang -shared -Wl,--whole-archive ~/prefix/lib/libsodium.a -o ~/prefix/lib/libsodium.so
127127
cd ..
128128
```
@@ -131,13 +131,13 @@ cd ..
131131

132132
We can obtain `libsecp256k1 ` from GitHub [here](https://github.com/bitcoin-core/secp256k1). So we can use `git` to get its source code:
133133

134-
```console
134+
```bash
135135
git clone "https://github.com/bitcoin-core/secp256k1.git"
136136
```
137137

138138
Then we get into the downloaded folder, and we build it as follows:
139139

140-
```console
140+
```bash
141141
cd secp256k1
142142
./autogen.sh
143143
./configure --prefix=$HOME/prefix --host=wasm32-wasi --enable-module-schnorrsig SECP_CFLAGS=-fPIC
@@ -147,7 +147,7 @@ make install
147147

148148
Finally we generate a dynamic version of the library, and we leave the folder:
149149

150-
```console
150+
```bash
151151
wasm32-wasi-clang -shared -Wl,--whole-archive ~/prefix/lib/libsecp256k1.a -o ~/prefix/lib/libsecp256k1.so
152152
cd ..
153153
```
@@ -156,46 +156,46 @@ cd ..
156156

157157
First we make sure we have `pkg-config` installed, in Debian based distros this can be done with apt:
158158

159-
```console
159+
```bash
160160
sudo apt install pkgconf
161161
```
162162

163163
And we set the variable `PKG_CONFIG_PATH` to inform `pkg-config` of where the entries for wasm are stored:
164164

165-
```console
165+
```bash
166166
export PKG_CONFIG_PATH=$HOME/prefix/lib/pkgconfig
167167
```
168168

169169
Then we get into the `cardano-wasm` subfolder of the clone of `cardano-api`.
170170

171-
```console
171+
```bash
172172
cd cardano-api/cardano-wasm
173173
```
174174

175175
### Compiling `cardano-wasm`
176176

177-
Once we have the environment set up we can procede to build the wasm module as follows:
177+
Once we have the environment set up we can proceed to build the wasm module as follows:
178178

179-
```console
179+
```bash
180180
wasm32-wasi-cabal update
181-
wasm32-wasi-cabal build
181+
wasm32-wasi-cabal build cardano-wasm
182182
```
183183

184184
That will generate the `wasm` module, and you can find where it was generated by using the following command:
185185

186-
```console
186+
```bash
187187
echo "$(env -u CABAL_CONFIG wasm32-wasi-cabal list-bin exe:cardano-wasm | tail -n1)"
188188
```
189189

190190
And you can see the exported functions by using the following command:
191191

192-
```console
192+
```bash
193193
wasm-dis "$(env -u CABAL_CONFIG wasm32-wasi-cabal list-bin exe:cardano-wasm | tail -n1)" | grep "export "
194194
```
195195

196196
To generate a post-link module with the exports you can write:
197197

198-
```console
198+
```bash
199199
$(wasm32-wasi-ghc --print-libdir)/post-link.mjs -i "$(env -u CABAL_CONFIG wasm32-wasi-cabal list-bin exe:cardano-wasm | tail -n1)" -o cardano-wasm.js
200200
```
201201

@@ -205,6 +205,54 @@ You can find more information in [this url](https://ghc.gitlab.haskell.org/ghc/d
205205

206206
And you can find an example of how to use it in the `example` subfolder. This example assumes that the generated `.wasm` and `.js` files as well as the files from the `lib-wrapper` subfolder, all reside in the same folder as the code in `example` subfolder.
207207

208+
### Troubleshooting guide
209+
210+
#### `Failed to load dynamic interface file for ...` GHC-47808 error
211+
212+
When you're seeing an error similar to:
213+
```console
214+
[1 of 4] Compiling Data.Constraint.Compose ( src/Data/Constraint/Compose.hs, dist/build/Data/Constraint/Compose.o, dist/build/Data/Constraint/Compose.dyn_o )
215+
src/Data/Constraint/Compose.hs:14:1: error: [GHC-47808]
216+
Failed to load dynamic interface file for Data.Constraint:
217+
Exception when reading interface file /home/mgalazyn/.local/share/cabal/store/ghc-9.10.1.20250327-inplace/constraints-0.14.2-7fc45a1b31889530ee7f295fd8bc8535f521e6eaac4bbc3cc5df34e174a5eacf/lib/Data/Constraint.dyn_hi
218+
/home/mgalazyn/.local/share/cabal/store/ghc-9.10.1.20250327-inplace/constraints-0.14.2-7fc45a1b31889530ee7f295fd8bc8535f521e6eaac4bbc3cc5df34e174a5eacf/lib/Data/Constraint.dyn_hi: withBinaryFile: does not exist (No such file or directory)
219+
|
220+
14 | import Data.Constraint
221+
| ^^^^^^^^^^^^^^^^^^^^^^
222+
```
223+
224+
Most likely the reason is that `cabal` store is polluted with non-wasm and/or nix and non-nix package information and `ghc-pkg` is unable to find the right dependencies.
225+
You can try to isolate `cabal` environment for WASM build from other builds.
226+
In other words, paths in `cabal path` command output should be different than for other builds.
227+
228+
##### When using `nix`
229+
230+
Try entering development shell in an isolated environment, and setting `$HOME` to a different directory, making `cabal` use different state directory.
231+
```bash
232+
nix develop -i .#wasm
233+
export HOME=`pwd` # or some other path that you want cabal state in
234+
wasm32-wasi-cabal update
235+
wasm32-wasi-cabal build cardano-wasm
236+
```
237+
238+
#### Cabal error Cabal-7125 `Failed to download ... The exception was: user error (https not supported)`
239+
240+
This can happen when using `nix develop -i`. To work around that add to your `cabal.project.local`:
241+
242+
```
243+
http-transport: curl
244+
```
245+
thus making `cabal` use `curl` for HTTP connections instead.
246+
247+
#### Build is getting stuck at one of the dependencies
248+
249+
It may happen that the build gets stuck on one of the dependencies, and if you check the CPU usage of the compiler process, you'll see a value close to 0.
250+
To work around that, disable build concurrency:
251+
252+
```bash
253+
wasm32-wasi-cabal build cardano-wasm -j1 --ghc-options="-j1" --no-semaphore
254+
```
255+
208256
## Running the example
209257

210258
To run the example in the `example` subfolder:
@@ -234,7 +282,7 @@ To run the example in the `example` subfolder:
234282
description: "Ledger Cddl Format", type: "Tx ConwayEra"} (test, line 20)
235283
```
236284

237-
## Running the GRPC example
285+
## Running the GRPC example
238286

239287
To run the example in the `grpc-example` subfolder:
240288

flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
lib.optionalAttrs (system != "x86_64-darwin") {
206206
wasm = wasm-pkgs.mkShell {
207207
packages = [
208+
wasm-pkgs.curl
208209
inputs.ghc-wasm-meta.packages.${system}.all_9_10
209210
wasm-pkgs.pkg-config
210211
wasm.libsodium

0 commit comments

Comments
 (0)