Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve build.zig + use v0.14.0 idioms #21

Merged
merged 24 commits into from
Mar 22, 2025
Merged

Conversation

Torbatti
Copy link
Contributor

@Torbatti Torbatti commented Mar 13, 2025

  • added build steps and build options
    this will make finding what steps are used and what options are available easier

  • you only need zig build and zig build run
    specifiy examples to build/run by using -Dexample=[nameOfExample] option

  • added static step to build tardy as a standalone lib zig build static

  • e2e test been ported zig build test_e2e -Dasync=auto

  • unit tests been ported zig build test_unit

  • added formatting checker test zig build test_fmt

  • zig build test will now run unit tests and fmting tests

  • decoupled build example module and build example exe functions

  • replaced root_module with the new createMod and addImport introduced in v0.14.0

  • note: github actions wont compile any examples for os piplines / can be added via run step zig build -Dexample=all

@mookums mookums added the build Related to the Build System label Mar 14, 2025
build.zig Outdated
inline for (std.meta.fields(Example)) |f| {

// skip .none and .all for building step
if (f.value == 1 or f.value == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably use the enum name here instead of the values.

stat,
stream,

fn toString(ex: Example) []const u8 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this, why not just use @tagName on the enum? You would get the same strings and avoid a bespoke conversion function.

Copy link
Contributor Author

@Torbatti Torbatti Mar 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here are some advantages of using toString helper function instead of @tagName

  1. use nested paths and get a simpler enum field name
  2. put similar examples inside a single directory
using toString:
examples/echo/v1/ -> .echo_v1
examples/echo/v2/ -> .echo_v2

using @tagName
examples/echo_v1/ -> .echo_v1
examples/echo_v2/ -> .echo_v2

disadvantages :
may forget to add example to toString helper function


// create a private example module
const example_mod = b.createModule(.{
.root_source_file = b.path(b.fmt("examples/{s}/main.zig", .{options.example.toString()})),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tagName(options.example)

build.zig Outdated
steps.install.dependOn(&install_artifact.step);

// Should not run all examples at the same time
if (options.allExamples) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should log that you can't run all examples at the same time.

});

const example_exe = b.addExecutable(.{
.name = options.example.toString(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tagName(options.example)

const AsyncKind = @import("src/aio/lib.zig").AsyncKind;
fn build_static_lib(
b: *std.Build,
steps: struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason to have this be a struct.

// build/run a specific example
fn build_example_exe(
b: *std.Build,
steps: struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I might prefer you just pass the types in like run_step or install_step instead of having inline structs

Copy link
Contributor Author

@Torbatti Torbatti Mar 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having steps struct in every main build function makes it easier to read available steps and can infer to them using steps.[name] , even if there is only one step

can also be replaced by step_run and step_install for code reviewing sake like you mentioned

build.zig Outdated
options: struct {
tardy_mod: *std.Build.Module,
example: Example,
allExamples: bool,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allExamples -> all_examples

README.md Outdated
```

## Example
## Building and running example folder
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Building and Running Examples

@mookums
Copy link
Collaborator

mookums commented Mar 15, 2025

Most of it is pretty good, left a few comments of things that might be worth taking a look at.

@Torbatti
Copy link
Contributor Author

Torbatti commented Mar 16, 2025

fixes part 1:

  • convert captured field value to field enum for ease of read
  • changed campleCase allExamples to snake_case all_examples
  • add info log: let user know zig build run -Dexample=all will only build examples and will not runt them
  • added usage comments inside main build function
  • fixed readme

@Torbatti Torbatti requested a review from mookums March 18, 2025 16:10
@mookums mookums merged commit c07afd0 into tardy-org:main Mar 22, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Related to the Build System
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants