diff --git a/.cargo/config.toml b/.cargo/config.toml index b148603255..4aedaf1800 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -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" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6926de817f..e3fcb61ba8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -11,6 +11,9 @@ on: arch: required: true type: string + release: + required: false + type: string jobs: build: @@ -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: | @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5933f7eef5..7cb558a671 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -5,7 +5,7 @@ on: - "v*.*.*" jobs: - prebuild: + build: strategy: fail-fast: ${{ startsWith(github.ref, 'refs/tags/') }} matrix: @@ -13,60 +13,25 @@ jobs: - 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 @@ -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 diff --git a/Makefile b/Makefile index 1697979b6e..7a86311a69 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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: diff --git a/README.md b/README.md index 4333c4a223..cb71b9f388 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pack b/pack index e1d971796e..d72ec06626 100755 --- a/pack +++ b/pack @@ -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" @@ -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) diff --git a/tests/compile.test.ts b/tests/compile.test.ts index 27d06753f0..395e78a6af 100644 --- a/tests/compile.test.ts +++ b/tests/compile.test.ts @@ -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); diff --git a/zigar b/zigar deleted file mode 100755 index 4a5011d578..0000000000 --- a/zigar +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -e - -zig ar "$@" \ No newline at end of file diff --git a/zigcc b/zigcc deleted file mode 100755 index 7b274676b1..0000000000 --- a/zigcc +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -set -e - -if [ -z ${COMPILE_TARGET+x} ]; then - COMPILE_TARGET="${CARGO_CFG_TARGET_ARCH}-${CARGO_CFG_TARGET_VENDOR}-${CARGO_CFG_TARGET_OS}-${CARGO_CFG_TARGET_ENV}" -fi - -CC_TARGET="" - -if [[ $COMPILE_TARGET == "x86_64-unknown-linux-gnu" ]]; then - CC_TARGET="x86_64-linux-gnu" -elif [[ $COMPILE_TARGET == "aarch64-unknown-linux-gnu" ]]; then - CC_TARGET="aarch64-linux-gnu" -elif [[ $COMPILE_TARGET == "x86_64-unknown-linux-musl" ]]; then - CC_TARGET="x86_64-linux-musl" -elif [[ $COMPILE_TARGET == "aarch64-unknown-linux-musl" ]]; then - CC_TARGET="aarch64-linux-musl" -fi - -new_array=() -for value in "$@" -do - [[ $value != *self-contained/*crt* ]] && new_array+=($value) -done - -zig cc -target $CC_TARGET "${new_array[@]}" \ No newline at end of file