From 993f4b5a06b5984d3d81e5910403cf15293f25b1 Mon Sep 17 00:00:00 2001 From: Jay Petacat Date: Fri, 1 Jan 2021 18:40:46 -0500 Subject: [PATCH 1/2] ci: fix `git describe` git describe is used for version string creation, but it had to be reverted in commit 69da6ba because it was broken in CI builds. Azure Pipelines and Drone perform shallow clones by default. This change reconfigures them to fetch history and tags. It adds tens of seconds, which is negligible compared to overall build and test time. Related: #6466, #6509, #7601 --- ci/azure/linux_script | 2 ++ ci/azure/macos_script | 2 ++ ci/azure/windows_msvc_script.bat | 2 ++ ci/drone/linux_script | 2 ++ ci/srht/freebsd_script | 2 ++ 5 files changed, 10 insertions(+) diff --git a/ci/azure/linux_script b/ci/azure/linux_script index bf338ce22f7129f17c7df45c52194fff617e8c70..251c3bfaf1542c7744d6b4aabeb4e34317989973 100755 --- a/ci/azure/linux_script +++ b/ci/azure/linux_script @@ -49,6 +49,8 @@ PATH=$PWD/$WASMTIME:$PATH # Make the `zig version` number consistent. # This will affect the cmake command below. git config core.abbrev 9 +git fetch --unshallow || true +git fetch --tags export CC=gcc-7 export CXX=g++-7 diff --git a/ci/azure/macos_script b/ci/azure/macos_script index cb14b7f1196fb96c511f9a407fe8ff3604d77a63..b83f9c940b71689a42318cb8bcb4883aa2d81689 100755 --- a/ci/azure/macos_script +++ b/ci/azure/macos_script @@ -28,6 +28,8 @@ cd $ZIGDIR # Make the `zig version` number consistent. # This will affect the cmake command below. git config core.abbrev 9 +git fetch --unshallow || true +git fetch --tags mkdir build cd build diff --git a/ci/azure/windows_msvc_script.bat b/ci/azure/windows_msvc_script.bat index efc68e5d2d8210bc8a4793ba6098701b4057d1b9..cd5e3d5bca39150da6724c1be8850628e723f2eb 100644 --- a/ci/azure/windows_msvc_script.bat +++ b/ci/azure/windows_msvc_script.bat @@ -18,6 +18,8 @@ call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliar REM Make the `zig version` number consistent. REM This will affect the cmake command below. git.exe config core.abbrev 9 +git.exe fetch --unshallow +git.exe fetch --tags mkdir %ZIGBUILDDIR% cd %ZIGBUILDDIR% diff --git a/ci/drone/linux_script b/ci/drone/linux_script index fdc1704fb7e38a328796499603f3bb248d1f3738..422949e607528ebccb29e7b69be9449b1d82a9cb 100755 --- a/ci/drone/linux_script +++ b/ci/drone/linux_script @@ -14,6 +14,8 @@ pip3 install s3cmd # Make the `zig version` number consistent. # This will affect the cmake command below. git config core.abbrev 9 +git fetch --unshallow || true +git fetch --tags mkdir build cd build diff --git a/ci/srht/freebsd_script b/ci/srht/freebsd_script index 02849da8de63fc4ab678f2e2ed6da7ac73b54d7b..337d715b7c3828b2f90ba196dbb4ce21a9fa0d0e 100755 --- a/ci/srht/freebsd_script +++ b/ci/srht/freebsd_script @@ -20,6 +20,8 @@ cd $ZIGDIR # Make the `zig version` number consistent. # This will affect the cmake command below. git config core.abbrev 9 +git fetch --unshallow || true +git fetch --tags # SourceHut reports that it is a terminal that supports escape codes, but it # is a filthy liar. Here we tell Zig to not try to send any terminal escape -- 2.54.0 From ec8c25fd8f1de07bff7850a5b8ce1bafd1add223 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 1 Jan 2021 21:01:51 -0700 Subject: [PATCH 2/2] Restore the reverted semantic versioning commits restore "Comply with semantic versioning pre-release format" restore "stage2: SemVer compliance for development builds" restore "Remove 'g' prefix from commit hash in Zig semver" This reverts commit d96d8639e592b4bb4b2e330c4bd7412256b297d2. This reverts commit e8810f579406673edad0c9780c9d990f3034717a. This reverts commit 9afe5859a3e428b259a306c36a860e4d82dbb4bb. --- CMakeLists.txt | 29 +++++++++++++++++------- build.zig | 56 +++++++++++++++++++++++++++++++++-------------- src/config.zig.in | 7 +----- 3 files changed, 62 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a9dc38b40b56f3a60f0479d26b4dd850d7dadde2..291a2f7839b8632abbc5f30989bcb353bd9d937f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,8 +25,8 @@ project(zig C CXX) set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) set(ZIG_VERSION_MAJOR 0) -set(ZIG_VERSION_MINOR 7) -set(ZIG_VERSION_PATCH 1) +set(ZIG_VERSION_MINOR 8) +set(ZIG_VERSION_PATCH 0) set(ZIG_VERSION "" CACHE STRING "Override Zig version string. Default is to find out with git.") if("${ZIG_VERSION}" STREQUAL "") @@ -34,18 +34,31 @@ if("${ZIG_VERSION}" STREQUAL "") find_program(GIT_EXE NAMES git) if(GIT_EXE) execute_process( - COMMAND ${GIT_EXE} -C ${CMAKE_SOURCE_DIR} name-rev HEAD --tags --name-only --no-undefined --always + COMMAND ${GIT_EXE} -C ${CMAKE_SOURCE_DIR} describe --match *.*.* --tags RESULT_VARIABLE EXIT_STATUS - OUTPUT_VARIABLE ZIG_GIT_REV + OUTPUT_VARIABLE GIT_DESCRIBE OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) if(EXIT_STATUS EQUAL "0") - if(ZIG_GIT_REV MATCHES "\\^0$") - if(NOT("${ZIG_GIT_REV}" STREQUAL "${ZIG_VERSION}^0")) - message("WARNING: Tag does not match configured Zig version") + if(GIT_DESCRIBE MATCHES "^v?([0-9]+\\.[0-9]+\\.[0-9]+)$") + # Tagged release version. + set(GIT_TAG ${CMAKE_MATCH_1}) + if(NOT GIT_TAG VERSION_EQUAL ZIG_VERSION) + message(SEND_ERROR "Zig version (${ZIG_VERSION}) does not match Git tag (${GIT_TAG}).") endif() + elseif(GIT_DESCRIBE MATCHES "^v?([0-9]+\\.[0-9]+\\.[0-9]+)-([0-9]+)-g(.+)$") + # Untagged pre-release. The Zig version is updated to include the number of commits + # since the last tagged version and the commit hash. The version is formatted in + # accordance with the https://semver.org specification. + set(GIT_TAG ${CMAKE_MATCH_1}) + set(GIT_COMMITS_AFTER_TAG ${CMAKE_MATCH_2}) + set(GIT_COMMIT ${CMAKE_MATCH_3}) + if(NOT ZIG_VERSION VERSION_GREATER GIT_TAG) + message(SEND_ERROR "Zig version (${ZIG_VERSION}) must be greater than tagged ancestor (${GIT_TAG}).") + endif() + set(ZIG_VERSION "${ZIG_VERSION}-dev.${GIT_COMMITS_AFTER_TAG}+${GIT_COMMIT}") else() - set(ZIG_VERSION "${ZIG_VERSION}+${ZIG_GIT_REV}") + message(WARNING "Failed to parse version from output of `git describe`.") endif() endif() endif() diff --git a/build.zig b/build.zig index bef3b3b58faafcb413b6450f83168a5ca14c0944..fe588ea2cb35aa820879aa1ad895c2cd73d08c43 100644 --- a/build.zig +++ b/build.zig @@ -11,7 +11,7 @@ const fs = std.fs; const InstallDirectoryOptions = std.build.InstallDirectoryOptions; const assert = std.debug.assert; -const zig_version = std.builtin.Version{ .major = 0, .minor = 7, .patch = 1 }; +const zig_version = std.builtin.Version{ .major = 0, .minor = 8, .patch = 0 }; pub fn build(b: *Builder) !void { b.setPreferredReleaseMode(.ReleaseFast); @@ -227,24 +227,48 @@ pub fn build(b: *Builder) !void { const version_string = b.fmt("{}.{}.{}", .{ zig_version.major, zig_version.minor, zig_version.patch }); var code: u8 = undefined; - const git_sha_untrimmed = b.execAllowFail(&[_][]const u8{ - "git", "-C", b.build_root, "name-rev", "HEAD", - "--tags", "--name-only", "--no-undefined", "--always", + const git_describe_untrimmed = b.execAllowFail(&[_][]const u8{ + "git", "-C", b.build_root, "describe", "--match", "*.*.*", "--tags", }, &code, .Ignore) catch { break :v version_string; }; - const git_sha_trimmed = mem.trim(u8, git_sha_untrimmed, " \n\r"); - - // This will look like e.g. "0.7.0^0" for a tag commit. - if (mem.endsWith(u8, git_sha_trimmed, "^0")) { - const git_ver_string = git_sha_trimmed[0 .. git_sha_trimmed.len - 2]; - if (!mem.eql(u8, git_ver_string, version_string)) { - std.debug.print("Expected git tag '{}', found '{}'\n", .{ version_string, git_ver_string }); - std.process.exit(1); - } - break :v b.fmt("{}", .{version_string}); - } else { - break :v b.fmt("{}+{}", .{ version_string, git_sha_trimmed }); + const git_describe = mem.trim(u8, git_describe_untrimmed, " \n\r"); + + switch (mem.count(u8, git_describe, "-")) { + 0 => { + // Tagged release version (e.g. 0.7.0). + if (!mem.eql(u8, git_describe, version_string)) { + std.debug.print("Zig version '{}' does not match Git tag '{}'\n", .{ version_string, git_describe }); + std.process.exit(1); + } + break :v version_string; + }, + 2 => { + // Untagged development build (e.g. 0.7.0-684-gbbe2cca1a). + var it = mem.split(git_describe, "-"); + const tagged_ancestor = it.next() orelse unreachable; + const commit_height = it.next() orelse unreachable; + const commit_id = it.next() orelse unreachable; + + const ancestor_ver = try std.builtin.Version.parse(tagged_ancestor); + if (zig_version.order(ancestor_ver) != .gt) { + std.debug.print("Zig version '{}' must be greater than tagged ancestor '{}'\n", .{ zig_version, ancestor_ver }); + std.process.exit(1); + } + + // Check that the commit hash is prefixed with a 'g' (a Git convention). + if (commit_id.len < 1 or commit_id[0] != 'g') { + std.debug.print("Unexpected `git describe` output: {}\n", .{git_describe}); + break :v version_string; + } + + // The version is reformatted in accordance with the https://semver.org specification. + break :v b.fmt("{}-dev.{}+{}", .{ version_string, commit_height, commit_id[1..] }); + }, + else => { + std.debug.print("Unexpected `git describe` output: {}\n", .{git_describe}); + break :v version_string; + }, } }; exe.addBuildOption([:0]const u8, "version", try b.allocator.dupeZ(u8, version)); diff --git a/src/config.zig.in b/src/config.zig.in index 871c6e9abf3bf35a856580af9787d05b848397a4..9d16cf382441e4de89ef7b615e05d8e5ebbab4c3 100644 --- a/src/config.zig.in +++ b/src/config.zig.in @@ -1,11 +1,6 @@ pub const have_llvm = true; pub const version: [:0]const u8 = "@ZIG_VERSION@"; -pub const semver: @import("std").SemanticVersion = .{ - .major = @ZIG_VERSION_MAJOR@, - .minor = @ZIG_VERSION_MINOR@, - .patch = @ZIG_VERSION_PATCH@, - .build = "@ZIG_GIT_REV@", -}; +pub const semver = try @import("std").SemanticVersion.parse(version); pub const log_scopes: []const []const u8 = &[_][]const u8{}; pub const zir_dumps: []const []const u8 = &[_][]const u8{}; pub const enable_tracy = false; -- 2.54.0