Created
March 6, 2019 01:51
-
-
Save andrewrk/fb26f8f8953d0eb489d3e1f3a9391778 to your computer and use it in GitHub Desktop.
using zig instead of bash template
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 os = std.os; | |
const src = "/home/andy/downloads/glibc"; | |
const j_arg = "-j2"; | |
const configure = src ++ "/configure"; | |
const Target = struct { | |
glibc_name: []const u8, | |
zig_name: []const u8, | |
}; | |
fn t(glibc_name: []const u8, zig_name: []const u8) Target { | |
return Target{ | |
.glibc_name = glibc_name, | |
.zig_name = zig_name, | |
}; | |
} | |
// The commented out ones, glibc recognizes, but prints this message: | |
const targets = []Target{ | |
t("aarch64", "aarch64"), | |
t("aarch64_be", "aarch64_be"), | |
t("amdgcn", "amdgcn"), | |
t("arm", "arm"), | |
t("armeb", "armeb"), | |
t("avr", "avr"), | |
t("hexagon", "hexagon"), | |
t("mips", "mips"), | |
t("mipsel", "mipsel"), | |
t("mips64", "mips64"), | |
t("mipsel", "mipsel"), | |
t("msp430", "msp430"), | |
t("riscv32", "riscv32"), | |
t("riscv64", "riscv64"), | |
t("powerpc", "powerpc"), | |
t("powerpc64", "powerpc64"), | |
t("powerpc64le", "powerpc64le"), | |
t("sparc", "sparc"), | |
t("sparcv9", "sparcv9"), | |
t("x86", "i386"), | |
t("x86_64", "x86_64"), | |
t("nvptx", "nvptx"), | |
t("wasm32", "wasm32"), | |
}; | |
fn exec(argv: []const []const u8) void { | |
for (argv) |arg| { | |
std.debug.warn("{} ", arg); | |
} | |
std.debug.warn("\n"); | |
const child = os.ChildProcess.init(argv, allocator) catch unreachable; | |
defer child.deinit(); | |
const term = child.spawnAndWait() catch |err| { | |
std.debug.panic("Unable to spawn {}: {}\n", argv[0], @errorName(err)); | |
}; | |
switch (term) { | |
os.ChildProcess.Term.Exited => |code| { | |
if (code != 0) { | |
std.debug.panic("The following command exited with error code {}:\n", code); | |
} | |
}, | |
else => { | |
std.debug.panic("The following command terminated unexpectedly:\n"); | |
}, | |
} | |
} | |
var allocator: *std.mem.Allocator = undefined; | |
pub fn main() !void { | |
var direct_allocator = std.heap.DirectAllocator.init(); | |
defer direct_allocator.deinit(); | |
var arena = std.heap.ArenaAllocator.init(&direct_allocator.allocator); | |
defer arena.deinit(); | |
allocator = &arena.allocator; | |
for (targets) |target| { | |
try buildTarget(target); | |
} | |
} | |
fn buildTarget(target: Target) !void { | |
const build_dir = try std.fmt.allocPrint(allocator, "{}/build-{}", src, target.zig_name); | |
os.makeDir(build_dir) catch |err| switch (err) { | |
error.PathAlreadyExists => {}, | |
else => return err, | |
}; | |
std.debug.warn("cd {}\n", build_dir); | |
try os.changeCurDir(build_dir); | |
const prefix_arg = try std.fmt.allocPrint(allocator, "--prefix={}/prefix", build_dir); | |
const exec_prefix_arg = try std.fmt.allocPrint(allocator, "--exec-prefix={}/prefix", build_dir); | |
const host_arg = try std.fmt.allocPrint(allocator, "--host={}-linux-gnu", target.glibc_name); | |
const build_arg = try std.fmt.allocPrint(allocator, "--build=x86_64-linux-gnu"); | |
exec([][]const u8{ configure, prefix_arg, exec_prefix_arg, host_arg, build_arg }); | |
exec([][]const u8{ "make", j_arg }); | |
exec([][]const u8{ "make", "install" }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment