Skip to content

Commit 76b5170

Browse files
committed
Add zig example
1 parent 23d5e68 commit 76b5170

File tree

8 files changed

+210
-1
lines changed

8 files changed

+210
-1
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ bin/
88
debug/
99
target/
1010
obj/
11-
*.exe
11+
*.exe
12+
.zig-cache
13+
zig-out

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This extension is designed to work with **all languages** supported by Visual St
2020
| C | GDB ||| |
2121
| Rust | lldb ||| |
2222
| C# | dotnet ||| dotnet 8 + 6 |
23+
| Zig | lldb ||| version 0.14 |
2324

2425
✅ tested | ❌ not working | ❔ not tested
2526

test_code/zig/.vscode/extensions.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"vadimcn.vscode-lldb"
4+
]
5+
}

test_code/zig/.vscode/launch.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "(lldb) Launch",
6+
"type": "lldb",
7+
"request": "launch",
8+
"program": "${workspaceFolder}/zig-out/bin/zig",
9+
"cwd": "${workspaceFolder}",
10+
"preLaunchTask": "build"
11+
}
12+
]
13+
}

test_code/zig/.vscode/tasks.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"type": "shell",
7+
"command": "zig build",
8+
"problemMatcher": [],
9+
"group": {
10+
"kind": "build",
11+
"isDefault": true
12+
}
13+
}
14+
]
15+
}

test_code/zig/build.zig

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const std = @import("std");
2+
3+
// Although this function looks imperative, note that its job is to
4+
// declaratively construct a build graph that will be executed by an external
5+
// runner.
6+
pub fn build(b: *std.Build) void {
7+
// Standard target options allows the person running `zig build` to choose
8+
// what target to build for. Here we do not override the defaults, which
9+
// means any target is allowed, and the default is native. Other options
10+
// for restricting supported target set are available.
11+
const target = b.standardTargetOptions(.{});
12+
13+
// Standard optimization options allow the person running `zig build` to select
14+
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
15+
// set a preferred release mode, allowing the user to decide how to optimize.
16+
const optimize = b.standardOptimizeOption(.{});
17+
18+
// We will also create a module for our other entry point, 'main.zig'.
19+
const exe_mod = b.createModule(.{
20+
// `root_source_file` is the Zig "entry point" of the module. If a module
21+
// only contains e.g. external object files, you can make this `null`.
22+
// In this case the main source file is merely a path, however, in more
23+
// complicated build scripts, this could be a generated file.
24+
.root_source_file = b.path("src/main.zig"),
25+
.target = target,
26+
.optimize = optimize,
27+
});
28+
29+
// This creates another `std.Build.Step.Compile`, but this one builds an executable
30+
// rather than a static library.
31+
const exe = b.addExecutable(.{
32+
.name = "zig",
33+
.root_module = exe_mod,
34+
});
35+
36+
// This declares intent for the executable to be installed into the
37+
// standard location when the user invokes the "install" step (the default
38+
// step when running `zig build`).
39+
b.installArtifact(exe);
40+
41+
// This *creates* a Run step in the build graph, to be executed when another
42+
// step is evaluated that depends on it. The next line below will establish
43+
// such a dependency.
44+
const run_cmd = b.addRunArtifact(exe);
45+
46+
// By making the run step depend on the install step, it will be run from the
47+
// installation directory rather than directly from within the cache directory.
48+
// This is not necessary, however, if the application depends on other installed
49+
// files, this ensures they will be present and in the expected location.
50+
run_cmd.step.dependOn(b.getInstallStep());
51+
52+
// This allows the user to pass arguments to the application in the build
53+
// command itself, like this: `zig build run -- arg1 arg2 etc`
54+
if (b.args) |args| {
55+
run_cmd.addArgs(args);
56+
}
57+
58+
// This creates a build step. It will be visible in the `zig build --help` menu,
59+
// and can be selected like this: `zig build run`
60+
// This will evaluate the `run` step rather than the default, which is "install".
61+
const run_step = b.step("run", "Run the app");
62+
run_step.dependOn(&run_cmd.step);
63+
64+
const exe_unit_tests = b.addTest(.{
65+
.root_module = exe_mod,
66+
});
67+
68+
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
69+
70+
// Similar to creating the run step earlier, this exposes a `test` step to
71+
// the `zig build --help` menu, providing a way for the user to request
72+
// running the unit tests.
73+
const test_step = b.step("test", "Run unit tests");
74+
test_step.dependOn(&run_exe_unit_tests.step);
75+
}

test_code/zig/build.zig.zon

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
.{
2+
// This is the default name used by packages depending on this one. For
3+
// example, when a user runs `zig fetch --save <url>`, this field is used
4+
// as the key in the `dependencies` table. Although the user can choose a
5+
// different name, most users will stick with this provided value.
6+
//
7+
// It is redundant to include "zig" in this name because it is already
8+
// within the Zig package namespace.
9+
.name = "zig",
10+
11+
// This is a [Semantic Version](https://semver.org/).
12+
// In a future version of Zig it will be used for package deduplication.
13+
.version = "0.0.0",
14+
15+
// This field is optional.
16+
// This is currently advisory only; Zig does not yet do anything
17+
// with this value.
18+
//.minimum_zig_version = "0.11.0",
19+
20+
// This field is optional.
21+
// Each dependency must either provide a `url` and `hash`, or a `path`.
22+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
23+
// Once all dependencies are fetched, `zig build` no longer requires
24+
// internet connectivity.
25+
.dependencies = .{
26+
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
27+
//.example = .{
28+
// // When updating this field to a new URL, be sure to delete the corresponding
29+
// // `hash`, otherwise you are communicating that you expect to find the old hash at
30+
// // the new URL. If the contents of a URL change this will result in a hash mismatch
31+
// // which will prevent zig from using it.
32+
// .url = "https://example.com/foo.tar.gz",
33+
//
34+
// // This is computed from the file contents of the directory of files that is
35+
// // obtained after fetching `url` and applying the inclusion rules given by
36+
// // `paths`.
37+
// //
38+
// // This field is the source of truth; packages do not come from a `url`; they
39+
// // come from a `hash`. `url` is just one of many possible mirrors for how to
40+
// // obtain a package matching this `hash`.
41+
// //
42+
// // Uses the [multihash](https://multiformats.io/multihash/) format.
43+
// .hash = "...",
44+
//
45+
// // When this is provided, the package is found in a directory relative to the
46+
// // build root. In this case the package's hash is irrelevant and therefore not
47+
// // computed. This field and `url` are mutually exclusive.
48+
// .path = "foo",
49+
//
50+
// // When this is set to `true`, a package is declared to be lazily
51+
// // fetched. This makes the dependency only get fetched if it is
52+
// // actually used.
53+
// .lazy = false,
54+
//},
55+
},
56+
57+
// Specifies the set of files and directories that are included in this package.
58+
// Only files and directories listed here are included in the `hash` that
59+
// is computed for this package. Only files listed here will remain on disk
60+
// when using the zig package manager. As a rule of thumb, one should list
61+
// files required for compilation plus any license(s).
62+
// Paths are relative to the build root. Use the empty string (`""`) to refer to
63+
// the build root itself.
64+
// A directory listed here means that all files within, recursively, are included.
65+
.paths = .{
66+
"build.zig",
67+
"build.zig.zon",
68+
"src",
69+
// For example...
70+
//"LICENSE",
71+
//"README.md",
72+
},
73+
}

test_code/zig/src/main.zig

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
5+
6+
const stdout_file = std.io.getStdOut().writer();
7+
var bw = std.io.bufferedWriter(stdout_file);
8+
const stdout = bw.writer();
9+
10+
try stdout.print("Run `zig build test` to run the tests.\n", .{});
11+
12+
try hello(stdout.any(), "my number is {}");
13+
14+
try bw.flush();
15+
}
16+
17+
fn hello(writer: std.io.AnyWriter, comptime str: []const u8) !void {
18+
const number = getNumber();
19+
20+
try writer.print(str, .{number});
21+
}
22+
23+
fn getNumber() i32 {
24+
return 1337;
25+
}

0 commit comments

Comments
 (0)