From e5d837bc3649818add61e659ff30515054b05b56 Mon Sep 17 00:00:00 2001 From: Kiron Date: Wed, 17 Apr 2024 14:42:57 +0800 Subject: [PATCH] Add `--version` flag --- bazel/version_h.bzl | 31 +++++++++++++++++++++++++++++++ bcc/BUILD | 17 +++++++++++++++-- bcc/options.cpp | 7 +++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 bazel/version_h.bzl diff --git a/bazel/version_h.bzl b/bazel/version_h.bzl new file mode 100644 index 0000000..9beddcd --- /dev/null +++ b/bazel/version_h.bzl @@ -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", + ] + 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.", +) diff --git a/bcc/BUILD b/bcc/BUILD index 68ebd55..a89b3f9 100644 --- a/bcc/BUILD +++ b/bcc/BUILD @@ -1,3 +1,4 @@ +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") @@ -5,7 +6,9 @@ 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": [], @@ -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", @@ -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", diff --git a/bcc/options.cpp b/bcc/options.cpp index ca08662..1ab0176 100644 --- a/bcc/options.cpp +++ b/bcc/options.cpp @@ -1,4 +1,5 @@ #include "bcc/options.hpp" +#include "bcc/version.h" #include #include @@ -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") @@ -93,6 +95,11 @@ options::from_argv(int argc, char* argv[]) std::exit(0); } + if (vm.count("version")) { + std::cerr << "bazel-compile-commands: " << BCC_VERSION << '\n'; + std::exit(0); + } + if (vm.count("compiler")) { result.compiler = vm["compiler"].as(); }