Skip to content

Commit 23d2cc3

Browse files
committed
Version: print libc version used when compiling
1 parent 1dfa031 commit 23d2cc3

File tree

8 files changed

+171
-2
lines changed

8 files changed

+171
-2
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ if(LINUX)
385385
src/detection/gtk_qt/gtk.c
386386
src/detection/host/host_linux.c
387387
src/detection/icons/icons_linux.c
388+
src/detection/libc/libc_linux.c
388389
src/detection/lm/lm_linux.c
389390
src/detection/localip/localip_linux.c
390391
src/detection/gamepad/gamepad_linux.c
@@ -433,6 +434,7 @@ elseif(ANDROID)
433434
src/detection/gpu/gpu_nosupport.c
434435
src/detection/host/host_android.c
435436
src/detection/icons/icons_nosupport.c
437+
src/detection/libc/libc_android.c
436438
src/detection/lm/lm_nosupport.c
437439
src/detection/localip/localip_linux.c
438440
src/detection/gamepad/gamepad_nosupport.c
@@ -488,6 +490,7 @@ elseif(BSD)
488490
src/detection/host/host_bsd.c
489491
src/detection/lm/lm_linux.c
490492
src/detection/icons/icons_linux.c
493+
src/detection/libc/libc_nosupport.c
491494
src/detection/localip/localip_linux.c
492495
src/detection/gamepad/gamepad_bsd.c
493496
src/detection/media/media_linux.c
@@ -537,6 +540,7 @@ elseif(APPLE)
537540
src/detection/host/host_apple.c
538541
src/detection/lm/lm_nosupport.c
539542
src/detection/icons/icons_nosupport.c
543+
src/detection/libc/libc_nosupport.c
540544
src/detection/localip/localip_linux.c
541545
src/detection/gamepad/gamepad_apple.c
542546
src/detection/media/media_apple.m
@@ -585,6 +589,7 @@ elseif(WIN32)
585589
src/detection/gpu/gpu_windows.c
586590
src/detection/host/host_windows.c
587591
src/detection/icons/icons_windows.c
592+
src/detection/libc/libc_windows.cpp
588593
src/detection/lm/lm_nosupport.c
589594
src/detection/localip/localip_windows.c
590595
src/detection/gamepad/gamepad_windows.c

src/detection/libc/libc.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#ifndef FF_INCLUDED_detection_libc_libc
4+
#define FF_INCLUDED_detection_libc_libc
5+
6+
#include "fastfetch.h"
7+
8+
typedef struct FFLibcResult
9+
{
10+
const char* name;
11+
const char* version;
12+
} FFLibcResult;
13+
14+
const char* ffDetectLibc(FFLibcResult* result);
15+
16+
#endif

src/detection/libc/libc_android.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "libc.h"
2+
3+
#define FF_STR_INDIR(x) #x
4+
#define FF_STR(x) FF_STR_INDIR(x)
5+
6+
#include <features.h>
7+
8+
const char* ffDetectLibc(FFLibcResult* result)
9+
{
10+
#if __ANDROID_NDK__
11+
result->name = "ndk-bionic";
12+
result->version = FF_STR(__NDK_MAJOR__) "." FF_STR(__NDK_MINOR__) "." FF_STR(__NDK_BUILD__)
13+
14+
#if __NDK_BETA__
15+
"-beta" FF_STR(__NDK_BETA__)
16+
#elif __NDK_CANARY__
17+
"-canary"
18+
#endif
19+
20+
;
21+
return NULL;
22+
#else
23+
return "Unknown Android libc";
24+
#endif
25+
}

src/detection/libc/libc_linux.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "libc.h"
2+
3+
#define FF_STR_INDIR(x) #x
4+
#define FF_STR(x) FF_STR_INDIR(x)
5+
6+
#include <features.h>
7+
8+
const char* ffDetectLibc(FFLibcResult* result)
9+
{
10+
#ifdef __GNU_LIBRARY__
11+
result->name = "glibc";
12+
result->version = FF_STR(__GLIBC__) "." FF_STR(__GLIBC_MINOR__);
13+
#else
14+
result->name = "musl";
15+
result->version = NULL;
16+
#endif
17+
18+
return NULL;
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "libc.h"
2+
3+
#define FF_STR_INDIR(x) #x
4+
#define FF_STR(x) FF_STR_INDIR(x)
5+
6+
const char* ffDetectLibc(FF_MAYBE_UNUSED FFLibcResult* result)
7+
{
8+
return "Not supported on this platform";
9+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
extern "C"
2+
{
3+
#include "libc.h"
4+
}
5+
6+
#ifdef __MINGW32__
7+
#include <_mingw.h>
8+
#endif
9+
10+
template<uint32_t Major, uint32_t Minor>
11+
class version_t {
12+
constexpr static auto buflen() noexcept {
13+
unsigned int len = 2; // "."
14+
if (Major == 0)
15+
len++;
16+
else
17+
for (auto n = Major; n; len++, n /= 10);
18+
19+
if (Minor == 0)
20+
len++;
21+
else
22+
for (auto n = Minor; n; len++, n /= 10);
23+
return len;
24+
}
25+
26+
char buf[buflen()] = {};
27+
28+
public:
29+
constexpr version_t() noexcept {
30+
auto ptr = buf + buflen();
31+
*--ptr = '\0';
32+
33+
if (Minor == 0) {
34+
*--ptr = '0';
35+
} else {
36+
for (auto n = Minor; n; n /= 10)
37+
*--ptr = "0123456789"[n % 10];
38+
}
39+
*--ptr = '.';
40+
if (Major == 0) {
41+
*--ptr = '0';
42+
} else {
43+
for (auto n = Major; n; n /= 10)
44+
*--ptr = "0123456789"[n % 10];
45+
}
46+
}
47+
48+
constexpr operator const char *() const { return buf; }
49+
};
50+
51+
template<uint32_t Major, uint32_t Minor>
52+
constexpr version_t<Major, Minor> version;
53+
54+
extern "C"
55+
const char* ffDetectLibc(FFLibcResult* result)
56+
{
57+
#ifdef _UCRT
58+
result->name = "ucrt";
59+
#else
60+
result->name = "msvcrt";
61+
#endif
62+
63+
result->version = version<(__MSVCRT_VERSION__ >> 8), (__MSVCRT_VERSION__ & 8)>;
64+
return NULL;
65+
}

src/fastfetch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ static inline void printCommandHelp(const char* command)
468468
}
469469
else if(ffStrEqualsIgnCase(command, "version-format"))
470470
{
471-
constructAndPrintCommandHelpFormat("version", "{1} {2}{3} ({5})", 8,
471+
constructAndPrintCommandHelpFormat("version", "{1} {2}{3} ({5})", 9,
472472
"Project name",
473473
"Version",
474474
"Version tweak",

src/modules/version/version.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include "common/printing.h"
22
#include "common/jsonconfig.h"
3+
#include "detection/libc/libc.h"
34
#include "detection/version/version.h"
45
#include "modules/version/version.h"
56
#include "util/stringUtils.h"
67

7-
#define FF_VERSION_NUM_FORMAT_ARGS 8
8+
#define FF_VERSION_NUM_FORMAT_ARGS 9
89

910
void ffPrintVersion(FFVersionOptions* options)
1011
{
@@ -18,6 +19,18 @@ void ffPrintVersion(FFVersionOptions* options)
1819
}
1920
else
2021
{
22+
FFLibcResult libcResult;
23+
FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate();
24+
if (!ffDetectLibc(&libcResult))
25+
{
26+
ffStrbufSetS(&buf, libcResult.name);
27+
if (libcResult.version)
28+
{
29+
ffStrbufAppendC(&buf, ' ');
30+
ffStrbufAppendS(&buf, libcResult.version);
31+
}
32+
}
33+
2134
ffPrintFormat(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_VERSION_NUM_FORMAT_ARGS, (FFformatarg[]){
2235
{FF_FORMAT_ARG_TYPE_STRING, result.projectName},
2336
{FF_FORMAT_ARG_TYPE_STRING, result.version},
@@ -27,6 +40,7 @@ void ffPrintVersion(FFVersionOptions* options)
2740
{FF_FORMAT_ARG_TYPE_STRING, result.cmakeBuiltType},
2841
{FF_FORMAT_ARG_TYPE_STRING, result.compileTime},
2942
{FF_FORMAT_ARG_TYPE_STRING, result.compiler},
43+
{FF_FORMAT_ARG_TYPE_STRBUF, &buf},
3044
});
3145
}
3246
}
@@ -83,4 +97,20 @@ void ffGenerateVersionJson(FF_MAYBE_UNUSED FFVersionOptions* options, yyjson_mut
8397
yyjson_mut_obj_add_str(doc, obj, "compileTime", result.compileTime);
8498
yyjson_mut_obj_add_str(doc, obj, "compiler", result.compiler);
8599
yyjson_mut_obj_add_bool(doc, obj, "debugMode", result.debugMode);
100+
101+
FFLibcResult libcResult;
102+
if (ffDetectLibc(&libcResult))
103+
{
104+
yyjson_mut_obj_add_null(doc, obj, "libc");
105+
}
106+
else
107+
{
108+
FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreateS(libcResult.name);
109+
if (libcResult.version)
110+
{
111+
ffStrbufAppendC(&buf, ' ');
112+
ffStrbufAppendS(&buf, libcResult.version);
113+
}
114+
yyjson_mut_obj_add_strbuf(doc, obj, "libc", &buf);
115+
}
86116
}

0 commit comments

Comments
 (0)