From f6e015125f602be30c5c81911ea5e2c5966c708e Mon Sep 17 00:00:00 2001 From: Timo Dempwolf Date: Sat, 5 Oct 2024 16:51:19 +0200 Subject: [PATCH 1/2] Add zig support --- .../zig/.codecrafters/compile.sh | 11 +++ compiled_starters/zig/.codecrafters/run.sh | 11 +++ compiled_starters/zig/.gitattributes | 1 + compiled_starters/zig/.gitignore | 18 +++++ compiled_starters/zig/README.md | 34 ++++++++++ compiled_starters/zig/build.zig | 33 +++++++++ compiled_starters/zig/build.zig.zon | 68 +++++++++++++++++++ compiled_starters/zig/codecrafters.yml | 11 +++ compiled_starters/zig/src/main.zig | 21 ++++++ compiled_starters/zig/your_program.sh | 24 +++++++ dockerfiles/zig-0.13.Dockerfile | 27 ++++++++ .../zig/01-vi6/code/.codecrafters/compile.sh | 11 +++ .../zig/01-vi6/code/.codecrafters/run.sh | 11 +++ solutions/zig/01-vi6/code/.gitattributes | 1 + solutions/zig/01-vi6/code/.gitignore | 18 +++++ solutions/zig/01-vi6/code/README.md | 34 ++++++++++ solutions/zig/01-vi6/code/build.zig | 33 +++++++++ solutions/zig/01-vi6/code/build.zig.zon | 68 +++++++++++++++++++ solutions/zig/01-vi6/code/codecrafters.yml | 11 +++ solutions/zig/01-vi6/code/src/main.zig | 17 +++++ solutions/zig/01-vi6/code/your_program.sh | 24 +++++++ solutions/zig/01-vi6/diff/src/main.zig.diff | 33 +++++++++ solutions/zig/01-vi6/explanation.md | 27 ++++++++ .../zig/code/.codecrafters/compile.sh | 11 +++ .../zig/code/.codecrafters/run.sh | 11 +++ starter_templates/zig/code/.gitignore | 18 +++++ starter_templates/zig/code/build.zig | 33 +++++++++ starter_templates/zig/code/build.zig.zon | 68 +++++++++++++++++++ starter_templates/zig/code/src/main.zig | 21 ++++++ starter_templates/zig/config.yml | 3 + 30 files changed, 712 insertions(+) create mode 100755 compiled_starters/zig/.codecrafters/compile.sh create mode 100755 compiled_starters/zig/.codecrafters/run.sh create mode 100644 compiled_starters/zig/.gitattributes create mode 100644 compiled_starters/zig/.gitignore create mode 100644 compiled_starters/zig/README.md create mode 100644 compiled_starters/zig/build.zig create mode 100644 compiled_starters/zig/build.zig.zon create mode 100644 compiled_starters/zig/codecrafters.yml create mode 100644 compiled_starters/zig/src/main.zig create mode 100755 compiled_starters/zig/your_program.sh create mode 100644 dockerfiles/zig-0.13.Dockerfile create mode 100755 solutions/zig/01-vi6/code/.codecrafters/compile.sh create mode 100755 solutions/zig/01-vi6/code/.codecrafters/run.sh create mode 100644 solutions/zig/01-vi6/code/.gitattributes create mode 100644 solutions/zig/01-vi6/code/.gitignore create mode 100644 solutions/zig/01-vi6/code/README.md create mode 100644 solutions/zig/01-vi6/code/build.zig create mode 100644 solutions/zig/01-vi6/code/build.zig.zon create mode 100644 solutions/zig/01-vi6/code/codecrafters.yml create mode 100644 solutions/zig/01-vi6/code/src/main.zig create mode 100755 solutions/zig/01-vi6/code/your_program.sh create mode 100644 solutions/zig/01-vi6/diff/src/main.zig.diff create mode 100644 solutions/zig/01-vi6/explanation.md create mode 100755 starter_templates/zig/code/.codecrafters/compile.sh create mode 100755 starter_templates/zig/code/.codecrafters/run.sh create mode 100644 starter_templates/zig/code/.gitignore create mode 100644 starter_templates/zig/code/build.zig create mode 100644 starter_templates/zig/code/build.zig.zon create mode 100644 starter_templates/zig/code/src/main.zig create mode 100644 starter_templates/zig/config.yml diff --git a/compiled_starters/zig/.codecrafters/compile.sh b/compiled_starters/zig/.codecrafters/compile.sh new file mode 100755 index 0000000..5782342 --- /dev/null +++ b/compiled_starters/zig/.codecrafters/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to compile your program on CodeCrafters +# +# This runs before .codecrafters/run.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +zig build diff --git a/compiled_starters/zig/.codecrafters/run.sh b/compiled_starters/zig/.codecrafters/run.sh new file mode 100755 index 0000000..7c25eb9 --- /dev/null +++ b/compiled_starters/zig/.codecrafters/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to run your program on CodeCrafters +# +# This runs after .codecrafters/compile.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +exec zig-out/bin/main "$@" diff --git a/compiled_starters/zig/.gitattributes b/compiled_starters/zig/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/compiled_starters/zig/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/compiled_starters/zig/.gitignore b/compiled_starters/zig/.gitignore new file mode 100644 index 0000000..b7bb740 --- /dev/null +++ b/compiled_starters/zig/.gitignore @@ -0,0 +1,18 @@ +# Zig build artifacts +main +zig-cache/ +.zig-cache/ +zig-out/ + +# Compiled binary output +bin/ +!bin/.gitkeep + +# Ignore any .o or .h files generated during build +*.o +*.h + +# Ignore OS and editor specific files +**/.DS_Store +*.swp +*~ diff --git a/compiled_starters/zig/README.md b/compiled_starters/zig/README.md new file mode 100644 index 0000000..de60b67 --- /dev/null +++ b/compiled_starters/zig/README.md @@ -0,0 +1,34 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/kafka.png) + +This is a starting point for Zig solutions to the +["Build Your Own Kafka" Challenge](https://codecrafters.io/challenges/kafka). + +In this challenge, you'll build a toy Kafka clone that's capable of accepting +and responding to APIVersions & Fetch API requests. You'll also learn about +encoding and decoding messages using the Kafka wire protocol. You'll also learn +about handling the network protocol, event loops, TCP sockets and more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your Kafka implementation is in `src/main.zig`. Study and +uncomment the relevant code, and push your changes to pass the first stage: + +```sh +git commit -am "pass 1st stage" # any msg +git push origin master +``` + +That's all! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `zig (0.13+)` installed locally +1. Run `./your_program.sh` to run your Kafka broker, which is implemented in + `src/main.zig`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. diff --git a/compiled_starters/zig/build.zig b/compiled_starters/zig/build.zig new file mode 100644 index 0000000..d211b57 --- /dev/null +++ b/compiled_starters/zig/build.zig @@ -0,0 +1,33 @@ +const std = @import("std"); + +// Learn more about this file here: https://ziglang.org/learn/build-system +pub fn build(b: *std.Build) void { + const exe = b.addExecutable(.{ + .name = "main", + .root_source_file = b.path("src/main.zig"), + .target = b.standardTargetOptions(.{}), + .optimize = b.standardOptimizeOption(.{}), + }); + + // 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(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); + + // 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); + + // 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); + } +} diff --git a/compiled_starters/zig/build.zig.zon b/compiled_starters/zig/build.zig.zon new file mode 100644 index 0000000..f6a6e95 --- /dev/null +++ b/compiled_starters/zig/build.zig.zon @@ -0,0 +1,68 @@ +.{ + .name = "codecrafters-kafka", + + // 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 = .{ + // See `zig fetch --save ` for a command-line interface for adding dependencies. + //.example = .{ + // // When updating this field to a new URL, be sure to delete the corresponding + // // `hash`, otherwise you are communicating that you expect to find the old hash at + // // the new URL. + // .url = "https://example.com/foo.tar.gz", + // + // // This is computed from the file contents of the directory of files that is + // // obtained after fetching `url` and applying the inclusion rules given by + // // `paths`. + // // + // // This field is the source of truth; packages do not come from a `url`; they + // // come from a `hash`. `url` is just one of many possible mirrors for how to + // // obtain a package matching this `hash`. + // // + // // Uses the [multihash](https://multiformats.io/multihash/) format. + // .hash = "...", + // + // // When this is provided, the package is found in a directory relative to the + // // build root. In this case the package's hash is irrelevant and therefore not + // // computed. This field and `url` are mutually exclusive. + // .path = "foo", + + // // When this is set to `true`, a package is declared to be lazily + // // fetched. This makes the dependency only get fetched if it is + // // actually used. + // .lazy = false, + //}, + }, + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + // This makes *all* files, recursively, included in this package. It is generally + // better to explicitly list the files and directories instead, to insure that + // fetching from tarballs, file system paths, and version control all result + // in the same contents hash. + "", + // For example... + //"build.zig", + //"build.zig.zon", + //"src", + //"LICENSE", + //"README.md", + }, +} diff --git a/compiled_starters/zig/codecrafters.yml b/compiled_starters/zig/codecrafters.yml new file mode 100644 index 0000000..be2c6fc --- /dev/null +++ b/compiled_starters/zig/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the Zig version used to run your code +# on Codecrafters. +# +# Available versions: zig-0.13 +language_pack: zig-0.13 diff --git a/compiled_starters/zig/src/main.zig b/compiled_starters/zig/src/main.zig new file mode 100644 index 0000000..c99a2c9 --- /dev/null +++ b/compiled_starters/zig/src/main.zig @@ -0,0 +1,21 @@ +const std = @import("std"); +const net = std.net; + +pub fn main() !void { + // You can use print statements as follows for debugging, they'll be visible when running tests. + std.debug.print("Logs from your program will appear here!\n", .{}); + + // Uncomment this block to pass the first stage + //const address = try net.Address.resolveIp("127.0.0.1", 9092); + // + //var listener = try address.listen(.{ + // .reuse_address = true, + //}); + //defer listener.deinit(); + // + //while (true) { + // const connection = try listener.accept(); + // std.debug.print("accepted new connection\n", .{}); + // connection.stream.close(); + //} +} diff --git a/compiled_starters/zig/your_program.sh b/compiled_starters/zig/your_program.sh new file mode 100755 index 0000000..5098df7 --- /dev/null +++ b/compiled_starters/zig/your_program.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# +# Use this script to run your program LOCALLY. +# +# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit early if any commands fail + +# Copied from .codecrafters/compile.sh +# +# - Edit this to change how your program compiles locally +# - Edit .codecrafters/compile.sh to change how your program compiles remotely +( + cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory + zig build +) + +# Copied from .codecrafters/run.sh +# +# - Edit this to change how your program runs locally +# - Edit .codecrafters/run.sh to change how your program runs remotely +exec zig-out/bin/main "$@" diff --git a/dockerfiles/zig-0.13.Dockerfile b/dockerfiles/zig-0.13.Dockerfile new file mode 100644 index 0000000..0fa2dd9 --- /dev/null +++ b/dockerfiles/zig-0.13.Dockerfile @@ -0,0 +1,27 @@ +# syntax=docker/dockerfile:1.7-labs +FROM alpine:3.20 + +RUN apk add --no-cache 'xz>=5.6' 'curl>=8.9' + +# Download and install Zig +RUN curl -O https://ziglang.org/download/0.13.0/zig-linux-x86_64-0.13.0.tar.xz \ + && tar -xf zig-linux-x86_64-0.13.0.tar.xz \ + && mv zig-linux-x86_64-0.13.0 /usr/local/zig \ + && rm zig-linux-x86_64-0.13.0.tar.xz + +# Add Zig to PATH +ENV PATH="/usr/local/zig:${PATH}" + +ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="build.zig,build.zig.zon" + +WORKDIR /app + +# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses +COPY --exclude=.git --exclude=README.md . /app + +# This runs zig build +RUN .codecrafters/compile.sh + +# Cache build directory +RUN mkdir -p /app-cached +RUN mv /app/.zig-cache /app-cached/.zig-cache || true diff --git a/solutions/zig/01-vi6/code/.codecrafters/compile.sh b/solutions/zig/01-vi6/code/.codecrafters/compile.sh new file mode 100755 index 0000000..5782342 --- /dev/null +++ b/solutions/zig/01-vi6/code/.codecrafters/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to compile your program on CodeCrafters +# +# This runs before .codecrafters/run.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +zig build diff --git a/solutions/zig/01-vi6/code/.codecrafters/run.sh b/solutions/zig/01-vi6/code/.codecrafters/run.sh new file mode 100755 index 0000000..7c25eb9 --- /dev/null +++ b/solutions/zig/01-vi6/code/.codecrafters/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to run your program on CodeCrafters +# +# This runs after .codecrafters/compile.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +exec zig-out/bin/main "$@" diff --git a/solutions/zig/01-vi6/code/.gitattributes b/solutions/zig/01-vi6/code/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/solutions/zig/01-vi6/code/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/solutions/zig/01-vi6/code/.gitignore b/solutions/zig/01-vi6/code/.gitignore new file mode 100644 index 0000000..b7bb740 --- /dev/null +++ b/solutions/zig/01-vi6/code/.gitignore @@ -0,0 +1,18 @@ +# Zig build artifacts +main +zig-cache/ +.zig-cache/ +zig-out/ + +# Compiled binary output +bin/ +!bin/.gitkeep + +# Ignore any .o or .h files generated during build +*.o +*.h + +# Ignore OS and editor specific files +**/.DS_Store +*.swp +*~ diff --git a/solutions/zig/01-vi6/code/README.md b/solutions/zig/01-vi6/code/README.md new file mode 100644 index 0000000..de60b67 --- /dev/null +++ b/solutions/zig/01-vi6/code/README.md @@ -0,0 +1,34 @@ +![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/kafka.png) + +This is a starting point for Zig solutions to the +["Build Your Own Kafka" Challenge](https://codecrafters.io/challenges/kafka). + +In this challenge, you'll build a toy Kafka clone that's capable of accepting +and responding to APIVersions & Fetch API requests. You'll also learn about +encoding and decoding messages using the Kafka wire protocol. You'll also learn +about handling the network protocol, event loops, TCP sockets and more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your Kafka implementation is in `src/main.zig`. Study and +uncomment the relevant code, and push your changes to pass the first stage: + +```sh +git commit -am "pass 1st stage" # any msg +git push origin master +``` + +That's all! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `zig (0.13+)` installed locally +1. Run `./your_program.sh` to run your Kafka broker, which is implemented in + `src/main.zig`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. diff --git a/solutions/zig/01-vi6/code/build.zig b/solutions/zig/01-vi6/code/build.zig new file mode 100644 index 0000000..d211b57 --- /dev/null +++ b/solutions/zig/01-vi6/code/build.zig @@ -0,0 +1,33 @@ +const std = @import("std"); + +// Learn more about this file here: https://ziglang.org/learn/build-system +pub fn build(b: *std.Build) void { + const exe = b.addExecutable(.{ + .name = "main", + .root_source_file = b.path("src/main.zig"), + .target = b.standardTargetOptions(.{}), + .optimize = b.standardOptimizeOption(.{}), + }); + + // 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(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); + + // 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); + + // 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); + } +} diff --git a/solutions/zig/01-vi6/code/build.zig.zon b/solutions/zig/01-vi6/code/build.zig.zon new file mode 100644 index 0000000..f6a6e95 --- /dev/null +++ b/solutions/zig/01-vi6/code/build.zig.zon @@ -0,0 +1,68 @@ +.{ + .name = "codecrafters-kafka", + + // 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 = .{ + // See `zig fetch --save ` for a command-line interface for adding dependencies. + //.example = .{ + // // When updating this field to a new URL, be sure to delete the corresponding + // // `hash`, otherwise you are communicating that you expect to find the old hash at + // // the new URL. + // .url = "https://example.com/foo.tar.gz", + // + // // This is computed from the file contents of the directory of files that is + // // obtained after fetching `url` and applying the inclusion rules given by + // // `paths`. + // // + // // This field is the source of truth; packages do not come from a `url`; they + // // come from a `hash`. `url` is just one of many possible mirrors for how to + // // obtain a package matching this `hash`. + // // + // // Uses the [multihash](https://multiformats.io/multihash/) format. + // .hash = "...", + // + // // When this is provided, the package is found in a directory relative to the + // // build root. In this case the package's hash is irrelevant and therefore not + // // computed. This field and `url` are mutually exclusive. + // .path = "foo", + + // // When this is set to `true`, a package is declared to be lazily + // // fetched. This makes the dependency only get fetched if it is + // // actually used. + // .lazy = false, + //}, + }, + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + // This makes *all* files, recursively, included in this package. It is generally + // better to explicitly list the files and directories instead, to insure that + // fetching from tarballs, file system paths, and version control all result + // in the same contents hash. + "", + // For example... + //"build.zig", + //"build.zig.zon", + //"src", + //"LICENSE", + //"README.md", + }, +} diff --git a/solutions/zig/01-vi6/code/codecrafters.yml b/solutions/zig/01-vi6/code/codecrafters.yml new file mode 100644 index 0000000..be2c6fc --- /dev/null +++ b/solutions/zig/01-vi6/code/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the Zig version used to run your code +# on Codecrafters. +# +# Available versions: zig-0.13 +language_pack: zig-0.13 diff --git a/solutions/zig/01-vi6/code/src/main.zig b/solutions/zig/01-vi6/code/src/main.zig new file mode 100644 index 0000000..2c050a8 --- /dev/null +++ b/solutions/zig/01-vi6/code/src/main.zig @@ -0,0 +1,17 @@ +const std = @import("std"); +const net = std.net; + +pub fn main() !void { + const address = try net.Address.resolveIp("127.0.0.1", 9092); + + var listener = try address.listen(.{ + .reuse_address = true, + }); + defer listener.deinit(); + + while (true) { + const connection = try listener.accept(); + std.debug.print("accepted new connection\n", .{}); + connection.stream.close(); + } +} diff --git a/solutions/zig/01-vi6/code/your_program.sh b/solutions/zig/01-vi6/code/your_program.sh new file mode 100755 index 0000000..5098df7 --- /dev/null +++ b/solutions/zig/01-vi6/code/your_program.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# +# Use this script to run your program LOCALLY. +# +# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit early if any commands fail + +# Copied from .codecrafters/compile.sh +# +# - Edit this to change how your program compiles locally +# - Edit .codecrafters/compile.sh to change how your program compiles remotely +( + cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory + zig build +) + +# Copied from .codecrafters/run.sh +# +# - Edit this to change how your program runs locally +# - Edit .codecrafters/run.sh to change how your program runs remotely +exec zig-out/bin/main "$@" diff --git a/solutions/zig/01-vi6/diff/src/main.zig.diff b/solutions/zig/01-vi6/diff/src/main.zig.diff new file mode 100644 index 0000000..4e331b9 --- /dev/null +++ b/solutions/zig/01-vi6/diff/src/main.zig.diff @@ -0,0 +1,33 @@ +@@ -1,21 +1,17 @@ + const std = @import("std"); + const net = std.net; + + pub fn main() !void { +- // You can use print statements as follows for debugging, they'll be visible when running tests. +- std.debug.print("Logs from your program will appear here!\n", .{}); ++ const address = try net.Address.resolveIp("127.0.0.1", 9092); + +- // Uncomment this block to pass the first stage +- //const address = try net.Address.resolveIp("127.0.0.1", 9092); +- // +- //var listener = try address.listen(.{ +- // .reuse_address = true, +- //}); +- //defer listener.deinit(); +- // +- //while (true) { +- // const connection = try listener.accept(); +- // std.debug.print("accepted new connection\n", .{}); +- // connection.stream.close(); +- //} ++ var listener = try address.listen(.{ ++ .reuse_address = true, ++ }); ++ defer listener.deinit(); ++ ++ while (true) { ++ const connection = try listener.accept(); ++ std.debug.print("accepted new connection\n", .{}); ++ connection.stream.close(); ++ } + } diff --git a/solutions/zig/01-vi6/explanation.md b/solutions/zig/01-vi6/explanation.md new file mode 100644 index 0000000..935115a --- /dev/null +++ b/solutions/zig/01-vi6/explanation.md @@ -0,0 +1,27 @@ +The entry point for your Kafka implementation is in `src/main.zig`. + +Study and uncomment the relevant code: + +```zig +// Uncomment this block to pass the first stage +const address = try net.Address.resolveIp("127.0.0.1", 9092); + +var listener = try address.listen(.{ + .reuse_address = true, +}); +defer listener.deinit(); + +while (true) { + const connection = try listener.accept(); + std.debug.print("accepted new connection\n", .{}); + connection.stream.close(); +} +``` + +Push your changes to pass the first stage: + +``` +git add . +git commit -m "pass 1st stage" # any msg +git push origin master +``` diff --git a/starter_templates/zig/code/.codecrafters/compile.sh b/starter_templates/zig/code/.codecrafters/compile.sh new file mode 100755 index 0000000..5782342 --- /dev/null +++ b/starter_templates/zig/code/.codecrafters/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to compile your program on CodeCrafters +# +# This runs before .codecrafters/run.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +zig build diff --git a/starter_templates/zig/code/.codecrafters/run.sh b/starter_templates/zig/code/.codecrafters/run.sh new file mode 100755 index 0000000..7c25eb9 --- /dev/null +++ b/starter_templates/zig/code/.codecrafters/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to run your program on CodeCrafters +# +# This runs after .codecrafters/compile.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +exec zig-out/bin/main "$@" diff --git a/starter_templates/zig/code/.gitignore b/starter_templates/zig/code/.gitignore new file mode 100644 index 0000000..b7bb740 --- /dev/null +++ b/starter_templates/zig/code/.gitignore @@ -0,0 +1,18 @@ +# Zig build artifacts +main +zig-cache/ +.zig-cache/ +zig-out/ + +# Compiled binary output +bin/ +!bin/.gitkeep + +# Ignore any .o or .h files generated during build +*.o +*.h + +# Ignore OS and editor specific files +**/.DS_Store +*.swp +*~ diff --git a/starter_templates/zig/code/build.zig b/starter_templates/zig/code/build.zig new file mode 100644 index 0000000..d211b57 --- /dev/null +++ b/starter_templates/zig/code/build.zig @@ -0,0 +1,33 @@ +const std = @import("std"); + +// Learn more about this file here: https://ziglang.org/learn/build-system +pub fn build(b: *std.Build) void { + const exe = b.addExecutable(.{ + .name = "main", + .root_source_file = b.path("src/main.zig"), + .target = b.standardTargetOptions(.{}), + .optimize = b.standardOptimizeOption(.{}), + }); + + // 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(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); + + // 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); + + // 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); + } +} diff --git a/starter_templates/zig/code/build.zig.zon b/starter_templates/zig/code/build.zig.zon new file mode 100644 index 0000000..f6a6e95 --- /dev/null +++ b/starter_templates/zig/code/build.zig.zon @@ -0,0 +1,68 @@ +.{ + .name = "codecrafters-kafka", + + // 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 = .{ + // See `zig fetch --save ` for a command-line interface for adding dependencies. + //.example = .{ + // // When updating this field to a new URL, be sure to delete the corresponding + // // `hash`, otherwise you are communicating that you expect to find the old hash at + // // the new URL. + // .url = "https://example.com/foo.tar.gz", + // + // // This is computed from the file contents of the directory of files that is + // // obtained after fetching `url` and applying the inclusion rules given by + // // `paths`. + // // + // // This field is the source of truth; packages do not come from a `url`; they + // // come from a `hash`. `url` is just one of many possible mirrors for how to + // // obtain a package matching this `hash`. + // // + // // Uses the [multihash](https://multiformats.io/multihash/) format. + // .hash = "...", + // + // // When this is provided, the package is found in a directory relative to the + // // build root. In this case the package's hash is irrelevant and therefore not + // // computed. This field and `url` are mutually exclusive. + // .path = "foo", + + // // When this is set to `true`, a package is declared to be lazily + // // fetched. This makes the dependency only get fetched if it is + // // actually used. + // .lazy = false, + //}, + }, + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + // This makes *all* files, recursively, included in this package. It is generally + // better to explicitly list the files and directories instead, to insure that + // fetching from tarballs, file system paths, and version control all result + // in the same contents hash. + "", + // For example... + //"build.zig", + //"build.zig.zon", + //"src", + //"LICENSE", + //"README.md", + }, +} diff --git a/starter_templates/zig/code/src/main.zig b/starter_templates/zig/code/src/main.zig new file mode 100644 index 0000000..c99a2c9 --- /dev/null +++ b/starter_templates/zig/code/src/main.zig @@ -0,0 +1,21 @@ +const std = @import("std"); +const net = std.net; + +pub fn main() !void { + // You can use print statements as follows for debugging, they'll be visible when running tests. + std.debug.print("Logs from your program will appear here!\n", .{}); + + // Uncomment this block to pass the first stage + //const address = try net.Address.resolveIp("127.0.0.1", 9092); + // + //var listener = try address.listen(.{ + // .reuse_address = true, + //}); + //defer listener.deinit(); + // + //while (true) { + // const connection = try listener.accept(); + // std.debug.print("accepted new connection\n", .{}); + // connection.stream.close(); + //} +} diff --git a/starter_templates/zig/config.yml b/starter_templates/zig/config.yml new file mode 100644 index 0000000..36066df --- /dev/null +++ b/starter_templates/zig/config.yml @@ -0,0 +1,3 @@ +attributes: + required_executable: zig (0.13+) + user_editable_file: src/main.zig From ac2fcbd8c668a80d960d26f2928af6a6d143748f Mon Sep 17 00:00:00 2001 From: Timo Dempwolf Date: Sat, 5 Oct 2024 16:55:05 +0200 Subject: [PATCH 2/2] Add zig to the course definition --- course-definition.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/course-definition.yml b/course-definition.yml index 02597c6..f045c6f 100644 --- a/course-definition.yml +++ b/course-definition.yml @@ -21,6 +21,7 @@ languages: - slug: "javascript" - slug: "python" - slug: "rust" + - slug: "zig" marketing: difficulty: medium