aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeer <himbeer@disroot.org>2024-08-02 00:29:50 +0200
committerHimbeer <himbeer@disroot.org>2024-08-02 00:30:35 +0200
commit2d8bd0c2a9d878adb950819cd7e18ed2d27b6766 (patch)
tree817d41d1ac762b79142ba3779b871ae6c31ff0d4
parent719b97c31f17f5530cbc6d5c2e6e52115313bb11 (diff)
examples: Add message_passing example program collection
-rw-r--r--examples/message_passing/.gitignore7
-rw-r--r--examples/message_passing/build.zig102
-rw-r--r--examples/message_passing/build.zig.zon44
-rw-r--r--examples/message_passing/src/main.zig33
-rw-r--r--examples/message_passing/src/main2.zig18
l---------examples/message_passing/src/program21
6 files changed, 205 insertions, 0 deletions
diff --git a/examples/message_passing/.gitignore b/examples/message_passing/.gitignore
new file mode 100644
index 0000000..69b00b1
--- /dev/null
+++ b/examples/message_passing/.gitignore
@@ -0,0 +1,7 @@
+# SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
+#
+# SPDX-License-Identifier: CC0-1.0
+
+# zig cache and output
+/zig-*/
+/.zig-*/
diff --git a/examples/message_passing/build.zig b/examples/message_passing/build.zig
new file mode 100644
index 0000000..0718d3b
--- /dev/null
+++ b/examples/message_passing/build.zig
@@ -0,0 +1,102 @@
+// SPDX-FileCopyrightText: Zig contributors
+// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
+//
+// SPDX-License-Identifier: MIT
+
+const std = @import("std");
+
+// Although this function looks imperative, note that its job is to
+// declaratively construct a build graph that will be executed by an external
+// runner.
+pub fn build(b: *std.Build) void {
+ const riscv64_freestanding = std.zig.CrossTarget{
+ .cpu_arch = .riscv64,
+ .os_tag = .freestanding,
+ .ofmt = .elf,
+ };
+ // Standard target options allows the person running `zig build` to choose
+ // what target to build for. Here we do not override the defaults, which
+ // means any target is allowed, and the default is native. Other options
+ // for restricting supported target set are available.
+ const target = b.standardTargetOptions(.{ .whitelist = &[_]std.zig.CrossTarget{riscv64_freestanding}, .default_target = riscv64_freestanding });
+
+ // Standard optimization options allow the person running `zig build` to select
+ // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
+ // set a preferred release mode, allowing the user to decide how to optimize.
+ const optimize = b.standardOptimizeOption(.{});
+
+ const srvre_sys = b.dependency("srvre_sys", .{
+ .target = target,
+ .optimize = optimize,
+ });
+
+ const exe2 = b.addExecutable(.{
+ .name = "program2",
+ .root_source_file = b.path("src/main2.zig"),
+ .target = target,
+ .optimize = optimize,
+ });
+ const exe = b.addExecutable(.{
+ .name = "message_passing",
+ .root_source_file = b.path("src/main.zig"),
+ .target = target,
+ .optimize = optimize,
+ });
+
+ exe.step.dependOn(&exe2.step);
+
+ exe2.root_module.addImport("srvre_sys", srvre_sys.module("srvre_sys"));
+ exe.root_module.addImport("srvre_sys", srvre_sys.module("srvre_sys"));
+
+ // This declares intent for the executable to be installed into the
+ // standard location when the user invokes the "install" step (the default
+ // step when running `zig build`).
+ b.installArtifact(exe2);
+ b.installArtifact(exe);
+
+ // This *creates* a Run step in the build graph, to be executed when another
+ // step is evaluated that depends on it. The next line below will establish
+ // such a dependency.
+ const run_cmd = b.addRunArtifact(exe);
+
+ // By making the run step depend on the install step, it will be run from the
+ // installation directory rather than directly from within the cache directory.
+ // This is not necessary, however, if the application depends on other installed
+ // files, this ensures they will be present and in the expected location.
+ run_cmd.step.dependOn(b.getInstallStep());
+
+ // This allows the user to pass arguments to the application in the build
+ // command itself, like this: `zig build run -- arg1 arg2 etc`
+ if (b.args) |args| {
+ run_cmd.addArgs(args);
+ }
+
+ // This creates a build step. It will be visible in the `zig build --help` menu,
+ // and can be selected like this: `zig build run`
+ // This will evaluate the `run` step rather than the default, which is "install".
+ const run_step = b.step("run", "Run the app");
+ run_step.dependOn(&run_cmd.step);
+
+ // Creates a step for unit testing. This only builds the test executable
+ // but does not run it.
+ const exe2_unit_tests = b.addTest(.{
+ .root_source_file = b.path("src/main2.zig"),
+ .target = target,
+ .optimize = optimize,
+ });
+ const exe_unit_tests = b.addTest(.{
+ .root_source_file = b.path("src/main.zig"),
+ .target = target,
+ .optimize = optimize,
+ });
+
+ const run_exe2_unit_tests = b.addRunArtifact(exe2_unit_tests);
+ const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
+
+ // Similar to creating the run step earlier, this exposes a `test` step to
+ // the `zig build --help` menu, providing a way for the user to request
+ // running the unit tests.
+ const test_step = b.step("test", "Run unit tests");
+ test_step.dependOn(&run_exe2_unit_tests.step);
+ test_step.dependOn(&run_exe_unit_tests.step);
+}
diff --git a/examples/message_passing/build.zig.zon b/examples/message_passing/build.zig.zon
new file mode 100644
index 0000000..4cff5c1
--- /dev/null
+++ b/examples/message_passing/build.zig.zon
@@ -0,0 +1,44 @@
+// SPDX-FileCopyrightText: Zig contributors
+// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
+//
+// SPDX-License-Identifier: MIT
+
+.{
+ // This is the default name used by packages depending on this one. For
+ // example, when a user runs `zig fetch --save <url>`, this field is used
+ // as the key in the `dependencies` table. Although the user can choose a
+ // different name, most users will stick with this provided value.
+ //
+ // It is redundant to include "zig" in this name because it is already
+ // within the Zig package namespace.
+ .name = "message_passing",
+
+ // This is a [Semantic Version](https://semver.org/).
+ // In a future version of Zig it will be used for package deduplication.
+ .version = "0.0.0",
+
+ // This field is optional.
+ // This is currently advisory only; Zig does not yet do anything
+ // with this value.
+ //.minimum_zig_version = "0.11.0",
+
+ // This field is optional.
+ // Each dependency must either provide a `url` and `hash`, or a `path`.
+ // `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
+ // Once all dependencies are fetched, `zig build` no longer requires
+ // internet connectivity.
+ .dependencies = .{
+ .srvre_sys = .{
+ .url = "https://codeberg.org/Himbeer/srvre_sys/archive/e1cfddc48ae3f439b898405bbe165efcde0c92e6.tar.gz",
+ .hash = "1220759f392fa652192b071a7455b033c820c013a6ad345397c7cbf7fc278c68ecce",
+ },
+ },
+ .paths = .{
+ "build.zig",
+ "build.zig.zon",
+ "src",
+ // For example...
+ //"LICENSE",
+ //"README.md",
+ },
+}
diff --git a/examples/message_passing/src/main.zig b/examples/message_passing/src/main.zig
new file mode 100644
index 0000000..e637f1a
--- /dev/null
+++ b/examples/message_passing/src/main.zig
@@ -0,0 +1,33 @@
+// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+const srvre_sys = @import("srvre_sys");
+const os = srvre_sys.os;
+
+usingnamespace srvre_sys;
+
+const program2 = @embedFile("program2");
+var program2_aligned: [program2.len]u8 = undefined;
+
+pub fn main() void {
+ _ = os.consoleWrite("Hello from program 1\r\n") catch unreachable;
+
+ os.join(0) catch unreachable;
+
+ @memcpy(program2_aligned[0..], program2);
+ _ = os.launch(@alignCast(program2_aligned[0..])) catch unreachable;
+
+ var sender: u16 = undefined;
+ var buffer: [256]u8 = undefined;
+ while (true) {
+ const n = os.receive(0, &sender, &buffer) catch continue;
+ const message = buffer[0..n];
+
+ _ = os.consoleWrite("Program 1 received message from PID ") catch unreachable;
+ _ = os.consoleWrite(&[_]u8{'0' + @as(u8, @intCast(sender))}) catch unreachable;
+ _ = os.consoleWrite(": ") catch unreachable;
+ _ = os.consoleWrite(message) catch unreachable;
+ _ = os.consoleWrite("\r\n") catch unreachable;
+ }
+}
diff --git a/examples/message_passing/src/main2.zig b/examples/message_passing/src/main2.zig
new file mode 100644
index 0000000..c56f76f
--- /dev/null
+++ b/examples/message_passing/src/main2.zig
@@ -0,0 +1,18 @@
+// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+const srvre_sys = @import("srvre_sys");
+const os = srvre_sys.os;
+
+usingnamespace srvre_sys;
+
+pub fn main() void {
+ _ = os.consoleWrite("Hello from program 2\r\n") catch unreachable;
+
+ os.pass(0, 0, false, "Anonymous message") catch unreachable;
+ os.pass(0, 0, true, "Identified message") catch unreachable;
+ os.pass(1, 0, false, "Mischanneled message") catch unreachable;
+ os.pass(0, 1, false, "Unicast message") catch unreachable;
+ os.pass(0, 2, false, "Misdirected message") catch unreachable;
+}
diff --git a/examples/message_passing/src/program2 b/examples/message_passing/src/program2
new file mode 120000
index 0000000..03417da
--- /dev/null
+++ b/examples/message_passing/src/program2
@@ -0,0 +1 @@
+../zig-out/bin/program2 \ No newline at end of file