1
1
const std = @import ("std" );
2
2
const builtin = @import ("builtin" );
3
3
4
+ const build_zig_zon = @embedFile ("build.zig.zon" );
5
+
4
6
pub fn build (b : * std.Build ) void {
5
7
// const git_describe = std.mem.trimRight(u8, b.run(&.{ "git", "describe", "--tags" }), '\n');
6
8
@@ -49,7 +51,7 @@ pub fn build(b: *std.Build) void {
49
51
}
50
52
51
53
// zig build
52
- _ = buildCli (b , step_cli , target , optimize , module , .default );
54
+ _ = buildCli (b , step_cli , target , optimize , module , .default , "gatorcat" );
53
55
54
56
// zig build release
55
57
const installs = buildRelease (b , step_release ) catch @panic ("oom" );
@@ -185,6 +187,7 @@ pub fn buildCli(
185
187
optimize : std.builtin.OptimizeMode ,
186
188
gatorcat_module : * std.Build.Module ,
187
189
dest_dir : std.Build.Step.InstallArtifact.Options.Dir ,
190
+ exe_name : []const u8 ,
188
191
) * std.Build.Step.InstallArtifact {
189
192
const flags_module = b .dependency ("flags" , .{
190
193
.target = target ,
@@ -199,7 +202,7 @@ pub fn buildCli(
199
202
.optimize = optimize ,
200
203
}).module ("zenoh" );
201
204
const cli = b .addExecutable (.{
202
- .name = "gatorcat" ,
205
+ .name = exe_name ,
203
206
.root_source_file = b .path ("src/cli/main.zig" ),
204
207
.target = target ,
205
208
.optimize = optimize ,
@@ -253,14 +256,38 @@ pub fn buildRelease(
253
256
},
254
257
else = > {},
255
258
}
256
- try installs .append (buildCli (
257
- b ,
258
- step ,
259
- options .target ,
260
- options .optimize ,
261
- gatorcat_module ,
262
- .{ .override = .{ .custom = target .zigTriple (b .allocator ) catch @panic ("oom" ) } },
263
- ));
259
+ const triple = target .zigTriple (b .allocator ) catch @panic ("oom" );
260
+ try installs .append (
261
+ buildCli (
262
+ b ,
263
+ step ,
264
+ options .target ,
265
+ options .optimize ,
266
+ gatorcat_module ,
267
+ .{ .override = .{ .custom = "release" } },
268
+ try std .fmt .allocPrint (b .allocator , "gatorcat-{}-{s}" , .{ getVersionFromZon (), triple }),
269
+ ),
270
+ );
264
271
}
265
272
return installs ;
266
273
}
274
+
275
+ fn getVersionFromZon () std.SemanticVersion {
276
+ var buffer : [10 * build_zig_zon .len ]u8 = undefined ;
277
+ var fba = std .heap .FixedBufferAllocator .init (& buffer );
278
+ const version = std .zon .parse .fromSlice (
279
+ struct { version : []const u8 },
280
+ fba .allocator (),
281
+ build_zig_zon ,
282
+ null ,
283
+ .{ .ignore_unknown_fields = true },
284
+ ) catch @panic ("Invalid build.zig.zon!" );
285
+ const semantic_version = std .SemanticVersion .parse (version .version ) catch @panic ("Invalid version!" );
286
+ return std.SemanticVersion {
287
+ .major = semantic_version .major ,
288
+ .minor = semantic_version .minor ,
289
+ .patch = semantic_version .patch ,
290
+ .build = null , // dont return pointers to stack memory
291
+ .pre = null , // dont return pointers to stack memory
292
+ };
293
+ }
0 commit comments