From 4a7e73f706c0a2cc0d674c590a2b89036ce74660 Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Sat, 18 Jan 2025 13:44:32 -0800 Subject: [PATCH] Read version from MODULE.bazel as fallback. Uses a starlark native.module_version() feature to extract the version from module if git version is not defined or the workspace status could not run otherwise (e.g. when Verible is not a root module). While at it, replace build-version.py with a shell-script that does not require Python, making it easier on the system requirements and also easier to read and maintain. --- .bazelrc | 6 +-- .github/workflows/verible-ci.yml | 20 +++++--- MODULE.bazel | 1 + bazel/build-version.py | 58 ------------------------ bazel/build-version.sh | 38 ++++++++++++++++ shell.nix | 2 +- verible/common/util/BUILD | 3 ++ verible/common/util/init-command-line.cc | 20 +++++--- verible/common/util/module-version.bzl | 6 +++ 9 files changed, 77 insertions(+), 77 deletions(-) delete mode 100755 bazel/build-version.py create mode 100755 bazel/build-version.sh create mode 100644 verible/common/util/module-version.bzl diff --git a/.bazelrc b/.bazelrc index b0e64f9d9..fb78ca5f6 100644 --- a/.bazelrc +++ b/.bazelrc @@ -7,11 +7,7 @@ build --define="absl=1" build --enable_platform_specific_config # Gather build version information -build:linux --workspace_status_command="bazel/build-version.py" -build:freebsd --workspace_status_command="bazel/build-version.py" -build:openbsd --workspace_status_command="bazel/build-version.py" -build:macos --workspace_status_command="bazel/build-version.py" -build:windows --workspace_status_command="python bazel/build-version.py" +build --workspace_status_command="bash bazel/build-version.sh" # Systems with gcc or clang common:linux --cxxopt=-xc++ --host_cxxopt=-xc++ --cxxopt=-std=c++17 --host_cxxopt=-std=c++17 --client_env=BAZEL_CXXOPTS=-std=c++17 diff --git a/.github/workflows/verible-ci.yml b/.github/workflows/verible-ci.yml index 3ef8d4aa7..037851274 100644 --- a/.github/workflows/verible-ci.yml +++ b/.github/workflows/verible-ci.yml @@ -221,6 +221,8 @@ jobs: if: matrix.mode == 'compile' && matrix.arch == 'x86_64' run: | bazel build -c fastbuild :install-binaries + # Litmus test + bazel-bin/verible/verilog/tools/syntax/verible-verilog-syntax --version .github/bin/simple-install.sh $VERIBLE_BINDIR tar cfv verible-bin.tar -C $VERIBLE_BINDIR . @@ -331,8 +333,8 @@ jobs: with: path: | /private/var/tmp/_bazel_runner - key: bazelcache_macos1_${{ steps.cache_timestamp.outputs.time }} - restore-keys: bazelcache_macos1_ + key: bazelcache_macos_${{ steps.cache_timestamp.outputs.time }} + restore-keys: bazelcache_macos_ - name: Tests # MacOS has a broken patch utility: @@ -340,7 +342,10 @@ jobs: run: bazel test -c opt --noshow_progress --test_output=errors --cxxopt=-Wno-range-loop-analysis -- //... -//verible/verilog/tools/lint:lint-tool_test - name: Build - run: bazel build -c opt --noshow_progress --test_output=errors --cxxopt=-Wno-range-loop-analysis :install-binaries + run: | + bazel build -c opt --noshow_progress --test_output=errors --cxxopt=-Wno-range-loop-analysis :install-binaries + # Litmus test + bazel-bin/verible/verilog/tools/syntax/verible-verilog-syntax --version - name: Pack up run: | @@ -385,8 +390,8 @@ jobs: uses: actions/cache@v3 with: path: "c:/users/runneradmin/_bazel_runneradmin" - key: bazelcache_windows3_${{ steps.cache_timestamp.outputs.time }} - restore-keys: bazelcache_windows3_ + key: bazelcache_windows_${{ steps.cache_timestamp.outputs.time }} + restore-keys: bazelcache_windows_ - name: Install dependencies run: | @@ -404,7 +409,10 @@ jobs: run: C:/ProgramData/chocolatey/lib/bazel/bazel.exe test --keep_going --noshow_progress --test_output=errors //... - name: Build Verible Binaries - run: C:/ProgramData/chocolatey/lib/bazel/bazel.exe build --keep_going --noshow_progress -c opt :install-binaries + run: | + C:/ProgramData/chocolatey/lib/bazel/bazel.exe build --keep_going --noshow_progress -c opt :install-binaries + # Litmus test + bazel-bin/verible/verilog/tools/syntax/verible-verilog-syntax --version - name: Prepare release run: | diff --git a/MODULE.bazel b/MODULE.bazel index 87cbfe863..bafa992a0 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,5 +1,6 @@ module( name = "verible", + version = "head", ) bazel_dep(name = "platforms", version = "0.0.10") diff --git a/bazel/build-version.py b/bazel/build-version.py deleted file mode 100755 index a48047002..000000000 --- a/bazel/build-version.py +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env python3 -# Copyright 2020-2021 The Verible Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Invoke bazel with --workspace_status_command=bazel/build-version.py to get this invoked and populate bazel-out/volatile-status.txt -""" - -import os - -from subprocess import Popen, PIPE - - -def run(*cmd): - process = Popen(cmd, stdout=PIPE) - output, _ = process.communicate() - - return output.strip().decode() - - -def main(): - try: - date = run("git", "log", "-n1", "--date=short", "--format=%cd") - except: - date = "" - - try: - version = run("git", "describe") - except: - version = "" - - if not date: - try: - date = os.environ["GIT_DATE"] - except: - date = "" - - if not version: - try: - version = os.environ["GIT_VERSION"] - except: - version = "" - - print("GIT_DATE", '"{}"'.format(date)) - print("GIT_DESCRIBE", '"{}"'.format(version)) - - -if __name__ == "__main__": - main() diff --git a/bazel/build-version.sh b/bazel/build-version.sh new file mode 100755 index 000000000..ebb35f4a5 --- /dev/null +++ b/bazel/build-version.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +# Copyright 2020-2025 The Verible Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Invoke bazel with --workspace_status_command=bazel/build-version.py to get +# this invoked and populate bazel-out/volatile-status.txt + +# Get commit timestamp from git if available, otherwise attempt to get +# from COMMIT_TIMESTAMP or GIT_DATE environment variable. +OUTPUT_COMMIT_TIMESTAMP="$(git log -n1 --format=%cd --date=unix 2>/dev/null)" +if [ -z "${OUTPUT_COMMIT_TIMESTAMP}" ]; then + OUTPUT_COMMIT_TIMESTAMP="${COMMIT_TIMESTAMP}" # from environment +fi + +if [ ! -z "${OUTPUT_COMMIT_TIMESTAMP}" ]; then + echo "COMMIT_TIMESTAMP ${OUTPUT_COMMIT_TIMESTAMP}" +elif [ ! -z "${GIT_DATE}" ]; then # legacy environment variable only fallback + echo "GIT_DATE \"${GIT_DATE}\"" +fi + +OUTPUT_GIT_DESCRIBE="$(git describe 2>/dev/null)" +if [ -z "${OUTPUT_GIT_DESCRIBE}" ]; then + OUTPUT_GIT_DESCRIBE="${GIT_VERSION}" +fi +if [ ! -z "${OUTPUT_GIT_DESCRIBE}" ]; then + echo "GIT_DESCRIBE \"${OUTPUT_GIT_DESCRIBE}\"" +fi diff --git a/shell.nix b/shell.nix index abbbf51aa..32620cffb 100644 --- a/shell.nix +++ b/shell.nix @@ -18,9 +18,9 @@ verible_used_stdenv.mkDerivation { # For scripts used inside bzl rules and tests gnused - python3 # To run error-log-analyzer + python3 python3Packages.mdutils ripgrep diff --git a/verible/common/util/BUILD b/verible/common/util/BUILD index 945fea3bf..d02d94b44 100644 --- a/verible/common/util/BUILD +++ b/verible/common/util/BUILD @@ -1,6 +1,8 @@ # 'util' contains generic containers, adapters, algorithms # Any verible package may directly depend on this. +load(":module-version.bzl", "get_version_define_from_module") + package( default_applicable_licenses = ["//:license"], default_visibility = [ @@ -100,6 +102,7 @@ cc_library( name = "init-command-line", srcs = ["init-command-line.cc"], hdrs = ["init-command-line.h"], + copts = get_version_define_from_module(), deps = [ # these deps are needed by init_command_line.cc: ":build-version", diff --git a/verible/common/util/init-command-line.cc b/verible/common/util/init-command-line.cc index cda06bcd7..371ffe43a 100644 --- a/verible/common/util/init-command-line.cc +++ b/verible/common/util/init-command-line.cc @@ -36,21 +36,27 @@ namespace verible { std::string GetRepositoryVersion() { #ifdef VERIBLE_GIT_DESCRIBE - return VERIBLE_GIT_DESCRIBE; + return VERIBLE_GIT_DESCRIBE; // from --workspace_status_command +#elif defined(VERIBLE_MODULE_VERSION) + return VERIBLE_MODULE_VERSION; // from MODULE.bazel via module-version.bzl #else return ""; #endif } // Long-form of build version, might contain multiple lines +// Build a version string with as much as possible info. static std::string GetBuildVersion() { std::string result; - // Build a version string with as much as possible info. -#ifdef VERIBLE_GIT_DESCRIBE - result.append(VERIBLE_GIT_DESCRIBE).append("\n"); -#endif -#ifdef VERIBLE_GIT_DATE - result.append("Commit\t").append(VERIBLE_GIT_DATE).append("\n"); + result.append("Version\t").append(GetRepositoryVersion()).append("\n"); +#ifdef VERIBLE_COMMIT_TIMESTAMP + result.append("Commit-Timestamp\t") + .append(absl::FormatTime("%Y-%m-%dT%H:%M:%SZ", + absl::FromTimeT(VERIBLE_COMMIT_TIMESTAMP), + absl::UTCTimeZone())) + .append("\n"); +#elif defined(VERIBLE_GIT_DATE) // Legacy + result.append("Commit-Date\t").append(VERIBLE_GIT_DATE).append("\n"); #endif #ifdef VERIBLE_BUILD_TIMESTAMP result.append("Built\t") diff --git a/verible/common/util/module-version.bzl b/verible/common/util/module-version.bzl new file mode 100644 index 000000000..3e5b3b19e --- /dev/null +++ b/verible/common/util/module-version.bzl @@ -0,0 +1,6 @@ +def get_version_define_from_module(): + module_version = native.module_version() + if module_version: + return ['-DVERIBLE_MODULE_VERSION=\\"{0}\\"'.format(module_version)] + else: + return []