forked from lithdew/solana-zig
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.zig
111 lines (95 loc) · 3.33 KB
/
build.zig
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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Export self as a module
const solana_mod = b.addModule("solana-program-sdk", .{ .root_source_file = b.path("src/root.zig") });
const lib = b.addStaticLibrary(.{
.name = "solana-program-sdk",
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
const base58_dep = b.dependency("base58", .{
.target = target,
.optimize = optimize,
});
const base58_mod = base58_dep.module("base58");
lib.root_module.addImport("base58", base58_mod);
solana_mod.addImport("base58", base58_mod);
b.installArtifact(lib);
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
lib_unit_tests.root_module.addImport("base58", base58_mod);
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);
}
// General helper function to do all the tricky build steps, by adding the
// solana-sdk module, adding the BPF link script
pub fn buildProgram(b: *std.Build, program: *std.Build.Step.Compile, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Module {
const solana_dep = b.dependency("solana-program-sdk", .{
.target = target,
.optimize = optimize,
});
const solana_mod = solana_dep.module("solana-program-sdk");
program.root_module.addImport("solana-program-sdk", solana_mod);
linkSolanaProgram(b, program);
return solana_mod;
}
pub const sbf_target: std.Target.Query = .{
.cpu_arch = .sbf,
.os_tag = .solana,
.cpu_features_add = std.Target.sbf.featureSet(&.{.solana}),
};
pub const sbfv2_target: std.Target.Query = .{
.cpu_arch = .sbf,
.cpu_model = .{
.explicit = &std.Target.sbf.cpu.sbfv2,
},
.os_tag = .solana,
.cpu_features_add = std.Target.sbf.cpu.sbfv2.features,
};
pub const bpf_target: std.Target.Query = .{
.cpu_arch = .bpfel,
.os_tag = .freestanding,
.cpu_features_add = std.Target.bpf.featureSet(&.{.solana}),
};
pub fn linkSolanaProgram(b: *std.Build, lib: *std.Build.Step.Compile) void {
const linker_script = b.addWriteFile("bpf.ld",
\\PHDRS
\\{
\\text PT_LOAD ;
\\rodata PT_LOAD ;
\\data PT_LOAD ;
\\dynamic PT_DYNAMIC ;
\\}
\\
\\SECTIONS
\\{
\\. = SIZEOF_HEADERS;
\\.text : { *(.text*) } :text
\\.rodata : { *(.rodata*) } :rodata
\\.data.rel.ro : { *(.data.rel.ro*) } :rodata
\\.dynamic : { *(.dynamic) } :dynamic
\\.dynsym : { *(.dynsym) } :data
\\.dynstr : { *(.dynstr) } :data
\\.rel.dyn : { *(.rel.dyn) } :data
\\/DISCARD/ : {
\\*(.eh_frame*)
\\*(.gnu.hash*)
\\*(.hash*)
\\}
\\}
);
lib.step.dependOn(&linker_script.step);
lib.setLinkerScript(linker_script.files.items[0].getPath());
lib.stack_size = 4096;
lib.link_z_notext = true;
lib.root_module.pic = true;
lib.root_module.strip = true;
lib.entry = .{ .symbol_name = "entrypoint" };
}