CMake: be able to build as a dynamic linked binary

This commit is contained in:
Carter Li
2024-05-24 14:40:14 +08:00
parent 4b33e1b691
commit 3d48de8c98
5 changed files with 46 additions and 2 deletions
+30
View File
@@ -77,6 +77,13 @@ option(BUILD_TESTS "Build tests" OFF) # Also create test executables
option(SET_TWEAK "Add tweak to project version" ON) # This is set to off by github actions for release builds
option(IS_MUSL "Build with musl libc" OFF) # Used by Github Actions
set(BINARY_LINK_TYPE_OPTIONS dlopen dynamic static)
set(BINARY_LINK_TYPE dlopen CACHE STRING "How to link fastfetch")
set_property(CACHE BINARY_LINK_TYPE PROPERTY STRINGS ${BINARY_LINK_TYPE_OPTIONS})
if(NOT BINARY_LINK_TYPE IN_LIST BINARY_LINK_TYPE_OPTIONS)
message(FATAL_ERROR "BINARY_LINK_TYPE must be one of ${BINARY_LINK_TYPE_OPTIONS}")
endif()
if (LINUX)
set(CUSTOM_PCI_IDS_PATH "" CACHE STRING "Custom path to file pci.ids, defaults to `/usr/share/hwdata/pci.ids`")
set(CUSTOM_AMDGPU_IDS_PATH "" CACHE STRING "Custom path to file amdgpu.ids, defaults to `/usr/share/libdrm/amdgpu.ids`")
@@ -817,6 +824,11 @@ if(NOT "${CUSTOM_OS_RELEASE_PATH}" STREQUAL "")
target_compile_definitions(libfastfetch PRIVATE FF_CUSTOM_OS_RELEASE_PATH=${CUSTOM_OS_RELEASE_PATH})
endif()
if(NOT BINARY_LINK_TYPE STREQUAL "dlopen")
message(STATUS "Enabling custom link type: ${BINARY_LINK_TYPE}")
target_compile_definitions(libfastfetch PRIVATE FF_DISABLE_DLOPEN=1)
endif()
function(ff_lib_enable VARNAME PKGCONFIG_NAMES CMAKE_NAME)
if(NOT ENABLE_${VARNAME})
return()
@@ -845,6 +857,10 @@ function(ff_lib_enable VARNAME PKGCONFIG_NAMES CMAKE_NAME)
target_compile_definitions(libfastfetch PRIVATE FF_HAVE_${VARNAME}=1)
target_include_directories(libfastfetch PRIVATE ${${VARNAME}_INCLUDE_DIRS})
if(NOT BINARY_LINK_TYPE STREQUAL "dlopen")
target_link_libraries(libfastfetch PRIVATE ${${VARNAME}_LIBRARIES})
endif()
foreach(FLAG ${${VARNAME}_CFLAGS_OTHER})
if(FLAG MATCHES "-D.*")
string(SUBSTRING ${FLAG} 2 -1 FLAG)
@@ -853,6 +869,11 @@ function(ff_lib_enable VARNAME PKGCONFIG_NAMES CMAKE_NAME)
endforeach()
endfunction()
if(BINARY_LINK_TYPE STREQUAL "static")
set_target_properties(libfastfetch PROPERTIES LINK_SEARCH_START_STATIC 1)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
endif()
ff_lib_enable(VULKAN
"vulkan"
"Vulkan"
@@ -1037,6 +1058,10 @@ target_link_libraries(libfastfetch
PRIVATE ${CMAKE_DL_LIBS}
)
if(BINARY_LINK_TYPE STREQUAL "static")
set_target_properties(libfastfetch PROPERTIES LINK_SEARCH_END_STATIC 1)
endif()
######################
# Executable targets #
######################
@@ -1093,6 +1118,11 @@ elseif(APPLE)
)
endif()
if(BINARY_LINK_TYPE STREQUAL "static")
target_link_options(fastfetch PRIVATE "-static")
target_link_options(flashfetch PRIVATE "-static")
endif()
###################
# Testing targets #
###################
+1 -1
View File
@@ -1,7 +1,7 @@
#include "fastfetch.h"
#include "common/library.h"
#ifndef FF_LIBRARY_STATIC_BUILD
#ifndef FF_DISABLE_DLOPEN
#include <stdarg.h>
+1 -1
View File
@@ -3,7 +3,7 @@
#include "fastfetch.h"
#include "util/FFcheckmacros.h"
#ifndef FF_LIBRARY_STATIC_BUILD
#ifndef FF_DISABLE_DLOPEN
#if defined(_WIN32)
#include <libloaderapi.h>
+5
View File
@@ -106,11 +106,16 @@ static void getMate(FFstrbuf* result, FFDEOptions* options)
static const char* getXfce4ByLib(FFstrbuf* result)
{
#ifndef FF_DISABLE_DLOPEN
const char* xfce_version_string(void); // from `xfce4/libxfce4util/xfce-misutils.h
FF_LIBRARY_LOAD(xfce4util, NULL, "dlopen libxfce4util" FF_LIBRARY_EXTENSION "failed", "libxfce4util" FF_LIBRARY_EXTENSION, 7);
FF_LIBRARY_LOAD_SYMBOL_MESSAGE(xfce4util, xfce_version_string);
ffStrbufSetS(result, ffxfce_version_string());
return NULL;
#else
FF_UNUSED(result);
return "dlopen is disabled";
#endif
}
static void getXFCE4(FFstrbuf* result, FFDEOptions* options)
+9
View File
@@ -19,6 +19,8 @@ struct FFNvmlData {
const char* ffDetectNvidiaGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverResult result, const char* soName)
{
#ifndef FF_DISABLE_DLOPEN
if (!nvmlData.inited)
{
nvmlData.inited = true;
@@ -127,4 +129,11 @@ const char* ffDetectNvidiaGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverR
}
return NULL;
#else
FF_UNUSED(cond, result, soName);
return "dlopen is disabled";
#endif
}