Skip to content

Commit 68056ed

Browse files
committed
First commit
Signed-off-by: Pablo Alessandro Santos Hugen <phugen@redhat.com>
0 parents  commit 68056ed

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

.github/workflows/ci.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
workflow_dispatch:
10+
11+
jobs:
12+
build:
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
zig-version: [master]
17+
os: [ubuntu-latest]
18+
runs-on: ${{ matrix.os }}
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
23+
- name: Setup Zig
24+
uses: mlugg/setup-zig@v2
25+
with:
26+
version: ${{ matrix.zig-version }}
27+
28+
- name: Check Formatting
29+
run: zig fmt --ast-check --check .
30+
31+
- name: Build
32+
run: zig build --summary all

.gitignore

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

LICENSE

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

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# libdrm zig
2+
3+
[libdrm](https://gitlab.freedesktop.org/mesa/libdrm), packaged for the Zig build system.
4+
5+
Core library only (no GPU-specific sub-libraries).
6+
7+
## Using
8+
9+
First, update your `build.zig.zon`:
10+
11+
```
12+
zig fetch --save git+https://github.com/allyourcodebase/libdrm.git
13+
```
14+
15+
Then in your `build.zig`:
16+
17+
```zig
18+
const libdrm = b.dependency("libdrm", .{ .target = target, .optimize = optimize });
19+
exe.linkLibrary(libdrm.artifact("drm"));
20+
```

build.zig

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const std = @import("std");
2+
const manifest = @import("build.zig.zon");
3+
4+
pub fn build(b: *std.Build) !void {
5+
const target = b.standardTargetOptions(.{});
6+
const optimize = b.standardOptimizeOption(.{});
7+
const linkage = b.option(std.builtin.LinkMode, "linkage", "Library linkage type") orelse .static;
8+
9+
const upstream = b.dependency("upstream", .{});
10+
const src = upstream.path("");
11+
12+
const os = target.result.os.tag;
13+
const is_linux = os == .linux;
14+
15+
const gen_fourcc = b.addExecutable(.{
16+
.name = "gen_table_fourcc",
17+
.root_module = b.createModule(.{
18+
.root_source_file = b.path("tools/gen_table_fourcc.zig"),
19+
.target = b.graph.host,
20+
}),
21+
});
22+
const gen_run = b.addRunArtifact(gen_fourcc);
23+
gen_run.addFileArg(src.path(b, "include/drm/drm_fourcc.h"));
24+
const fourcc_wf = b.addWriteFiles();
25+
_ = fourcc_wf.addCopyFile(gen_run.captureStdOut(.{}), "generated_static_table_fourcc.h");
26+
27+
// Module
28+
const mod = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true });
29+
mod.addIncludePath(src);
30+
mod.addIncludePath(src.path(b, "include/drm"));
31+
mod.addIncludePath(fourcc_wf.getDirectory());
32+
33+
const flags: []const []const u8 = flags: {
34+
var list: std.ArrayListUnmanaged([]const u8) = .empty;
35+
try list.ensureTotalCapacity(b.allocator, 1 << 4);
36+
37+
list.appendSliceAssumeCapacity(&.{
38+
"-fvisibility=hidden",
39+
"-DHAVE_LIBDRM_ATOMIC_PRIMITIVES=1",
40+
"-DHAVE_VISIBILITY=1",
41+
"-D_GNU_SOURCE",
42+
"-DHAVE_OPEN_MEMSTREAM=1",
43+
"-DHAVE_SECURE_GETENV=1",
44+
"-DHAVE_SYS_SELECT_H=1",
45+
"-DHAVE_ALLOCA_H=1",
46+
});
47+
if (is_linux) list.appendAssumeCapacity("-DMAJOR_IN_SYSMACROS=1");
48+
break :flags try list.toOwnedSlice(b.allocator);
49+
};
50+
51+
mod.addCSourceFiles(.{ .root = src, .files = &.{
52+
"xf86drm.c",
53+
"xf86drmHash.c",
54+
"xf86drmRandom.c",
55+
"xf86drmSL.c",
56+
"xf86drmMode.c",
57+
}, .flags = flags });
58+
59+
// Library
60+
const lib = b.addLibrary(.{ .name = "drm", .root_module = mod, .linkage = linkage });
61+
62+
// Install headers
63+
inline for (.{ "xf86drm.h", "xf86drmMode.h", "libsync.h" }) |h|
64+
lib.installHeader(src.path(b, h), h);
65+
lib.installHeadersDirectory(src.path(b, "include/drm"), "libdrm", .{});
66+
67+
b.installArtifact(lib);
68+
}

build.zig.zon

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.{
2+
.name = .libdrm,
3+
.version = "2.4.131",
4+
.dependencies = .{
5+
.upstream = .{
6+
.url = "https://gitlab.freedesktop.org/mesa/libdrm/-/archive/libdrm-2.4.131/libdrm-libdrm-2.4.131.tar.gz",
7+
.hash = "N-V-__8AAC3VLACNGc7Wsy7XkL-SyMdb3mfe_MPouEAZNt-A",
8+
},
9+
},
10+
.minimum_zig_version = "0.16.0-dev.2653+784e89fd4",
11+
.paths = .{ "build.zig", "build.zig.zon", "tools", "README.md", "LICENSE" },
12+
.fingerprint = 0xaa2d215b57727d27,
13+
}

tools/gen_table_fourcc.zig

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
const std = @import("std");
2+
const Io = std.Io;
3+
4+
pub fn main(init: std.process.Init) !void {
5+
const alloc = init.arena.allocator();
6+
const io = init.io;
7+
8+
var args = std.process.Args.Iterator.init(init.minimal.args);
9+
_ = args.skip();
10+
const input_path = args.next() orelse return error.MissingArgument;
11+
12+
const data = try Io.Dir.readFileAlloc(.cwd(), io, input_path, alloc, .unlimited);
13+
14+
var intel_mods: std.ArrayList([]const u8) = .empty;
15+
var other_mods: std.ArrayList(struct { []const u8, []const u8 }) = .empty;
16+
var vendors: std.ArrayList([]const u8) = .empty;
17+
18+
var lines = std.mem.splitScalar(u8, data, '\n');
19+
while (lines.next()) |line| {
20+
if (parseAfter(line, "#define I915_FORMAT_MOD_")) |name| {
21+
try intel_mods.append(alloc, name);
22+
} else if (parseVendorMod(line)) |vm| {
23+
try other_mods.append(alloc, vm);
24+
} else if (parseAfter(line, "#define DRM_FORMAT_MOD_VENDOR_")) |name| {
25+
try vendors.append(alloc, name);
26+
}
27+
}
28+
29+
var buf: [4096]u8 = undefined;
30+
var fw = Io.File.stdout().writerStreaming(io, &buf);
31+
const w = &fw.interface;
32+
33+
try w.writeAll(
34+
\\/* AUTOMATICALLY GENERATED by gen_table_fourcc.zig */
35+
\\static const struct drmFormatModifierInfo drm_format_modifier_table[] = {
36+
\\ { DRM_MODIFIER_INVALID(NONE, INVALID) },
37+
\\ { DRM_MODIFIER_LINEAR(NONE, LINEAR) },
38+
\\
39+
);
40+
41+
for (intel_mods.items) |mod|
42+
try w.print(" {{ DRM_MODIFIER_INTEL({s}, {s}) }},\n", .{ mod, mod });
43+
44+
for (other_mods.items) |entry| {
45+
const vendor, const mod = entry;
46+
try w.print(" {{ DRM_MODIFIER({s}, {s}, {s}) }},\n", .{ vendor, mod, mod });
47+
}
48+
49+
try w.writeAll(
50+
\\};
51+
\\static const struct drmFormatModifierVendorInfo drm_format_modifier_vendor_table[] = {
52+
\\
53+
);
54+
55+
for (vendors.items) |v|
56+
try w.print(" {{ DRM_FORMAT_MOD_VENDOR_{s}, \"{s}\" }},\n", .{ v, v });
57+
58+
try w.writeAll("};\n");
59+
try fw.flush();
60+
}
61+
62+
fn parseAfter(line: []const u8, prefix: []const u8) ?[]const u8 {
63+
const trimmed = std.mem.trimStart(u8, line, " \t");
64+
if (!std.mem.startsWith(u8, trimmed, prefix)) return null;
65+
const after = trimmed[prefix.len..];
66+
const end = std.mem.indexOfAny(u8, after, " \t\r\n") orelse return null;
67+
if (end == 0) return null;
68+
const name = after[0..end];
69+
if (std.mem.indexOfScalar(u8, name, '(') != null) return null;
70+
return name;
71+
}
72+
73+
fn parseVendorMod(line: []const u8) ?struct { []const u8, []const u8 } {
74+
const prefix = "#define DRM_FORMAT_MOD_";
75+
const trimmed = std.mem.trimStart(u8, line, " \t");
76+
if (!std.mem.startsWith(u8, trimmed, prefix)) return null;
77+
const after = trimmed[prefix.len..];
78+
79+
for ([_][]const u8{ "ARM", "SAMSUNG", "QCOM", "VIVANTE", "NVIDIA", "BROADCOM", "ALLWINNER" }) |vendor| {
80+
if (std.mem.startsWith(u8, after, vendor) and after.len > vendor.len and after[vendor.len] == '_') {
81+
const mod_start = vendor.len + 1;
82+
// Require trailing whitespace (like the Python regex `\s` at end)
83+
const end = std.mem.indexOfAny(u8, after[mod_start..], " \t\r\n") orelse continue;
84+
if (end == 0) continue;
85+
const mod = after[mod_start..][0..end];
86+
if (std.mem.indexOfScalar(u8, mod, '(') != null) continue;
87+
if (std.mem.eql(u8, vendor, "ARM") and
88+
(std.mem.eql(u8, mod, "TYPE_AFBC") or
89+
std.mem.eql(u8, mod, "TYPE_MISC") or
90+
std.mem.eql(u8, mod, "TYPE_AFRC"))) continue;
91+
return .{ vendor, mod };
92+
}
93+
}
94+
return null;
95+
}

0 commit comments

Comments
 (0)