Skip to content

Commit

Permalink
Add --version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kiron1 committed Apr 17, 2024
1 parent d23da73 commit 46b9b4d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
31 changes: 31 additions & 0 deletions bazel/version_h.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

def _version_h_impl(ctx):
guard = "{}_{}_H_INCLUDED".format(ctx.label.name.upper(), ctx.attr.varname)
content = [
"#ifndef {}".format(guard),
"#define {} \"{}\"".format(ctx.attr.varname, ctx.attr.version[BuildSettingInfo].value),
"#endif {}".format(guard),
]
ctx.actions.write(
output = ctx.outputs.out,
content = "\n".join(content),
)
files = depset(direct = [ctx.outputs.out])
runfiles = ctx.runfiles(files = [ctx.outputs.out])
return [DefaultInfo(files = files, data_runfiles = runfiles)]

version_h = rule(
implementation = _version_h_impl,
output_to_genfiles = True,
attrs = {
"out": attr.output(mandatory = True),
"varname": attr.string(
doc = "Name of the varible to hold the versions",
),
"version": attr.label(
doc = "Version of this build.",
),
},
doc = "Version as C/C++ header file.",
)
17 changes: 15 additions & 2 deletions bcc/BUILD
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
load("@//bazel:version_h.bzl", "version_h")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@build_bazel_apple_support//rules:universal_binary.bzl", "universal_binary")

package(default_visibility = ["//visibility:public"])

cc_binary(
name = "bazel-compile-commands-binary",
srcs = ["main.cpp"],
srcs = [
"main.cpp",
],
linkopts = select({
"@platforms//os:windows": ["-DEFAULTLIB:shell32.lib"],
"//conditions:default": [],
Expand Down Expand Up @@ -108,7 +111,10 @@ cc_library(
cc_library(
name = "options",
srcs = ["options.cpp"],
hdrs = ["options.hpp"],
hdrs = [
"options.hpp",
":version",
],
defines = ["BOOST_PROCESS_USE_STD_FS=1"],
deps = [
"@boost//:process",
Expand Down Expand Up @@ -142,6 +148,13 @@ cc_library(
hdrs = ["replacements.hpp"],
)

version_h(
name = "version",
out = "version.h",
varname = "BCC_VERSION",
version = "//:version",
)

cc_test(
name = "replacements_test",
size = "small",
Expand Down
6 changes: 6 additions & 0 deletions bcc/options.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "bcc/options.hpp"
#include "bcc/version.h"

#include <cstdlib>
#include <fstream>
Expand Down Expand Up @@ -49,6 +50,7 @@ options::from_argv(int argc, char* argv[])
// clang-format off
cfg.add_options()
("help,h", "produce help message")
("version,V", "print version")
("verbose,v", po::bool_switch(&result.verbose), "verbose, report more information")
("arguments,a", po::bool_switch(&result.arguments), "include `arguments` array in output")
("resolve", po::bool_switch(&result.resolve), "resolve file symlinks when their target is inside the workspace")
Expand Down Expand Up @@ -92,6 +94,10 @@ options::from_argv(int argc, char* argv[])
std::cerr << desc << '\n';
std::exit(0);
}
if (vm.count("version")) {
std::cerr << BCC_VERSION << '\n';
std::exit(0);
}

if (vm.count("compiler")) {
result.compiler = vm["compiler"].as<std::string>();
Expand Down

0 comments on commit 46b9b4d

Please sign in to comment.