Skip to content

Commit fe05993

Browse files
Add version printing functionality to perf_to_profile (#9)
- Add --version command-line option using getopt_long - Create version.h header with PERF_TO_PROFILE_VERSION macro - Add PrintVersion() function to display version information - Create generate-version.sh script for git-based version generation - Update build scripts to generate version info before compilation - Add version.h to perf_to_profile_lib BUILD target - Support both tagged releases and development builds - Version format: tag-commits-hash (e.g., v1.0.0-5-g1a2b3c4) - Fallback to 'development' when not in git repository - Update CI workflow to generate version information Co-authored-by: fcostaoliveira <[email protected]>
1 parent ade9b59 commit fe05993

File tree

7 files changed

+109
-3
lines changed

7 files changed

+109
-3
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ jobs:
3131
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 1
3232
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 1
3333
34+
- name: Generate version information
35+
run: ./scripts/generate-version.sh
36+
3437
- name: Build
3538
run: bazel build //src:all //src/quipper:all
3639

scripts/build-deb.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ VERSION=$1 # e.g. "v1.2.3"
55
OUTDIR="$PWD/artifacts"
66
mkdir -p "$OUTDIR"
77

8+
# Generate version information
9+
echo "Generating version information..."
10+
./scripts/generate-version.sh
11+
812
# Build the project with Bazel (with static linking for better compatibility)
913
echo "Building perf_to_profile with Bazel..."
1014
bazel build \

scripts/generate-version.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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"

src/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ cc_test(
131131
cc_library(
132132
name = "perf_to_profile_lib",
133133
srcs = ["perf_to_profile_lib.cc"],
134-
hdrs = ["perf_to_profile_lib.h"],
134+
hdrs = [
135+
"perf_to_profile_lib.h",
136+
"version.h",
137+
],
135138
deps = [
136139
":perf_data_converter",
137140
"//src/quipper:base",

src/perf_to_profile_lib.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#include <sys/stat.h>
1111
#include <sstream>
12+
#include <getopt.h>
13+
14+
#include "src/version.h"
1215

1316
bool FileExists(const std::string& path) {
1417
struct stat file_stat;
@@ -57,6 +60,11 @@ void PrintUsage() {
5760
<< "profile.";
5861
LOG(INFO) << "If the -j option is given, allow unaligned MMAP events "
5962
<< "required by perf data from VMs with JITs.";
63+
LOG(INFO) << "Use --version to print version information.";
64+
}
65+
66+
void PrintVersion() {
67+
LOG(INFO) << "perf_to_profile version " << PERF_TO_PROFILE_VERSION;
6068
}
6169

6270
bool ParseArguments(int argc, const char* argv[], std::string* input,
@@ -66,9 +74,17 @@ bool ParseArguments(int argc, const char* argv[], std::string* input,
6674
*output = "";
6775
*overwrite_output = false;
6876
*allow_unaligned_jit_mappings = false;
77+
78+
// Define long options
79+
static struct option long_options[] = {
80+
{"version", no_argument, 0, 'v'},
81+
{0, 0, 0, 0}
82+
};
83+
6984
int opt;
70-
while ((opt = getopt(argc, const_cast<char* const*>(argv), ":jfi:o:")) !=
71-
-1) {
85+
int option_index = 0;
86+
while ((opt = getopt_long(argc, const_cast<char* const*>(argv), ":jfi:o:",
87+
long_options, &option_index)) != -1) {
7288
switch (opt) {
7389
case 'i':
7490
*input = optarg;
@@ -82,6 +98,10 @@ bool ParseArguments(int argc, const char* argv[], std::string* input,
8298
case 'j':
8399
*allow_unaligned_jit_mappings = true;
84100
break;
101+
case 'v': // --version
102+
PrintVersion();
103+
exit(EXIT_SUCCESS);
104+
break;
85105
case ':':
86106
LOG(ERROR) << "Must provide arguments for flags -i and -o";
87107
return false;

src/perf_to_profile_lib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ bool ParseArguments(int argc, const char* argv[], std::string* input,
4343
// Prints the usage of the tool.
4444
void PrintUsage();
4545

46+
// Prints the version of the tool.
47+
void PrintVersion();
48+
4649
#endif // PERFTOOLS_PERF_TO_PROFILE_LIB_H_

src/version.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2024, Google Inc.
3+
* All rights reserved.
4+
* Use of this source code is governed by a BSD-style license that can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
#ifndef PERFTOOLS_VERSION_H_
9+
#define PERFTOOLS_VERSION_H_
10+
11+
// Version information for perf_to_profile
12+
// Generated automatically by scripts/generate-version.sh
13+
14+
#define PERF_TO_PROFILE_VERSION "v0.0.5-7-g9ae6d28-dirty"
15+
16+
#endif // PERFTOOLS_VERSION_H_

0 commit comments

Comments
 (0)