|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Script to generate version information for perf_to_profile |
| 5 | +# Usage: ./scripts/generate-version.sh [output_file] |
| 6 | + |
| 7 | +OUTPUT_FILE="${1:-src/version.h}" |
| 8 | + |
| 9 | +# Try to get version from git |
| 10 | +if git rev-parse --git-dir > /dev/null 2>&1; then |
| 11 | + # Get the latest tag |
| 12 | + if GIT_TAG=$(git describe --tags --exact-match 2>/dev/null); then |
| 13 | + # We're on a tagged commit |
| 14 | + VERSION="$GIT_TAG" |
| 15 | + elif GIT_TAG=$(git describe --tags --abbrev=0 2>/dev/null); then |
| 16 | + # Get commit count since last tag and short hash |
| 17 | + COMMIT_COUNT=$(git rev-list --count "${GIT_TAG}..HEAD") |
| 18 | + SHORT_HASH=$(git rev-parse --short HEAD) |
| 19 | + VERSION="${GIT_TAG}-${COMMIT_COUNT}-g${SHORT_HASH}" |
| 20 | + else |
| 21 | + # No tags found, use commit hash |
| 22 | + SHORT_HASH=$(git rev-parse --short HEAD) |
| 23 | + VERSION="git-${SHORT_HASH}" |
| 24 | + fi |
| 25 | + |
| 26 | + # Add dirty suffix if working directory is not clean |
| 27 | + if ! git diff-index --quiet HEAD --; then |
| 28 | + VERSION="${VERSION}-dirty" |
| 29 | + fi |
| 30 | +else |
| 31 | + # Not a git repository, use default |
| 32 | + VERSION="development" |
| 33 | +fi |
| 34 | + |
| 35 | +echo "Generating version: $VERSION" |
| 36 | + |
| 37 | +# Create the version header file |
| 38 | +cat > "$OUTPUT_FILE" << EOF |
| 39 | +/* |
| 40 | + * Copyright (c) 2024, Google Inc. |
| 41 | + * All rights reserved. |
| 42 | + * Use of this source code is governed by a BSD-style license that can be |
| 43 | + * found in the LICENSE file. |
| 44 | + */ |
| 45 | +
|
| 46 | +#ifndef PERFTOOLS_VERSION_H_ |
| 47 | +#define PERFTOOLS_VERSION_H_ |
| 48 | +
|
| 49 | +// Version information for perf_to_profile |
| 50 | +// Generated automatically by scripts/generate-version.sh |
| 51 | +
|
| 52 | +#define PERF_TO_PROFILE_VERSION "$VERSION" |
| 53 | +
|
| 54 | +#endif // PERFTOOLS_VERSION_H_ |
| 55 | +EOF |
| 56 | + |
| 57 | +echo "Version header generated: $OUTPUT_FILE" |
0 commit comments