-
Notifications
You must be signed in to change notification settings - Fork 21
Replace wasm-pack #690
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: main
Are you sure you want to change the base?
Replace wasm-pack #690
Changes from all commits
7b38396
58b10b7
cb30942
46606cc
0edb697
04c2bf9
e18545b
8f2cc17
14c490d
3dc67dd
1b1ee7c
6aacd51
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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| target/ | ||
| target/ | ||
| .bin/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| #!/usr/bin/env pwsh | ||
| $ErrorActionPreference = "Stop" | ||
|
|
||
| # ------ CONFIGURATION ------ | ||
| $TARGET = "nodejs" | ||
| $OUT_DIR = "..\library\agent\hooks\instrumentation\wasm" | ||
| $GENERATE_DTS = $false | ||
|
|
||
| $BINARYEN_VERSION = 124 | ||
| $WASM_BINDGEN_VERSION = "0.2.101" | ||
|
|
||
| # --------------------------- | ||
|
|
||
| Write-Host "Starting build process for wasm module" | ||
|
|
||
| # Always run from script's own directory | ||
| $SCRIPT_DIR = Split-Path -Parent (Resolve-Path $MyInvocation.MyCommand.Path) | ||
| Push-Location $SCRIPT_DIR | ||
|
|
||
| # Create directories | ||
| New-Item -ItemType Directory -Path ".\.bin\tmp" -Force | Out-Null | ||
| New-Item -ItemType Directory -Path $OUT_DIR -Force | Out-Null | ||
|
|
||
| function Download-Binaryen { | ||
|
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. Duplicate download/extract logic in Download-Binaryen and Download-WasmBindgen should be consolidated into a shared helper to avoid repeated maintenance. Details✨ AI Reasoning 🔧 How do I fix it? More info - Comment |
||
| $BINARYEN_BASE_URL = "https://github.com/WebAssembly/binaryen/releases/download/version_$BINARYEN_VERSION" | ||
| $BINARYEN_EXTRACT_DIR = "binaryen" | ||
| $ARCH = "x86_64" | ||
| $OS = "windows" | ||
|
|
||
| $FILE = "binaryen-version_${BINARYEN_VERSION}-${ARCH}-${OS}.tar.gz" | ||
|
|
||
| Push-Location ".\.bin\tmp" | ||
| Write-Host "Downloading from $BINARYEN_BASE_URL/$FILE" | ||
| Invoke-WebRequest "$BINARYEN_BASE_URL/$FILE" -OutFile $FILE | ||
| New-Item -ItemType Directory -Path "..\$BINARYEN_EXTRACT_DIR" -Force | Out-Null | ||
| tar -xzf $FILE -C "..\$BINARYEN_EXTRACT_DIR" --strip-components=1 | ||
| Pop-Location | ||
| } | ||
|
|
||
| function Download-WasmBindgen { | ||
| $WASM_BINDGEN_URL_BASE = "https://github.com/wasm-bindgen/wasm-bindgen/releases/download/$WASM_BINDGEN_VERSION" | ||
| $WASM_BINDGEN_DIR = "wasm-bindgen" | ||
| $ARCH = "x86_64" | ||
| $OS = "pc-windows-msvc" | ||
|
|
||
| $FILE = "wasm-bindgen-${WASM_BINDGEN_VERSION}-${ARCH}-${OS}.tar.gz" | ||
|
|
||
| Push-Location ".\.bin\tmp" | ||
| Write-Host "Downloading from $WASM_BINDGEN_URL_BASE/$FILE" | ||
| Invoke-WebRequest "$WASM_BINDGEN_URL_BASE/$FILE" -OutFile $FILE | ||
| New-Item -ItemType Directory -Path "..\$WASM_BINDGEN_DIR" -Force | Out-Null | ||
| tar -xzf $FILE -C "..\$WASM_BINDGEN_DIR" --strip-components=1 | ||
| Pop-Location | ||
| } | ||
|
|
||
| # Ensure rustc >= 1.30.0 | ||
| $MIN_RUSTC_VERSION = "1.30.0" | ||
| $RUSTC_VERSION = (& rustc --version).Split()[1] | ||
|
|
||
| if ([Version]$RUSTC_VERSION -lt [Version]$MIN_RUSTC_VERSION) { | ||
| Write-Error "rustc >= $MIN_RUSTC_VERSION required, found $RUSTC_VERSION" | ||
| exit 1 | ||
| } | ||
|
|
||
| Write-Host "Using rustc version: $RUSTC_VERSION" | ||
|
|
||
| # Ensure wasm32 target is installed | ||
| $LIBDIR = & rustc --target wasm32-unknown-unknown --print target-libdir | ||
| if (-not (Test-Path $LIBDIR)) { | ||
| Write-Host "Target wasm32-unknown-unknown not installed. Installing..." | ||
| if (-not (Get-Command rustup -ErrorAction SilentlyContinue)) { | ||
| Write-Error "rustup not installed. Install target manually or install rustup." | ||
| exit 1 | ||
| } | ||
| rustup target add wasm32-unknown-unknown | ||
| } | ||
|
|
||
| # Check binaryen | ||
| if (-not (Test-Path ".bin\binaryen")) { | ||
| Write-Host "Downloading binaryen..." | ||
| Download-Binaryen | ||
| } | ||
| else { | ||
| Write-Host "Found existing binaryen installation." | ||
| } | ||
|
|
||
| # Check wasm-bindgen | ||
| if (-not (Test-Path ".bin\wasm-bindgen")) { | ||
| Write-Host "Downloading wasm-bindgen..." | ||
| Download-WasmBindgen | ||
| } | ||
| else { | ||
| Write-Host "Found existing wasm-bindgen installation." | ||
| } | ||
|
|
||
| Remove-Item -Recurse -Force ".\.bin\tmp" | ||
|
|
||
| # Build wasm module | ||
| Write-Host "Building wasm module..." | ||
|
|
||
| cargo build --target wasm32-unknown-unknown --release | ||
|
|
||
| # Extract crate name using PowerShell JSON parsing | ||
| $metadata = & cargo metadata --format-version=1 --no-deps | ||
| $CRATE_NAME = ($metadata | ConvertFrom-Json).packages[0].name | ||
| $WASM_PATH = "target\wasm32-unknown-unknown\release\${CRATE_NAME}.wasm" | ||
|
|
||
| # wasm-bindgen | ||
| Write-Host "Running wasm-bindgen..." | ||
| $WASM_BINDGEN_OPTS = @("--out-dir", $OUT_DIR, "--target", $TARGET) | ||
| if (-not $GENERATE_DTS) { | ||
| $WASM_BINDGEN_OPTS += "--no-typescript" | ||
| } | ||
| & ".\.bin\wasm-bindgen\wasm-bindgen.exe" @WASM_BINDGEN_OPTS $WASM_PATH | ||
|
|
||
| # wasm-opt | ||
| Write-Host "Running wasm-opt..." | ||
| & ".\.bin\binaryen\bin\wasm-opt.exe" -O3 --enable-bulk-memory --enable-nontrapping-float-to-int -o "$OUT_DIR\${CRATE_NAME}_bg.wasm" "$OUT_DIR\${CRATE_NAME}_bg.wasm" | ||
|
|
||
| Write-Host "Build completed successfully. Output is in $OUT_DIR" | ||
|
|
||
| Pop-Location | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| #!/usr/bin/env bash | ||
| set -e | ||
|
|
||
| # ------ CONFIGURATION ------ | ||
|
|
||
| TARGET=nodejs | ||
| OUT_DIR=../library/agent/hooks/instrumentation/wasm | ||
| GENERATE_DTS=false | ||
|
|
||
| BINARYEN_VERSION=124 # wasm-opt | ||
| WASM_BINDGEN_VERSION=0.2.101 | ||
|
|
||
| # --------------------------- | ||
|
|
||
| echo "Starting build process for wasm module" | ||
|
|
||
| # Always run from script's own directory and return at the end | ||
| SCRIPT_DIR="$(cd "$(dirname "$(realpath "$0")")" && pwd)" | ||
| pushd "$SCRIPT_DIR" > /dev/null | ||
|
|
||
| # Create directories | ||
|
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. Comment '# Create directories' only states what the code does; explain why these directories are needed instead. Details✨ AI Reasoning 🔧 How do I fix it? More info - Comment |
||
| mkdir -p ./.bin/tmp | ||
| mkdir -p "$OUT_DIR" | ||
|
|
||
| # ------ Functions ------ | ||
|
|
||
| download_binaryen() { | ||
|
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. Function download_binaryen() implements multi-step OS/ARCH detection and archive download/extract but lacks an explanatory comment. Details✨ AI Reasoning 🔧 How do I fix it? More info - Comment |
||
| set -e | ||
|
|
||
| BINARYEN_BASE_URL="https://github.com/WebAssembly/binaryen/releases/download/version_$BINARYEN_VERSION" | ||
| BINARYEN_EXTRACT_DIR="binaryen" | ||
|
|
||
| # Detect OS | ||
| case "$(uname -s)" in | ||
| Linux*) OS="linux" ;; | ||
| Darwin*) OS="macos" ;; | ||
| *) echo "Unsupported OS"; exit 1 ;; | ||
| esac | ||
|
|
||
| # Detect ARCH | ||
| case "$(uname -m)" in | ||
| x86_64) ARCH="x86_64" ;; | ||
| aarch64|arm64) ARCH="arm64" ;; | ||
| *) echo "Unsupported arch"; exit 1 ;; | ||
| esac | ||
|
|
||
| # Adjust ARM label for Linux | ||
| [ "$ARCH" = "arm64" ] && [ "$OS" = "linux" ] && ARCH="aarch64" | ||
|
|
||
| FILE="binaryen-version_${BINARYEN_VERSION}-${ARCH}-${OS}.tar.gz" | ||
|
|
||
| mkdir -p ./.bin/tmp | ||
|
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. Obvious duplication: repeated download/extract/cleanup sequence appears in both download_binaryen() and download_wasm_bindgen(). Details✨ AI Reasoning 🔧 How do I fix it? More info - Comment |
||
| cd ./.bin/tmp | ||
|
|
||
| echo "Downloading from $BINARYEN_BASE_URL/$FILE" | ||
|
|
||
| curl -LO --fail "$BINARYEN_BASE_URL/$FILE" | ||
|
|
||
| mkdir -p "../$BINARYEN_EXTRACT_DIR" | ||
| tar -xzf "$FILE" -C "../$BINARYEN_EXTRACT_DIR" --strip-components=1 | ||
|
|
||
| cd ../.. | ||
| rm -rf ./.bin/tmp | ||
| } | ||
|
|
||
| download_wasm_bindgen() { | ||
| set -e | ||
|
|
||
| WASM_BINDGEN_URL_BASE="https://github.com/wasm-bindgen/wasm-bindgen/releases/download/$WASM_BINDGEN_VERSION" | ||
| WASM_BINDGEN_DIR="wasm-bindgen" | ||
|
|
||
| # Detect OS and ARCH for wasm-bindgen | ||
| case "$(uname -s)" in | ||
| Linux*) | ||
| case "$(uname -m)" in | ||
| x86_64) | ||
| OS="unknown-linux-musl" | ||
| ARCH="x86_64" | ||
| ;; | ||
| aarch64|arm64) | ||
| OS="unknown-linux-gnu" | ||
| ARCH="aarch64" | ||
| ;; | ||
| *) echo "Unsupported arch for Linux"; exit 1 ;; | ||
| esac | ||
| ;; | ||
| Darwin*) | ||
| OS="apple-darwin" | ||
| case "$(uname -m)" in | ||
| x86_64) ARCH="x86_64" ;; | ||
| aarch64|arm64) ARCH="aarch64" ;; | ||
| *) echo "Unsupported arch for Darwin"; exit 1 ;; | ||
| esac | ||
| ;; | ||
| *) echo "Unsupported OS"; exit 1 ;; | ||
| esac | ||
|
|
||
| FILE="wasm-bindgen-${WASM_BINDGEN_VERSION}-${ARCH}-${OS}.tar.gz" | ||
|
|
||
| mkdir -p ./.bin/tmp | ||
| cd ./.bin/tmp | ||
|
|
||
| echo "Downloading from $WASM_BINDGEN_URL_BASE/$FILE" | ||
|
|
||
| curl -LO --fail "$WASM_BINDGEN_URL_BASE/$FILE" | ||
|
|
||
| mkdir -p "../$WASM_BINDGEN_DIR" | ||
| tar -xzf "$FILE" -C "../$WASM_BINDGEN_DIR" --strip-components=1 | ||
|
|
||
| cd ../.. | ||
| rm -rf ./.bin/tmp | ||
| } | ||
|
|
||
| # ------ Main Script ------ | ||
|
|
||
| # Ensure rustc >= 1.30.0 | ||
| MIN_RUSTC_VERSION=1.30.0 | ||
| RUSTC_VERSION=$(rustc --version | cut -d' ' -f2) | ||
|
|
||
| # Compare versions using sort | ||
| if ! printf '%s\n%s\n' "$MIN_RUSTC_VERSION" "$RUSTC_VERSION" | sort -VC; then | ||
| echo "rustc >= $MIN_RUSTC_VERSION required, found $RUSTC_VERSION" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Using rustc version: $RUSTC_VERSION" | ||
|
|
||
| # Ensure wasm32 target is installed | ||
| LIBDIR=$(rustc --target wasm32-unknown-unknown --print target-libdir) | ||
|
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. Calling 'rustc --target ... --print target-libdir' will fail (and exit the script due to set -e) if the target isn't installed, preventing the intended installation check. Details✨ AI Reasoning 🔧 How do I fix it? More info - Comment |
||
|
|
||
| if [ ! -d "$LIBDIR" ]; then | ||
| echo "Target wasm32-unknown-unknown not installed. Installing using rustup..." | ||
|
|
||
| # Check if rustup is installed | ||
| if ! command -v rustup &> /dev/null; then | ||
| echo "rustup is not installed. Please install the wasm32-unknown-unknown target manually or install rustup." | ||
| exit 1 | ||
| fi | ||
|
|
||
| rustup target add wasm32-unknown-unknown | ||
| fi | ||
|
|
||
| # Check if binaryen is already downloaded | ||
| if [ ! -d ".bin/binaryen" ]; then | ||
| echo "Downloading binaryen..." | ||
| download_binaryen | ||
| else | ||
| echo "Found existing binaryen installation." | ||
| fi | ||
|
|
||
| # Check if wasm-bindgen is already downloaded | ||
| if [ ! -d ".bin/wasm-bindgen" ]; then | ||
| echo "Downloading wasm-bindgen..." | ||
| download_wasm_bindgen | ||
| else | ||
| echo "Found existing wasm-bindgen installation." | ||
| fi | ||
|
|
||
| # Build the wasm module | ||
| echo "Building wasm module..." | ||
| cargo build --target wasm32-unknown-unknown --release | ||
|
|
||
| CRATE_NAME=$(basename "$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[0].name')") | ||
| WASM_PATH=target/wasm32-unknown-unknown/release/${CRATE_NAME}.wasm | ||
|
|
||
| # wasm-bindgen | ||
| echo "Running wasm-bindgen..." | ||
| WASM_BINDGEN_OPTS=(--out-dir "$OUT_DIR" --target "$TARGET") | ||
| if [ "$GENERATE_DTS" != true ]; then | ||
| WASM_BINDGEN_OPTS+=(--no-typescript) | ||
| fi | ||
| ./.bin/wasm-bindgen/wasm-bindgen "${WASM_BINDGEN_OPTS[@]}" "$WASM_PATH" | ||
|
|
||
| # wasm-opt | ||
| echo "Running wasm-opt..." | ||
| ./.bin/binaryen/bin/wasm-opt -O3 --enable-bulk-memory --enable-nontrapping-float-to-int -o "$OUT_DIR/${CRATE_NAME}_bg.wasm" "$OUT_DIR/${CRATE_NAME}_bg.wasm" | ||
|
|
||
| echo "Build completed successfully. Output is in $OUT_DIR" | ||
|
|
||
| # Switch back to the original directory | ||
| popd > /dev/null | ||
Uh oh!
There was an error while loading. Please reload this page.