From 57d2129baeabbf799abbe58a0c90d035b3134d52 Mon Sep 17 00:00:00 2001 From: chloe caruso Date: Tue, 23 Jul 2024 04:56:31 -0700 Subject: [PATCH] add automatic version detection from changelog --- CHANGELOG.md | 2 ++ build.zig | 22 ++++++++++++++++++---- src/core/js/Link.zig | 14 +++++++++----- src/core/mod.zig | 3 +++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1385b355b836d05ea471ce9a62cda2797578a651..d839acacc037994a584756874aaeb0b6a4883fcd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## 0.1.5 + ## 0.1.4 - feat: implement basic struct types (#42) diff --git a/build.zig b/build.zig index 517d5d22517572c1750f5a7c4568e2f869ef3e86..1d89a00731183659d272831ae7928c5871cf622a 100644 --- a/build.zig +++ b/build.zig @@ -3,7 +3,7 @@ const Build = std.Build; const Target = std.Target; const OsVersion = Target.Query.OsVersion; -pub fn build(b: *std.Build) void { +pub fn build(b: *std.Build) !void { b.reference_trace = 20; const native_target = b.resolveTargetQuery(.{}); @@ -40,8 +40,20 @@ pub fn build(b: *std.Build) void { "Experiment with the Zig self-hosted backends", ) orelse false; - const native_debug = buildWithTarget(b, native_target, .Debug, .none, no_llvm); - const main = buildWithTarget(b, target, optimize, tracing, no_llvm); + const version = version: { + const text = try b.build_root.handle.readFileAlloc( + b.graph.arena, + "CHANGELOG.md", + std.math.maxInt(usize), + ); + const first_header = std.mem.indexOf(u8, text, "## ") orelse unreachable; + var tokenizer = std.mem.tokenizeAny(u8, text[first_header + 2 ..], " \t\n"); + const version_text = tokenizer.next() orelse unreachable; + break :version std.SemanticVersion.parse(version_text) catch unreachable; + }; + + const native_debug = buildWithTarget(b, native_target, .Debug, .none, version, no_llvm); + const main = buildWithTarget(b, target, optimize, tracing, version, no_llvm); b.installArtifact(main.compiler); @@ -133,7 +145,7 @@ pub fn build(b: *std.Build) void { }, ) catch @panic("OOM"); - const artifacts = buildWithTarget(b, cross_target, .ReleaseSafe, .none, no_llvm); + const artifacts = buildWithTarget(b, cross_target, .ReleaseSafe, .none, version, no_llvm); inline for (.{ .{ artifacts.compiler, "compiler" }, @@ -173,6 +185,7 @@ fn buildWithTarget( target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, tracing: TracingMode, + version: std.SemanticVersion, no_llvm: bool, ) Artifacts { const dep_opts = .{ .target = target, .optimize = optimize }; @@ -185,6 +198,7 @@ fn buildWithTarget( // Options const opts = b.addOptions(); + opts.addOption(std.SemanticVersion, "version", version); opts.addOption([]const u8, "tracing", @tagName(tracing)); const opts_mod = opts.createModule(); diff --git a/src/core/js/Link.zig b/src/core/js/Link.zig index 05a565491a6436eeab9e4c67060e66f8a0e2d81a..be43e842e1deb0c2d507dfc42662b09c6b9130c6 100644 --- a/src/core/js/Link.zig +++ b/src/core/js/Link.zig @@ -70,7 +70,10 @@ pub fn linkJsCode(link: *Link, entry_decl: Decl.Index, writer: Writer) !void { try link.decls.putNoClobber(link.comp.gpa, value, @enumFromInt(gop.index)); } - try writer.print("// Language Compiler v0.1.4. Alpha quality software.\n", .{}); + try writer.writeAll(std.fmt.comptimePrint( + "// Language Compiler v{}. Alpha quality software.\n", + .{core.version}, + )); if (root_function.nodes.len > 1) { // 2. Print every function decl @@ -843,14 +846,15 @@ const Wyhash = std.hash.Wyhash; const log = std.log.scoped(.js_link); const Writer = std.io.AnyWriter; -const Comp = @import("../Comp.zig"); +const core = @import("../mod.zig"); +const Comp = core.Comp; const Decl = Comp.Decl; -const Js = @import("Js.zig"); -const Tree = @import("Tree.zig"); +const Js = core.Js; +const Tree = Js.Tree; const Node = Tree.Node; -const InternPool = @import("../InternPool.zig"); +const InternPool = core.InternPool; const String = InternPool.String; const tracer = @import("tracer"); diff --git a/src/core/mod.zig b/src/core/mod.zig index 5dc23f406d46cd95b8b865846bd53e1bec40c756..cc3a1e17802775e1f7583fd91c2e5237571d1db1 100644 --- a/src/core/mod.zig +++ b/src/core/mod.zig @@ -48,6 +48,9 @@ pub const unicode_props = @import("unicode_props.zig"); // The rest of this file is utility functions, which are not specific to any of // the above namespaces. Remember Zig discourages namespaces like `utils` +/// Language SDK version according to the latest entry in `CHANGELOG.md` +pub const version = @import("build_options").version; + /// This is an alternative to `std.debug.assert`, where you want to print some /// debug info instead of "reached unreachable code". Usage is as follows: /// -- 2.54.0