Skip to content
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

Migrate to cargo zigbuild #77

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[unstable]
build-std = ["core", "compiler_builtins", "alloc", "std", "panic_abort"]
build-std-features = ["panic_immediate_abort"]

[env]
CC_aarch64_unknown_linux_gnu = "zig cc -target aarch64-linux-gnu"
CC_x86_64_unknown_linux_gnu = "zig cc -target x86_64-linux-gnu"
31 changes: 28 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ on:
arch:
required: true
type: string
release:
required: false
type: string

jobs:
build:
Expand Down Expand Up @@ -65,6 +68,9 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-
- name: Install cargo-zigbuild
run: |
cargo install cargo-zigbuild || true
- name: Run tests
if: inputs.arch != 'aarch64' || inputs.platform != 'linux'
run: |
Expand All @@ -76,12 +82,31 @@ jobs:
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUNNER: qemu-aarch64
run: |
sudo apt-get install -y \
libc6-arm64-cross \
libc6-dev-arm64-cross \
crossbuild-essential-arm64 \
qemu-system-arm \
qemu-efi-aarch64 \
qemu-utils \
qemu-user

make CURRENT_TARGET=aarch64-unknown-linux-gnu test-ci
- name: Build Linux binaries
if: inputs.release && inputs.platform == 'linux'
run: |
make llrt-lambda-${{ inputs.release }}.zip
make release-${{ inputs.release }}
- name: Build Darwin binaries
if: inputs.release && inputs.platform == 'darwin'
run: |
make llrt-darwin-${{ inputs.release }}.zip
- name: Upload artifacts
if: inputs.release
uses: actions/upload-artifact@v4
with:
name: artifacts-${{ inputs.platform }}
path: |
*.zip
- name: Upload changelog
if: inputs.release && inputs.platform == 'linux' && inputs.arch == 'x86_64'
uses: actions/upload-artifact@v4
with:
name: changelog
path: CHANGELOG.md
51 changes: 8 additions & 43 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,33 @@ on:
- "v*.*.*"

jobs:
prebuild:
build:
strategy:
fail-fast: ${{ startsWith(github.ref, 'refs/tags/') }}
matrix:
include:
- os: ubuntu-latest
platform: linux
arch: x86_64
release: x64
- os: ubuntu-latest
platform: linux
arch: aarch64
release: arm64
- os: macos-latest
platform: darwin
arch: x86_64
release: x64
- os: macos-latest
platform: darwin
arch: aarch64
release: arm64
uses: ./.github/workflows/build.yml
with:
os: ${{ matrix.os }}
platform: ${{ matrix.platform }}
arch: ${{ matrix.arch }}
build:
needs:
- prebuild
strategy:
fail-fast: ${{ startsWith(github.ref, 'refs/tags/') }}
matrix:
include:
- os: ubuntu-latest
platform: linux
arch: x86_64
- os: ubuntu-latest
platform: linux
arch: aarch64
- os: macos-latest
platform: darwin
arch: x86_64
- os: macos-latest
platform: darwin
arch: aarch64
runs-on: ${{ matrix.os }}
steps:
- name: Build binaries
run: |
make release
- name: Build Lambda binaries
if: matrix.platform == 'linux'
run: |
make release-lambda
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: artifacts-${{ matrix.platform }}
path: |
*.zip
- name: Upload changelog
if: matrix.platform == 'linux' && matrix.arch == 'x86_64'
uses: actions/upload-artifact@v4
with:
name: changelog
path: CHANGELOG.md
release: ${{ matrix.release }}
release:
permissions:
contents: write
Expand All @@ -87,5 +52,5 @@ jobs:
./artifacts-linux/llrt-lambda-arm64.zip
./artifacts-linux/llrt-linux-x64.zip
./artifacts-linux/llrt-linux-arm64.zip
./artifacts-darwin/llrt-macos-x64.zip
./artifacts-darwin/llrt-macos-arm64.zip
./artifacts-darwin/llrt-darwin-x64.zip
./artifacts-darwin/llrt-darwin-arm64.zip
93 changes: 29 additions & 64 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
TARGET_linux_x86_64 = x86_64-unknown-linux-gnu
TARGET_linux_arm64 = aarch64-unknown-linux-gnu
TARGET_clib = gnu
TARGET_linux_x86_64 = x86_64-unknown-linux-$(TARGET_clib)
TARGET_linux_arm64 = aarch64-unknown-linux-$(TARGET_clib)
TARGET_darwin_x86_64 = x86_64-apple-darwin
TARGET_darwin_arm64 = aarch64-apple-darwin
RUST_VERSION = nightly
TOOLCHAIN = +$(RUST_VERSION)
ZIGBUILD_ARG = $(TOOLCHAIN) zigbuild -r
BUILD_ARG = $(TOOLCHAIN) build -r
BUILD_DIR = ./target/release
BUNDLE_DIR = bundle
ZSTD_LIB_ARGS = -j lib-nomt CC="$(CURDIR)/zigcc -s -O3 -flto" UNAME=Linux ZSTD_LIB_COMPRESSION=0 ZSTD_LIB_DICTBUILDER=0
ZSTD_LIB_ARGS = -j lib-nomt UNAME=Linux ZSTD_LIB_COMPRESSION=0 ZSTD_LIB_DICTBUILDER=0 AR="zig ar"
ZSTD_LIB_CC_ARGS = -s -O3 -flto
ZSTD_LIB_CC_arm64 = CC="zig cc -target aarch64-linux-musl $(ZSTD_LIB_CC_ARGS)"
ZSTD_LIB_CC_x64 = CC="zig cc -target aarch64-linux-musl $(ZSTD_LIB_CC_ARGS)"
CARGO_TEST_CMD = cargo-zigbuild

TS_SOURCES = $(wildcard src/js/*.ts) $(wildcard src/js/@llrt/*.ts) $(wildcard tests/*.ts)
STD_JS_FILE = $(BUNDLE_DIR)/@llrt/std.js
Expand All @@ -28,59 +34,40 @@ else
endif

ifeq ($(DETECTED_OS),darwin)
export AR = $(CURDIR)/zigar
export CC_aarch64_unknown_linux_gnu = $(CURDIR)/zigcc
export CCX_aarch64_unknown_linux_gnu = $(CURDIR)/zigcc
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = $(CURDIR)/zigcc
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS = -Ctarget-feature=+lse -Ctarget-cpu=neoverse-n1

export CC_x86_64_unknown_linux_gnu = $(CURDIR)/zigcc
export CXX_x86_64_unknown_linux_gnu = $(CURDIR)/zigcc
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = $(CURDIR)/zigcc
else ifeq ($(DETECTED_OS),linux)
export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = x86_64-linux-gnu-gcc

export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS = -Clink-arg=-Wl,--allow-multiple-definition -Ctarget-feature=+lse -Ctarget-cpu=neoverse-n1
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = aarch64-linux-gnu-gcc

RUNFLAGS = RUSTFLAGS="-Clink-arg=-Wl,--allow-multiple-definition -Ctarget-feature=+crt-static"
CARGO_TEST_CMD = cargo
endif

CURRENT_TARGET ?= $(TARGET_$(DETECTED_OS)_$(ARCH))

export COMPILE_TARGET = $(CURRENT_TARGET)

lambda-all: clean-js | libs $(RELEASE_ZIPS)
release-all: clean-js | lambda-all llrt-linux-x64.zip llrt-linux-arm64.zip llrt-darwin-x64.zip llrt-darwin-arm64.zip

release-lambda: clean-js | libs-$(ARCH) $(LAMBDA_PREFIX)-$(DETECTED_OS)-$(ARCH).zip
release: clean-js | llrt-$(DETECTED_OS)-$(ARCH).zip
CURRENT_TARGET ?= $(TARGET_$(DETECTED_OS)_$(ARCH))

release-linux: clean-js | lambda-all llrt-linux-x64.zip llrt-linux-arm64.zip
release-darwin: clean-js | llrt-darwin-x64.zip llrt-darwin-arm64.zip
lambda-all: libs $(RELEASE_ZIPS)
release-all: | lambda-all llrt-linux-x64.zip llrt-linux-arm64.zip llrt-darwin-x64.zip llrt-darwin-arm64.zip
release: llrt-$(DETECTED_OS)-$(ARCH).zip
release-linux: | lambda-all llrt-linux-x64.zip llrt-linux-arm64.zip
release-darwin: | llrt-darwin-x64.zip llrt-darwin-arm64.zip

llrt-darwin-x64.zip: js
llrt-darwin-x64.zip: | clean-js js
cargo $(BUILD_ARG) --target $(TARGET_darwin_x86_64)
zip -j $@ target/$(TARGET_darwin_x86_64)/release/llrt

llrt-darwin-arm64.zip: js
llrt-darwin-arm64.zip: | clean-js js
cargo $(BUILD_ARG) --target $(TARGET_darwin_arm64)
zip -j $@ target/$(TARGET_darwin_arm64)/release/llrt

llrt-linux-x64.zip: js
cargo $(BUILD_ARG) --target $(TARGET_linux_x86_64)
llrt-linux-x64.zip: | clean-js js
cargo $(ZIGBUILD_ARG) --target $(TARGET_linux_x86_64)
zip -j $@ target/$(TARGET_linux_x86_64)/release/llrt

llrt-linux-arm64.zip: js
cargo $(BUILD_ARG) --target $(TARGET_linux_arm64)
llrt-linux-arm64.zip: | clean-js js
cargo $(ZIGBUILD_ARG) --target $(TARGET_linux_arm64)
zip -j $@ target/$(TARGET_linux_arm64)/release/llrt

define release_template
release-${1}: js
COMPILE_TARGET=$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})) cargo $$(BUILD_ARG) --target $$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})) --features lambda -vv
release-${1}: | clean-js js
cargo $$(ZIGBUILD_ARG) --target $$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})) --features lambda -vv
./pack target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/llrt target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap
@rm -rf llrt-lambda-${1}.zip
zip -j llrt-lambda-${1}.zip target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap index.mjs
zip -j llrt-lambda-${1}.zip target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap

llrt-lambda-${1}: release-${1}
endef
Expand Down Expand Up @@ -115,18 +102,11 @@ js: $(STD_JS_FILE)
bundle/%.js: $(TS_SOURCES)
node build.mjs

patch:
cargo clean -p rquickjs-sys
cargo patch

fix:
cargo fix --allow-dirty
cargo clippy --fix --allow-dirty
cargo fmt

linux-flame: js
cargo build --profile=flame --target $(TARGET_linux_x86_64)

bloat: js
cargo build --profile=flame --target $(TARGET_linux_x86_64)
cargo bloat --profile=flame --crates
Expand All @@ -136,27 +116,12 @@ run: export AWS_LAMBDA_FUNCTION_MEMORY_SIZE = 1
run: export AWS_LAMBDA_FUNCTION_VERSION = 1
run: export AWS_LAMBDA_RUNTIME_API = localhost:3000
run: export _EXIT_ITERATIONS = 1
run: export AWS_REGION=eu-north-1
run: export TABLE_NAME=quickjs-table
run: export BUCKET_NAME=llrt-demo-bucket2
run: export JS_MINIFY = 0
run: export RUST_LOG = llrt=trace
run: export _HANDLER = index.handler
run: js
cargo run -r -vv

run-js: export _HANDLER = index.handler
run-js:
touch build.rs
cargo run

run-release: export _HANDLER = fixtures/local.handler
run-release: js
cargo build
time target/release/llrt
time target/release/llrt
time target/release/llrt

run-ssr: export AWS_LAMBDA_RUNTIME_API = localhost:3000
run-ssr: export TABLE_NAME=quickjs-table
run-ssr: export AWS_REGION = us-east-1
Expand All @@ -179,8 +144,8 @@ test: js

test-ci: export JS_MINIFY = 0
test-ci: clean-js | toolchain js
$(RUNFLAGS) cargo $(TOOLCHAIN) -Z panic-abort-tests test --target $(CURRENT_TARGET)
$(RUNFLAGS) cargo $(TOOLCHAIN) run -r --target $(CURRENT_TARGET) -- test -d bundle
$(CARGO_TEST_CMD) $(TOOLCHAIN) -Z panic-abort-tests test --target $(CURRENT_TARGET)
$(CARGO_TEST_CMD) $(TOOLCHAIN) run -r --target $(CURRENT_TARGET) -- test -d bundle

libs-arm64: lib/arm64/libzstd.a lib/zstd.h
libs-x64: lib/x64/libzstd.a lib/zstd.h
Expand All @@ -193,13 +158,13 @@ lib/zstd.h:
lib/arm64/libzstd.a:
mkdir -p $(dir $@)
rm -f zstd/lib/-.o
cd zstd/lib && make clean && COMPILE_TARGET=$(TARGET_linux_arm64) make $(ZSTD_LIB_ARGS)
cd zstd/lib && make clean && make $(ZSTD_LIB_ARGS) $(ZSTD_LIB_CC_arm64)
cp zstd/lib/libzstd.a $@

lib/x64/libzstd.a:
mkdir -p $(dir $@)
rm -f zstd/lib/-.o
cd zstd/lib && make clean && COMPILE_TARGET=$(TARGET_linux_x86_64) make $(ZSTD_LIB_ARGS)
cd zstd/lib && make clean && make $(ZSTD_LIB_ARGS) $(ZSTD_LIB_CC_x64)
cp zstd/lib/libzstd.a $@

bench:
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,13 @@ Install dependencies
# MacOS
brew install zig make zstd node corepack

# Ubuntu x
sudo apt -y install make zstd gcc \
libc6-arm64-cross \
libc6-dev-arm64-cross \
crossbuild-essential-arm64
# Ubuntu
sudo apt -y install make zstd
sudo snap install zig --classic --beta

Install cargo zigbuild

cargo install cargo-zigbuild

Clone code and cd to directory

Expand Down
6 changes: 3 additions & 3 deletions pack
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ fi
arch_hex=$(hexdump -s 18 -n 1 -e '"%02x"' $srcfile)

if [[ "$arch_hex" == "3e" ]]; then
target="x86_64-linux-gnu"
target="x86_64-linux-musl"
arch="x64"
elif [[ "$arch_hex" == "b7" ]]; then
target="aarch64-linux-gnu"
target="aarch64-linux-musl"
arch="arm64"
else
echo "The binary is not an ARM64 or X86_64 ELF executable"
Expand Down Expand Up @@ -97,7 +97,7 @@ echo "Compiling..."

original_size=$(du -h $srcfile | cut -f1)

zig cc -target $target -Wno-null-character -std=c99 -Wall -O3 -flto -s ${temp_folder}/main.c -o $dstfile -Ilib -Llib/${arch} -Wl,-Bstatic -lzstd -Wl,-Bdynamic -lpthread -lrt -lc
zig cc -target $target -Wno-null-character -std=c99 -Wall -O3 -flto -s ${temp_folder}/main.c -o $dstfile -Ilib -Llib/${arch} -static -lzstd

new_size=$(du -h $dstfile | cut -f1)

Expand Down
2 changes: 1 addition & 1 deletion tests/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe("llrt compile", async () => {
assert.strictEqual(runResult.status, 0);
});

it.only("can compile and run throws", async () => {
it("can compile and run throws", async () => {
const tmpOutput = `${tmpDir}/throws.lrt`;

const compileResult = await compile("fixtures/throw.js", tmpOutput);
Expand Down
4 changes: 0 additions & 4 deletions zigar

This file was deleted.

Loading