Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 1040e6f

Browse files
feat: initial code
1 parent 1678fc0 commit 1040e6f

File tree

5 files changed

+247
-0
lines changed

5 files changed

+247
-0
lines changed

Step/Autoconf.zig

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const std = @import("std");
2+
const Autoconf = @This();
3+
const evalChildProcess = @import("build").evalChildProcessInDirectory;
4+
5+
pub const Options = struct {
6+
source: std.Build.LazyPath,
7+
};
8+
9+
step: std.Build.Step,
10+
source: std.Build.LazyPath,
11+
output_file: std.Build.GeneratedFile,
12+
13+
pub fn create(b: *std.Build, options: Options) Autoconf {
14+
const arena = b.allocator;
15+
const self = arena.create(Autoconf) catch @panic("OOM");
16+
self.* = .{
17+
.step = std.Build.Step.init(.{
18+
.id = .custom,
19+
.name = b.fmt("Autoconf {}", .{options.source.getDisplayName()}),
20+
.owner = b,
21+
.makeFn = make,
22+
}),
23+
.source = options.source,
24+
.output_file = .{ .step = &self.step },
25+
};
26+
27+
self.source.addStepDependencies(&self.step);
28+
return self;
29+
}
30+
31+
fn make(step: *std.Build.Step, _: *std.Progress.Node) anyerror!void {
32+
const b = step.owner;
33+
const self = @fieldParentPtr(Autoconf, "step", step);
34+
35+
var man = b.graph.cache.obtain();
36+
defer man.deinit();
37+
38+
try man.addFile(b.pathJoin(&.{ self.source.getPath2(b, step), "configure.ac" }), null);
39+
40+
self.output_file.path = b.pathJoin(&.{ self.source.getPath2(b, step), "configure.ac" });
41+
42+
if (try step.cacheHit(&man)) return;
43+
44+
const cmd = b.findProgram(&.{"autoconf"}, &.{}) catch return step.fail("Cannot locate autoconf", .{});
45+
46+
try evalChildProcess(step, &.{
47+
cmd,
48+
}, self.source.getPath2(b, step));
49+
try step.writeManifest(&man);
50+
}

Step/Configure.zig

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
const std = @import("std");
2+
const Configure = @This();
3+
const evalChildProcess = @import("build").evalChildProcessInDirectory;
4+
5+
pub const Options = struct {
6+
source: std.Build.LazyPath,
7+
target: std.Build.ResolvedTarget,
8+
optimize: std.builtin.OptimizeMode = .Debug,
9+
linkage: std.Build.Step.Compile.Linkage = .Static,
10+
flags: ?[]const []const u8 = null,
11+
};
12+
13+
step: std.Build.Step,
14+
source: std.Build.LazyPath,
15+
target: std.Build.ResolvedTarget,
16+
optimize: std.builtin.OptimizeMode,
17+
linkage: std.Build.Step.Compile.Linkage,
18+
flags: std.ArrayListUnmanaged([]const u8),
19+
20+
pub fn create(b: *std.Build, options: Options) Configure {
21+
const arena = b.allocator;
22+
const self = arena.create(Configure) catch @panic("OOM");
23+
self.* = .{
24+
.step = std.Build.Step.init(.{
25+
.id = .custom,
26+
.name = b.fmt("Configure {}", .{options.source.getDisplayName()}),
27+
.owner = b,
28+
.makeFn = make,
29+
}),
30+
.source = options.source,
31+
.target = options.target,
32+
.optimize = options.optimize,
33+
.linkage = options.linkage,
34+
.flags = .{},
35+
};
36+
37+
self.source.addStepDependencies(&self.step);
38+
39+
if (options.flags) |flags| {
40+
self.flags.ensureTotalCapacity(arena, flags.len) catch @panic("OOM");
41+
42+
for (flags) |flag| {
43+
self.flags.appendAssumeCapacity(arena.dupe(u8, flag) catch @panic("OOM"));
44+
}
45+
}
46+
return self;
47+
}
48+
49+
fn make(step: *std.Build.Step, _: *std.Progress.Node) anyerror!void {
50+
const b = step.owner;
51+
const arena = b.allocator;
52+
const self = @fieldParentPtr(Configure, "step", step);
53+
54+
var man = b.graph.cache.obtain();
55+
defer man.deinit();
56+
57+
try man.addFile(b.pathJoin(&.{ self.source.getPath2(b, step), "configure" }), null);
58+
59+
if (try step.cacheHit(&man)) return;
60+
61+
const cmd = b.findProgram(&.{ "bash", "sh" }, &.{}) catch return step.fail("Cannot locate a shell", .{});
62+
63+
var args = std.ArrayList([]const u8).init(arena);
64+
defer args.deinit();
65+
66+
try args.appendSlice(&.{
67+
cmd,
68+
b.pathJoin(&.{ self.source.getPath2(b, step), "configure" }),
69+
b.fmt("--build={s}", .{try self.target.query.zigTriple(arena)}),
70+
"--prefix=/usr",
71+
b.fmt("--includedir={s}", .{b.cache_root.join(b.allocator, &.{ "expidus-dev", "include" })}),
72+
b.fmt("--enable-{s}", .{@as([]const u8, switch (self.linkage) {
73+
.dynamic => "shared",
74+
else => @tagName(self.linkage),
75+
})}),
76+
});
77+
78+
try args.appendSlice(self.flags.items);
79+
80+
try step.handleChildProcUnsupported(null, args.items);
81+
try std.Build.Step.handleVerbose(b, null, args.items);
82+
83+
var env_map = std.process.EnvMap.init(arena);
84+
defer env_map.deinit();
85+
86+
{
87+
var iter = b.graph.env_map.iterator();
88+
while (iter.next()) |entry| {
89+
try env_map.put(entry.key_ptr.*, entry.value_ptr.*);
90+
}
91+
}
92+
93+
try env_map.put("CC", b.fmt("{s} cc", .{
94+
b.graph.zig_exe,
95+
}));
96+
97+
try env_map.put("CXX", b.fmt("{s} c++", .{
98+
b.graph.zig_exe,
99+
}));
100+
101+
try env_map.put("CFLAGS", b.fmt("-target={s} -mcpu={s} -O{s}", .{
102+
try self.target.query.zigTriple(arena),
103+
try self.target.query.serializeCpuAlloc(arena),
104+
@as([]const u8, switch (self.optimize) {
105+
.ReleaseSmall => "s",
106+
.ReleaseSafe => "3",
107+
.ReleaseFast => "fast",
108+
.Debug => "g",
109+
}),
110+
}));
111+
112+
if (b.findProgram(&.{"pkg-config"}, &.{}) catch null) |pkgconfig| {
113+
try env_map.put("PKG_CONFIG", pkgconfig);
114+
}
115+
116+
const result = std.ChildProcess.run(.{
117+
.allocator = arena,
118+
.argv = args.items,
119+
.cwd = self.source.getPath2(b, step),
120+
.env_map = env_map,
121+
}) catch |err| return step.fail("unable to spawn {s}: {s}", .{ cmd, @errorName(err) });
122+
123+
if (result.stderr.len > 0) {
124+
try step.result_error_msgs.append(arena, result.stderr);
125+
}
126+
127+
try step.handleChildProcessTerm(result.term, null, args.items);
128+
try step.writeManifest(&man);
129+
}

Step/Make.zig

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const std = @import("std");
2+
const Make = @This();
3+
const evalChildProcess = @import("build").evalChildProcessInDirectory;
4+
5+
pub const Options = struct {
6+
source: std.Build.LazyPath,
7+
};
8+
9+
step: std.Build.Step,
10+
source: std.Build.LazyPath,
11+
output_file: std.Build.GeneratedFile,
12+
13+
pub fn create(b: *std.Build, options: Options) Make {
14+
const arena = b.allocator;
15+
const self = arena.create(Make) catch @panic("OOM");
16+
self.* = .{
17+
.step = std.Build.Step.init(.{
18+
.id = .custom,
19+
.name = b.fmt("Make {}", .{options.source.getDisplayName()}),
20+
.owner = b,
21+
.makeFn = make,
22+
}),
23+
.source = options.source,
24+
.output_file = .{ .step = &self.step },
25+
};
26+
27+
self.source.addStepDependencies(&self.step);
28+
return self;
29+
}
30+
31+
fn make(step: *std.Build.Step, _: *std.Progress.Node) anyerror!void {
32+
const b = step.owner;
33+
const self = @fieldParentPtr(Make, "step", step);
34+
35+
var man = b.graph.cache.obtain();
36+
defer man.deinit();
37+
38+
try man.addFile(b.pathJoin(&.{ self.source.getPath2(b, step), "Makefile" }), null);
39+
40+
if (try step.cacheHit(&man)) return;
41+
42+
const cmd = b.findProgram(&.{"make"}, &.{}) catch return step.fail("Cannot locate GNU Make", .{});
43+
44+
try evalChildProcess(step, &.{
45+
cmd,
46+
}, self.source.getPath2(b, step));
47+
try step.writeManifest(&man);
48+
}

build.zig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const std = @import("std");
2+
3+
pub const Step = struct {
4+
pub const Autoconf = @import("Step/Autoconf.zig");
5+
pub const Configure = @import("Step/Configure.zig");
6+
pub const Make = @import("Step/Make.zig");
7+
};
8+
9+
pub fn build(_: *std.Build) void {}

build.zig.zon

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.{
2+
.name = "pkgbuild",
3+
.version = "0.0.0",
4+
.paths = .{""},
5+
.dependencies = .{
6+
.build = .{
7+
.url = "https://github.com/ExpidusOS/build/archive/aab78d6bd30004e002361dc5e66c090719bb88a7.tar.gz",
8+
.hash = "1220fe80f8f80b02213a1aa25d000f65fc889e8f8e39d0a0344a35caaef9c1b35bb7",
9+
},
10+
},
11+
}

0 commit comments

Comments
 (0)