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

build: add -Dversion-string config option #2183

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,13 @@ pub fn build(b: *Build) !void {
const use_llvm = b.option(bool, "use-llvm", "Use Zig's llvm code backend");

const resolved_zls_version = getVersion(b);
const resolved_zls_version_string = b.fmt("{}", .{resolved_zls_version});

const build_options = blk: {
const build_options = b.addOptions();
build_options.step.name = "ZLS build options";

build_options.addOption(std.SemanticVersion, "version", resolved_zls_version);
build_options.addOption([]const u8, "version_string", resolved_zls_version_string);
build_options.addOption([]const u8, "version_string", b.fmt("{}", .{resolved_zls_version}));
build_options.addOption([]const u8, "minimum_runtime_zig_version_string", minimum_runtime_zig_version);

break :blk build_options.createModule();
Expand Down Expand Up @@ -284,6 +283,13 @@ pub fn build(b: *Build) !void {

/// Returns `MAJOR.MINOR.PATCH-dev` when `git describe` failed.
fn getVersion(b: *Build) std.SemanticVersion {
const version_string = b.option([]const u8, "version-string", "Override the version of this build. Must be a semantic version.");
if (version_string) |semver_string| {
return std.SemanticVersion.parse(semver_string) catch |err| {
std.debug.panic("Expected -Dversion-string={s} to be a semantic version: {}", .{ semver_string, err });
};
}

if (zls_version.pre == null and zls_version.build == null) return zls_version;

const argv: []const []const u8 = &.{
Expand All @@ -292,9 +298,12 @@ fn getVersion(b: *Build) std.SemanticVersion {
var code: u8 = undefined;
const git_describe_untrimmed = b.runAllowFail(argv, &code, .Ignore) catch |err| {
const argv_joined = std.mem.join(b.allocator, " ", argv) catch @panic("OOM");
std.log.warn("Failed to run git describe to resolve ZLS version: {}\ncommand: {s}", .{
err, argv_joined,
});
std.log.warn(
\\Failed to run git describe to resolve ZLS version: {}
\\command: {s}
\\
\\Consider passing the -Dversion-string flag to specify the ZLS version.
, .{ err, argv_joined });
return zls_version;
};

Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const Env = struct {
///
/// The semantic version can have one of the following formats:
/// - `MAJOR.MINOR.PATCH` is a tagged release of ZLS
/// - `MAJOR.MINOR.PATCH-dev.COMMIT_HEIGHT-SHORT_COMMIT_HASH` is a development build of ZLS
/// - `MAJOR.MINOR.PATCH-dev.COMMIT_HEIGHT+SHORT_COMMIT_HASH` is a development build of ZLS
/// - `MAJOR.MINOR.PATCH-dev` is a development build of ZLS where the exact version could not be resolved.
///
version: []const u8,
Expand Down