Skip to content

Commit 3a0d437

Browse files
committed
Update to Zig 0.13
1 parent a145362 commit 3a0d437

File tree

7 files changed

+43
-40
lines changed

7 files changed

+43
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
solana-zig/
66
zig-cache/
77
zig-out/
8+
.zig-cache/

README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# solana-zig-sdk
1+
# solana-program-sdk-zig
22

33
Write Solana on-chain programs in Zig!
44

@@ -10,6 +10,7 @@ which also provides tests and a CLI.
1010

1111
Here are some other packages to help with developing Solana programs with Zig:
1212

13+
* [Base-58](https://github.com/joncinque/base58-zig)
1314
* [Bincode](https://github.com/joncinque/bincode-zig)
1415
* [Borsh](https://github.com/joncinque/borsh-zig)
1516
* [Solana Program Library](https://github.com/joncinque/solana-program-library-zig)
@@ -36,16 +37,24 @@ You can run the convenience script in this repo to download the compiler to
3637
1. Add this package and `base58-zig` to your project:
3738

3839
```console
39-
zig fetch --save https://github.com/joncinque/base58-zig/archive/refs/tags/v0.12.2.tar.gz
40-
zig fetch --save https://github.com/joncinque/solana-sdk-zig/archive/refs/tags/v0.12.0.tar.gz
40+
zig fetch --save https://github.com/joncinque/base58-zig/archive/refs/tags/v0.13.3.tar.gz
41+
zig fetch --save https://github.com/joncinque/solana-program-sdk-zig/archive/refs/tags/v0.13.0.tar.gz
4142
```
4243

43-
2. In your build.zig, add the modules that you want one by one, or use the
44+
2. (Optional) if you want to generate a keypair during building, you'll also
45+
need to install clap:
46+
47+
```console
48+
zig fetch --save https://github.com/Hejsil/zig-clap/archive/refs/tags/0.9.1.tar.gz
49+
```
50+
51+
3. In your build.zig, add the modules that you want one by one, or use the
4452
helpers in `build.zig`:
4553

4654
```zig
4755
const std = @import("std");
4856
const solana = @import("solana-program-sdk");
57+
const base58 = @import("base58");
4958
5059
pub fn build(b: *std.build.Builder) !void {
5160
// Choose the on-chain target (bpf, sbf v1, sbf v2, etc)
@@ -60,28 +69,33 @@ pub fn build(b: *std.build.Builder) !void {
6069
const program = b.addSharedLibrary(.{
6170
.name = "program_name",
6271
// Give the root of your program, where the entrypoint is defined
63-
.root_source_file = .{ .path = "src/main.zig" },
72+
.root_source_file = b.path("src/main.zig"),
6473
.optimize = optimize,
6574
.target = target,
6675
});
6776
// Use the `buildProgram` helper to create the solana-sdk module, add all
68-
// of its required dependencies, link the program properly, and generate
69-
// a keypair in `zig-out/lib` along with the compiled program.
77+
// of its required dependencies, link the program properly.
7078
const solana_mod = solana.buildProgram(b, program, target, optimize);
7179
80+
// Install the program artifact
81+
b.installArtifact(program);
82+
83+
// Optional: to generate a keypair in `zig-out/lib`, be sure to run this too:
84+
base58.generateProgramKeypair(b, program);
85+
7286
// Optional, but if you define unit tests in your program files, you can run
7387
// them with `zig build test` with this step included
7488
const test_step = b.step("test", "Run unit tests");
7589
const lib_unit_tests = b.addTest(.{
76-
.root_source_file = .{ .path = "src/main.zig" },
90+
.root_source_file = b.path("src/main.zig"),
7791
});
7892
lib_unit_tests.root_module.addImport("solana-program-sdk", solana_mod);
7993
const run_unit_tests = b.addRunArtifact(lib_unit_tests);
8094
test_step.dependOn(&run_unit_tests.step);
8195
}
8296
```
8397

84-
3. Setup `src/main.zig`:
98+
4. Setup `src/main.zig`:
8599

86100
```zig
87101
const solana = @import("solana-program-sdk");
@@ -92,13 +106,13 @@ export fn entrypoint(_: [*]u8) callconv(.C) u64 {
92106
}
93107
```
94108

95-
4. Download the solana-zig compiler using the script in this repository:
109+
5. Download the solana-zig compiler using the script in this repository:
96110

97111
```console
98112
$ ./install-solana-zig.sh
99113
```
100114

101-
5. Build and deploy your program on Solana devnet:
115+
6. Build and deploy your program on Solana devnet:
102116

103117
```console
104118
$ ./solana-zig/zig build --summary all
@@ -123,8 +137,8 @@ The helpers in build.zig contain various Solana targets. Here are their analogue
123137
to the Rust build tools:
124138

125139
* `sbf_target` -> `cargo build-sbf`
126-
* `bpf_target` -> `cargo build-bpf`
127140
* `sbfv2_target` -> `cargo build-sbf --arch sbfv2`
141+
* **Deprecated** `bpf_target` -> `cargo build-bpf`
128142

129143
## Unit tests
130144

@@ -150,4 +164,4 @@ cd program-test/
150164
```
151165

152166
These tests require a Rust compiler along with the solana-zig compiler, as
153-
mentioned in the prerequisites.
167+
mentioned in the prerequisites. Be sure to run `./install-solana-zig.sh` first.

build.zig

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const std = @import("std");
2-
const generateKeypairRunStep = @import("base58").generateKeypairRunStep;
32

43
pub fn build(b: *std.Build) void {
54
const target = b.standardTargetOptions(.{});
@@ -38,7 +37,7 @@ pub fn build(b: *std.Build) void {
3837
test_step.dependOn(&run_lib_unit_tests.step);
3938
}
4039

41-
pub fn addDependencies(b: *std.Build, solana_mod: *std.Build.Module, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) void {
40+
fn addDependencies(b: *std.Build, solana_mod: *std.Build.Module, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) void {
4241
const base58_dep = b.dependency("base58", .{
4342
.target = target,
4443
.optimize = optimize,
@@ -48,8 +47,7 @@ pub fn addDependencies(b: *std.Build, solana_mod: *std.Build.Module, target: std
4847
}
4948

5049
// General helper function to do all the tricky build steps, by creating the
51-
// solana-sdk module, adding its dependencies, adding the BPF link script, and
52-
// generating a keypair to use for deployments
50+
// solana-sdk module, adding its dependencies, adding the BPF link script
5351
pub fn buildProgram(b: *std.Build, program: *std.Build.Step.Compile, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Module {
5452
const solana_dep = b.dependency("solana-program-sdk", .{
5553
.target = target,
@@ -59,19 +57,9 @@ pub fn buildProgram(b: *std.Build, program: *std.Build.Step.Compile, target: std
5957
program.root_module.addImport("solana-program-sdk", solana_mod);
6058
addDependencies(b, solana_mod, target, optimize);
6159
linkSolanaProgram(b, program);
62-
generateKeypair(b, program);
63-
b.installArtifact(program);
6460
return solana_mod;
6561
}
6662

67-
pub fn generateKeypair(b: *std.Build, program: *std.Build.Step.Compile) void {
68-
const program_name = program.out_filename[0 .. program.out_filename.len - std.fs.path.extension(program.out_filename).len];
69-
const path = b.fmt("{s}-keypair.json", .{program_name});
70-
const lib_path = b.getInstallPath(.lib, path);
71-
const run_step = generateKeypairRunStep(b, lib_path);
72-
b.getInstallStep().dependOn(&run_step.step);
73-
}
74-
7563
pub const sbf_target: std.Target.Query = .{
7664
.cpu_arch = .sbf,
7765
.os_tag = .solana,
@@ -94,9 +82,6 @@ pub const bpf_target: std.Target.Query = .{
9482
};
9583

9684
pub fn linkSolanaProgram(b: *std.Build, lib: *std.Build.Step.Compile) void {
97-
// TODO: Due to https://github.com/ziglang/zig/issues/18404, this script
98-
// maps .data into .rodata, which only catches issues at runtime rather than
99-
// compile-time, if the program tries to use .data
10085
const linker_script = b.addWriteFile("bpf.ld",
10186
\\PHDRS
10287
\\{
@@ -111,7 +96,7 @@ pub fn linkSolanaProgram(b: *std.Build, lib: *std.Build.Step.Compile) void {
11196
\\. = SIZEOF_HEADERS;
11297
\\.text : { *(.text*) } :text
11398
\\.rodata : { *(.rodata*) } :rodata
114-
\\.rodata : { *(.data*) } :data
99+
\\.data : { *(.data*) } :data
115100
\\.data.rel.ro : { *(.data.rel.ro*) } :rodata
116101
\\.dynamic : { *(.dynamic) } :dynamic
117102
\\.dynsym : { *(.dynsym) } :data

build.zig.zon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = "solana-program-sdk",
3-
.version = "0.12.0",
3+
.version = "0.13.0",
44
.minimum_zig_version = "0.12.0",
55

66
// This field is optional.
@@ -10,8 +10,8 @@
1010
// internet connectivity.
1111
.dependencies = .{
1212
.base58 = .{
13-
.url = "https://github.com/joncinque/base58-zig/archive/refs/tags/v0.12.2.tar.gz",
14-
.hash = "12203cf62befaa692c11ed82c1cea15ae6da153a87d89a969b006d99267fc4ba8626",
13+
.url = "https://github.com/joncinque/base58-zig/archive/refs/tags/v0.13.3.tar.gz",
14+
.hash = "1220fd067bf167b9062cc29ccf715ff97643c2d3f8958beea863b6036876bb71bcb8",
1515
},
1616
},
1717
.paths = .{

install-solana-zig.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
if [[ -n $SOLANA_ZIG_VERSION ]]; then
44
solana_zig_version="$SOLANA_ZIG_VERSION"
55
else
6-
solana_zig_version="v1.41"
6+
solana_zig_version="v1.43.0"
77
fi
88
solana_zig_release_url="https://github.com/joncinque/solana-zig-bootstrap/releases/download/solana-$solana_zig_version"
99

program-test/build.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
const std = @import("std");
22
const solana = @import("solana-program-sdk");
3+
const base58 = @import("base58");
34

45
pub fn build(b: *std.Build) !void {
56
const optimize = .ReleaseSmall;
67
const target = b.resolveTargetQuery(solana.sbf_target);
78
const program = b.addSharedLibrary(.{
89
.name = "pubkey",
9-
.root_source_file = .{ .path = "pubkey/main.zig" },
10+
.root_source_file = b.path("pubkey/main.zig"),
1011
.optimize = optimize,
1112
.target = target,
1213
});
1314

1415
// Adding required dependencies, link the program properly, and get a
1516
// prepared modules
1617
_ = solana.buildProgram(b, program, target, optimize);
18+
b.installArtifact(program);
19+
base58.generateProgramKeypair(b, program);
1720
}

program-test/build.zig.zon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
.minimum_zig_version = "0.12.0",
55
.dependencies = .{
66
.base58 = .{
7-
.url = "https://github.com/joncinque/base58-zig/archive/refs/tags/v0.12.2.tar.gz",
8-
.hash = "12203cf62befaa692c11ed82c1cea15ae6da153a87d89a969b006d99267fc4ba8626",
7+
.url = "https://github.com/joncinque/base58-zig/archive/refs/tags/v0.13.3.tar.gz",
8+
.hash = "1220fd067bf167b9062cc29ccf715ff97643c2d3f8958beea863b6036876bb71bcb8",
99
},
1010
.clap = .{
11-
.url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.8.0.tar.gz",
12-
.hash = "1220949d4e88864579067b6d4cdad6476c6176f27e782782c2c39b7f2c4817a10efb",
11+
.url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.9.1.tar.gz",
12+
.hash = "122062d301a203d003547b414237229b09a7980095061697349f8bef41be9c30266b",
1313
},
1414
.@"solana-program-sdk" = .{
1515
.path = "..",

0 commit comments

Comments
 (0)