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
This commit is contained in:
PatTheMav 2023-05-19 15:05:07 +02:00 • committed by Lain
commit 2966c4030e
15 changed files with 43 additions and 11 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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;

View file

@ -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;

View file

@ -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

View file

@ -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)

View file

@ -40,6 +40,8 @@ target_sources(
util/windows/window-helpers.h)
target_compile_options(libobs PRIVATE $<$<COMPILE_LANGUAGE:C,CXX>:/EHc->)
set_source_files_properties(obs-win-crash-handler.c PROPERTIES COMPILE_DEFINITIONS
OBS_VERSION="${OBS_VERSION_CANONICAL}")
target_link_libraries(
libobs

View file

@ -38,6 +38,7 @@
#include "obs.h"
#include <obsversion.h>
#include <caption/caption.h>
/* Custom helpers for the UUID hash table */

View file

@ -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

5
libobs/obsversion.c.in Normal file
View file

@ -0,0 +1,5 @@
#include <obsversion.h>
const char *OBS_VERSION = "@OBS_VERSION@";
const char *OBS_VERSION_CANONICAL = "@OBS_VERSION_CANONICAL@";
const char *OBS_COMMIT = "@OBS_COMMIT@";

5
libobs/obsversion.h Normal file
View file

@ -0,0 +1,5 @@
#pragma once
extern const char *OBS_VERSION;
extern const char *OBS_VERSION_CANONICAL;
extern const char *OBS_COMMIT;

View file

@ -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);

View file

@ -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);

View file

@ -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;

View file

@ -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")