xref: /linux/tools/perf/builtin-version.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "builtin.h"
3 #include "color.h"
4 #include "util/debug.h"
5 #include "util/header.h"
6 #include <tools/config.h>
7 #include <stdbool.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <subcmd/parse-options.h>
11 
12 struct version {
13 	bool	build_options;
14 };
15 
16 static struct version version;
17 
18 static struct option version_options[] = {
19 	OPT_BOOLEAN(0, "build-options", &version.build_options,
20 		    "display the build options"),
21 	OPT_END(),
22 };
23 
24 static const char * const version_usage[] = {
25 	"perf version [<options>]",
26 	NULL
27 };
28 
on_off_print(const char * status)29 static void on_off_print(const char *status)
30 {
31 	printf("[ ");
32 
33 	if (!strcmp(status, "OFF"))
34 		color_fprintf(stdout, PERF_COLOR_RED, "%-3s", status);
35 	else
36 		color_fprintf(stdout, PERF_COLOR_GREEN, "%-3s", status);
37 
38 	printf(" ]");
39 }
40 
status_print(const char * name,const char * macro,const char * status)41 static void status_print(const char *name, const char *macro,
42 			 const char *status)
43 {
44 	printf("%22s: ", name);
45 	on_off_print(status);
46 	printf("  # %s\n", macro);
47 }
48 
49 #define STATUS(feature)                                   \
50 do {                                                      \
51 	if (feature.is_builtin)                               \
52 		status_print(feature.name, feature.macro, "on");  \
53 	else                                                  \
54 		status_print(feature.name, feature.macro, "OFF"); \
55 } while (0)
56 
library_status(void)57 static void library_status(void)
58 {
59 	for (int i = 0; supported_features[i].name; ++i)
60 		STATUS(supported_features[i]);
61 }
62 
cmd_version(int argc,const char ** argv)63 int cmd_version(int argc, const char **argv)
64 {
65 	argc = parse_options(argc, argv, version_options, version_usage,
66 			     PARSE_OPT_STOP_AT_NON_OPTION);
67 
68 	printf("perf version %s\n", perf_version_string);
69 
70 	if (version.build_options || verbose > 0)
71 		library_status();
72 
73 	return 0;
74 }
75