forked from joncinque/solana-program-sdk-zig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
228 lines (192 loc) · 7.65 KB
/
build.zig
File metadata and controls
228 lines (192 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// MCL option: -Dwith-mcl enables MCL for off-chain BN254 operations
// - If vendor/mcl/lib/libmcl.a exists, uses it directly
// - If not, automatically builds MCL (requires clang)
const with_mcl = b.option(bool, "with-mcl", "Enable MCL library for off-chain BN254 operations") orelse false;
// Determine if MCL is enabled
const mcl_enabled = with_mcl;
// Create build options module to pass MCL availability to source code
const build_options = b.addOptions();
build_options.addOption(bool, "mcl_linked", mcl_enabled);
// Export self as a module
const solana_mod = b.addModule("solana_program_sdk", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
// Add build options to module
solana_mod.addOptions("build_options", build_options);
const anchor_mod = b.createModule(.{
.root_source_file = b.path("anchor/src/root.zig"),
.target = target,
.optimize = optimize,
});
anchor_mod.addImport("solana_program_sdk", solana_mod);
const base58_dep = b.dependency("base58", .{
.target = target,
.optimize = optimize,
});
const base58_mod = base58_dep.module("base58");
solana_mod.addImport("base58", base58_mod);
// Get the shared SDK module (no syscall dependencies)
const solana_sdk_dep = b.dependency("solana_sdk", .{
.target = target,
.optimize = optimize,
});
const solana_sdk_mod = solana_sdk_dep.module("solana_sdk");
// Add SDK as an import to program SDK so src/ modules can use SDK types
solana_mod.addImport("solana_sdk", solana_sdk_mod);
// Also export the shared SDK module for consumers who only need core types
_ = b.addModule("solana_sdk", .{
.root_source_file = solana_sdk_dep.path("src/root.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "base58", .module = base58_mod },
},
});
const idl_program_path = b.option([]const u8, "idl-program", "Program module path for IDL generation") orelse "anchor/src/idl_example.zig";
const idl_output_dir = b.option([]const u8, "idl-output-dir", "IDL output directory") orelse "idl";
const idl_output_path = b.option([]const u8, "idl-output", "IDL output path (overrides idl-output-dir)") orelse "";
const idl_options = b.addOptions();
idl_options.addOption([]const u8, "idl_output_dir", idl_output_dir);
idl_options.addOption([]const u8, "idl_output_path", idl_output_path);
const idl_program_mod = b.createModule(.{
.root_source_file = b.path(idl_program_path),
.target = b.graph.host,
.optimize = optimize,
.imports = &.{
.{ .name = "solana_program_sdk", .module = solana_mod },
.{ .name = "sol_anchor_zig", .module = anchor_mod },
},
});
const idl_exe = b.addExecutable(.{
.name = "anchor-idl",
.root_module = b.createModule(.{
.root_source_file = b.path("anchor/src/idl_cli.zig"),
.target = b.graph.host,
.optimize = optimize,
.imports = &.{
.{ .name = "solana_program_sdk", .module = solana_mod },
.{ .name = "sol_anchor_zig", .module = anchor_mod },
.{ .name = "idl_program", .module = idl_program_mod },
},
}),
});
idl_exe.root_module.addOptions("build_options", idl_options);
const run_idl = b.addRunArtifact(idl_exe);
const idl_step = b.step("idl", "Generate Anchor IDL JSON");
idl_step.dependOn(&run_idl.step);
const lib_unit_tests = b.addTest(.{
.root_module = solana_mod,
});
lib_unit_tests.root_module.addImport("base58", base58_mod);
lib_unit_tests.root_module.addOptions("build_options", build_options);
// Handle MCL linking
if (with_mcl) {
const mcl_build = buildMcl(b);
lib_unit_tests.step.dependOn(&mcl_build.step);
lib_unit_tests.root_module.addObjectFile(b.path("vendor/mcl/lib/libmcl.a"));
lib_unit_tests.root_module.addIncludePath(b.path("vendor/mcl/include"));
lib_unit_tests.root_module.link_libcpp = true;
}
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
// Add a separate step to just build MCL
const mcl_step = b.step("mcl", "Build MCL library");
mcl_step.dependOn(&buildMcl(b).step);
}
/// Build MCL library using make with Clang + libc++
/// Note: Requires clang-20 or clang to be installed
fn buildMcl(b: *std.Build) *std.Build.Step.Run {
const cc = b.findProgram(&.{ "clang-20", "clang" }, &.{}) catch "clang";
const cxx = b.findProgram(&.{ "clang++-20", "clang++" }, &.{}) catch "clang++";
// Build MCL with Clang + libc++ for Zig compatibility.
const build_cmd = b.addSystemCommand(&.{
"make",
"-C",
"vendor/mcl",
b.fmt("CXX={s} -stdlib=libc++", .{cxx}),
b.fmt("CC={s}", .{cc}),
"MCL_FP_BIT=256",
"MCL_FR_BIT=256",
"lib/libmcl.a",
"-j4",
});
return build_cmd;
}
pub const LinkedProgram = struct {
name: []const u8,
module: *std.Build.Module,
so: std.Build.LazyPath,
step: *std.Build.Step,
};
pub const BuildProgramElf2SbpfOptions = struct {
name: []const u8,
root_source_file: std.Build.LazyPath,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
elf2sbpf_bin: []const u8 = "elf2sbpf",
};
/// Build a Solana program with the standard Zig → LLVM bitcode → zig cc →
/// elf2sbpf pipeline.
///
/// Output lands in `zig-out/lib/<name>.so` via the normal install step.
pub fn buildProgramElf2sbpf(b: *std.Build, options: BuildProgramElf2SbpfOptions) LinkedProgram {
const solana_dep = b.dependency("solana_program_sdk", .{
.target = options.target,
.optimize = options.optimize,
});
const solana_mod = solana_dep.module("solana_program_sdk");
const program_mod = b.createModule(.{
.root_source_file = options.root_source_file,
.target = options.target,
.optimize = options.optimize,
.imports = &.{
.{ .name = "solana_program_sdk", .module = solana_mod },
},
});
program_mod.pic = true;
program_mod.strip = true;
const bitcode_obj = b.addObject(.{
.name = b.fmt("{s}-bitcode", .{options.name}),
.root_module = program_mod,
});
const bitcode = bitcode_obj.getEmittedLlvmBc();
const zig_cc = b.addSystemCommand(&.{
b.graph.zig_exe,
"cc",
"-target",
"bpfel-freestanding",
"-mcpu=v2",
"-O2",
"-mllvm",
"-bpf-stack-size=4096",
"-c",
});
zig_cc.addFileArg(bitcode);
zig_cc.addArg("-o");
const obj = zig_cc.addOutputFileArg(b.fmt("{s}.o", .{options.name}));
const link_program = b.addSystemCommand(&.{options.elf2sbpf_bin});
link_program.addFileArg(obj);
const so = link_program.addOutputFileArg(b.fmt("{s}.so", .{options.name}));
b.getInstallStep().dependOn(&b.addInstallLibFile(so, b.fmt("{s}.so", .{options.name})).step);
return .{
.name = options.name,
.module = solana_mod,
.so = so,
.step = &link_program.step,
};
}
pub const bpf_target: std.Target.Query = .{
.cpu_arch = .bpfel,
.cpu_model = .{
.explicit = &std.Target.bpf.cpu.v2,
},
.os_tag = .freestanding,
.cpu_features_add = std.Target.bpf.cpu.v2.features,
};