Skip to content

Instantly share code, notes, and snippets.

@ostcar
Created April 2, 2024 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ostcar/a984e7227e71826e53cd4c9b0a9892ca to your computer and use it in GitHub Desktop.
Save ostcar/a984e7227e71826e53cd4c9b0a9892ca to your computer and use it in GitHub Desktop.
Example of a build.zig file, that preprocesses a roc platform
const std = @import("std");
const CrossTarget = std.zig.CrossTarget;
const OptimizeMode = std.builtin.OptimizeMode;
const LazyPath = std.Build.LazyPath;
const Compile = std.Build.Step.Compile;
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Build libapp.so
const build_libapp_so = b.addSystemCommand(&.{"roc"});
build_libapp_so.addArgs(&.{ "build", "--lib" });
build_libapp_so.addFileArg(.{ .path = "examples/main.roc" });
build_libapp_so.addArg("--output");
const libapp_filename = build_libapp_so.addOutputFileArg("libapp.so");
// Build dynhost
const dynhost = buildDynhost(b, target, optimize, libapp_filename);
// Copy dynhost to platform
const copy_dynhost = b.addWriteFiles();
copy_dynhost.addCopyFileToSource(dynhost.getEmittedBin(), "platform/dynhost");
copy_dynhost.step.dependOn(&dynhost.step);
// Command to create dynhost
const cmd_dynhost = b.step("dynhost", "Build the platform");
cmd_dynhost.dependOn(&copy_dynhost.step);
// Preprocess host
const preprocess_host = b.addSystemCommand(&.{"roc"});
preprocess_host.addArg("preprocess-host");
preprocess_host.addFileArg(.{ .path = "examples/main.roc" });
preprocess_host.step.dependOn(&copy_dynhost.step);
// Command to preprocess host
const cmd_preprocess = b.step("preprocess", "preprocess the platform");
cmd_preprocess.dependOn(&preprocess_host.step);
// Bundle platform
const bundle_platform = b.addSystemCommand(&.{"roc"});
bundle_platform.addArgs(&.{ "build", "--bundle", ".tar.br", "platform/main.roc" });
bundle_platform.step.dependOn(&preprocess_host.step);
// Comand to bundle the platform
const cmd_bundle = b.step("bundle", "bundle the platform");
cmd_bundle.dependOn(&bundle_platform.step);
b.default_step = &bundle_platform.step;
}
fn buildDynhost(b: *std.Build, target: CrossTarget, optimize: OptimizeMode, libapp_filename: LazyPath) *Compile {
const dynhost = b.addExecutable(.{
.name = "dynhost",
.root_source_file = .{ .path = "platform/host.zig" },
.target = target,
.optimize = optimize,
});
dynhost.pie = true;
dynhost.rdynamic = true;
dynhost.bundle_compiler_rt = true;
dynhost.linkLibC();
const roc_std = b.createModule(.{ .source_file = .{ .path = "roc-std/glue.zig" } });
dynhost.addModule("roc-std", roc_std);
dynhost.addObjectFile(libapp_filename);
return dynhost;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment