Skip to content

Package Manager MVP #14265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ pub fn build(b: *Builder) !void {
exe_options.addOption(bool, "llvm_has_arc", llvm_has_arc);
exe_options.addOption(bool, "force_gpa", force_gpa);
exe_options.addOption(bool, "only_c", only_c);
exe_options.addOption(bool, "omit_pkg_fetching_code", false);

if (link_libc) {
exe.linkLibC();
Expand Down Expand Up @@ -567,14 +568,14 @@ fn addCmakeCfgOptionsToExe(
// back to -lc++ and cross our fingers.
addCxxKnownPath(b, cfg, exe, b.fmt("libstdc++.{s}", .{lib_suffix}), "", need_cpp_includes) catch |err| switch (err) {
error.RequiredLibraryNotFound => {
exe.linkSystemLibrary("c++");
exe.linkLibCpp();
},
else => |e| return e,
};
exe.linkSystemLibrary("unwind");
},
.ios, .macos, .watchos, .tvos => {
exe.linkSystemLibrary("c++");
.ios, .macos, .watchos, .tvos, .windows => {
exe.linkLibCpp();
},
.freebsd => {
if (static) {
Expand Down
14 changes: 4 additions & 10 deletions lib/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const process = std.process;
const ArrayList = std.ArrayList;
const File = std.fs.File;

pub const dependencies = @import("@dependencies");

pub fn main() !void {
// Here we use an ArenaAllocator backed by a DirectAllocator because a build is a short-lived,
// one shot program. We don't need to waste time freeing memory and finding places to squish
Expand Down Expand Up @@ -207,7 +209,7 @@ pub fn main() !void {

builder.debug_log_scopes = debug_log_scopes.items;
builder.resolveInstallPrefix(install_prefix, dir_list);
try runBuild(builder);
try builder.runBuild(root);

if (builder.validateUserInputDidItFail())
return usageAndErr(builder, true, stderr_stream);
Expand All @@ -223,19 +225,11 @@ pub fn main() !void {
};
}

fn runBuild(builder: *Builder) anyerror!void {
switch (@typeInfo(@typeInfo(@TypeOf(root.build)).Fn.return_type.?)) {
.Void => root.build(builder),
.ErrorUnion => try root.build(builder),
else => @compileError("expected return type of build to be 'void' or '!void'"),
}
}

fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void {
// run the build script to collect the options
if (!already_ran_build) {
builder.resolveInstallPrefix(null, .{});
try runBuild(builder);
try builder.runBuild(root);
}

try out_stream.print(
Expand Down
66 changes: 66 additions & 0 deletions lib/std/Ini.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
bytes: []const u8,

pub const SectionIterator = struct {
ini: Ini,
next_index: ?usize,
header: []const u8,

pub fn next(it: *SectionIterator) ?[]const u8 {
const bytes = it.ini.bytes;
const start = it.next_index orelse return null;
const end = mem.indexOfPos(u8, bytes, start, "\n[") orelse bytes.len;
const result = bytes[start..end];
if (mem.indexOfPos(u8, bytes, start, it.header)) |next_index| {
it.next_index = next_index + it.header.len;
} else {
it.next_index = null;
}
return result;
}
};

/// Asserts that `header` includes "\n[" at the beginning and "]\n" at the end.
/// `header` must remain valid for the lifetime of the iterator.
pub fn iterateSection(ini: Ini, header: []const u8) SectionIterator {
assert(mem.startsWith(u8, header, "\n["));
assert(mem.endsWith(u8, header, "]\n"));
const first_header = header[1..];
const next_index = if (mem.indexOf(u8, ini.bytes, first_header)) |i|
i + first_header.len
else
null;
return .{
.ini = ini,
.next_index = next_index,
.header = header,
};
}

const std = @import("std.zig");
const mem = std.mem;
const assert = std.debug.assert;
const Ini = @This();
const testing = std.testing;

test iterateSection {
const example =
\\[package]
\\name=libffmpeg
\\version=5.1.2
\\
\\[dependency]
\\id=libz
\\url=url1
\\
\\[dependency]
\\id=libmp3lame
\\url=url2
;
var ini: Ini = .{ .bytes = example };
var it = ini.iterateSection("\n[dependency]\n");
const section1 = it.next() orelse return error.TestFailed;
try testing.expectEqualStrings("id=libz\nurl=url1\n", section1);
const section2 = it.next() orelse return error.TestFailed;
try testing.expectEqualStrings("id=libmp3lame\nurl=url2", section2);
try testing.expect(it.next() == null);
}
161 changes: 159 additions & 2 deletions lib/std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ pub const Builder = struct {
search_prefixes: ArrayList([]const u8),
libc_file: ?[]const u8 = null,
installed_files: ArrayList(InstalledFile),
/// Path to the directory containing build.zig.
build_root: []const u8,
cache_root: []const u8,
global_cache_root: []const u8,
release_mode: ?std.builtin.Mode,
is_release: bool,
/// zig lib dir
override_lib_dir: ?[]const u8,
vcpkg_root: VcpkgRoot,
vcpkg_root: VcpkgRoot = .unattempted,
pkg_config_pkg_list: ?(PkgConfigError![]const PkgConfigPkg) = null,
args: ?[][]const u8 = null,
debug_log_scopes: []const []const u8 = &.{},
Expand All @@ -100,6 +102,8 @@ pub const Builder = struct {
/// Information about the native target. Computed before build() is invoked.
host: NativeTargetInfo,

dep_prefix: []const u8 = "",

pub const ExecError = error{
ReadFailure,
ExitCodeFailure,
Expand Down Expand Up @@ -223,7 +227,6 @@ pub const Builder = struct {
.is_release = false,
.override_lib_dir = null,
.install_path = undefined,
.vcpkg_root = VcpkgRoot{ .unattempted = {} },
.args = null,
.host = host,
};
Expand All @@ -233,6 +236,92 @@ pub const Builder = struct {
return self;
}

fn createChild(
parent: *Builder,
dep_name: []const u8,
build_root: []const u8,
args: anytype,
) !*Builder {
const child = try createChildOnly(parent, dep_name, build_root);
try applyArgs(child, args);
return child;
}

fn createChildOnly(parent: *Builder, dep_name: []const u8, build_root: []const u8) !*Builder {
const allocator = parent.allocator;
const child = try allocator.create(Builder);
child.* = .{
.allocator = allocator,
.install_tls = .{
.step = Step.initNoOp(.top_level, "install", allocator),
.description = "Copy build artifacts to prefix path",
},
.uninstall_tls = .{
.step = Step.init(.top_level, "uninstall", allocator, makeUninstall),
.description = "Remove build artifacts from prefix path",
},
.user_input_options = UserInputOptionsMap.init(allocator),
.available_options_map = AvailableOptionsMap.init(allocator),
.available_options_list = ArrayList(AvailableOption).init(allocator),
.verbose = parent.verbose,
.verbose_link = parent.verbose_link,
.verbose_cc = parent.verbose_cc,
.verbose_air = parent.verbose_air,
.verbose_llvm_ir = parent.verbose_llvm_ir,
.verbose_cimport = parent.verbose_cimport,
.verbose_llvm_cpu_features = parent.verbose_llvm_cpu_features,
.prominent_compile_errors = parent.prominent_compile_errors,
.color = parent.color,
.reference_trace = parent.reference_trace,
.invalid_user_input = false,
.zig_exe = parent.zig_exe,
.default_step = undefined,
.env_map = parent.env_map,
.top_level_steps = ArrayList(*TopLevelStep).init(allocator),
.install_prefix = undefined,
.dest_dir = parent.dest_dir,
.lib_dir = parent.lib_dir,
.exe_dir = parent.exe_dir,
.h_dir = parent.h_dir,
.install_path = parent.install_path,
.sysroot = parent.sysroot,
.search_prefixes = ArrayList([]const u8).init(allocator),
.libc_file = parent.libc_file,
.installed_files = ArrayList(InstalledFile).init(allocator),
.build_root = build_root,
.cache_root = parent.cache_root,
.global_cache_root = parent.global_cache_root,
.release_mode = parent.release_mode,
.is_release = parent.is_release,
.override_lib_dir = parent.override_lib_dir,
.debug_log_scopes = parent.debug_log_scopes,
.debug_compile_errors = parent.debug_compile_errors,
.enable_darling = parent.enable_darling,
.enable_qemu = parent.enable_qemu,
.enable_rosetta = parent.enable_rosetta,
.enable_wasmtime = parent.enable_wasmtime,
.enable_wine = parent.enable_wine,
.glibc_runtimes_dir = parent.glibc_runtimes_dir,
.host = parent.host,
.dep_prefix = parent.fmt("{s}{s}.", .{ parent.dep_prefix, dep_name }),
};
try child.top_level_steps.append(&child.install_tls);
try child.top_level_steps.append(&child.uninstall_tls);
child.default_step = &child.install_tls.step;
return child;
}

fn applyArgs(b: *Builder, args: anytype) !void {
// TODO this function is the way that a build.zig file communicates
// options to its dependencies. It is the programmatic way to give
// command line arguments to a build.zig script.
_ = args;
// TODO create a hash based on the args and the package hash, use this
// to compute the install prefix.
const install_prefix = b.pathJoin(&.{ b.cache_root, "pkg" });
b.resolveInstallPrefix(install_prefix, .{});
}

pub fn destroy(self: *Builder) void {
self.env_map.deinit();
self.top_level_steps.deinit();
Expand Down Expand Up @@ -1068,6 +1157,10 @@ pub const Builder = struct {
return self.addInstallFileWithDir(source.dupe(self), .lib, dest_rel_path);
}

pub fn addInstallHeaderFile(b: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep {
return b.addInstallFileWithDir(.{ .path = src_path }, .header, dest_rel_path);
}

pub fn addInstallRaw(self: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8, options: InstallRawStep.CreateOptions) *InstallRawStep {
return InstallRawStep.create(self, artifact, dest_filename, options);
}
Expand Down Expand Up @@ -1300,6 +1393,70 @@ pub const Builder = struct {
&[_][]const u8{ base_dir, dest_rel_path },
) catch unreachable;
}

pub const Dependency = struct {
builder: *Builder,

pub fn artifact(d: *Dependency, name: []const u8) *LibExeObjStep {
var found: ?*LibExeObjStep = null;
for (d.builder.install_tls.step.dependencies.items) |dep_step| {
const inst = dep_step.cast(InstallArtifactStep) orelse continue;
if (mem.eql(u8, inst.artifact.name, name)) {
if (found != null) panic("artifact name '{s}' is ambiguous", .{name});
found = inst.artifact;
}
}
return found orelse {
for (d.builder.install_tls.step.dependencies.items) |dep_step| {
const inst = dep_step.cast(InstallArtifactStep) orelse continue;
log.info("available artifact: '{s}'", .{inst.artifact.name});
}
panic("unable to find artifact '{s}'", .{name});
};
}
};

pub fn dependency(b: *Builder, name: []const u8, args: anytype) *Dependency {
const build_runner = @import("root");
const deps = build_runner.dependencies;

inline for (@typeInfo(deps.imports).Struct.decls) |decl| {
if (mem.startsWith(u8, decl.name, b.dep_prefix) and
mem.endsWith(u8, decl.name, name) and
decl.name.len == b.dep_prefix.len + name.len)
{
const build_zig = @field(deps.imports, decl.name);
const build_root = @field(deps.build_root, decl.name);
return dependencyInner(b, name, build_root, build_zig, args);
}
}

const full_path = b.pathFromRoot("build.zig.ini");
std.debug.print("no dependency named '{s}' in '{s}'\n", .{ name, full_path });
std.process.exit(1);
}

fn dependencyInner(
b: *Builder,
name: []const u8,
build_root: []const u8,
comptime build_zig: type,
args: anytype,
) *Dependency {
const sub_builder = b.createChild(name, build_root, args) catch unreachable;
sub_builder.runBuild(build_zig) catch unreachable;
const dep = b.allocator.create(Dependency) catch unreachable;
dep.* = .{ .builder = sub_builder };
return dep;
}

pub fn runBuild(b: *Builder, build_zig: anytype) anyerror!void {
switch (@typeInfo(@typeInfo(@TypeOf(build_zig.build)).Fn.return_type.?)) {
.Void => build_zig.build(b),
.ErrorUnion => try build_zig.build(b),
else => @compileError("expected return type of build to be 'void' or '!void'"),
}
}
};

test "builder.findProgram compiles" {
Expand Down
Loading