|
| 1 | +# Zig 0.16 Migration Guide |
| 2 | + |
| 3 | +This document describes the changes required to migrate zigxll from Zig 0.14/0.15 to Zig 0.16. |
| 4 | + |
| 5 | +## build.zig Changes |
| 6 | + |
| 7 | +### Compile Step Methods Moved to root_module |
| 8 | + |
| 9 | +Methods that were on `*std.Build.Step.Compile` have moved to `compile.root_module`: |
| 10 | + |
| 11 | +```zig |
| 12 | +// Old (0.14/0.15) |
| 13 | +xll.addIncludePath(b.path("excel/include")); |
| 14 | +xll.addLibraryPath(b.path("excel/lib")); |
| 15 | +xll.addCSourceFiles(.{ .root = b.path("src"), .files = &.{"file.c"} }); |
| 16 | +xll.linkLibC(); |
| 17 | +xll.linkSystemLibrary("user32"); |
| 18 | +
|
| 19 | +// New (0.16) |
| 20 | +xll.root_module.addIncludePath(b.path("excel/include")); |
| 21 | +xll.root_module.addLibraryPath(b.path("excel/lib")); |
| 22 | +xll.root_module.addCSourceFiles(.{ .root = b.path("src"), .files = &.{"file.c"} }); |
| 23 | +xll.root_module.link_libc = true; |
| 24 | +xll.root_module.linkSystemLibrary("user32", .{}); |
| 25 | +``` |
| 26 | + |
| 27 | +### Environment Variables |
| 28 | + |
| 29 | +```zig |
| 30 | +// Old |
| 31 | +const home = std.process.getEnvVarOwned(b.allocator, "HOME") catch return; |
| 32 | +
|
| 33 | +// New |
| 34 | +const home = b.graph.environ_map.get("HOME") orelse return; |
| 35 | +``` |
| 36 | + |
| 37 | +### Filesystem Access |
| 38 | + |
| 39 | +```zig |
| 40 | +// Old |
| 41 | +var dir = std.fs.openDirAbsolute(path, .{}) catch return; |
| 42 | +dir.close(); |
| 43 | +
|
| 44 | +// New |
| 45 | +std.Io.Dir.accessAbsolute(b.graph.io, path, .{}) catch return; |
| 46 | +``` |
| 47 | + |
| 48 | +### Path Joining |
| 49 | + |
| 50 | +```zig |
| 51 | +// Old |
| 52 | +const path = std.fs.path.join(allocator, &.{ home, ".xwin" }) catch return; |
| 53 | +
|
| 54 | +// New |
| 55 | +const path = std.Io.Dir.path.join(allocator, &.{ home, ".xwin" }) catch return; |
| 56 | +``` |
| 57 | + |
| 58 | +### File Reading |
| 59 | + |
| 60 | +```zig |
| 61 | +// Old |
| 62 | +const bytes = dir.handle.readFileAlloc(allocator, sub_path, max_size) catch ...; |
| 63 | +
|
| 64 | +// New (requires io parameter, sub_path before allocator, Io.Limit for max_size) |
| 65 | +const bytes = dir.handle.readFileAlloc(b.graph.io, sub_path, allocator, std.Io.Limit.limited(max_size)) catch ...; |
| 66 | +``` |
| 67 | + |
| 68 | +### Directory Operations |
| 69 | + |
| 70 | +```zig |
| 71 | +// Old |
| 72 | +var dir = std.fs.cwd().openDir(path, .{ .iterate = true }) catch ...; |
| 73 | +defer dir.close(); |
| 74 | +var it = dir.iterate(); |
| 75 | +while (it.next() catch ...) |entry| { ... } |
| 76 | +
|
| 77 | +// New (cwd() takes no args, openDir/close/next require io parameter) |
| 78 | +var dir = std.Io.Dir.cwd().openDir(b.graph.io, path, .{ .iterate = true }) catch ...; |
| 79 | +defer dir.close(b.graph.io); |
| 80 | +var it = dir.iterate(); |
| 81 | +while (it.next(b.graph.io) catch ...) |entry| { ... } |
| 82 | +``` |
| 83 | + |
| 84 | +### captureStdOut |
| 85 | + |
| 86 | +```zig |
| 87 | +// Old |
| 88 | +const output = run.captureStdOut(); |
| 89 | +
|
| 90 | +// New (requires options struct) |
| 91 | +const output = run.captureStdOut(.{}); |
| 92 | +``` |
| 93 | + |
| 94 | +## Source File Changes |
| 95 | + |
| 96 | +### Mutex API |
| 97 | + |
| 98 | +`std.Thread.Mutex` has been replaced with `std.Io.Mutex`, which requires an `Io` instance for lock/unlock operations: |
| 99 | + |
| 100 | +```zig |
| 101 | +// Old |
| 102 | +var mutex: std.Thread.Mutex = .{}; |
| 103 | +mutex.lock(); |
| 104 | +defer mutex.unlock(); |
| 105 | +
|
| 106 | +// New |
| 107 | +var mutex: std.Io.Mutex = std.Io.Mutex.init; |
| 108 | +const io = std.Options.debug_io; |
| 109 | +mutex.lock(io) catch {}; |
| 110 | +defer mutex.unlock(io); |
| 111 | +``` |
| 112 | + |
| 113 | +### ArrayListUnmanaged Initialization |
| 114 | + |
| 115 | +```zig |
| 116 | +// Old |
| 117 | +var list = std.ArrayListUnmanaged(u8){}; |
| 118 | +
|
| 119 | +// New |
| 120 | +var list: std.ArrayListUnmanaged(u8) = .empty; |
| 121 | +``` |
| 122 | + |
| 123 | +### ArrayList API Changes |
| 124 | + |
| 125 | +The `writer()` method has been removed from ArrayList. Use ArrayList methods directly: |
| 126 | + |
| 127 | +```zig |
| 128 | +// Old |
| 129 | +var buf = std.ArrayListUnmanaged(u8){}; |
| 130 | +errdefer buf.deinit(allocator); |
| 131 | +const writer = buf.writer(allocator); |
| 132 | +try writer.writeAll("hello"); |
| 133 | +try writer.writeByte('|'); |
| 134 | +try writer.print("{d}", .{42}); |
| 135 | +return buf.toOwnedSlice(allocator); |
| 136 | +
|
| 137 | +// New (use ArrayList methods directly, allocator passed to each method) |
| 138 | +var buf: std.ArrayList(u8) = .empty; |
| 139 | +errdefer buf.deinit(allocator); |
| 140 | +try buf.appendSlice(allocator, "hello"); |
| 141 | +try buf.append(allocator, '|'); |
| 142 | +try buf.print(allocator, "{d}", .{42}); |
| 143 | +return buf.toOwnedSlice(allocator); |
| 144 | +``` |
| 145 | + |
| 146 | +Note: `std.ArrayList` in 0.16 is now the unmanaged version (formerly `ArrayListUnmanaged`). |
| 147 | +The managed version is `std.array_list.Managed` but is deprecated. |
| 148 | + |
| 149 | +### Thread.sleep Removed |
| 150 | + |
| 151 | +`std.Thread.sleep` has been removed. For Windows targets, use the Win32 API directly |
| 152 | +(this is what `std.Thread.sleep` called internally anyway): |
| 153 | + |
| 154 | +```zig |
| 155 | +// Old |
| 156 | +std.Thread.sleep(2 * std.time.ns_per_s); |
| 157 | +
|
| 158 | +// New (Windows) - use kernel32.Sleep (milliseconds) |
| 159 | +const kernel32 = struct { |
| 160 | + extern "kernel32" fn Sleep(dwMilliseconds: u32) callconv(.winapi) void; |
| 161 | +}; |
| 162 | +kernel32.Sleep(2000); // 2 seconds |
| 163 | +
|
| 164 | +// Or use the zigxll helper: |
| 165 | +xll.async_infra.sleepMs(2000); |
| 166 | +``` |
| 167 | + |
| 168 | +### Windows Types Removed |
| 169 | + |
| 170 | +`std.os.windows.HRESULT` has been removed. Define locally: |
| 171 | + |
| 172 | +```zig |
| 173 | +// Old |
| 174 | +pub const HRESULT = std.os.windows.HRESULT; |
| 175 | +
|
| 176 | +// New |
| 177 | +pub const HRESULT = i32; |
| 178 | +``` |
| 179 | + |
| 180 | +## Cross-Compilation (xwin) Workaround |
| 181 | + |
| 182 | +Zig 0.16 may request `MSVCRTD.lib` (debug CRT) even in release builds when cross-compiling to Windows. The xwin SDK only includes release libraries. |
| 183 | + |
| 184 | +**Workaround:** Create a symlink from the release library: |
| 185 | + |
| 186 | +```bash |
| 187 | +ln -sf ~/.xwin/crt/lib/x86_64/msvcrt.lib ~/.xwin/crt/lib/x86_64/MSVCRTD.lib |
| 188 | +``` |
| 189 | + |
| 190 | +## Reference |
| 191 | + |
| 192 | +- [Zig 0.16.0 Release Notes](https://ziglang.org/download/0.16.0/release-notes.html) |
0 commit comments