Skip to content

Commit

Permalink
Updating to latest zig (#36)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Arwalk authored Feb 3, 2024
1 parent 844a2dc commit 46c4e1f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 8 additions & 2 deletions .devcontainer/install-zig.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


set -e
set -x

ZIG_VERSION=$1
echo "v" $ZIG_VERSION
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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",
Expand Down
32 changes: 17 additions & 15 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, .{
Expand All @@ -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" },
Expand Down Expand Up @@ -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`
Expand All @@ -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" }),
Expand All @@ -140,16 +140,16 @@ 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;

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{
Expand All @@ -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");
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand Down

0 comments on commit 46c4e1f

Please sign in to comment.