Created
October 30, 2022 03:56
-
-
Save theoparis/91ac2394453fc9971e653d741f7ce7a3 to your computer and use it in GitHub Desktop.
build.zig wip errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const builtin = @import("builtin"); | |
const builder = @import("builder/main.zig"); | |
const Features = std.Target.x86.Feature; | |
const kernel_build_mode: std.builtin.Mode = .Debug; | |
const user_build_mode: std.builtin.Mode = .ReleaseSafe; | |
const target = blk: { | |
var tgt = std.zig.CrossTarget{ | |
.cpu_arch = .x86_64, | |
.os_tag = .freestanding, | |
.abi = .none, | |
}; | |
tgt.cpu_features_sub.addFeature(@enumToInt(Features.mmx)); | |
tgt.cpu_features_sub.addFeature(@enumToInt(Features.sse)); | |
tgt.cpu_features_sub.addFeature(@enumToInt(Features.sse2)); | |
tgt.cpu_features_sub.addFeature(@enumToInt(Features.avx)); | |
tgt.cpu_features_sub.addFeature(@enumToInt(Features.avx2)); | |
tgt.cpu_features_add.addFeature(@enumToInt(Features.soft_float)); | |
break :blk tgt; | |
}; | |
pub fn build(b: *std.build.Builder) !void { | |
// const init = try buildProgram(b, "init"); | |
// const init_path = b.getInstallPath(init.install_step.?.dest_dir, init.out_filename); | |
// | |
// image.step.dependOn(&init.install_step.?.step); | |
var download_mlibc_archive = builder.DownloadStep.init( | |
"download_mlibc_archive", | |
b.allocator, | |
"github.com", | |
443, | |
"/managarm/mlibc/archive/master.zip", | |
"mlibc.zip", | |
); | |
var extract_mlibc_archive = builder.ExtractZipStep.init( | |
"extract_mlibc_archive", | |
b.allocator, | |
"mlibc.zip", | |
"mlibc-master", | |
); | |
extract_mlibc_archive.step.dependOn(&download_mlibc_archive.step); | |
const patch_mlibc = b.addSystemCommand(&[_][]const u8{ | |
"patch", | |
"-p1", | |
"-i", | |
"mlibc.patch", | |
}); | |
patch_mlibc.step.dependOn(&extract_mlibc_archive.step); | |
patch_mlibc.cwd = "mlibc-master"; | |
const kernel = try buildKernel(b); | |
kernel.step.dependOn(&patch_mlibc.step); | |
const image_dir = b.pathJoin(&.{ b.cache_root, "image-root" }); | |
const image_path = b.pathJoin(&.{ b.cache_root, "image.iso" }); | |
const sysroot_path = b.pathJoin(&.{ b.cache_root, "sysroot.tar" }); | |
const kernel_path = b.getInstallPath(kernel.install_step.?.dest_dir, kernel.out_filename); | |
const qemu = b.addSystemCommand(&.{ "sh", "misc/run-emulator.sh", image_path }); | |
const image = b.addSystemCommand(&.{ | |
"sh", | |
"misc/create-image.sh", | |
image_dir, | |
image_path, | |
"sysroot", | |
sysroot_path, | |
kernel_path, | |
}); | |
image.step.dependOn(&kernel.install_step.?.step); | |
qemu.step.dependOn(&image.step); | |
if (b.args) |args| { | |
qemu.addArgs(args); | |
} | |
b.step("image", "Builds the image").dependOn(&image.step); | |
b.step("run", "Runs the image").dependOn(&qemu.step); | |
} | |
fn buildKernel(b: *std.build.Builder) !*std.build.LibExeObjStep { | |
const kernel = b.addExecutable("kernel", "src/main.zig"); | |
kernel.code_model = .kernel; | |
kernel.setBuildMode(kernel_build_mode); | |
kernel.setLinkerScriptPath(.{ .path = "misc/linker.ld" }); | |
kernel.addIncludePath("mlibc-master"); | |
kernel.addIncludePath("mlibc-master/options/ansi/include"); | |
kernel.addIncludePath("mlibc-master/options/internal/include"); | |
kernel.addIncludePath("mlibc-master/options/linux-headers/include"); | |
kernel.addIncludePath("mlibc-master/options/posix/include"); | |
kernel.addIncludePath("mlibc-master/sysdeps/zigux/include"); | |
kernel.install(); | |
kernel.setTarget(target); | |
return kernel; | |
} | |
fn buildProgram(b: *std.build.Builder, comptime name: []const u8) !*std.build.LibExeObjStep { | |
const kernel = b.addExecutable(name, "user/" ++ name ++ "/main.zig"); | |
kernel.code_model = .small; | |
kernel.setBuildMode(user_build_mode); | |
kernel.install(); | |
kernel.setTarget(target); | |
return kernel; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const deps = @import("../deps.zig"); | |
// Might want to use zfetch instead, not sure | |
const client = deps.imports.tls_client; | |
const archive = deps.imports.archive; | |
const Step = std.build.Step; | |
pub fn download( | |
allocator: std.mem.Allocator, | |
host: []const u8, | |
port: u16, | |
path: []const u8, | |
output_file_path: []const u8, | |
) !void { | |
std.log.info("started download of {s}/{s}", .{ host, path }); | |
var tls_client = try client.TLSClientTCP.init(allocator); | |
defer tls_client.deinit(); | |
try tls_client.connect(host, port); | |
const http_req = try std.fmt.allocPrint( | |
allocator, | |
"GET / HTTP/1.1\r\nHost:{s}\r\nUser-Agent: tls13-zig\r\nAccept: */*\r\n\r\n", | |
.{host}, | |
); | |
_ = try tls_client.send(http_req); | |
var recv_bytes: [4096]u8 = undefined; | |
const recv_size = try tls_client.recv(&recv_bytes); | |
const output_file = try std.fs.cwd().openFile(output_file_path, .{ .mode = .read_write }); | |
defer output_file.close(); | |
_ = try output_file.write(recv_bytes[0..recv_size]); | |
try tls_client.close(); | |
std.log.info("finished download of {s}/{s}", .{ host, path }); | |
} | |
pub fn extractZip( | |
allocator: std.mem.Allocator, | |
input_file_path: []const u8, | |
output_dir_path: []const u8, | |
) !void { | |
var file = try std.fs.cwd().openFile(input_file_path, .{}); | |
defer file.close(); | |
var stream_source = std.io.StreamSource{ | |
.file = file, | |
}; | |
var arc = archive.formats.zip.reader.ArchiveReader.init(allocator, &stream_source); | |
defer arc.deinit(); | |
// Load | |
try arc.load(); | |
// Extract | |
for (arc.directory.items) |_, j| { | |
const hdr = arc.getHeader(j); | |
if (hdr.uncompressed_size > 0) { | |
const out = try std.fs.cwd().createFile( | |
try std.fmt.allocPrint( | |
allocator, | |
"{s}/{s}", | |
.{ output_dir_path, hdr.filename }, | |
), | |
.{}, | |
); | |
defer out.close(); | |
try arc.extractFile(hdr, out.writer(), true); | |
} | |
} | |
} | |
pub const ExtractZipStep = struct { | |
const Self = @This(); | |
step: Step, | |
input_file_path: []const u8, | |
output_dir_path: []const u8, | |
allocator: std.mem.Allocator, | |
pub fn init( | |
name: []const u8, | |
allocator: std.mem.Allocator, | |
input_file_path: []const u8, | |
output_dir_path: []const u8, | |
) Self { | |
return .{ | |
.step = Step.init(.custom, name, allocator, Self.doStep), | |
.input_file_path = input_file_path, | |
.output_dir_path = output_dir_path, | |
.allocator = allocator, | |
}; | |
} | |
pub fn doStep(step: *Step) anyerror!void { | |
const self = @fieldParentPtr(Self, "step", step); | |
try extractZip(self.allocator, self.input_file_path, self.output_dir_path); | |
} | |
}; | |
pub const DownloadStep = struct { | |
const Self = @This(); | |
step: Step, | |
allocator: std.mem.Allocator, | |
host: []const u8, | |
port: u16, | |
path: []const u8, | |
output_file_path: []const u8, | |
pub fn init( | |
name: []const u8, | |
allocator: std.mem.Allocator, | |
host: []const u8, | |
port: u16, | |
path: []const u8, | |
output_file_path: []const u8, | |
) Self { | |
return .{ | |
.step = Step.init(.custom, name, allocator, Self.doStep), | |
.host = host, | |
.port = port, | |
.path = path, | |
.output_file_path = output_file_path, | |
.allocator = allocator, | |
}; | |
} | |
pub fn doStep(step: *Step) anyerror!void { | |
const self = @fieldParentPtr(Self, "step", step); | |
try download(self.allocator, self.host, self.port, self.path, self.output_file_path); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment