Skip to content

Commit 7e6fd82

Browse files
committed
initial commit
0 parents  commit 7e6fd82

File tree

6 files changed

+240
-0
lines changed

6 files changed

+240
-0
lines changed

.github/workflows/ci.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
pull_request:
8+
paths:
9+
- ".github/workflows/ci.yml"
10+
- "build.zig"
11+
- "build.zig.zon"
12+
13+
workflow_dispatch:
14+
15+
jobs:
16+
build:
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
zig-version: ["master", "0.14.0"]
21+
os: [ubuntu-latest]
22+
optimize: [ReleaseSafe, ReleaseFast]
23+
24+
runs-on: ${{ matrix.os }}
25+
26+
steps:
27+
- name: Check out repository
28+
uses: actions/checkout@v4
29+
with:
30+
submodules: false
31+
32+
- name: Set up Zig
33+
uses: mlugg/setup-zig@v2
34+
with:
35+
version: ${{ matrix.zig-version }}
36+
use-cache: false
37+
38+
- name: Run `build`
39+
run: zig build ${{ matrix.build-options }} -Doptimize=${{ matrix.optimize }} --summary all
40+

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
zig-out
2+
.zig-cache

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 AllYourCodebase contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# libbpf
2+
3+
This is [libbf](https://github.com/libbpf/libbpf) packaged for [Zig](https://ziglang.org).
4+

build.zig

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
const upstream = b.dependency("libbpf-upstream", .{});
8+
const libelf_dep = b.dependency("libelf", .{
9+
.target = target,
10+
.optimize = optimize,
11+
});
12+
const zlib_dep = b.dependency("zlib", .{
13+
.target = target,
14+
.optimize = optimize,
15+
});
16+
17+
const lib = portableStaticLibrary(b, "bpf", target, optimize);
18+
portableLinkLibC(lib);
19+
20+
portableLinkLibrary(lib, libelf_dep.artifact("elf"));
21+
portableLinkLibrary(lib, zlib_dep.artifact("z"));
22+
23+
const sources = [_][]const u8{
24+
"bpf.c",
25+
"btf.c",
26+
"libbpf.c",
27+
"libbpf_utils.c",
28+
"netlink.c",
29+
"nlattr.c",
30+
"bpf_prog_linfo.c",
31+
"libbpf_probes.c",
32+
"btf_dump.c",
33+
"btf_iter.c",
34+
"btf_relocate.c",
35+
"hashmap.c",
36+
"strset.c",
37+
"ringbuf.c",
38+
"linker.c",
39+
"gen_loader.c",
40+
"relo_core.c",
41+
"usdt.c",
42+
"zip.c",
43+
"elf.c",
44+
"features.c",
45+
};
46+
47+
const cflags = [_][]const u8{
48+
"-D_LARGEFILE64_SOURCE",
49+
"-D_FILE_OFFSET_BITS=64",
50+
"-std=gnu89",
51+
};
52+
53+
portableAddCSourceFiles(lib, .{
54+
.root = upstream.path("src"),
55+
.files = &sources,
56+
.flags = &cflags,
57+
});
58+
59+
// libbpf internal includes
60+
portableAddIncludePath(lib, upstream.path("src"));
61+
portableAddIncludePath(lib, upstream.path("include"));
62+
portableAddIncludePath(lib, upstream.path("include/uapi"));
63+
64+
// Install public BPF headers (for BPF program compilation and userspace API)
65+
const public_headers = [_][]const u8{
66+
"bpf.h",
67+
"libbpf.h",
68+
"btf.h",
69+
"libbpf_common.h",
70+
"libbpf_legacy.h",
71+
"bpf_helpers.h",
72+
"bpf_helper_defs.h",
73+
"bpf_tracing.h",
74+
"bpf_endian.h",
75+
"bpf_core_read.h",
76+
"skel_internal.h",
77+
"libbpf_version.h",
78+
"usdt.bpf.h",
79+
};
80+
81+
for (public_headers) |header| {
82+
lib.installHeader(upstream.path(b.fmt("src/{s}", .{header})), b.fmt("bpf/{s}", .{header}));
83+
}
84+
85+
// Install all include/ subdirectories for BPF program compilation
86+
lib.installHeadersDirectory(upstream.path("include/uapi/linux"), "linux", .{});
87+
lib.installHeadersDirectory(upstream.path("include/linux"), "linux", .{});
88+
lib.installHeadersDirectory(upstream.path("include/asm"), "asm", .{});
89+
90+
b.installArtifact(lib);
91+
}
92+
93+
// Portable helpers for Zig 0.14/0.15+ compatibility
94+
95+
const PortableAddCSourceFilesOptions = if (@hasDecl(std.Build.Module, "AddCSourceFilesOptions"))
96+
std.Build.Module.AddCSourceFilesOptions
97+
else
98+
std.Build.Step.Compile.AddCSourceFilesOptions;
99+
100+
fn portableAddCSourceFiles(c: *std.Build.Step.Compile, options: PortableAddCSourceFilesOptions) void {
101+
if (@hasDecl(std.Build.Step.Compile, "addCSourceFiles")) {
102+
c.addCSourceFiles(options);
103+
} else {
104+
c.root_module.addCSourceFiles(options);
105+
}
106+
}
107+
108+
fn portableLinkLibC(c: *std.Build.Step.Compile) void {
109+
if (@hasDecl(std.Build.Step.Compile, "linkLibC")) {
110+
c.linkLibC();
111+
} else {
112+
c.root_module.link_libc = true;
113+
}
114+
}
115+
116+
fn portableLinkLibrary(c: *std.Build.Step.Compile, library: *std.Build.Step.Compile) void {
117+
if (@hasDecl(std.Build.Step.Compile, "linkLibrary")) {
118+
c.linkLibrary(library);
119+
} else {
120+
c.root_module.linkLibrary(library);
121+
}
122+
}
123+
124+
fn portableAddIncludePath(c: *std.Build.Step.Compile, path: std.Build.LazyPath) void {
125+
if (@hasDecl(std.Build.Step.Compile, "addIncludePath")) {
126+
c.addIncludePath(path);
127+
} else {
128+
c.root_module.addIncludePath(path);
129+
}
130+
}
131+
132+
fn portableStaticLibrary(b: *std.Build, name: []const u8, target: anytype, optimize: anytype) *std.Build.Step.Compile {
133+
if (@hasDecl(std.Build, "addStaticLibrary")) {
134+
return b.addStaticLibrary(.{
135+
.name = name,
136+
.target = target,
137+
.optimize = optimize,
138+
});
139+
} else {
140+
return b.addLibrary(.{
141+
.name = name,
142+
.linkage = .static,
143+
.root_module = b.createModule(.{
144+
.target = target,
145+
.optimize = optimize,
146+
}),
147+
});
148+
}
149+
}

build.zig.zon

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.{
2+
.name = .libbpf,
3+
.version = "1.7.0",
4+
.minimum_zig_version = "0.14.0",
5+
.fingerprint = 0xb44e6e3ca68a158,
6+
.dependencies = .{
7+
.@"libbpf-upstream" = .{
8+
.url = "https://github.com/libbpf/libbpf/archive/refs/tags/v1.7.0.tar.gz",
9+
.hash = "N-V-__8AAJb6QwBAkBOVjg7v-0jgJT_ABV0UBSdxTjcmmEy4",
10+
},
11+
.libelf = .{
12+
.url = "https://github.com/allyourcodebase/libelf/archive/15ce4e5d7f9226a48b0227497c85b7ad8f069a33.tar.gz",
13+
.hash = "libelf-0.192.0-y7TJK9MnAACe3OcZQnArNTazJ9AuYk3obROaJxSxMyEv",
14+
},
15+
.zlib = .{
16+
.url = "https://github.com/allyourcodebase/zlib/archive/refs/tags/1.3.1.tar.gz",
17+
.hash = "zlib-1.3.1-AAAAACEMAAA0qyoSrfgBb_p25ItL4yRf_TBRk-26TYMF",
18+
},
19+
},
20+
.paths = .{
21+
"build.zig",
22+
"build.zig.zon",
23+
},
24+
}

0 commit comments

Comments
 (0)