|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2024 The StableHLO Authors. |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# This script: |
| 16 | +# 1. Replaces current version in stablehlo/dialect/Version.h with the next |
| 17 | +# patch version |
| 18 | +# static Version getCurrentVersion() { return Version(x, y, z); } |
| 19 | +# new version will be (x, y, z + 1) |
| 20 | +# 2. Replaces WEEK_4 and WEEK12 versions in stablehlo/dialect/Version.cpp |
| 21 | +# return Version(a, b, c); // WEEK_4 ANCHOR: DO NOT MODIFY |
| 22 | +# return Version(m, n, p); // WEEK_12 ANCHOR: DO NOT MODIFY |
| 23 | +# WEEK_4 version - The most recent git tag that was created at least 28 days ago. |
| 24 | +# WEEK_12 version - The most recent git tag that was created at least 84 days ago. |
| 25 | + |
| 26 | +set -o errexit |
| 27 | +set -o nounset |
| 28 | +set -o pipefail |
| 29 | + |
| 30 | +script_dir="$(dirname "$(realpath "$0")")" |
| 31 | +version_h="$script_dir/../stablehlo/dialect/Version.h" |
| 32 | +version_cpp="$script_dir/../stablehlo/dialect/Version.cpp" |
| 33 | + |
| 34 | +fetch_current_version() { |
| 35 | + # getCurrentVersion() { Version(X, Y, Z); } |
| 36 | + ver_str=$(grep -A1 getCurrentVersion "$version_h" | grep -o 'Version(.*[0-9])') |
| 37 | + REGEX="Version\(([0-9]+), ([0-9]+), ([0-9]+)\)" |
| 38 | + if [[ $ver_str =~ $REGEX ]]; then |
| 39 | + curr_ver=("${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}") |
| 40 | + else |
| 41 | + echo "Error: Could not find current version string in $version_h" >&2 |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | +} |
| 45 | +fetch_current_version |
| 46 | + |
| 47 | +# calculate next current ver (patch ver + 1) |
| 48 | +next_curr_z=$((curr_ver[2] + 1)) |
| 49 | + |
| 50 | +utc_time=$(date -u +%s) |
| 51 | + |
| 52 | +week_4_tag="" |
| 53 | +week_12_tag="" |
| 54 | + |
| 55 | +while IFS= read -r line; do |
| 56 | + # split line CSV |
| 57 | + IFS=',' read -r tag_ts tag_v <<< "$line" |
| 58 | + ts_diff=$(( (utc_time - tag_ts) / 86400 )) |
| 59 | + |
| 60 | + if [ -z "$week_4_tag" ] && [ "$ts_diff" -ge 28 ]; then |
| 61 | + week_4_tag=$tag_v |
| 62 | + fi |
| 63 | + |
| 64 | + if [ -z "$week_12_tag" ] && [ "$ts_diff" -ge 84 ]; then |
| 65 | + week_12_tag=$tag_v |
| 66 | + break |
| 67 | + fi |
| 68 | +done < <(git for-each-ref --sort=taggerdate --format '%(taggerdate:unix),%(refname:short)' refs/tags | tail -40 | tac) |
| 69 | + |
| 70 | +if [ -z "$week_4_tag" ] || [ -z "$week_12_tag" ]; then |
| 71 | + echo "Error: WEEK_4 or WEEK_12 tag not found." >&2 |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | + |
| 75 | +week_4_tag=$(echo "$week_4_tag" | sed -n 's/.*v\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\).*/\1, \2, \3/p') |
| 76 | +week_12_tag=$(echo "$week_12_tag" | sed -n 's/.*v\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\).*/\1, \2, \3/p') |
| 77 | + |
| 78 | +if [ -z "$week_4_tag" ] || [ -z "$week_12_tag" ]; then |
| 79 | + echo "Error: Unable to parse the WEEK_4 or WEEK_12 tag" >&2 |
| 80 | + exit 1 |
| 81 | +fi |
| 82 | + |
| 83 | +echo "Next Current Version: ${curr_ver[0]}, ${curr_ver[1]}, $next_curr_z" >&2 |
| 84 | +echo "WEEK_4 Version: $week_4_tag" >&2 |
| 85 | +echo "WEEK_12 Version: $week_12_tag" >&2 |
| 86 | + |
| 87 | +# Saving Version.h values |
| 88 | +echo "Saving $version_h..." >&2 |
| 89 | + |
| 90 | +sed -i -E \ |
| 91 | +-e "s/(static Version getCurrentVersion\(\) \{ return Version\()([0-9]+), ([0-9]+), ([0-9]+)(\); \})/\1\2, \3, $next_curr_z\5/" \ |
| 92 | +"$version_h" |
| 93 | + |
| 94 | +# Checking Version.h values |
| 95 | +grep "static Version getCurrentVersion()" "$version_h" >&2 |
| 96 | + |
| 97 | +if [ "$(grep -c "static Version getCurrentVersion() { return Version(${curr_ver[0]}, ${curr_ver[1]}, $next_curr_z); }" "$version_h")" -ne 1 ]; then |
| 98 | + echo "ERROR: getCurrentVersion() version is not correct" >&2 |
| 99 | + exit 1 |
| 100 | +fi |
| 101 | + |
| 102 | +# Saving Version.cpp values |
| 103 | +echo "Saving $version_cpp..." >&2 |
| 104 | + |
| 105 | +sed -i -E \ |
| 106 | +-e "s/(return Version\()([0-9]+), ([0-9]+), ([0-9]+)(\);\s+\/\/ WEEK_4 ANCHOR: DO NOT MODIFY)/\1$week_4_tag\5/" \ |
| 107 | +-e "s/(return Version\()([0-9]+), ([0-9]+), ([0-9]+)(\);\s+\/\/ WEEK_12 ANCHOR: DO NOT MODIFY)/\1$week_12_tag\5/" \ |
| 108 | +"$version_cpp" |
| 109 | + |
| 110 | +# Checking Version.cpp values |
| 111 | +grep "WEEK_4 ANCHOR: DO NOT MODIFY" "$version_cpp" >&2 |
| 112 | +grep "WEEK_12 ANCHOR: DO NOT MODIFY" "$version_cpp" >&2 |
| 113 | + |
| 114 | +if [ "$(grep -c -E "return Version\($week_4_tag\);\s+\/\/ WEEK_4 ANCHOR: DO NOT MODIFY" "$version_cpp")" -ne 1 ]; then |
| 115 | + echo "ERROR: WEEK_4 version is not correct" >&2 |
| 116 | + exit 1 |
| 117 | +fi |
| 118 | + |
| 119 | +if [ "$(grep -c -E "return Version\($week_12_tag\);\s+\/\/ WEEK_12 ANCHOR: DO NOT MODIFY" "$version_cpp")" -ne 1 ]; then |
| 120 | + echo "ERROR: WEEK_12 version is not correct" >&2 |
| 121 | + exit 1 |
| 122 | +fi |
| 123 | + |
| 124 | +echo "Done" >&2 |
0 commit comments