Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
banjo-info.c
Go to the documentation of this file.
1
18
19#include <banjo/api.h>
20#include <banjo/cli.h>
21#include <banjo/log.h>
22#include <banjo/main.h>
23#include <banjo/system.h>
24#include <banjo/version.h>
25
26#include <ctype.h>
27#include <stdio.h>
28
29// Output flags - set by CLI parser
30static int flag_help = 0;
31static int flag_all = 0; // -a: all main info (shorthand for -nvsdc)
32static int flag_name_lower = 0; // -n: lowercase name (banjo)
33static int flag_name = 0; // -N: name (Banjo)
34static int flag_version = 0; // -v: version (1.0.0)
35static int flag_stage = 0; // -s: stage (rc.1)
36static int flag_debug = 0; // -d: "Debug Build" if debug
37static int flag_compiler = 0; // -c: compiler info
38static int flag_backends = 0; // -b: list backends
39
40// Helper: get stage type name from stage byte
41static const char* stage_type_name(uint8_t stage) {
42 if (stage == BJ_VERSION_STABLE) return "stable";
43 switch (stage & 0xC0) {
44 case BJ_VERSION_ALPHA: return "alpha";
45 case BJ_VERSION_BETA: return "beta";
46 case BJ_VERSION_PREVIEW: return "preview";
47 case BJ_VERSION_RC: return "rc";
48 default: return "unknown";
49 }
50}
51
52// Helper: format stage string (e.g., "rc.2", "alpha", "stable")
53static size_t format_stage(char* buffer, size_t bufsize, uint8_t stage) {
54 uint8_t num = stage & 0x3F;
55 if (stage == BJ_VERSION_STABLE || num == 0) {
56 return (size_t)snprintf(buffer, bufsize, "%s", stage_type_name(stage));
57 }
58 return (size_t)snprintf(buffer, bufsize, "%s.%u", stage_type_name(stage), num);
59}
60
61// Print full information (default when no flags)
62static void print_all(const bj_build_info* info) {
63 char version_string[32];
64 bj_format_version(version_string, sizeof(version_string), info->version);
65
66 printf("%s version %s (0x%08X) [%s] %s build\n",
67 info->name,
68 version_string, info->version,
69 info->variant,
70 info->debug ? "Debug" : "Release"
71 );
72
73 printf("Compiler: %s %d\n", info->compiler_name, info->compiler_version);
74
75 printf("\nBackends:\n");
76 printf(" %c alsa\n", info->backend_alsa ? '+' : '-');
77 printf(" %c cocoa\n", info->backend_cocoa ? '+' : '-');
78 printf(" %c emscripten\n", info->backend_emscripten ? '+' : '-');
79 printf(" %c mme\n", info->backend_mme ? '+' : '-');
80 printf(" %c wayland\n", info->backend_wayland ? '+' : '-');
81 printf(" %c win32\n", info->backend_win32 ? '+' : '-');
82 printf(" %c x11\n", info->backend_x11 ? '+' : '-');
83
84 printf("\nConfiguration:\n");
85 printf(" %c checks_abort\n", info->checks_abort ? '+' : '-');
86 printf(" %c checks_log\n", info->checks_log ? '+' : '-');
87 printf(" %c fastmath\n", info->fastmath ? '+' : '-');
88 printf(" %c log_color\n", info->log_color ? '+' : '-');
89 printf(" %c pedantic\n", info->pedantic ? '+' : '-');
90
91 const size_t count = bj_video_backends(0, 0);
92 printf("\nActive video backends (%zu):\n", count);
93 const char* names[16];
94 size_t got = bj_video_backends(names, count < 16 ? count : 16);
95 for (size_t i = 0; i < got; ++i) {
96 printf(" %s\n", names[i]);
97 }
98}
99
100// Print combined output based on flags
101static void print_combined(const bj_build_info* info) {
102 int printed = 0; // Track if anything printed on main line
103
104 // Identity segment: name and version components
105 if (flag_name_lower) {
106 for (size_t i = 0; info->name[i]; ++i) {
107 putchar(tolower((unsigned char)info->name[i]));
108 }
109 printed = 1;
110 } else if (flag_name) {
111 printf("%s", info->name);
112 printed = 1;
113 }
114
115 // Version and stage
116 if (flag_version || flag_stage) {
117 if (printed) putchar('-');
118 }
119
120 if (flag_version) {
121 printf("%u.%u.%u",
125 printed = 1;
126 }
127
128 if (flag_stage) {
129 if (flag_version) putchar('-');
130 char stage_str[16];
131 format_stage(stage_str, sizeof(stage_str), BJ_VERSION_STAGE(info->version));
132 printf("%s", stage_str);
133 printed = 1;
134 }
135
136 // Build segment: debug and compiler
137 if (flag_debug && info->debug) {
138 if (printed) putchar(' ');
139 printf("Debug Build");
140 printed = 1;
141 }
142
143 if (flag_compiler) {
144 if (printed) printf(", ");
145 printf("%s %d", info->compiler_name, info->compiler_version);
146 printed = 1;
147 }
148
149 // End main line if we printed anything
150 if (printed) {
151 putchar('\n');
152 }
153
154 // Backends segment: one per line
155 if (flag_backends) {
156 const size_t count = bj_video_backends(0, 0);
157 const char* names[16];
158 size_t got = bj_video_backends(names, count < 16 ? count : 16);
159 for (size_t i = 0; i < got; ++i) {
160 printf("%s\n", names[i]);
161 }
162 }
163}
164
165int main(int argc, char* argv[]) {
166 struct bj_cli_argument args[] = {
167 {
168 .shortname = 'h',
169 .name = "help",
170 .help = "Show this help message and exit",
171 .action = bj_print_cli_help_action,
172 .dest = &flag_help
173 },
174 {
175 .shortname = 'a',
176 .name = "all",
177 .help = "Print all info (shorthand for -nvsdc)",
178 .dest = &flag_all
179 },
180 {
181 .shortname = 'n',
182 .name = "name-lower",
183 .help = "Print lowercase name (banjo)",
184 .dest = &flag_name_lower
185 },
186 {
187 .shortname = 'N',
188 .name = "name",
189 .help = "Print name (Banjo)",
190 .dest = &flag_name
191 },
192 {
193 .shortname = 'v',
194 .name = "version",
195 .help = "Print version (1.0.0)",
196 .dest = &flag_version
197 },
198 {
199 .shortname = 's',
200 .name = "stage",
201 .help = "Print release stage (rc.1, alpha, stable)",
202 .dest = &flag_stage
203 },
204 {
205 .shortname = 'd',
206 .name = "debug",
207 .help = "Print 'Debug Build' if debug build",
208 .dest = &flag_debug
209 },
210 {
211 .shortname = 'c',
212 .name = "compiler",
213 .help = "Print compiler name and version",
214 .dest = &flag_compiler
215 },
216 {
217 .shortname = 'b',
218 .name = "backends",
219 .help = "Print available backends (one per line)",
220 .dest = &flag_backends
221 },
222 };
223
224 struct bj_cli parser = {
225 .prog = "banjo-info",
226 .description = "Query Banjo build information.\n"
227 "Flags can be combined to build custom output with sensible separators.\n"
228 "With no flags, prints full human-readable information.",
229 .epilog = "Examples:\n"
230 " banjo-info -nvs # banjo-1.0.0-rc.1\n"
231 " banjo-info -Nv # Banjo-1.0.0\n"
232 " banjo-info -a # banjo-1.0.0-rc.1 Debug Build, GCC 13\n"
233 " banjo-info -v # 1.0.0\n"
234 " banjo-info -b # list backends\n"
235 " banjo-info # full output",
236 .arguments_len = sizeof(args) / sizeof(args[0]),
237 .arguments = args,
238 };
239
240 if (!bj_parse_cli(&parser, argc, argv, 0)) {
241 bj_print_cli_help(&parser);
242 return 1;
243 }
244
245 const bj_build_info* info = bj_build_information();
246
247 // Expand -a to -nvsdc
248 if (flag_all) {
249 flag_name_lower = 1;
250 flag_version = 1;
251 flag_stage = 1;
252 flag_debug = 1;
253 flag_compiler = 1;
254 }
255
256 // Check if any output flag was set
257 int any_flag = flag_name_lower || flag_name || flag_version || flag_stage ||
259
260 if (!any_flag) {
261 print_all(info);
262 } else {
263 print_combined(info);
264 }
265
266 return 0;
267}
General-purpose definitions for Banjo API.
int main(int argc, char *argv[])
Definition audio_pcm.c:177
static int flag_version
Definition banjo-info.c:34
static int flag_backends
Definition banjo-info.c:38
static int flag_all
Definition banjo-info.c:31
static int flag_name_lower
Definition banjo-info.c:32
static void print_all(const bj_build_info *info)
Definition banjo-info.c:62
static int flag_stage
Definition banjo-info.c:35
static int flag_compiler
Definition banjo-info.c:37
static const char * stage_type_name(uint8_t stage)
Definition banjo-info.c:41
static int flag_help
Definition banjo-info.c:30
static int flag_name
Definition banjo-info.c:33
static void print_combined(const bj_build_info *info)
Definition banjo-info.c:101
static size_t format_stage(char *buffer, size_t bufsize, uint8_t stage)
Definition banjo-info.c:53
static int flag_debug
Definition banjo-info.c:36
POSIX/GNU-like command-line argument parser.
void bj_print_cli_help(const struct bj_cli *parser)
Print help message using Banjo's logging system.
bj_bool bj_parse_cli(struct bj_cli *parser, int argc, char *argv[], struct bj_error **error)
Parse command-line arguments according to parser configuration.
bj_bool bj_print_cli_help_action(const struct bj_cli *parser, const struct bj_cli_argument *arg, const char *value, void *dest, struct bj_error **error)
Print help argument action.
Parser context and argument list descriptor.
Definition cli.h:173
Descriptor for a single command line argument.
Definition cli.h:151
bj_bool checks_log
Checks log failures.
Definition api.h:293
const char * name
API name (see BJ_NAME).
Definition api.h:279
bj_bool pedantic
Extra runtime checks enabled.
Definition api.h:296
uint32_t version
Packed API version (see BJ_VERSION).
Definition api.h:281
bj_bool log_color
Colored log output enabled.
Definition api.h:295
bj_bool backend_win32
Built with Win32 window support.
Definition api.h:290
bj_bool backend_alsa
Built with ALSA audio.
Definition api.h:285
bj_bool fastmath
Built with fast-math optimizations.
Definition api.h:294
bj_bool backend_wayland
Built with Wayland window support.
Definition api.h:289
const char * compiler_name
Compiler name string.
Definition api.h:282
bj_bool backend_x11
Built with X11 window support.
Definition api.h:291
bj_bool backend_emscripten
Built with Emscripten support.
Definition api.h:287
const char * variant
API name variant (see BJ_NAME_VARIANT).
Definition api.h:280
bj_bool debug
Non-zero if built with debug info.
Definition api.h:284
bj_bool backend_cocoa
Built with Cocoa/macOS support.
Definition api.h:286
bj_bool checks_abort
Checks abort execution on failure.
Definition api.h:292
int compiler_version
Compiler version number.
Definition api.h:283
bj_bool backend_mme
Built with Windows MME audio.
Definition api.h:288
const struct bj_build_info * bj_build_information(void)
Get runtime build information for the loaded Banjo binaries.
Structure holding build information of the binary.
Definition api.h:278
size_t bj_video_backends(const char **p_names, size_t cap)
Enumerate the video backends compiled into this build.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for system interactions.
32-bit packed version storage following the SemVer standard.
#define BJ_VERSION_PREVIEW
Preview (feature freeze, bug fixes only)
Definition version.h:51
#define BJ_VERSION_STAGE(version)
Extract the stage byte from a packed 32-bit version.
Definition version.h:92
#define BJ_VERSION_ALPHA
Version stage flags.
Definition version.h:49
size_t bj_format_version(char *buffer, size_t bufsize, uint32_t version)
Format a packed version number as a SemVer-compatible string.
#define BJ_VERSION_MINOR(version)
Extract the minor version from a packed 32-bit version.
Definition version.h:73
#define BJ_VERSION_MAJOR(version)
Extract the major version from a packed 32-bit version.
Definition version.h:64
#define BJ_VERSION_STABLE
Stable Release.
Definition version.h:53
#define BJ_VERSION_PATCH(version)
Extract the patch version from a packed 32-bit version.
Definition version.h:82
#define BJ_VERSION_RC
Release Candidate (critical fixes only)
Definition version.h:52
#define BJ_VERSION_BETA
Beta (maturing, can still add features)
Definition version.h:50