GPU: support embedding amdgpu.ids into fastfetch
Some checks are pending
CI / spellcheck (push) Waiting to run
CI / No-features-test (push) Waiting to run
CI / Linux-amd64 (push) Waiting to run
CI / Linux-aarch64 (push) Waiting to run
CI / Linux-armv7l (push) Waiting to run
CI / Linux-armv6l (push) Waiting to run
CI / Linux-riscv64 (push) Waiting to run
CI / Linux-ppc64le (push) Waiting to run
CI / Linux-s390x (push) Waiting to run
CI / Musl-amd64 (push) Waiting to run
CI / macOS-universal (push) Waiting to run
CI / SunOS-amd64 (push) Waiting to run
CI / FreeBSD-amd64 (push) Waiting to run
CI / DragonFly-amd64 (push) Waiting to run
CI / OpenBSD-amd64 (push) Waiting to run
CI / NetBSD-amd64 (push) Waiting to run
CI / Windows-amd64 (push) Waiting to run
CI / Release (push) Blocked by required conditions

This commit is contained in:
李通洲 2025-02-13 10:43:11 +08:00
parent 379c39d8b5
commit b8e25bdc4e
8 changed files with 116 additions and 28 deletions

View File

@ -94,6 +94,7 @@ option(SET_TWEAK "Add tweak to project version" ON) # This is set to off by gith
option(IS_MUSL "Build with musl libc" OFF) # Used by Github Actions
option(INSTALL_LICENSE "Install license into /usr/share/licenses" ON)
option(ENABLE_EMBEDDED_PCIIDS "Embed pci.ids into fastfetch, requires `python`" OFF)
option(ENABLE_EMBEDDED_AMDGPUIDS "Embed amdgpu.ids into fastfetch, requires `python`" OFF)
set(BINARY_LINK_TYPE_OPTIONS dlopen dynamic static)
set(BINARY_LINK_TYPE dlopen CACHE STRING "How to link fastfetch")
@ -289,6 +290,26 @@ if(ENABLE_EMBEDDED_PCIIDS AND NOT EXISTS "${PROJECT_BINARY_DIR}/fastfetch_pciids
endif()
endif()
if(ENABLE_EMBEDDED_AMDGPUIDS AND NOT EXISTS "${PROJECT_BINARY_DIR}/fastfetch_amdgpuids.c.inc")
if(Python_FOUND)
if(NOT EXISTS "${PROJECT_BINARY_DIR}/amdgpu.ids")
message(STATUS "'${PROJECT_BINARY_DIR}/amdgpu.ids' is missing, downloading...")
file(DOWNLOAD "https://gitlab.freedesktop.org/mesa/drm/-/raw/main/data/amdgpu.ids" "${PROJECT_BINARY_DIR}/amdgpu.ids")
endif()
message(STATUS "Generating 'fastfetch_amdgpuids.c.inc'")
execute_process(COMMAND ${Python_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/scripts/gen-amdgpuids.py" "${PROJECT_BINARY_DIR}/amdgpu.ids"
OUTPUT_FILE "${PROJECT_BINARY_DIR}/fastfetch_amdgpuids.c.inc"
RESULT_VARIABLE PYTHON_AMDGPUIDS_RETCODE)
if(NOT PYTHON_AMDGPUIDS_RETCODE EQUAL 0)
file(REMOVE "${PROJECT_BINARY_DIR}/fastfetch_amdgpuids.c.inc")
message(FATAL_ERROR "Failed to generate 'fastfetch_amdgpuids.c.inc'")
endif()
else()
message(WARNING "Python3 is not found, 'fastfetch_amdgpuids.c.inc' will not be generated")
set(ENABLE_EMBEDDED_AMDGPUIDS OFF)
endif()
endif()
if(Python_FOUND)
message(STATUS "Generating 'fastfetch.1'")
execute_process(COMMAND ${Python_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/scripts/gen-man.py"
@ -1517,6 +1538,9 @@ endif()
if(ENABLE_EMBEDDED_PCIIDS)
target_compile_definitions(libfastfetch PRIVATE FF_HAVE_EMBEDDED_PCIIDS=1)
endif()
if(ENABLE_EMBEDDED_AMDGPUIDS)
target_compile_definitions(libfastfetch PRIVATE FF_HAVE_EMBEDDED_AMDGPUIDS=1)
endif()
if(LINUX)
target_link_libraries(libfastfetch

43
scripts/gen-amdgpuids.py Executable file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env python3
import sys
def main(amdgpu_ids_path: str):
with open(amdgpu_ids_path, 'r') as f:
full_text = f.read()
products = []
for line in full_text.split('\n'):
if not line or line[0] == '#' or not ',\t' in line:
continue
device, revision, name = line.split(',\t', maxsplit=2)
products.append((device, revision, name))
code = """\
// SPDX-License-Identifier: MIT
// https://opensource.org/license/mit
// Generated from https://gitlab.freedesktop.org/mesa/drm/-/raw/main/data/amdgpu.ids
#include <stdint.h>
#include <stddef.h>
typedef struct FFArmGpuProduct
{
const uint32_t id; // device << 8 | revision
const char* name;
} FFArmGpuProduct;
const FFArmGpuProduct ffAmdGpuProducts[] = {
"""
for device, revision, name in products:
code += f" {{ 0x{device} << 8 | 0x{revision}, \"{name.replace('"', '\\"')}\" }},\n"
code += "};\n"
print(code)
if __name__ == '__main__':
len(sys.argv) == 2 or sys.exit('Usage: gen-amdgpuids.py </path/to/amdgpu.ids>')
main(sys.argv[1])

View File

@ -50,4 +50,5 @@ const char* ffGPUGetVendorString(unsigned vendorId);
#if defined(__linux__) || defined(__FreeBSD__) || defined(__sun) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__HAIKU__)
void ffGPUFillVendorAndName(uint8_t subclass, uint16_t vendor, uint16_t device, FFGPUResult* gpu);
void ffGPUQueryAmdGpuName(uint16_t deviceId, uint8_t revisionId, FFGPUResult* gpu);
#endif

View File

@ -1,7 +1,6 @@
#include "gpu_driver_specific.h"
#include "common/io/io.h"
#include "common/properties.h"
#include <sys/pciio.h>
#include <fcntl.h>
@ -76,11 +75,7 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus)
if (gpu->name.length == 0)
{
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_AMD)
{
char query[32];
snprintf(query, ARRAY_SIZE(query), "%X,\t%X,", (unsigned) pc->pc_device, (unsigned) pc->pc_revid);
ffParsePropFileData("libdrm/amdgpu.ids", query, &gpu->name);
}
ffGPUQueryAmdGpuName(pc->pc_device, pc->pc_revid, gpu);
if (gpu->name.length == 0)
ffGPUFillVendorAndName(pc->pc_subclass, pc->pc_vendor, pc->pc_device, gpu);
}

View File

@ -2,7 +2,6 @@
#ifdef FF_HAVE_PCIACCESS
#include "common/properties.h"
#include "common/io/io.h"
#include "common/library.h"
@ -43,11 +42,7 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist*
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_AMD)
{
char query[32];
snprintf(query, sizeof(query), "%X,\t%X,", (unsigned) dev->device_id, (unsigned) dev->revision);
ffParsePropFileData("libdrm/amdgpu.ids", query, &gpu->name);
}
ffGPUQueryAmdGpuName(dev->device_id, dev->revision, gpu);
if (gpu->name.length == 0)
{

View File

@ -4,7 +4,6 @@
#include "detection/gpu/gpu_driver_specific.h"
#include "common/io/io.h"
#include "common/library.h"
#include "common/properties.h"
#include "util/stringUtils.h"
#include "util/mallocHelper.h"
@ -491,15 +490,7 @@ static const char* detectPci(const FFGPUOptions* options, FFlist* gpus, FFstrbuf
char* pend;
uint64_t revision = strtoul(buffer->chars, &pend, 16);
if (pend != buffer->chars)
{
char query[32];
snprintf(query, ARRAY_SIZE(query), "%X,\t%X,", (unsigned) deviceId, (unsigned) revision);
#ifdef FF_CUSTOM_AMDGPU_IDS_PATH
ffParsePropFile(FF_STR(FF_CUSTOM_AMDGPU_IDS_PATH), query, &gpu->name);
#else
ffParsePropFileData("libdrm/amdgpu.ids", query, &gpu->name);
#endif
}
ffGPUQueryAmdGpuName((uint16_t) deviceId, (uint8_t) revision, gpu);
}
ffStrbufSubstrBefore(deviceDir, drmDirPathLength);
}

View File

@ -1,5 +1,6 @@
#include "gpu.h"
#include "common/io/io.h"
#include "common/properties.h"
#include <stdlib.h>
#ifdef __FreeBSD__
@ -16,6 +17,9 @@
#if FF_HAVE_EMBEDDED_PCIIDS
#include "fastfetch_pciids.c.inc"
#endif
#if FF_HAVE_EMBEDDED_AMDGPUIDS
#include "fastfetch_amdgpuids.c.inc"
#endif
#define FF_STR_INDIR(x) #x
#define FF_STR(x) FF_STR_INDIR(x)
@ -191,3 +195,43 @@ void ffGPUFillVendorAndName(uint8_t subclass, uint16_t vendor, uint16_t device,
#endif
return parsePciIdsFile(loadPciIds(), subclass, vendor, device, gpu);
}
#if FF_HAVE_EMBEDDED_AMDGPUIDS
static inline int amdGpuCmp(const uint32_t* key, const FFArmGpuProduct* element)
{
// Maximum value of *key is 0x00FFFFFF. `(int) *key` should never overflow
return (int) *key - (int) element->id;
}
static bool loadAmdGpuIdsInc(uint16_t deviceId, uint8_t revision, FFGPUResult* gpu)
{
uint32_t key = (deviceId << 8u) | revision;
FFArmGpuProduct* product = bsearch(&key, ffAmdGpuProducts, ARRAY_SIZE(ffAmdGpuProducts), sizeof(*ffAmdGpuProducts), (void*) amdGpuCmp);
if (product)
{
ffStrbufSetS(&gpu->name, product->name);
return true;
}
return false;
}
#endif
static void parseAmdGpuIdsFile(uint16_t deviceId, uint8_t revision, FFGPUResult* gpu)
{
char query[32];
snprintf(query, ARRAY_SIZE(query), "%X,\t%X,", (unsigned) deviceId, (unsigned) revision);
#ifdef FF_CUSTOM_AMDGPU_IDS_PATH
ffParsePropFile(FF_STR(FF_CUSTOM_AMDGPU_IDS_PATH), query, &gpu->name);
#else
ffParsePropFileData("libdrm/amdgpu.ids", query, &gpu->name);
#endif
}
void ffGPUQueryAmdGpuName(uint16_t deviceId, uint8_t revisionId, FFGPUResult* gpu)
{
#if FF_HAVE_EMBEDDED_AMDGPUIDS
bool ok = loadAmdGpuIdsInc(deviceId, revisionId, gpu);
if (ok) return;
#endif
return parseAmdGpuIdsFile(deviceId, revisionId, gpu);
}

View File

@ -1,5 +1,4 @@
#include "gpu.h"
#include "common/properties.h"
#include "common/io/io.h"
#include "common/processing.h"
@ -65,11 +64,7 @@ const char* ffDetectGPUImpl(FF_MAYBE_UNUSED const FFGPUOptions* options, FFlist*
gpu->frequency = FF_GPU_FREQUENCY_UNSET;
if (gpu->vendor.chars == FF_GPU_VENDOR_NAME_AMD)
{
char query[32];
snprintf(query, ARRAY_SIZE(query), "%X,\t%X,", (unsigned) deviceId, (unsigned) revision);
ffParsePropFileData("libdrm/amdgpu.ids", query, &gpu->name);
}
ffGPUQueryAmdGpuName((uint16_t) deviceId, (uint8_t) revision, gpu);
if (gpu->name.length == 0)
{