-
Notifications
You must be signed in to change notification settings - Fork 29
Add script for preparing the wasm libraries for building without Nix #1194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,219 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #!/usr/bin/env bash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Build libsodium, libsecp256k1, and libblst for wasm32-wasi and install them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # into a single prefix that cabal.project can point at via extra-lib-dirs / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # extra-include-dirs. Mirrors ./nix/{libsodium,secp256k1,blst}.nix but uses | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # wasm32-wasi-clang directly (no Nix). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Requirements on PATH: wasm32-wasi-clang (from wasi-sdk), autoreconf, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # automake, libtool, make, git, pkg-config. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to say this in the help message too |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set -euo pipefail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Script must be run from the root of the project | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PROJECT_DIR="$(pwd)" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ -f "$PROJECT_DIR/cabal.project" ] || { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "Error: cabal.project not found in $PROJECT_DIR. Run this script from the root of the project" >&2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| missing=() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ -n "${PREFIX:-}" ] || missing+=(PREFIX) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [ -n "${SRC_ROOT:-}" ] || missing+=(SRC_ROOT) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ "${#missing[@]}" -gt 0 ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cat >&2 <<EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Error: required environment variable(s) not set: ${missing[*]} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Both values are interpreted relative to the cabal.project directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ($PROJECT_DIR) unless given as an absolute path. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PREFIX where the wasm libs/headers will be installed, e.g. wasm-libs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Will be created if missing. Point cabal.project's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| extra-lib-dirs / extra-include-dirs at \$PREFIX/lib and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| \$PREFIX/include. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SRC_ROOT where the libsodium / secp256k1 / blst source trees live (or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| will be cloned into as \$SRC_ROOT/{libsodium,secp256k1,blst}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.g. wasm-deps. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Example: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PREFIX=wasm-libs SRC_ROOT=wasm-libs-src $0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resolve_rel() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case "$1" in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /*) printf '%s\n' "$1" ;; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| *) printf '%s\n' "$PROJECT_DIR/$1" ;; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| esac | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ABS_PREFIX="$(resolve_rel "$PREFIX")" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ABS_SRC_ROOT="$(resolve_rel "$SRC_ROOT")" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ABS_SRC_ROOT="$(resolve_rel "$SRC_ROOT")" | |
| ABS_SRC_ROOT="$(resolve_rel "$SRC_ROOT")" | |
| mkdir -p "$ABS_SRC_ROOT" |
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The script checks for wasm32-wasi-clang/wasm-ld, but later also depends on tools like ar and file (used in verify_wasm) and nproc. Consider extending the prerequisite checks (or the header docs) so failures are detected early with a clear message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The script checks for wasm32-wasi-clang/wasm-ld, but later also depends on tools like ar and file (used in verify_wasm) and nproc. Consider extending the prerequisite checks (or the header docs) so failures are detected early with a clear message.
I was going to say this too, the comment at the beginning says it needs: autoreconf, automake, libtool, make, git, pkg-config. All these could be checked here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SECP256K1_REPO and BLST_REPO are cloned without a pinned revision/tag, so running this script at different times can produce different binaries (and potentially break builds). Consider pinning these to the same revisions used by the Nix build (from the repo’s pinned nixpkgs/flake.lock) and passing those revs into clone_if_missing.
| mkdir -p "$ABS_PREFIX/lib" "$ABS_PREFIX/include" "$ABS_PREFIX/lib/pkgconfig" | |
| clone_if_missing() { | |
| local repo="$1" dir="$2" rev="${3:-}" | |
| if [ ! -d "$dir/.git" ]; then | |
| git clone "$repo" "$dir" | |
| fi | |
| # Pin these to the same revisions used by the Nix build. They may also be | |
| # overridden from the environment when invoking this script. | |
| SECP256K1_REV="${SECP256K1_REV:-}" | |
| BLST_REV="${BLST_REV:-}" | |
| mkdir -p "$ABS_PREFIX/lib" "$ABS_PREFIX/include" "$ABS_PREFIX/lib/pkgconfig" | |
| pinned_rev_for_repo() { | |
| local repo="$1" | |
| case "$repo" in | |
| "$LIBSODIUM_REPO") printf '%s\n' "$LIBSODIUM_REV" ;; | |
| "$SECP256K1_REPO") printf '%s\n' "$SECP256K1_REV" ;; | |
| "$BLST_REPO") printf '%s\n' "$BLST_REV" ;; | |
| *) printf '%s\n' "" ;; | |
| esac | |
| } | |
| clone_if_missing() { | |
| local repo="$1" dir="$2" rev="${3:-}" | |
| if [ -z "$rev" ]; then | |
| rev="$(pinned_rev_for_repo "$repo")" | |
| fi | |
| if [ ! -d "$dir/.git" ]; then | |
| if [ -z "$rev" ]; then | |
| echo "Error: refusing to clone $repo without a pinned revision. Configure the matching *_REV to the revision used by the Nix build." >&2 | |
| exit 1 | |
| fi | |
| git clone "$repo" "$dir" | |
| fi |
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
build_libsodium relies on ./configure --host=wasm32-wasi to pick the right compiler, but doesn’t explicitly set CC=wasm32-wasi-clang (or related tools). If the environment isn’t set up as expected, this can silently compile for the wrong target and only fail late; consider exporting CC (and possibly AR, RANLIB) for this build step.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| } | |
| cd - | |
| } |
I would suggest returning to the previous folder to make this function more self-contained. Currently this does not make a difference but if we move the call to the function to somewhere else it could break things
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
build_secp256k1 doesn’t explicitly set CC=wasm32-wasi-clang (only --host=wasm32-wasi). To reduce reliance on ambient toolchain configuration, consider setting CC (and related binutils) for the configure/make step, similar to how build_blst sets CC.
| clone_if_missing "$SECP256K1_REPO" "$dir" | |
| cd "$dir" | |
| [ -x ./configure ] || ./autogen.sh | |
| ./configure \ | |
| --host=wasm32-wasi \ | |
| --enable-module-schnorrsig \ | |
| --prefix="$ABS_PREFIX" \ | |
| SECP_CFLAGS=-fPIC | |
| make -j"$(nproc)" | |
| make install | |
| local cc="wasm32-wasi-clang" | |
| local ar="llvm-ar" | |
| local ranlib="llvm-ranlib" | |
| clone_if_missing "$SECP256K1_REPO" "$dir" | |
| cd "$dir" | |
| [ -x ./configure ] || ./autogen.sh | |
| CC="$cc" AR="$ar" RANLIB="$ranlib" ./configure \ | |
| --host=wasm32-wasi \ | |
| --enable-module-schnorrsig \ | |
| --prefix="$ABS_PREFIX" \ | |
| SECP_CFLAGS=-fPIC | |
| make -j"$(nproc)" CC="$cc" AR="$ar" RANLIB="$ranlib" | |
| make install CC="$cc" AR="$ar" RANLIB="$ranlib" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| -o "$ABS_PREFIX/lib/libsecp256k1.so" | |
| -o "$ABS_PREFIX/lib/libsecp256k1.so" | |
| cd - |
Same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When testing the script I found an issue with the version of libblst in the pkg-config file (libblst.pc), which was Version: v0.3.16-36-gdece82e and the "v" at the beginning was tripping cabal. Not sure why that happens. Did you get that too?
Maybe it would be fixed by fixing a revision?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| EOF | |
| EOF | |
| cd - |
And here
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generated wasm-libs-without-nix.cabal is written into the repo root and uses absolute paths ($ABS_PREFIX). This can clutter the working tree and makes the fragment non-portable if the checkout moves; consider writing it under scripts/wasm-without-nix/ (or similar) and using relative paths when possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe would be good to mention adding the pkg-config path to PKG_CONFIG_PATH if it is not standard, and print which it is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR appears to be missing the required Herald changelog fragment in
.changes/(CI enforces this). Please add a new.changes/*.ymlentry for this change (pick the appropriateproject:andkind:).