From 2966c4030ed77258ffe2125e19f2d7d1f66e7421 Mon Sep 17 00:00:00 2001 From: PatTheMav Date: Fri, 19 May 2023 15:05:07 +0200 Subject: [PATCH] libobs: Use static library for version string information Switching to a static library that contains version information as const char strings has multiple benefits: * The version information provided externally via compiler definitions will fail compilation early if malformed * An updated version string (which will happen with every commit) will not invalidate existing compilation units, because only the static library is affected by the change * An update of the version change just requires a recompilation of the static library and a linker update * An update of the version will _not_ infect the rest of the codebase (as it does currently, because everything includes obsconfig.h one way or another) * Other modules which used the macro definition directly have been updated as much as possible to use the proper getter method from `libobs` instead (some Windows-specific modules use preprocessor string composition, the value has been added as a compiler definition directly in those cases) * Because the impact of a version change due to a commit hash change is limited to the static library, ccache hit rates should be improved considerably --- UI/cmake/legacy.cmake | 1 + UI/cmake/os-windows.cmake | 1 + UI/obs-app.cpp | 2 +- UI/window-basic-about.cpp | 2 +- libobs/CMakeLists.txt | 9 +++++++++ libobs/cmake/legacy.cmake | 13 ++++++++++++- libobs/cmake/os-windows.cmake | 2 ++ libobs/obs-internal.h | 1 + libobs/obsconfig.h.in | 4 ---- libobs/obsversion.c.in | 5 +++++ libobs/obsversion.h | 5 +++++ plugins/obs-ffmpeg/obs-ffmpeg-hls-mux.c | 2 +- plugins/obs-outputs/flv-mux.c | 2 +- plugins/obs-outputs/ftl-stream.c | 2 +- plugins/win-capture/CMakeLists.txt | 3 ++- 15 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 libobs/obsversion.c.in create mode 100644 libobs/obsversion.h diff --git a/UI/cmake/legacy.cmake b/UI/cmake/legacy.cmake index 1918b3515..0650d3d3a 100644 --- a/UI/cmake/legacy.cmake +++ b/UI/cmake/legacy.cmake @@ -370,6 +370,7 @@ if(OS_WINDOWS) target_compile_definitions(obs PRIVATE UNICODE _UNICODE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_WARNINGS PSAPI_VERSION=2) + set_source_files_properties(update/win-update.cpp PROPERTIES COMPILE_DEFINITIONS OBS_COMMIT="${OBS_COMMIT}") if(MSVC) target_link_options(obs PRIVATE "LINKER:/IGNORE:4098" "LINKER:/IGNORE:4099") target_link_libraries(obs PRIVATE OBS::w32-pthreads) diff --git a/UI/cmake/os-windows.cmake b/UI/cmake/os-windows.cmake index 5b443c2e8..67cb6c817 100644 --- a/UI/cmake/os-windows.cmake +++ b/UI/cmake/os-windows.cmake @@ -37,6 +37,7 @@ add_library(OBS::update-helpers ALIAS obs-update-helpers) target_sources(obs-update-helpers INTERFACE win-update/win-update-helpers.cpp win-update/win-update-helpers.hpp) target_include_directories(obs-update-helpers INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/win-update") +set_source_files_properties(update/win-update.cpp PROPERTIES COMPILE_DEFINITIONS OBS_COMMIT="${OBS_COMMIT}") add_subdirectory(win-update/updater) diff --git a/UI/obs-app.cpp b/UI/obs-app.cpp index 0f8c423e7..4d5d61225 100644 --- a/UI/obs-app.cpp +++ b/UI/obs-app.cpp @@ -1743,7 +1743,7 @@ string OBSApp::GetVersionString(bool platform) const stringstream ver; #ifdef HAVE_OBSCONFIG_H - ver << OBS_VERSION; + ver << obs_get_version_string(); #else ver << LIBOBS_API_MAJOR_VER << "." << LIBOBS_API_MINOR_VER << "." << LIBOBS_API_PATCH_VER; diff --git a/UI/window-basic-about.cpp b/UI/window-basic-about.cpp index 90e146d34..317d072b9 100644 --- a/UI/window-basic-about.cpp +++ b/UI/window-basic-about.cpp @@ -24,7 +24,7 @@ OBSAbout::OBSAbout(QWidget *parent) : QDialog(parent), ui(new Ui::OBSAbout) bitness = " (64 bit)"; #ifdef HAVE_OBSCONFIG_H - ver += OBS_VERSION; + ver += obs_get_version_string(); #else ver += LIBOBS_API_MAJOR_VER + "." + LIBOBS_API_MINOR_VER + "." + LIBOBS_API_PATCH_VER; diff --git a/libobs/CMakeLists.txt b/libobs/CMakeLists.txt index d2e26710d..d7bb1839c 100644 --- a/libobs/CMakeLists.txt +++ b/libobs/CMakeLists.txt @@ -2,6 +2,14 @@ cmake_minimum_required(VERSION 3.16...3.25) legacy_check() +# cmake-format: off +add_library(libobs-version STATIC EXCLUDE_FROM_ALL) +add_library(OBS::libobs-version ALIAS libobs-version) +# cmake-format: on +configure_file(obsversion.c.in obsversion.c @ONLY) +target_sources(libobs-version PRIVATE obsversion.c obsversion.h) +set_property(TARGET libobs-version PROPERTY FOLDER core) + add_library(libobs SHARED) add_library(OBS::libobs ALIAS libobs) @@ -226,6 +234,7 @@ target_link_libraries( libobs PRIVATE OBS::caption OBS::uthash + OBS::libobs-version FFmpeg::avcodec FFmpeg::avformat FFmpeg::avutil diff --git a/libobs/cmake/legacy.cmake b/libobs/cmake/legacy.cmake index fcbacd7b9..e8458591c 100644 --- a/libobs/cmake/legacy.cmake +++ b/libobs/cmake/legacy.cmake @@ -4,6 +4,15 @@ endif() project(libobs) +# cmake-format: off +add_library(libobs-version STATIC EXCLUDE_FROM_ALL) +add_library(OBS::libobs-version ALIAS libobs-version) +# cmake-format: on +configure_file(obsversion.c.in obsversion.c @ONLY) +target_sources(libobs-version PRIVATE obsversion.c obsversion.h) +target_include_directories(libobs-version PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +set_property(TARGET libobs-version PROPERTY FOLDER core) + find_package(Threads REQUIRED) find_package( FFmpeg REQUIRED @@ -249,6 +258,7 @@ target_link_libraries( Jansson::Jansson OBS::caption OBS::uthash + OBS::libobs-version ZLIB::ZLIB PUBLIC Threads::Threads) @@ -304,7 +314,8 @@ if(OS_WINDOWS) audio-monitoring/win32/wasapi-monitoring-available.c) target_compile_definitions(libobs PRIVATE UNICODE _UNICODE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_WARNINGS) - + set_source_files_properties(obs-win-crash-handler.c PROPERTIES COMPILE_DEFINITIONS + OBS_VERSION="${OBS_VERSION_CANONICAL}") target_link_libraries(libobs PRIVATE dxgi Avrt Dwmapi winmm Rpcrt4) if(MSVC) diff --git a/libobs/cmake/os-windows.cmake b/libobs/cmake/os-windows.cmake index fc2c0548f..dd3b77da3 100644 --- a/libobs/cmake/os-windows.cmake +++ b/libobs/cmake/os-windows.cmake @@ -40,6 +40,8 @@ target_sources( util/windows/window-helpers.h) target_compile_options(libobs PRIVATE $<$:/EHc->) +set_source_files_properties(obs-win-crash-handler.c PROPERTIES COMPILE_DEFINITIONS + OBS_VERSION="${OBS_VERSION_CANONICAL}") target_link_libraries( libobs diff --git a/libobs/obs-internal.h b/libobs/obs-internal.h index 46402a9f8..2cd6faa6d 100644 --- a/libobs/obs-internal.h +++ b/libobs/obs-internal.h @@ -38,6 +38,7 @@ #include "obs.h" +#include #include /* Custom helpers for the UUID hash table */ diff --git a/libobs/obsconfig.h.in b/libobs/obsconfig.h.in index d34ab0fe0..b4de06623 100644 --- a/libobs/obsconfig.h.in +++ b/libobs/obsconfig.h.in @@ -1,13 +1,9 @@ #pragma once -#define OBS_VERSION "@OBS_VERSION@" - -#cmakedefine OBS_VERSION_CANONICAL "@OBS_VERSION_CANONICAL@" #cmakedefine OBS_DATA_PATH "@OBS_DATA_PATH@" #cmakedefine OBS_PLUGIN_PATH "@OBS_PLUGIN_PATH@" #cmakedefine OBS_PLUGIN_DESTINATION "@OBS_PLUGIN_DESTINATION@" -#cmakedefine OBS_COMMIT "@OBS_COMMIT@" #cmakedefine GIO_FOUND #cmakedefine PULSEAUDIO_FOUND #cmakedefine XCB_XINPUT_FOUND diff --git a/libobs/obsversion.c.in b/libobs/obsversion.c.in new file mode 100644 index 000000000..d03d8cbcc --- /dev/null +++ b/libobs/obsversion.c.in @@ -0,0 +1,5 @@ +#include + +const char *OBS_VERSION = "@OBS_VERSION@"; +const char *OBS_VERSION_CANONICAL = "@OBS_VERSION_CANONICAL@"; +const char *OBS_COMMIT = "@OBS_COMMIT@"; diff --git a/libobs/obsversion.h b/libobs/obsversion.h new file mode 100644 index 000000000..7f1cdafc8 --- /dev/null +++ b/libobs/obsversion.h @@ -0,0 +1,5 @@ +#pragma once + +extern const char *OBS_VERSION; +extern const char *OBS_VERSION_CANONICAL; +extern const char *OBS_COMMIT; diff --git a/plugins/obs-ffmpeg/obs-ffmpeg-hls-mux.c b/plugins/obs-ffmpeg/obs-ffmpeg-hls-mux.c index 0ad4bd786..fdd28713b 100644 --- a/plugins/obs-ffmpeg/obs-ffmpeg-hls-mux.c +++ b/plugins/obs-ffmpeg/obs-ffmpeg-hls-mux.c @@ -136,7 +136,7 @@ bool ffmpeg_hls_mux_start(void *data) dstr_copy(&stream->muxer_settings, "method=PUT http_persistent=1 ignore_io_errors=1 "); dstr_catf(&stream->muxer_settings, "http_user_agent=libobs/%s", - OBS_VERSION); + obs_get_version_string()); vencoder = obs_output_get_video_encoder(stream->output); settings = obs_encoder_get_settings(vencoder); diff --git a/plugins/obs-outputs/flv-mux.c b/plugins/obs-outputs/flv-mux.c index 3d29934fb..bc2eb5209 100644 --- a/plugins/obs-outputs/flv-mux.c +++ b/plugins/obs-outputs/flv-mux.c @@ -167,7 +167,7 @@ static void build_flv_meta_data(obs_output_t *context, uint8_t **output, dstr_printf(&encoder_name, "%s (libobs version ", MODULE_NAME); #ifdef HAVE_OBSCONFIG_H - dstr_cat(&encoder_name, OBS_VERSION); + dstr_cat(&encoder_name, obs_get_version_string()); #else dstr_catf(&encoder_name, "%d.%d.%d", LIBOBS_API_MAJOR_VER, LIBOBS_API_MINOR_VER, LIBOBS_API_PATCH_VER); diff --git a/plugins/obs-outputs/ftl-stream.c b/plugins/obs-outputs/ftl-stream.c index 053bc3a1a..7200ea9f8 100644 --- a/plugins/obs-outputs/ftl-stream.c +++ b/plugins/obs-outputs/ftl-stream.c @@ -1074,7 +1074,7 @@ static int init_connect(struct ftl_stream *stream) stream->params.audio_codec = FTL_AUDIO_OPUS; stream->params.ingest_hostname = stream->path.array; stream->params.vendor_name = "OBS Studio"; - stream->params.vendor_version = OBS_VERSION; + stream->params.vendor_version = obs_get_version_string(); stream->params.peak_kbps = stream->peak_kbps < 0 ? 0 : stream->peak_kbps; diff --git a/plugins/win-capture/CMakeLists.txt b/plugins/win-capture/CMakeLists.txt index b366835b5..21ef2ff53 100644 --- a/plugins/win-capture/CMakeLists.txt +++ b/plugins/win-capture/CMakeLists.txt @@ -51,7 +51,8 @@ if(MSVC) target_link_options(win-capture PRIVATE "LINKER:/IGNORE:4098") endif() -target_compile_definitions(win-capture PRIVATE UNICODE _UNICODE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_WARNINGS) +target_compile_definitions(win-capture PRIVATE UNICODE _UNICODE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_WARNINGS + OBS_VERSION="${OBS_VERSION_CANONICAL}") set_property(GLOBAL APPEND PROPERTY OBS_MODULE_LIST "win-capture")