From 23d2cc3fdc1d1531174a67b0dd708b05a5cba43c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 5 Oct 2023 19:06:15 +0800 Subject: [PATCH] Version: print libc version used when compiling --- CMakeLists.txt | 5 +++ src/detection/libc/libc.h | 16 +++++++ src/detection/libc/libc_android.c | 25 +++++++++++ src/detection/libc/libc_linux.c | 19 +++++++++ src/detection/libc/libc_nosupport.c | 9 ++++ src/detection/libc/libc_windows.cpp | 65 +++++++++++++++++++++++++++++ src/fastfetch.c | 2 +- src/modules/version/version.c | 32 +++++++++++++- 8 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 src/detection/libc/libc.h create mode 100644 src/detection/libc/libc_android.c create mode 100644 src/detection/libc/libc_linux.c create mode 100644 src/detection/libc/libc_nosupport.c create mode 100644 src/detection/libc/libc_windows.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 04eebf679..47af8e6af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -385,6 +385,7 @@ if(LINUX) src/detection/gtk_qt/gtk.c src/detection/host/host_linux.c src/detection/icons/icons_linux.c + src/detection/libc/libc_linux.c src/detection/lm/lm_linux.c src/detection/localip/localip_linux.c src/detection/gamepad/gamepad_linux.c @@ -433,6 +434,7 @@ elseif(ANDROID) src/detection/gpu/gpu_nosupport.c src/detection/host/host_android.c src/detection/icons/icons_nosupport.c + src/detection/libc/libc_android.c src/detection/lm/lm_nosupport.c src/detection/localip/localip_linux.c src/detection/gamepad/gamepad_nosupport.c @@ -488,6 +490,7 @@ elseif(BSD) src/detection/host/host_bsd.c src/detection/lm/lm_linux.c src/detection/icons/icons_linux.c + src/detection/libc/libc_nosupport.c src/detection/localip/localip_linux.c src/detection/gamepad/gamepad_bsd.c src/detection/media/media_linux.c @@ -537,6 +540,7 @@ elseif(APPLE) src/detection/host/host_apple.c src/detection/lm/lm_nosupport.c src/detection/icons/icons_nosupport.c + src/detection/libc/libc_nosupport.c src/detection/localip/localip_linux.c src/detection/gamepad/gamepad_apple.c src/detection/media/media_apple.m @@ -585,6 +589,7 @@ elseif(WIN32) src/detection/gpu/gpu_windows.c src/detection/host/host_windows.c src/detection/icons/icons_windows.c + src/detection/libc/libc_windows.cpp src/detection/lm/lm_nosupport.c src/detection/localip/localip_windows.c src/detection/gamepad/gamepad_windows.c diff --git a/src/detection/libc/libc.h b/src/detection/libc/libc.h new file mode 100644 index 000000000..dc89cc2be --- /dev/null +++ b/src/detection/libc/libc.h @@ -0,0 +1,16 @@ +#pragma once + +#ifndef FF_INCLUDED_detection_libc_libc +#define FF_INCLUDED_detection_libc_libc + +#include "fastfetch.h" + +typedef struct FFLibcResult +{ + const char* name; + const char* version; +} FFLibcResult; + +const char* ffDetectLibc(FFLibcResult* result); + +#endif diff --git a/src/detection/libc/libc_android.c b/src/detection/libc/libc_android.c new file mode 100644 index 000000000..845a66fa4 --- /dev/null +++ b/src/detection/libc/libc_android.c @@ -0,0 +1,25 @@ +#include "libc.h" + +#define FF_STR_INDIR(x) #x +#define FF_STR(x) FF_STR_INDIR(x) + +#include + +const char* ffDetectLibc(FFLibcResult* result) +{ +#if __ANDROID_NDK__ + result->name = "ndk-bionic"; + result->version = FF_STR(__NDK_MAJOR__) "." FF_STR(__NDK_MINOR__) "." FF_STR(__NDK_BUILD__) + + #if __NDK_BETA__ + "-beta" FF_STR(__NDK_BETA__) + #elif __NDK_CANARY__ + "-canary" + #endif + + ; + return NULL; +#else + return "Unknown Android libc"; +#endif +} diff --git a/src/detection/libc/libc_linux.c b/src/detection/libc/libc_linux.c new file mode 100644 index 000000000..399a67870 --- /dev/null +++ b/src/detection/libc/libc_linux.c @@ -0,0 +1,19 @@ +#include "libc.h" + +#define FF_STR_INDIR(x) #x +#define FF_STR(x) FF_STR_INDIR(x) + +#include + +const char* ffDetectLibc(FFLibcResult* result) +{ +#ifdef __GNU_LIBRARY__ + result->name = "glibc"; + result->version = FF_STR(__GLIBC__) "." FF_STR(__GLIBC_MINOR__); +#else + result->name = "musl"; + result->version = NULL; +#endif + + return NULL; +} diff --git a/src/detection/libc/libc_nosupport.c b/src/detection/libc/libc_nosupport.c new file mode 100644 index 000000000..0c6a968a3 --- /dev/null +++ b/src/detection/libc/libc_nosupport.c @@ -0,0 +1,9 @@ +#include "libc.h" + +#define FF_STR_INDIR(x) #x +#define FF_STR(x) FF_STR_INDIR(x) + +const char* ffDetectLibc(FF_MAYBE_UNUSED FFLibcResult* result) +{ + return "Not supported on this platform"; +} diff --git a/src/detection/libc/libc_windows.cpp b/src/detection/libc/libc_windows.cpp new file mode 100644 index 000000000..8a699d765 --- /dev/null +++ b/src/detection/libc/libc_windows.cpp @@ -0,0 +1,65 @@ +extern "C" +{ +#include "libc.h" +} + +#ifdef __MINGW32__ +#include <_mingw.h> +#endif + +template +class version_t { + constexpr static auto buflen() noexcept { + unsigned int len = 2; // "." + if (Major == 0) + len++; + else + for (auto n = Major; n; len++, n /= 10); + + if (Minor == 0) + len++; + else + for (auto n = Minor; n; len++, n /= 10); + return len; + } + + char buf[buflen()] = {}; + +public: + constexpr version_t() noexcept { + auto ptr = buf + buflen(); + *--ptr = '\0'; + + if (Minor == 0) { + *--ptr = '0'; + } else { + for (auto n = Minor; n; n /= 10) + *--ptr = "0123456789"[n % 10]; + } + *--ptr = '.'; + if (Major == 0) { + *--ptr = '0'; + } else { + for (auto n = Major; n; n /= 10) + *--ptr = "0123456789"[n % 10]; + } + } + + constexpr operator const char *() const { return buf; } +}; + +template +constexpr version_t version; + +extern "C" +const char* ffDetectLibc(FFLibcResult* result) +{ +#ifdef _UCRT + result->name = "ucrt"; +#else + result->name = "msvcrt"; +#endif + + result->version = version<(__MSVCRT_VERSION__ >> 8), (__MSVCRT_VERSION__ & 8)>; + return NULL; +} diff --git a/src/fastfetch.c b/src/fastfetch.c index bff53eff7..d00d05120 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -468,7 +468,7 @@ static inline void printCommandHelp(const char* command) } else if(ffStrEqualsIgnCase(command, "version-format")) { - constructAndPrintCommandHelpFormat("version", "{1} {2}{3} ({5})", 8, + constructAndPrintCommandHelpFormat("version", "{1} {2}{3} ({5})", 9, "Project name", "Version", "Version tweak", diff --git a/src/modules/version/version.c b/src/modules/version/version.c index 54781eb62..a5a6ca7d8 100644 --- a/src/modules/version/version.c +++ b/src/modules/version/version.c @@ -1,10 +1,11 @@ #include "common/printing.h" #include "common/jsonconfig.h" +#include "detection/libc/libc.h" #include "detection/version/version.h" #include "modules/version/version.h" #include "util/stringUtils.h" -#define FF_VERSION_NUM_FORMAT_ARGS 8 +#define FF_VERSION_NUM_FORMAT_ARGS 9 void ffPrintVersion(FFVersionOptions* options) { @@ -18,6 +19,18 @@ void ffPrintVersion(FFVersionOptions* options) } else { + FFLibcResult libcResult; + FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate(); + if (!ffDetectLibc(&libcResult)) + { + ffStrbufSetS(&buf, libcResult.name); + if (libcResult.version) + { + ffStrbufAppendC(&buf, ' '); + ffStrbufAppendS(&buf, libcResult.version); + } + } + ffPrintFormat(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_VERSION_NUM_FORMAT_ARGS, (FFformatarg[]){ {FF_FORMAT_ARG_TYPE_STRING, result.projectName}, {FF_FORMAT_ARG_TYPE_STRING, result.version}, @@ -27,6 +40,7 @@ void ffPrintVersion(FFVersionOptions* options) {FF_FORMAT_ARG_TYPE_STRING, result.cmakeBuiltType}, {FF_FORMAT_ARG_TYPE_STRING, result.compileTime}, {FF_FORMAT_ARG_TYPE_STRING, result.compiler}, + {FF_FORMAT_ARG_TYPE_STRBUF, &buf}, }); } } @@ -83,4 +97,20 @@ void ffGenerateVersionJson(FF_MAYBE_UNUSED FFVersionOptions* options, yyjson_mut yyjson_mut_obj_add_str(doc, obj, "compileTime", result.compileTime); yyjson_mut_obj_add_str(doc, obj, "compiler", result.compiler); yyjson_mut_obj_add_bool(doc, obj, "debugMode", result.debugMode); + + FFLibcResult libcResult; + if (ffDetectLibc(&libcResult)) + { + yyjson_mut_obj_add_null(doc, obj, "libc"); + } + else + { + FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreateS(libcResult.name); + if (libcResult.version) + { + ffStrbufAppendC(&buf, ' '); + ffStrbufAppendS(&buf, libcResult.version); + } + yyjson_mut_obj_add_strbuf(doc, obj, "libc", &buf); + } }