Skip to content

Commit 2892b13

Browse files
authored
Merge pull request #77 from codecrafters-io/upgrade-zig
Update Zig version to 0.12 in the starter code and solution code.
2 parents 0448481 + c466a38 commit 2892b13

File tree

21 files changed

+543
-155
lines changed

21 files changed

+543
-155
lines changed

compiled_starters/zig/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Time to move on to the next stage!
3030

3131
Note: This section is for stages 2 and beyond.
3232

33-
1. Ensure you have `zig (0.11)` installed locally
33+
1. Ensure you have `zig (0.12)` installed locally
3434
1. Run `./your_sqlite3.sh` to run your program, which is implemented in
3535
`app/main.zig`.
3636
1. Commit your changes and run `git push origin master` to submit your solution

compiled_starters/zig/app/main.zig

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
const std = @import("std");
22

33
pub fn main() !void {
4-
// You can use print statements as follows for debugging, they'll be visible when running tests.
5-
try std.io.getStdOut().writer().print("Logs from your program will appear here\n", .{});
6-
7-
// Uncomment this to pass the first stage
8-
//
9-
// var gpa = std.heap.GeneralPurposeAllocator(.{}){};
10-
// defer _ = gpa.deinit();
11-
// const allocator = gpa.allocator();
12-
//
13-
// const args = try std.process.argsAlloc(allocator);
14-
// defer std.process.argsFree(allocator, args);
15-
//
16-
//
17-
// if (args.len < 3) {
18-
// try std.io.getStdErr().writer().print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
19-
// return;
20-
// }
21-
//
22-
// var database_file_path: []const u8 = args[1];
23-
// var command: []const u8 = args[2];
24-
//
25-
// if (std.mem.eql(u8, command, ".dbinfo")) {
26-
// var file = try std.fs.cwd().openFile(database_file_path, .{});
27-
// defer file.close();
28-
//
29-
// var buf: [2]u8 = undefined;
30-
// _ = try file.seekTo(16);
31-
// _ = try file.read(&buf);
32-
// const page_size = std.mem.readInt(u16, &buf, .Big);
33-
// try std.io.getStdOut().writer().print("database page size: {}\n", .{page_size});
34-
// }
4+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
5+
defer _ = gpa.deinit();
6+
const allocator = gpa.allocator();
7+
8+
const args = try std.process.argsAlloc(allocator);
9+
defer std.process.argsFree(allocator, args);
10+
11+
if (args.len < 3) {
12+
try std.io.getStdErr().writer().print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
13+
return;
14+
}
15+
16+
const database_file_path: []const u8 = args[1];
17+
const command: []const u8 = args[2];
18+
19+
20+
if (std.mem.eql(u8, command, ".dbinfo")) {
21+
var file = try std.fs.cwd().openFile(database_file_path, .{});
22+
defer file.close();
23+
24+
// You can use print statements as follows for debugging, they'll be visible when running tests.
25+
try std.io.getStdOut().writer().print("Logs from your program will appear here\n", .{});
26+
27+
// Uncomment this block to pass the first stage
28+
// var buf: [2]u8 = undefined;
29+
// _ = try file.seekTo(16);
30+
// _ = try file.read(&buf);
31+
// const page_size = std.mem.readInt(u16, &buf, .big);
32+
// try std.io.getStdOut().writer().print("database page size: {}\n", .{page_size});
33+
}
3534
}

compiled_starters/zig/build.zig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const std = @import("std");
2+
3+
// Learn more about this file here: https://ziglang.org/learn/build-system
4+
pub fn build(b: *std.Build) void {
5+
const exe = b.addExecutable(.{
6+
.name = "zig",
7+
.root_source_file = b.path("src/main.zig"),
8+
.target = b.standardTargetOptions(.{}),
9+
.optimize = b.standardOptimizeOption(.{}),
10+
});
11+
12+
// This declares intent for the executable to be installed into the
13+
// standard location when the user invokes the "install" step (the default
14+
// step when running `zig build`).
15+
b.installArtifact(exe);
16+
17+
// This *creates* a Run step in the build graph, to be executed when another
18+
// step is evaluated that depends on it. The next line below will establish
19+
// such a dependency.
20+
const run_cmd = b.addRunArtifact(exe);
21+
22+
// This creates a build step. It will be visible in the `zig build --help` menu,
23+
// and can be selected like this: `zig build run`
24+
// This will evaluate the `run` step rather than the default, which is "install".
25+
const run_step = b.step("run", "Run the app");
26+
run_step.dependOn(&run_cmd.step);
27+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.{
2+
.name = "zig",
3+
// This is a [Semantic Version](https://semver.org/).
4+
// In a future version of Zig it will be used for package deduplication.
5+
.version = "0.0.0",
6+
7+
// This field is optional.
8+
// This is currently advisory only; Zig does not yet do anything
9+
// with this value.
10+
//.minimum_zig_version = "0.12.0",
11+
12+
// This field is optional.
13+
// Each dependency must either provide a `url` and `hash`, or a `path`.
14+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
15+
// Once all dependencies are fetched, `zig build` no longer requires
16+
// internet connectivity.
17+
.dependencies = .{
18+
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
19+
//.example = .{
20+
// // When updating this field to a new URL, be sure to delete the corresponding
21+
// // `hash`, otherwise you are communicating that you expect to find the old hash at
22+
// // the new URL.
23+
// .url = "https://example.com/foo.tar.gz",
24+
//
25+
// // This is computed from the file contents of the directory of files that is
26+
// // obtained after fetching `url` and applying the inclusion rules given by
27+
// // `paths`.
28+
// //
29+
// // This field is the source of truth; packages do not come from a `url`; they
30+
// // come from a `hash`. `url` is just one of many possible mirrors for how to
31+
// // obtain a package matching this `hash`.
32+
// //
33+
// // Uses the [multihash](https://multiformats.io/multihash/) format.
34+
// .hash = "...",
35+
//
36+
// // When this is provided, the package is found in a directory relative to the
37+
// // build root. In this case the package's hash is irrelevant and therefore not
38+
// // computed. This field and `url` are mutually exclusive.
39+
// .path = "foo",
40+
41+
// // When this is set to `true`, a package is declared to be lazily
42+
// // fetched. This makes the dependency only get fetched if it is
43+
// // actually used.
44+
// .lazy = false,
45+
//},
46+
},
47+
48+
// Specifies the set of files and directories that are included in this package.
49+
// Only files and directories listed here are included in the `hash` that
50+
// is computed for this package.
51+
// Paths are relative to the build root. Use the empty string (`""`) to refer to
52+
// the build root itself.
53+
// A directory listed here means that all files within, recursively, are included.
54+
.paths = .{
55+
// This makes *all* files, recursively, included in this package. It is generally
56+
// better to explicitly list the files and directories instead, to insure that
57+
// fetching from tarballs, file system paths, and version control all result
58+
// in the same contents hash.
59+
"",
60+
// For example...
61+
//"build.zig",
62+
//"build.zig.zon",
63+
//"src",
64+
//"LICENSE",
65+
//"README.md",
66+
},
67+
}

compiled_starters/zig/codecrafters.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ debug: false
77
# Use this to change the Zig version used to run your code
88
# on Codecrafters.
99
#
10-
# Available versions: zig-0.11
11-
language_pack: zig-0.11
10+
# Available versions: zig-0.12
11+
language_pack: zig-0.12

compiled_starters/zig/src/main.zig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
5+
defer _ = gpa.deinit();
6+
const allocator = gpa.allocator();
7+
8+
const args = try std.process.argsAlloc(allocator);
9+
defer std.process.argsFree(allocator, args);
10+
11+
if (args.len < 3) {
12+
try std.io.getStdErr().writer().print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
13+
return;
14+
}
15+
16+
const database_file_path: []const u8 = args[1];
17+
const command: []const u8 = args[2];
18+
19+
20+
if (std.mem.eql(u8, command, ".dbinfo")) {
21+
var file = try std.fs.cwd().openFile(database_file_path, .{});
22+
defer file.close();
23+
24+
// You can use print statements as follows for debugging, they'll be visible when running tests.
25+
try std.io.getStdOut().writer().print("Logs from your program will appear here\n", .{});
26+
27+
// Uncomment this block to pass the first stage
28+
// var buf: [2]u8 = undefined;
29+
// _ = try file.seekTo(16);
30+
// _ = try file.read(&buf);
31+
// const page_size = std.mem.readInt(u16, &buf, .big);
32+
// try std.io.getStdOut().writer().print("database page size: {}\n", .{page_size});
33+
}
34+
}

dockerfiles/zig-0.12.Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# syntax=docker/dockerfile:1.7-labs
2+
FROM alpine:3.20
3+
4+
# Add the testing repository
5+
RUN echo "@community http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
6+
7+
# We should use build.zig & build.zig.zon to cache downloading deps, but we don't have this wired up yet.
8+
#
9+
# For anyone interesting in helping out here, this'll need to:
10+
#
11+
# (a) Add build.zig and build.zig.zon to CODECRAFTERS_DEPENDENCY_FILE_PATHS so that the Docker container
12+
# is re-built when these files change.
13+
# (b) Add step(s) to copy only these files (and not the entire submission) into the Docker container.
14+
# (c) Add a step to force Zig to download dependencies and cache them (they'll then be used in the precompile script).
15+
#
16+
# ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="build.zig,build.zig.zon"
17+
18+
# Update the package list and install Zig
19+
RUN apk add --no-cache zig@community=0.12.0-r0
20+
21+
WORKDIR /app
22+
23+
# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses
24+
COPY --exclude=.git --exclude=README.md . /app
25+
26+
RUN zig build
27+
28+
RUN printf "set -e \ncd \${CODECRAFTERS_SUBMISSION_DIR} \necho 'Running zig build' \nzig build \necho 'zig build completed.' \n" > /codecrafters-precompile.sh
29+
RUN chmod +x /codecrafters-precompile.sh
30+
31+
# Once the heavy steps are done, we can copy all files back
32+
COPY . /app

solutions/zig/01-dr6/code/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Time to move on to the next stage!
3030

3131
Note: This section is for stages 2 and beyond.
3232

33-
1. Ensure you have `zig (0.11)` installed locally
33+
1. Ensure you have `zig (0.12)` installed locally
3434
1. Run `./your_sqlite3.sh` to run your program, which is implemented in
3535
`app/main.zig`.
3636
1. Commit your changes and run `git push origin master` to submit your solution

solutions/zig/01-dr6/code/app/main.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ pub fn main() !void {
88
const args = try std.process.argsAlloc(allocator);
99
defer std.process.argsFree(allocator, args);
1010

11-
1211
if (args.len < 3) {
1312
try std.io.getStdErr().writer().print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
1413
return;
1514
}
1615

17-
var database_file_path: []const u8 = args[1];
18-
var command: []const u8 = args[2];
16+
const database_file_path: []const u8 = args[1];
17+
const command: []const u8 = args[2];
18+
1919

2020
if (std.mem.eql(u8, command, ".dbinfo")) {
2121
var file = try std.fs.cwd().openFile(database_file_path, .{});
@@ -24,7 +24,7 @@ pub fn main() !void {
2424
var buf: [2]u8 = undefined;
2525
_ = try file.seekTo(16);
2626
_ = try file.read(&buf);
27-
const page_size = std.mem.readInt(u16, &buf, .Big);
27+
const page_size = std.mem.readInt(u16, &buf, .big);
2828
try std.io.getStdOut().writer().print("database page size: {}\n", .{page_size});
2929
}
3030
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const std = @import("std");
2+
3+
// Learn more about this file here: https://ziglang.org/learn/build-system
4+
pub fn build(b: *std.Build) void {
5+
const exe = b.addExecutable(.{
6+
.name = "zig",
7+
.root_source_file = b.path("src/main.zig"),
8+
.target = b.standardTargetOptions(.{}),
9+
.optimize = b.standardOptimizeOption(.{}),
10+
});
11+
12+
// This declares intent for the executable to be installed into the
13+
// standard location when the user invokes the "install" step (the default
14+
// step when running `zig build`).
15+
b.installArtifact(exe);
16+
17+
// This *creates* a Run step in the build graph, to be executed when another
18+
// step is evaluated that depends on it. The next line below will establish
19+
// such a dependency.
20+
const run_cmd = b.addRunArtifact(exe);
21+
22+
// This creates a build step. It will be visible in the `zig build --help` menu,
23+
// and can be selected like this: `zig build run`
24+
// This will evaluate the `run` step rather than the default, which is "install".
25+
const run_step = b.step("run", "Run the app");
26+
run_step.dependOn(&run_cmd.step);
27+
}

0 commit comments

Comments
 (0)