From 46c4e1fd6d0f7e3fc11e673849d6b972161eee5b Mon Sep 17 00:00:00 2001
From: Arwalk <siragentprovocateurarwalk@gmail.com>
Date: Sat, 3 Feb 2024 19:22:57 +0100
Subject: [PATCH] Updating to latest zig (#36)

* updated toolchain in devcontainer

* updating build.zig to accomodate to changes

* better way to get the latest zls

This doesn't involve compiling and is much faster.

* update doc to accomodate the changes + better argument order
---
 .devcontainer/Dockerfile     |  2 +-
 .devcontainer/install-zig.sh | 10 ++++++++--
 README.md                    |  4 ++--
 build.zig                    | 32 +++++++++++++++++---------------
 4 files changed, 28 insertions(+), 20 deletions(-)
 mode change 100644 => 100755 .devcontainer/install-zig.sh

diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile
index de6c2eb..9c38fac 100644
--- a/.devcontainer/Dockerfile
+++ b/.devcontainer/Dockerfile
@@ -3,6 +3,6 @@ RUN git config --global core.autocrlf input
 
 ADD . .
 
-RUN bash ./install-zig.sh 0.12.0-dev.1856+94c63f31f
+RUN bash ./install-zig.sh 0.12.0-dev.2341+92211135f
 
 ENV AGREE=true
diff --git a/.devcontainer/install-zig.sh b/.devcontainer/install-zig.sh
old mode 100644
new mode 100755
index 27c614b..bfb031f
--- a/.devcontainer/install-zig.sh
+++ b/.devcontainer/install-zig.sh
@@ -2,6 +2,7 @@
 
 
 set -e
+set -x
 
 ZIG_VERSION=$1
 echo "v" $ZIG_VERSION
@@ -14,7 +15,7 @@ fi
 # Clean up
 rm -rf /var/lib/apt/lists/*
 
-ARCH="$(uname -m)"
+ARCH="$(uname -m)"	
 
 # Checks if packages are installed and installs them if not
 check_packages() {
@@ -42,7 +43,12 @@ rm /usr/local/bin/zig || true
 ln -s /usr/local/lib/zig/zig /usr/local/bin/zig
 
 # install language server
-wget -c https://zig.pm/zls/downloads/$(arch)-linux/bin/zls -O /usr/local/bin/zls
+
+LATEST_ZLS=$(curl https://zigtools-releases.nyc3.digitaloceanspaces.com/zls/index.json | /usr/bin/jq -r '.latest')
+
+wget https://zigtools-releases.nyc3.digitaloceanspaces.com/zls/$LATEST_ZLS/$ARCH-linux/zls
+
+mv zls /usr/local/bin/zls
 
 # make binary executable
 chmod +x /usr/local/bin/zls
diff --git a/README.md b/README.md
index cd76042..7b0e1bb 100644
--- a/README.md
+++ b/README.md
@@ -18,7 +18,7 @@ Zig's compile-time evaluation becomes extremely strong and useful in this contex
 
 This repository, so far, only aims at implementing [protocol buffers version 3](https://developers.google.com/protocol-buffers/docs/proto3#simple).
 
-The latest version of the zig compiler used for this project is 0.12.0-dev.293+f33bb0228.
+The latest version of the zig compiler used for this project is 0.12.0-dev.2341+92211135f.
 
 This project is currently able to handle all scalar types for encoding, decoding, and generation through the plugin.
 
@@ -73,7 +73,7 @@ pub fn build(b: *std.build.Builder) !void {
 
     const gen_proto = b.step("gen-proto", "generates zig files from protocol buffer definitions");
 
-    const protoc_step = protobuf.RunProtocStep.create(b, protobuf_dep.builder, .{
+    const protoc_step = protobuf.RunProtocStep.create(b, protobuf_dep.builder, target, .{
         .destination_directory = .{
             // out directory for the generated zig files
             .path = "src/proto",
diff --git a/build.zig b/build.zig
index bec16bd..498ef5e 100644
--- a/build.zig
+++ b/build.zig
@@ -7,7 +7,7 @@ const mem = std.mem;
 
 const PROTOC_VERSION = "23.4";
 
-pub fn build(b: *std.build.Builder) !void {
+pub fn build(b: *std.Build) !void {
     // 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
@@ -34,7 +34,7 @@ pub fn build(b: *std.build.Builder) !void {
     b.installArtifact(lib);
 
     const module = b.addModule("protobuf", .{
-        .source_file = .{ .path = "src/protobuf.zig" },
+        .root_source_file = .{ .path = "src/protobuf.zig" },
     });
 
     const exe = buildGenerator(b, .{
@@ -49,7 +49,7 @@ pub fn build(b: *std.build.Builder) !void {
 
     const test_step = b.step("test", "Run library tests");
 
-    const tests = [_]*std.build.LibExeObjStep{
+    const tests = [_]*std.Build.Step.Compile{
         b.addTest(.{
             .name = "protobuf",
             .root_source_file = .{ .path = "src/protobuf.zig" },
@@ -94,20 +94,20 @@ pub fn build(b: *std.build.Builder) !void {
         }),
     };
 
-    const convertStep = RunProtocStep.create(b, b, .{
+    const convertStep = RunProtocStep.create(b, b, target,  .{
         .destination_directory = .{ .path = "tests/.generated" },
         .source_files = &.{"tests/protos_for_test/generated_in_ci.proto"},
         .include_directories = &.{"tests/protos_for_test"},
     });
 
-    const convertStep2 = RunProtocStep.create(b, b, .{
+    const convertStep2 = RunProtocStep.create(b, b, target,  .{
         .destination_directory = .{ .path = "tests/generated" },
         .source_files = &.{ "tests/protos_for_test/all.proto", "tests/protos_for_test/whitespace-in-name.proto" },
         .include_directories = &.{"tests/protos_for_test"},
     });
 
     for (tests) |test_item| {
-        test_item.addModule("protobuf", module);
+        test_item.root_module.addImport("protobuf", module);
 
         // This creates a build step. It will be visible in the `zig build --help` menu,
         // and can be selected like this: `zig build test`
@@ -124,7 +124,7 @@ pub fn build(b: *std.build.Builder) !void {
 
     const bootstrap = b.step("bootstrap", "run the generator over its own sources");
 
-    const bootstrapConversion = RunProtocStep.create(b, b, .{
+    const bootstrapConversion = RunProtocStep.create(b, b, target, .{
         .destination_directory = .{ .path = "bootstrapped-generator" },
         .source_files = &.{
             b.pathJoin(&.{ wd, "include/google/protobuf/compiler/plugin.proto" }),
@@ -140,8 +140,8 @@ pub const RunProtocStep = struct {
     step: Step,
     source_files: []const []const u8,
     include_directories: []const []const u8,
-    destination_directory: std.build.FileSource,
-    generator: *std.build.Step.Compile,
+    destination_directory: std.Build.LazyPath,
+    generator: *std.Build.Step.Compile,
     verbose: bool = false, // useful for debugging if you need to know what protoc command is sent
 
     pub const base_id = .protoc;
@@ -149,7 +149,7 @@ pub const RunProtocStep = struct {
     pub const Options = struct {
         source_files: []const []const u8,
         include_directories: []const []const u8 = &.{},
-        destination_directory: std.build.FileSource,
+        destination_directory: std.Build.LazyPath,
     };
 
     pub const StepErr = error{
@@ -159,6 +159,7 @@ pub const RunProtocStep = struct {
     pub fn create(
         owner: *std.Build,
         dependency_builder: *std.Build,
+        target: std.Build.ResolvedTarget,
         options: Options,
     ) *RunProtocStep {
         var self: *RunProtocStep = owner.allocator.create(RunProtocStep) catch @panic("OOM");
@@ -172,7 +173,7 @@ pub const RunProtocStep = struct {
             .source_files = owner.dupeStrings(options.source_files),
             .include_directories = owner.dupeStrings(options.include_directories),
             .destination_directory = options.destination_directory.dupe(owner),
-            .generator = buildGenerator(dependency_builder, .{}),
+            .generator = buildGenerator(dependency_builder, .{ .target = target }),
         };
 
         self.step.dependOn(&self.generator.step);
@@ -239,11 +240,11 @@ pub const RunProtocStep = struct {
 };
 
 pub const GenOptions = struct {
-    target: std.zig.CrossTarget = .{},
+    target: std.Build.ResolvedTarget,
     optimize: std.builtin.OptimizeMode = .Debug,
 };
 
-pub fn buildGenerator(b: *std.build.Builder, opt: GenOptions) *std.build.Step.Compile {
+pub fn buildGenerator(b: *std.Build, opt: GenOptions) *std.Build.Step.Compile {
     const exe = b.addExecutable(.{
         .name = "protoc-gen-zig",
         // In this case the main source file is merely a path, however, in more
@@ -254,10 +255,11 @@ pub fn buildGenerator(b: *std.build.Builder, opt: GenOptions) *std.build.Step.Co
     });
 
     const module = b.addModule("protobuf", .{
-        .source_file = .{ .path = "src/protobuf.zig" },
+        .root_source_file = .{ .path = "src/protobuf.zig" },
     });
 
-    exe.addModule("protobuf", module);
+
+    exe.root_module.addImport("protobuf", module);
 
     b.installArtifact(exe);