xref: /petsc/src/snes/tutorials/build.zig (revision a69119a591a03a9d906b29c0a4e9802e4d7c9795)
1// Uses zig to compiler ex1z.zig
2
3const std = @import("std");
4
5pub fn build(b: *std.build.Builder) void {
6    // Standard target options allows the person running `zig build` to choose
7    // what target to build for. Here we do not override the defaults, which
8    // means any target is allowed, and the default is native. Other options
9    // for restricting supported target set are available.
10    const target = b.standardTargetOptions(.{});
11
12    // Standard release options allow the person running `zig build` to select
13    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
14    const mode = b.standardReleaseOptions();
15
16    const exe = b.addExecutable("ex1z", "ex1z.zig");
17
18    const PETSC_DIR = std.os.getenv("PETSC_DIR") orelse unreachable;
19    const PETSC_ARCH = std.os.getenv("PETSC_ARCH") orelse  unreachable;
20    var path = std.fs.path.join(std.heap.c_allocator, &[_][] const u8 { PETSC_DIR,PETSC_ARCH,"lib"});
21    if (path) |value| {exe.addLibPath(value);}  else |_| {std.debug.print("Error bad path: {s}\n", .{path});}
22    if (path) |value| {exe.addRPath(value);}  else |_| {}
23    // This should not be needed but is export DYLD_LIBRARY_PATH=$PETSC_DIR/$PETSC_ARCH/lib
24    path = std.fs.path.join(std.heap.c_allocator, &[_][] const u8 { PETSC_DIR,"include"});
25    if (path) |value| {exe.addIncludeDir(value);}  else |_| {}
26    path = std.fs.path.join(std.heap.c_allocator, &[_][] const u8 { PETSC_DIR,PETSC_ARCH,"include"});
27    if (path) |value| {exe.addIncludeDir(value);}  else |_| {}
28
29    exe.linkSystemLibrary("petsc");
30    exe.setTarget(target);
31    exe.setBuildMode(mode);
32    exe.install();
33
34    const run_cmd = exe.run();
35    run_cmd.step.dependOn(b.getInstallStep());
36    if (b.args) |args| {
37        run_cmd.addArgs(args);
38    }
39
40    const run_step = b.step("run", "Run the app");
41    run_step.dependOn(&run_cmd.step);
42}
43