From aef4bab2868f000844cfaba0df1a6e8174b3d2fe Mon Sep 17 00:00:00 2001 From: Luis Covarrubias Date: Wed, 17 Dec 2025 16:38:56 -0800 Subject: [PATCH] fix: enable Mac builds using Homebrew LLVM Apple's Clang doesn't support the wasm32-unknown-unknown target, causing secp256k1-sys compilation to fail on Mac. This change automatically detects Mac and uses Homebrew's LLVM which has proper WASM support. Changes: - Makefile: Auto-detect Mac and set CC/AR to use Homebrew LLVM - Makefile: Remove trailing slashes from target declarations for compatibility with both old (Mac 3.81) and new (Linux 4.x) Make - package.json: Fix Make target names to match Makefile - scripts/wasm-pack-test.sh: Wrapper script to set correct compiler for wasm-pack tests on Mac - README.md: Update build instructions for Mac This allows Mac users to build with 'npm run build' without Docker, while maintaining full compatibility with Linux CI builds. Tested on Mac (Apple Silicon) Requires: brew install llvm" Ticket: BTC-0 --- packages/wasm-utxo/Makefile | 30 ++++++++++++++++---- packages/wasm-utxo/README.md | 14 +++++++-- packages/wasm-utxo/package.json | 6 ++-- packages/wasm-utxo/scripts/wasm-pack-test.sh | 15 ++++++++++ 4 files changed, 54 insertions(+), 11 deletions(-) create mode 100755 packages/wasm-utxo/scripts/wasm-pack-test.sh diff --git a/packages/wasm-utxo/Makefile b/packages/wasm-utxo/Makefile index 9c0add0..8b624ed 100644 --- a/packages/wasm-utxo/Makefile +++ b/packages/wasm-utxo/Makefile @@ -6,6 +6,24 @@ ifdef WASM_PACK_DEV WASM_PACK_FLAGS += --dev endif +# Auto-detect Mac and use Homebrew LLVM for WASM compilation +# Apple's Clang doesn't support wasm32-unknown-unknown target +UNAME_S := $(shell uname -s) + +ifeq ($(UNAME_S),Darwin) + # Mac detected - check for Homebrew LLVM installation + HOMEBREW_LLVM := $(shell brew --prefix llvm 2>/dev/null) + + ifdef HOMEBREW_LLVM + export CC = $(HOMEBREW_LLVM)/bin/clang + export AR = $(HOMEBREW_LLVM)/bin/llvm-ar + $(info Using Homebrew LLVM: $(HOMEBREW_LLVM)) + else + $(warning Homebrew LLVM not found. Install with: brew install llvm) + $(warning Continuing with system clang - may fail on Apple Silicon) + endif +endif + define WASM_PACK_COMMAND $(WASM_PACK) build --no-opt --out-dir $(1) $(WASM_PACK_FLAGS) --target $(2) endef @@ -34,16 +52,16 @@ define BUILD $(call SHOW_WASM_SIZE,$(1)) endef -.PHONY: js/wasm/ -js/wasm/: +.PHONY: js/wasm +js/wasm: $(call BUILD,$@,bundler) -.PHONY: dist/esm/js/wasm/ -dist/esm/js/wasm/: +.PHONY: dist/esm/js/wasm +dist/esm/js/wasm: $(call BUILD,$@,bundler) -.PHONY: dist/cjs/js/wasm/ -dist/cjs/js/wasm/: +.PHONY: dist/cjs/js/wasm +dist/cjs/js/wasm: $(call BUILD,$@,nodejs) .PHONY: lint diff --git a/packages/wasm-utxo/README.md b/packages/wasm-utxo/README.md index d3fc2b3..cee6ca2 100644 --- a/packages/wasm-utxo/README.md +++ b/packages/wasm-utxo/README.md @@ -24,10 +24,20 @@ This project is under active development. ## Building -If your system has problems with `wasm-pack` (Mac M1), you can use the `Container.mk` Makefile to build the wasm files: +### Mac + +Requires Homebrew LLVM (Apple's Clang doesn't support WASM targets): + +```bash +brew install llvm +npm run build +``` + +### Docker (optional) + +If you prefer a containerized build environment: ```bash -cd packages/wasm-utxo make -f Container.mk build-image make -f Container.mk build-wasm ``` diff --git a/packages/wasm-utxo/package.json b/packages/wasm-utxo/package.json index a38f484..c2e4db4 100644 --- a/packages/wasm-utxo/package.json +++ b/packages/wasm-utxo/package.json @@ -36,12 +36,12 @@ "test": "npm run test:mocha && npm run test:wasm-pack && npm run test:imports", "test:mocha": "mocha --recursive test", "test:wasm-pack": "npm run test:wasm-pack-node && npm run test:wasm-pack-chrome", - "test:wasm-pack-node": "wasm-pack test --node", - "test:wasm-pack-chrome": "wasm-pack test --headless --chrome", + "test:wasm-pack-node": "./scripts/wasm-pack-test.sh --node", + "test:wasm-pack-chrome": "./scripts/wasm-pack-test.sh --headless --chrome", "test:esm-import": "node --experimental-wasm-modules bundler-test/test-esm-import.mjs", "test:cjs-import": "node bundler-test/test-cjs-import.cjs", "test:imports": "npm run test:esm-import && npm run test:cjs-import", - "build:wasm": "make js/wasm/ && make dist/esm/js/wasm/ && make dist/cjs/js/wasm/", + "build:wasm": "make js/wasm && make dist/esm/js/wasm && make dist/cjs/js/wasm", "build:ts-esm": "tsc", "build:ts-cjs": "tsc --project tsconfig.cjs.json", "build:ts": "npm run build:ts-esm && npm run build:ts-cjs", diff --git a/packages/wasm-utxo/scripts/wasm-pack-test.sh b/packages/wasm-utxo/scripts/wasm-pack-test.sh new file mode 100755 index 0000000..c0a0733 --- /dev/null +++ b/packages/wasm-utxo/scripts/wasm-pack-test.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# Wrapper script for wasm-pack test that sets correct compiler on Mac + +# Detect Mac and set LLVM compiler +if [[ "$(uname -s)" == "Darwin" ]]; then + LLVM_PATH=$(brew --prefix llvm 2>/dev/null) + if [ -n "$LLVM_PATH" ]; then + export CC="$LLVM_PATH/bin/clang" + export AR="$LLVM_PATH/bin/llvm-ar" + fi +fi + +# Run wasm-pack test with all passed arguments +wasm-pack test "$@" +