-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
189 lines (152 loc) · 6.01 KB
/
build.zig
File metadata and controls
189 lines (152 loc) · 6.01 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
const std = @import("std");
const Build = @import("build");
const AfProtosStep = struct {
step: std.Build.Step,
target: std.Target,
output: std.Build.GeneratedFile,
pub fn create(b: *std.Build, target: std.Target) *AfProtosStep {
const self = b.allocator.create(AfProtosStep) catch @panic("OOM");
self.* = .{
.step = std.Build.Step.init(.{
.id = .custom,
.name = "Generate af_protos.h",
.owner = b,
.makeFn = make,
}),
.target = target,
.output = .{ .step = &self.step },
};
return self;
}
pub fn getDirectory(self: *AfProtosStep) std.Build.LazyPath {
return .{ .generated_dirname = .{
.generated = &self.output,
.up = 0,
} };
}
fn make(step: *std.Build.Step, _: *std.Progress.Node) anyerror!void {
const b = step.owner;
const self = @fieldParentPtr(AfProtosStep, "step", step);
const tempPath = b.makeTempPath();
{
var tempFile = try std.fs.createFileAbsolute(b.pathJoin(&.{ tempPath, "af_protos-input.h" }), .{});
defer tempFile.close();
try tempFile.writeAll("#include <netinet/in.h>");
}
try step.evalChildProcess(&.{
b.graph.zig_exe,
"cc",
b.fmt("--target={s}", .{try self.target.zigTriple(b.allocator)}),
"-E",
"-dM",
b.pathJoin(&.{ tempPath, "af_protos-input.h" }),
"-o",
b.pathJoin(&.{ tempPath, "af_protos-gen.h" }),
});
var man = b.graph.cache.obtain();
defer man.deinit();
var tempFile = try std.fs.openFileAbsolute(b.pathJoin(&.{ tempPath, "af_protos-gen.h" }), .{});
defer tempFile.close();
const tempFileMeta = try tempFile.metadata();
_ = try man.addFile(b.pathJoin(&.{ tempPath, "af_protos-gen.h" }), tempFileMeta.size());
if (try step.cacheHit(&man)) {
const digest = man.final();
self.output.path = try b.cache_root.join(b.allocator, &.{ "o", &digest, "af_protos.h" });
return;
}
const digest = man.final();
const cache_path = "o" ++ std.fs.path.sep_str ++ digest;
self.output.path = try b.cache_root.join(b.allocator, &.{ "o", &digest, "af_protos.h" });
var cache_dir = b.cache_root.handle.makeOpenPath(cache_path, .{}) catch |err| {
return step.fail("unable to make path '{}{s}': {s}", .{
b.cache_root, cache_path, @errorName(err),
});
};
defer cache_dir.close();
var outputFile = try cache_dir.createFile("af_protos.h", .{});
defer outputFile.close();
while (try tempFile.reader().readUntilDelimiterOrEofAlloc(b.allocator, '\n', tempFileMeta.size())) |line| {
defer b.allocator.free(line);
if (std.mem.indexOf(u8, line, "IPPROTO_MAX") != null) continue;
try outputFile.writer().writeAll(line);
try outputFile.writer().writeByte('\n');
}
try step.writeManifest(&man);
}
};
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const linkage = b.option(std.builtin.LinkMode, "linkage", "whether to statically or dynamically link the library") orelse @as(std.builtin.LinkMode, if (target.result.isGnuLibC()) .dynamic else .static);
const source = b.dependency("apparmor", .{});
const libapparmor = std.Build.Step.Compile.create(b, .{
.name = "apparmor",
.root_module = .{
.target = target,
.optimize = optimize,
.link_libc = true,
},
.kind = .lib,
.linkage = linkage,
.version = .{
.major = 1,
.minor = 12,
.patch = 3,
},
});
libapparmor.expect_errors = .{ .contains = "" };
libapparmor.version_script = source.path("libraries/libapparmor/src/libapparmor.map");
libapparmor.addIncludePath(source.path("libraries/libapparmor/include"));
libapparmor.addIncludePath(source.path("libraries/libapparmor/src"));
libapparmor.addIncludePath(AfProtosStep.create(b, target.result).getDirectory());
{
const lex = Build.LexStep.create(b, source.path("libraries/libapparmor/src/scanner.l"));
libapparmor.addIncludePath(.{ .generated_dirname = .{
.generated = &lex.output_header,
.up = 0,
} });
libapparmor.addCSourceFile(.{ .file = .{
.generated = &lex.output_source,
} });
libapparmor.step.dependOn(&lex.step);
}
{
const yacc = Build.YaccStep.create(b, .{
.source = source.path("libraries/libapparmor/src/grammar.y"),
.prefix = "aalogparse_",
});
libapparmor.addIncludePath(.{ .generated_dirname = .{
.generated = &yacc.output_header,
.up = 0,
} });
libapparmor.addCSourceFile(.{ .file = .{
.generated = &yacc.output_source,
} });
libapparmor.step.dependOn(&yacc.step);
}
libapparmor.addCSourceFiles(.{
.root = source.path("libraries/libapparmor/src"),
.files = &.{
"libaalogparse.c",
"kernel.c",
"private.c",
"features.c",
"kernel_interface.c",
"policy_cache.c",
"PMurHash.c",
},
.flags = &.{"-D_GNU_SOURCE"},
});
{
const headers: []const []const u8 = &.{
"sys/apparmor.h",
"sys/apparmor_private.h",
};
for (headers) |header| {
const install_file = b.addInstallFileWithDir(source.path(b.pathJoin(&.{ "libraries", "libapparmor", "include", header })), .header, header);
b.getInstallStep().dependOn(&install_file.step);
libapparmor.installed_headers.append(&install_file.step) catch @panic("OOM");
}
}
b.installArtifact(libapparmor);
}