From 8095599141916ffaeab7c26f5fab75e827de2366 Mon Sep 17 00:00:00 2001 From: Jon C Date: Sun, 7 Jul 2024 17:41:16 +0200 Subject: [PATCH] Update to using zig package manager --- .github/workflows/main.yml | 6 ++-- .gitignore | 2 +- .gitmodules | 3 -- README.md | 10 +++--- build.zig | 19 +++++++---- build.zig.zon | 46 +++++++++++++++++++++++++++ install-solana-zig.sh | 51 ++++++++++++++++++++++++++++++ program-test/install-build-deps.sh | 21 ++++++++++++ sol | 1 - src/main.zig | 2 +- 10 files changed, 139 insertions(+), 22 deletions(-) create mode 100644 build.zig.zon create mode 100755 install-solana-zig.sh create mode 100755 program-test/install-build-deps.sh delete mode 160000 sol diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 74769ca..12160d1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,7 +27,7 @@ jobs: - name: Download solana-zig compiler shell: bash - run: SOLANA_ZIG_VERSION=$SOLANA_ZIG_VERSION ./sol/install-solana-zig.sh $SOLANA_ZIG_DIR + run: SOLANA_ZIG_VERSION=$SOLANA_ZIG_VERSION ./install-solana-zig.sh $SOLANA_ZIG_DIR - name: Test program shell: bash @@ -57,7 +57,7 @@ jobs: - name: Download solana-zig compiler shell: bash - run: SOLANA_ZIG_VERSION=$SOLANA_ZIG_VERSION ./sol/install-solana-zig.sh $SOLANA_ZIG_DIR + run: SOLANA_ZIG_VERSION=$SOLANA_ZIG_VERSION ./install-solana-zig.sh $SOLANA_ZIG_DIR - name: Install Rust uses: dtolnay/rust-toolchain@master @@ -66,7 +66,7 @@ jobs: - name: Install build deps shell: bash - run: ./sol/program-test/install-build-deps.sh + run: ./program-test/install-build-deps.sh - name: Build and test program shell: bash diff --git a/.gitignore b/.gitignore index cd31e23..3c163d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -solana-zig/ +solana-zig zig-cache/ zig-out/ diff --git a/.gitmodules b/.gitmodules index 95faa24..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "sol"] - path = sol - url = https://github.com/joncinque/solana-sdk-zig.git diff --git a/README.md b/README.md index 39f744b..fa2b1ad 100644 --- a/README.md +++ b/README.md @@ -11,16 +11,14 @@ First, you need a zig compiler built with Solana's LLVM fork. See the README of on how to build it, or you can download it from the [GitHub releases page](https://github.com/joncinque/solana-zig-bootstrap/releases). -### Submodules +### Dependencies -Since zig's package manager is still in development, this project opts for -directly including the upstream Solana package requirements, hosted at +This project opts for the zig package manager and the package declared at [solana-sdk-zig](https://github.com/joncinque/solana-sdk-zig). -This repo uses git submodules: - ```console -$ git submodule update --init --recursive +zig fetch --save https://github.com/joncinque/base58-zig/archive/refs/tags/v0.12.2.tar.gz +zig fetch --save https://github.com/joncinque/solana-sdk-zig/archive/refs/tags/v0.12.0.tar.gz ``` ### Build diff --git a/build.zig b/build.zig index 8de8945..45978a0 100644 --- a/build.zig +++ b/build.zig @@ -1,20 +1,25 @@ const std = @import("std"); -const sol = @import("sol/sol.zig"); +const solana = @import("solana-program-sdk"); pub fn build(b: *std.Build) !void { + const target = b.resolveTargetQuery(solana.sbf_target); + const optimize = .ReleaseSmall; const program = b.addSharedLibrary(.{ .name = "helloworld", .root_source_file = .{ .path = "src/main.zig" }, - .optimize = .ReleaseSmall, - .target = b.resolveTargetQuery(sol.sbf_target), + .target = target, + .optimize = optimize, }); - try sol.buildProgram(b, program, "sol/"); + + // Adding required dependencies, link the program properly, and get a + // prepared modules + const solana_mod = solana.buildProgram(b, program, target, optimize); const test_step = b.step("test", "Run unit tests"); - const unit_tests = b.addTest(.{ + const lib_unit_tests = b.addTest(.{ .root_source_file = .{ .path = "src/main.zig" }, }); - sol.addSolModules(b, unit_tests, "sol/"); - const run_unit_tests = b.addRunArtifact(unit_tests); + lib_unit_tests.root_module.addImport("solana-program-sdk", solana_mod); + const run_unit_tests = b.addRunArtifact(lib_unit_tests); test_step.dependOn(&run_unit_tests.step); } diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..c234b36 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,46 @@ +.{ + .name = "solana-helloworld-zig", + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.12.0", + + // This field is optional. + // This is currently advisory only; Zig does not yet do anything + // with this value. + .minimum_zig_version = "0.12.0", + + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + .base58 = .{ + .url = "https://github.com/joncinque/base58-zig/archive/refs/tags/v0.12.2.tar.gz", + .hash = "12203cf62befaa692c11ed82c1cea15ae6da153a87d89a969b006d99267fc4ba8626", + }, + .clap = .{ + .url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.8.0.tar.gz", + .hash = "1220949d4e88864579067b6d4cdad6476c6176f27e782782c2c39b7f2c4817a10efb", + }, + .@"solana-program-sdk" = .{ + .url = "https://github.com/joncinque/solana-sdk-zig/archive/refs/tags/v0.12.0.tar.gz", + .hash = "122027f10b77c99010365b20f2bb32314fccee9814deec30ac70258f6ec62654d859", + }, + }, + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + // For example... + "build.zig", + "build.zig.zon", + "src", + "LICENSE", + "README.md", + }, +} diff --git a/install-solana-zig.sh b/install-solana-zig.sh new file mode 100755 index 0000000..67c78db --- /dev/null +++ b/install-solana-zig.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +if [[ -n $SOLANA_ZIG_VERSION ]]; then + solana_zig_version="$SOLANA_ZIG_VERSION" +else + solana_zig_version="v1.41" +fi +solana_zig_release_url="https://github.com/joncinque/solana-zig-bootstrap/releases/download/solana-$solana_zig_version" + +output_dir="$1" +if [[ -z $output_dir ]]; then + output_dir="solana-zig" +fi +output_dir="$(mkdir -p "$output_dir"; cd "$output_dir"; pwd)" +cd $output_dir + +arch=$(uname -m) +if [[ "$arch" == "arm64" ]]; then + arch="aarch64" +fi +case $(uname -s | cut -c1-7) in +"Linux") + os="linux" + abi="musl" + ;; +"Darwin") + os="macos" + abi="none" + ;; +"Windows" | "MINGW64") + os="windows" + abi="gnu" + ;; +*) + echo "install-solana-zig.sh: Unknown OS $(uname -s)" >&2 + exit 1 + ;; +esac + +solana_zig_tar=zig-$arch-$os-$abi.tar.bz2 +url="$solana_zig_release_url/$solana_zig_tar" +echo "Downloading $url" +curl --proto '=https' --tlsv1.2 -SfOL "$url" +echo "Unpacking $solana_zig_tar" +tar -xjf $solana_zig_tar +rm $solana_zig_tar + +solana_zig_dir="zig-$arch-$os-$abi-baseline" +mv "$solana_zig_dir"/* . +rmdir $solana_zig_dir +echo "solana-zig compiler available at $output_dir" diff --git a/program-test/install-build-deps.sh b/program-test/install-build-deps.sh new file mode 100755 index 0000000..5c5931f --- /dev/null +++ b/program-test/install-build-deps.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -e +case $(uname -s | cut -c1-7) in +"Windows" | "MINGW64") + vcpkg install openssl:x64-windows-static-md + vcpkg integrate install + choco install protoc + export PROTOC='C:\ProgramData\chocolatey\lib\protoc\tools\bin\protoc.exe' + ;; +"Darwin") + brew install protobuf + ;; +"Linux") + sudo apt update + sudo apt install protobuf-compiler -y + ;; +*) + echo "Unknown Operating System" + exit 1 + ;; +esac diff --git a/sol b/sol deleted file mode 160000 index fc63ed4..0000000 --- a/sol +++ /dev/null @@ -1 +0,0 @@ -Subproject commit fc63ed4f73bebd3e4cd8d6110e92e8820b900629 diff --git a/src/main.zig b/src/main.zig index ef0732e..fc52de4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const sol = @import("sol"); +const sol = @import("solana-program-sdk"); const ix = @import("instruction.zig"); const state = @import("state.zig");