An SMTP client and server library for Zig implementing RFC 5321.
0

Configure Feed

Select the types of activity you want to include in your feed.

zig-smtp / build.zig
12 kB 242 lines
1// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us> 2// SPDX-License-Identifier: MIT 3 4const std = @import("std"); 5 6// Although this function looks imperative, it does not perform the build 7// directly and instead it mutates the build graph (`b`) that will be then 8// executed by an external runner. The functions in `std.Build` implement a DSL 9// for defining build steps and express dependencies between them, allowing the 10// build runner to parallelize the build automatically (and the cache system to 11// know when a step doesn't need to be re-run). 12pub fn build(b: *std.Build) void { 13 // Standard target options allow the person running `zig build` to choose 14 // what target to build for. Here we do not override the defaults, which 15 // means any target is allowed, and the default is native. Other options 16 // for restricting supported target set are available. 17 const target = b.standardTargetOptions(.{}); 18 // Standard optimization options allow the person running `zig build` to select 19 // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not 20 // set a preferred release mode, allowing the user to decide how to optimize. 21 const optimize = b.standardOptimizeOption(.{}); 22 // It's also possible to define more custom flags to toggle optional features 23 // of this build script using `b.option()`. All defined flags (including 24 // target and optimize options) will be listed when running `zig build --help` 25 // in this directory. 26 27 // This creates a module, which represents a collection of source files alongside 28 // some compilation options, such as optimization mode and linked system libraries. 29 // Zig modules are the preferred way of making Zig code available to consumers. 30 // addModule defines a module that we intend to make available for importing 31 // to our consumers. We must give it a name because a Zig package can expose 32 // multiple modules and consumers will need to be able to specify which 33 // module they want to access. 34 // SASL mechanisms (PLAIN, LOGIN, CRAM-MD5, XOAUTH2, ...) live outside 35 // this library, so that SMTP, POP3 and IMAP clients share one 36 // implementation of each instead of keeping three. 37 const sasl_dep = b.dependency("sasl", .{ 38 .target = target, 39 .optimize = optimize, 40 }); 41 42 // The `Received:` field an RFC 5321 §4.4 server has to stamp: its 43 // grammar, its folding, the escaping of the parts the client chose, and 44 // the ESMTPSA-or-ESMTPS-or-ESMTPA question. Written once, in a library 45 // that is about message syntax, rather than a second time here. 46 const mime_dep = b.dependency("mime", .{ .target = target, .optimize = optimize }); 47 const datetime_dep = b.dependency("datetime", .{ .target = target, .optimize = optimize }); 48 49 const tls_dep = b.dependency("tls", .{ 50 .target = target, 51 .optimize = optimize, 52 }); 53 54 const mod = b.addModule("smtp", .{ 55 // The root source file is the "entry point" of this module. Users of 56 // this module will only be able to access public declarations contained 57 // in this file, which means that if you have declarations that you 58 // intend to expose to consumers that were defined in other files part 59 // of this module, you will have to make sure to re-export them from 60 // the root file. 61 .root_source_file = b.path("src/root.zig"), 62 // Later on we'll use this module as the root module of a test executable 63 // which requires us to specify a target. 64 .target = target, 65 .imports = &.{ 66 .{ .name = "tls", .module = tls_dep.module("tls") }, 67 .{ .name = "sasl", .module = sasl_dep.module("sasl") }, 68 .{ .name = "mime", .module = mime_dep.module("mime") }, 69 .{ .name = "datetime", .module = datetime_dep.module("datetime") }, 70 }, 71 }); 72 73 // Here we define an executable. An executable needs to have a root module 74 // which needs to expose a `main` function. While we could add a main function 75 // to the module defined above, it's sometimes preferable to split business 76 // logic and the CLI into two separate modules. 77 // 78 // If your goal is to create a Zig library for others to use, consider if 79 // it might benefit from also exposing a CLI tool. A parser library for a 80 // data serialization format could also bundle a CLI syntax checker, for example. 81 // 82 // If instead your goal is to create an executable, consider if users might 83 // be interested in also being able to embed the core functionality of your 84 // program in their own executable in order to avoid the overhead involved in 85 // subprocessing your CLI tool. 86 // 87 // If neither case applies to you, feel free to delete the declaration you 88 // don't need and to put everything under a single module. 89 const exe = b.addExecutable(.{ 90 .name = "zig-smtp", 91 .root_module = b.createModule(.{ 92 // b.createModule defines a new module just like b.addModule but, 93 // unlike b.addModule, it does not expose the module to consumers of 94 // this package, which is why in this case we don't have to give it a name. 95 .root_source_file = b.path("src/main.zig"), 96 // Target and optimization levels must be explicitly wired in when 97 // defining an executable or library (in the root module), and you 98 // can also hardcode a specific target for an executable or library 99 // definition if desireable (e.g. firmware for embedded devices). 100 .target = target, 101 .optimize = optimize, 102 // List of modules available for import in source files part of the 103 // root module. 104 .imports = &.{ 105 // Here "smtp" is the name you will use in your source code to 106 // import this module (e.g. `@import("smtp")`). The name is 107 // repeated because you are allowed to rename your imports, which 108 // can be extremely useful in case of collisions (which can happen 109 // importing modules from different packages). 110 .{ .name = "smtp", .module = mod }, 111 }, 112 }), 113 }); 114 115 // This declares intent for the executable to be installed into the 116 // install prefix when running `zig build` (i.e. when executing the default 117 // step). By default the install prefix is `zig-out/` but can be overridden 118 // by passing `--prefix` or `-p`. 119 b.installArtifact(exe); 120 121 // This creates a top level step. Top level steps have a name and can be 122 // invoked by name when running `zig build` (e.g. `zig build run`). 123 // This will evaluate the `run` step rather than the default step. 124 // For a top level step to actually do something, it must depend on other 125 // steps (e.g. a Run step, as we will see in a moment). 126 const run_step = b.step("run", "Run the app"); 127 128 // This creates a RunArtifact step in the build graph. A RunArtifact step 129 // invokes an executable compiled by Zig. Steps will only be executed by the 130 // runner if invoked directly by the user (in the case of top level steps) 131 // or if another step depends on it, so it's up to you to define when and 132 // how this Run step will be executed. In our case we want to run it when 133 // the user runs `zig build run`, so we create a dependency link. 134 const run_cmd = b.addRunArtifact(exe); 135 run_step.dependOn(&run_cmd.step); 136 137 // By making the run step depend on the default step, it will be run from the 138 // installation directory rather than directly from within the cache directory. 139 run_cmd.step.dependOn(b.getInstallStep()); 140 141 // This allows the user to pass arguments to the application in the build 142 // command itself, like this: `zig build run -- arg1 arg2 etc` 143 if (b.args) |args| { 144 run_cmd.addArgs(args); 145 } 146 147 const lib = b.addLibrary(.{ 148 .name = "zig-smtp", 149 .root_module = mod, 150 }); 151 152 const install_docs = b.addInstallDirectory(.{ 153 .source_dir = lib.getEmittedDocs(), 154 .install_dir = .prefix, 155 .install_subdir = "docs", 156 }); 157 158 const docs_step = b.step("docs", "Build the API docs"); 159 docs_step.dependOn(&install_docs.step); 160 161 // The is_email address corpus test embeds its XML test files from the 162 // lazy isemail dependency, fetched only on demand: 163 // zig build test -Disemail-corpus 164 const isemail_corpus = b.option(bool, "isemail-corpus", "Fetch Dominic Sayers' is_email suite and run the address corpus test") orelse false; 165 var isemail_available = false; 166 if (isemail_corpus) { 167 if (b.lazyDependency("isemail", .{})) |isemail_dep| { 168 mod.addAnonymousImport("isemail_tests_xml", .{ 169 .root_source_file = isemail_dep.path("test/tests.xml"), 170 }); 171 mod.addAnonymousImport("isemail_tests_original_xml", .{ 172 .root_source_file = isemail_dep.path("test/tests-original.xml"), 173 }); 174 isemail_available = true; 175 } 176 } 177 const test_options = b.addOptions(); 178 test_options.addOption(bool, "isemail_corpus", isemail_available); 179 mod.addOptions("build_options", test_options); 180 181 // Exim's scriptable SMTP test client, handy for driving the zig-smtp 182 // server through raw protocol dialogues with reply expectations (see 183 // test/protocol-torture.script). Guarded by an option so the lazy exim 184 // dependency is only fetched on demand: 185 // zig build -Dexim-client && ./zig-out/bin/exim-client <host> <port> 186 if (b.option(bool, "exim-client", "Build exim's scriptable SMTP test client (fetches the exim source)") orelse false) { 187 if (b.lazyDependency("exim", .{})) |exim_dep| { 188 const exim_client = b.addExecutable(.{ 189 .name = "exim-client", 190 .root_module = b.createModule(.{ 191 .target = target, 192 .optimize = optimize, 193 .link_libc = true, 194 }), 195 }); 196 exim_client.root_module.addCSourceFile(.{ 197 .file = exim_dep.path("test/src/client.c"), 198 .flags = &.{"-w"}, 199 }); 200 b.installArtifact(exim_client); 201 } 202 } 203 204 // Creates an executable that will run `test` blocks from the provided module. 205 // Here `mod` needs to define a target, which is why earlier we made sure to 206 // set the releative field. 207 const mod_tests = b.addTest(.{ 208 .root_module = mod, 209 }); 210 211 // A run step that will run the test executable. 212 const run_mod_tests = b.addRunArtifact(mod_tests); 213 214 // Creates an executable that will run `test` blocks from the executable's 215 // root module. Note that test executables only test one module at a time, 216 // hence why we have to create two separate ones. 217 const exe_tests = b.addTest(.{ 218 .root_module = exe.root_module, 219 }); 220 221 // A run step that will run the second test executable. 222 const run_exe_tests = b.addRunArtifact(exe_tests); 223 224 // A top level step for running all tests. dependOn can be called multiple 225 // times and since the two run steps do not depend on one another, this will 226 // make the two of them run in parallel. 227 const test_step = b.step("test", "Run tests"); 228 test_step.dependOn(&run_mod_tests.step); 229 test_step.dependOn(&run_exe_tests.step); 230 231 // Just like flags, top level steps are also listed in the `--help` menu. 232 // 233 // The Zig build system is entirely implemented in userland, which means 234 // that it cannot hook into private compiler APIs. All compilation work 235 // orchestrated by the build system will result in other Zig compiler 236 // subcommands being invoked with the right flags defined. You can observe 237 // these invocations when one fails (or you pass a flag to increase 238 // verbosity) to validate assumptions and diagnose problems. 239 // 240 // Lastly, the Zig build system is relatively simple and self-contained, 241 // and reading its source code will allow you to master it. 242}