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