Skip to content

Commit cc3b225

Browse files
committed
cmake: Get version from git tag
The canonical source of version information is the git tag instead of buildspec. The patch imports the git version decoding scripts from OBS Studio and adapts them for the plugin. The `cmake/common/versionconfig.cmake` file in particular is copied over from obs-studio with a couple of light modifications. First, all the {OBS_,_obs_}* variables are renamed to {PACKAGE,_package_}* because this is not the OBS studio package, and the tag parsing regex expressions are modified to handle a `v` prefix on the tag as is used in this project. Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
1 parent b0a82cc commit cc3b225

File tree

10 files changed

+152
-38
lines changed

10 files changed

+152
-38
lines changed

.github/scripts/Package-Windows.ps1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,18 @@ function Package {
4545

4646
$BuildSpec = Get-Content -Path ${BuildSpecFile} -Raw | ConvertFrom-Json
4747
$ProductName = $BuildSpec.name
48-
$ProductVersion = $BuildSpec.version
4948

50-
$OutputName = "${ProductName}-${ProductVersion}-windows-${Target}"
49+
$GitDescription = Invoke-External git describe --tags --long
50+
$Tokens = ($GitDescription -split '-')
51+
$CommitVersion = $Tokens[0..$($Tokens.Count - 3)] -join '-'
52+
$CommitHash = $($Tokens[-1]).SubString(1)
53+
$CommitDistance = $Tokens[-2]
54+
55+
if ( $CommitDistance -gt 0 ) {
56+
$OutputName = "${ProductName}-${CommitVersion}-${CommitHash}-windows-${Target}"
57+
} else {
58+
$OutputName = "${ProductName}-${CommitVersion}-windows-${Target}"
59+
}
5160

5261
$RemoveArgs = @{
5362
ErrorAction = 'SilentlyContinue'

.github/scripts/build-macos

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ build() {
9090

9191
local product_name
9292
local product_version
93-
read -r product_name product_version <<< \
94-
"$(jq -r '. | {name, version} | join(" ")' ${buildspec_file})"
93+
read -r product_name <<< \
94+
"$(jq -r '.name' ${buildspec_file})"
9595
9696
pushd ${project_root}
9797

.github/scripts/build-ubuntu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ ${_usage_host:-}"
171171

172172
local product_name
173173
local product_version
174-
read -r product_name product_version <<< \
175-
"$(jq -r '. | {name, version} | join(" ")' ${buildspec_file})"
174+
read -r product_name <<< \
175+
"$(jq -r '.name' ${buildspec_file})"
176176
177177
pushd ${project_root}
178178
if (( ! (${skips[(Ie)all]} + ${skips[(Ie)build]}) )) {

.github/scripts/package-macos

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,27 @@ package() {
9494

9595
local product_name
9696
local product_version
97-
read -r product_name product_version <<< \
98-
"$(jq -r '. | {name, version} | join(" ")' ${buildspec_file})"
97+
read -r product_name <<< \
98+
"$(jq -r '.name' ${buildspec_file})"
99+
100+
local commit_version='0.0.0'
101+
local commit_distance='0'
102+
local commit_hash
103+
104+
if [[ -d ${project_root}/.git ]] {
105+
local git_description="$(git describe --tags --long)"
106+
commit_version="${${git_description%-*}%-*}"
107+
commit_hash="${git_description##*-g}"
108+
commit_distance="${${git_description%-*}##*-}"
109+
}
110+
111+
local output_name
112+
if (( commit_distance > 0 )) {
113+
output_name="${product_name}-${commit_version}-${commit_hash}"
114+
} else {
115+
output_name="${product_name}-${commit_version}"
116+
}
99117
100-
local output_name="${product_name}-${product_version}-${host_os}-universal"
101118
102119
if [[ ! -d ${project_root}/release/${config}/${product_name}.plugin ]] {
103120
log_error 'No release artifact found. Run the build script or the CMake install procedure first.'

.github/scripts/package-ubuntu

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,27 @@ ${_usage_host:-}"
143143
}
144144

145145
local product_name
146-
local product_version
147-
read -r product_name product_version <<< \
148-
"$(jq -r '. | {name, version} | join(" ")' ${buildspec_file})"
146+
read -r product_name <<< \
147+
"$(jq -r '.name' ${buildspec_file})"
148+
149+
local commit_version='0.0.0'
150+
local commit_distance='0'
151+
local commit_hash
152+
153+
if [[ -d ${project_root}/.git ]] {
154+
local git_description="$(git describe --tags --long)"
155+
commit_version="${${git_description%-*}%-*}"
156+
commit_hash="${git_description##*-g}"
157+
commit_distance="${${git_description%-*}##*-}"
158+
}
159+
160+
local output_name
161+
if (( commit_distance > 0 )) {
162+
output_name="${project_name}-${commit_version}-${commit_hash}-${target##*-}-ubuntu-gnu"
163+
} else {
164+
output_name="${project_name}-${commit_version}-${target##*-}-ubuntu-gnu"
165+
}
166+
149167
150168
local -a cmake_args=()
151169
if (( _loglevel > 1 )) cmake_args+=(--verbose)
@@ -162,7 +180,6 @@ ${_usage_host:-}"
162180
popd
163181
} else {
164182
log_group "Archiving ${product_name}..."
165-
local output_name="${product_name}-${product_version}-${target##*-}-ubuntu-gnu"
166183
local _tarflags='cJf'
167184
if (( _loglevel > 1 || ${+CI} )) _tarflags="v${_tarflags}"
168185

.github/workflows/build-project.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ jobs:
106106
107107
local product_name
108108
local product_version
109-
read -r product_name product_version <<< \
110-
"$(jq -r '. | {name, version} | join(" ")' buildspec.json)"
109+
read -r product_name <<< \
110+
"$(jq -r '.name' buildspec.json)"
111+
read -r product_version <<< \
112+
"$(git describe --always --tags --dirty=-modified)"
111113
112114
print "pluginName=${product_name}" >> $GITHUB_OUTPUT
113115
print "pluginVersion=${product_version}" >> $GITHUB_OUTPUT
@@ -197,8 +199,10 @@ jobs:
197199
: Set Up Environment 🔧
198200
if [[ "${RUNNER_DEBUG}" ]]; then set -x; fi
199201
200-
read -r product_name product_version <<< \
201-
"$(jq -r '. | {name, version} | join(" ")' buildspec.json)"
202+
read -r product_name <<< \
203+
"$(jq -r '.name' buildspec.json)"
204+
read -r product_version <<< \
205+
"$(git describe --always --tags --dirty=-modified)"
202206
203207
echo "pluginName=${product_name}" >> $GITHUB_OUTPUT
204208
echo "pluginVersion=${product_version}" >> $GITHUB_OUTPUT
@@ -272,7 +276,9 @@ jobs:
272276
273277
$BuildSpec = Get-Content -Path buildspec.json -Raw | ConvertFrom-Json
274278
$ProductName = $BuildSpec.name
275-
$ProductVersion = $BuildSpec.version
279+
$GitDescription = Invoke-External git describe --tags --long
280+
$Tokens = ($GitDescription -split '-')
281+
$ProductVersion = $Tokens[0..$($Tokens.Count - 3)] -join '-'
276282
277283
"pluginName=${ProductName}" >> $env:GITHUB_OUTPUT
278284
"pluginVersion=${ProductVersion}" >> $env:GITHUB_OUTPUT

CMakeLists.txt

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@ cmake_minimum_required(VERSION 3.28...3.30)
22

33
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/common/bootstrap.cmake" NO_POLICY_SCOPE)
44

5-
project(${_name} VERSION ${_version})
6-
7-
# Generate a version number from the git tag that can be embedded in the binary
8-
execute_process(
9-
COMMAND git describe --tags --dirty
10-
OUTPUT_VARIABLE _git_version
11-
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
12-
OUTPUT_STRIP_TRAILING_WHITESPACE
13-
)
14-
add_compile_definitions(PLUGIN_NAME="${_name}" PLUGIN_VERSION="${_git_version}")
5+
project(${PLUGIN_NAME} VERSION ${PLUGIN_VERSION_CANONICAL})
6+
7+
add_compile_definitions(PLUGIN_NAME="${PLUGIN_NAME}" PLUGIN_VERSION="${PLUGIN_VERSION}")
158

169
option(ENABLE_FRONTEND_API "Use obs-frontend-api for UI functionality" ON)
1710
option(ENABLE_QT "Use Qt functionality" ON)
@@ -121,7 +114,7 @@ if(ENABLE_JOYSTICK)
121114
add_compile_definitions(ENABLE_JOYSTICK SDL_SUPPORTED)
122115
endif()
123116

124-
set_target_properties_plugin(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME ${_name})
117+
set_target_properties_plugin(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME ${PLUGIN_NAME})
125118

126119
if(OS_WINDOWS)
127120
configure_file(installer-Windows.iss.in ${CMAKE_BINARY_DIR}/installer-Windows.iss)

buildspec.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
},
5757
"name": "obs-ptz",
5858
"displayName": "PTZ Camera Control for OBS",
59-
"version": "0.18.0",
6059
"author": "Grant Likely",
6160
"website": "https://obsproject.com/forum/resources/ptz-controls.1284",
6261
"email": "grant.likely@secretlab.ca"

cmake/common/bootstrap.cmake

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,15 @@ string(JSON _name GET ${buildspec} name)
4949
string(JSON _website GET ${buildspec} website)
5050
string(JSON _author GET ${buildspec} author)
5151
string(JSON _email GET ${buildspec} email)
52-
string(JSON _version GET ${buildspec} version)
5352
string(JSON _bundleId GET ${buildspec} platformConfig macos bundleId)
5453

54+
set(PLUGIN_NAME ${_name})
5555
set(PLUGIN_AUTHOR ${_author})
5656
set(PLUGIN_WEBSITE ${_website})
5757
set(PLUGIN_EMAIL ${_email})
58-
set(PLUGIN_VERSION ${_version})
5958
set(MACOS_BUNDLEID ${_bundleId})
6059

61-
string(REPLACE "." ";" _version_canonical "${_version}")
62-
list(GET _version_canonical 0 PLUGIN_VERSION_MAJOR)
63-
list(GET _version_canonical 1 PLUGIN_VERSION_MINOR)
64-
list(GET _version_canonical 2 PLUGIN_VERSION_PATCH)
65-
unset(_version_canonical)
66-
60+
include(versionconfig)
6761
include(buildnumber)
6862
include(osconfig)
6963

cmake/common/versionconfig.cmake

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# OBS CMake common version helper module
2+
3+
include_guard(GLOBAL)
4+
5+
set(_plugin_version ${_plugin_default_version})
6+
set(_plugin_version_canonical ${_plugin_default_version})
7+
8+
# Attempt to automatically discover expected OBS version
9+
if(NOT DEFINED PLUGIN_VERSION_OVERRIDE AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
10+
execute_process(
11+
COMMAND git describe --always --tags --dirty=-modified
12+
OUTPUT_VARIABLE _plugin_version
13+
ERROR_VARIABLE _git_describe_err
14+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
15+
RESULT_VARIABLE _plugin_version_result
16+
OUTPUT_STRIP_TRAILING_WHITESPACE
17+
)
18+
19+
if(_git_describe_err)
20+
message(FATAL_ERROR "Could not fetch OBS version tag from git.\n" ${_git_describe_err})
21+
endif()
22+
23+
if(_plugin_version_result EQUAL 0)
24+
string(REGEX REPLACE "v?([0-9]+)\\.([0-9]+)\\.([0-9]+).*" "\\1;\\2;\\3" _plugin_version_canonical ${_plugin_version})
25+
endif()
26+
elseif(DEFINED PLUGIN_VERSION_OVERRIDE)
27+
if(PLUGIN_VERSION_OVERRIDE MATCHES "v?([0-9]+)\\.([0-9]+)\\.([0-9]+).*")
28+
string(
29+
REGEX REPLACE
30+
"v?([0-9]+)\\.([0-9]+)\\.([0-9]+).*"
31+
"\\1;\\2;\\3"
32+
_plugin_version_canonical
33+
${PLUGIN_VERSION_OVERRIDE}
34+
)
35+
set(_plugin_version ${PLUGIN_VERSION_OVERRIDE})
36+
else()
37+
message(FATAL_ERROR "Invalid version supplied - must be <MAJOR>.<MINOR>.<PATCH>[-(rc|beta)<NUMBER>].")
38+
endif()
39+
endif()
40+
41+
# Set beta/rc versions if suffix included in version string
42+
if(_plugin_version MATCHES "v?[0-9]+\\.[0-9]+\\.[0-9]+-rc[0-9]+")
43+
string(REGEX REPLACE "v?[0-9]+\\.[0-9]+\\.[0-9]+-rc([0-9]+).*$" "\\1" _plugin_release_candidate ${_plugin_version})
44+
elseif(_plugin_version MATCHES "v?[0-9]+\\.[0-9]+\\.[0-9]+-beta[0-9]+")
45+
string(REGEX REPLACE "v?[0-9]+\\.[0-9]+\\.[0-9]+-beta([0-9]+).*$" "\\1" _plugin_beta ${_plugin_version})
46+
endif()
47+
48+
list(GET _plugin_version_canonical 0 PLUGIN_VERSION_MAJOR)
49+
list(GET _plugin_version_canonical 1 PLUGIN_VERSION_MINOR)
50+
list(GET _plugin_version_canonical 2 PLUGIN_VERSION_PATCH)
51+
52+
set(PLUGIN_RELEASE_CANDIDATE ${_plugin_release_candidate})
53+
set(PLUGIN_BETA ${_plugin_beta})
54+
55+
string(REPLACE ";" "." PLUGIN_VERSION_CANONICAL "${_plugin_version_canonical}")
56+
string(REPLACE ";" "." PLUGIN_VERSION "${_plugin_version}")
57+
58+
if(PLUGIN_RELEASE_CANDIDATE GREATER 0)
59+
message(
60+
AUTHOR_WARNING
61+
"******************************************************************************\n"
62+
" + ${_name} - Release candidate detected, PLUGIN_VERSION is now: ${PLUGIN_VERSION}\n"
63+
"******************************************************************************"
64+
)
65+
elseif(PLUGIN_BETA GREATER 0)
66+
message(
67+
AUTHOR_WARNING
68+
"******************************************************************************\n"
69+
" + ${_name} - Beta detected, PLUGIN_VERSION is now: ${PLUGIN_VERSION}\n"
70+
"******************************************************************************"
71+
)
72+
endif()
73+
74+
unset(_plugin_default_version)
75+
unset(_plugin_version)
76+
unset(_plugin_version_canonical)
77+
unset(_plugin_release_candidate)
78+
unset(_plugin_beta)
79+
unset(_plugin_version_result)

0 commit comments

Comments
 (0)