Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
cmake_minimum_required(VERSION 3.5)
file(READ "project.properties" PROJECT_PROPERTIES)
string(REGEX MATCH "VERSION = ([0-9]*.[0-9]*.[0-9]*)" _ ${PROJECT_PROPERTIES})
execute_process(
COMMAND git describe --tags --abbrev=0
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_VER
)
string(REGEX MATCH "([0-9]*.[0-9]*.[0-9]*).*" _ ${GIT_VER})
set(PROJECT_VERSION ${CMAKE_MATCH_1})
if("${PROJECT_VERSION}" STREQUAL "")
set(PROJECT_VERSION 0.0.0)
endif()
Comment on lines +2 to +11

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current method of getting the version from git is not fully robust.

  1. The execute_process call doesn't check if the git command was successful. This can happen if git is not installed or if this is not a git repository (e.g., a source archive).
  2. The regex ([0-9]*.[0-9]*.[0-9]*).* is incorrect. The . matches any character, not a literal dot, and * allows empty version components. It also doesn't handle common v prefixes in tags.

I suggest a more robust implementation that handles git command failures and uses a more accurate regex. This will also make the fallback to 0.0.0 more explicit.

execute_process(
    COMMAND git describe --tags --abbrev=0
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    OUTPUT_VARIABLE GIT_VER
    RESULT_VARIABLE GIT_RESULT
    ERROR_QUIET
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

if(GIT_RESULT EQUAL 0)
    string(REGEX MATCH "v?([0-9]+\\.[0-9]+\\.[0-9]+)" _ ${GIT_VER})
    set(PROJECT_VERSION ${CMAKE_MATCH_1})
endif()

if(NOT PROJECT_VERSION)
    set(PROJECT_VERSION 0.0.0)
endif()

project(libosal VERSION ${PROJECT_VERSION})

set(CMAKE_CXX_STANDARD 11)
Expand Down Expand Up @@ -93,8 +100,8 @@ else()
endif()

set(LIBOSAL_STDC_HEADERS 1)
set(LIBOSAL_PACKAGE_VERSION "${CMAKE_PROJECT_VERSION}")
set(LIBOSAL_VERSION "${CMAKE_PROJECT_VERSION}")
set(LIBOSAL_PACKAGE_VERSION "${GIT_VER}")
set(LIBOSAL_VERSION "${GIT_VER}")
Comment on lines +103 to +104

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using ${GIT_VER} directly here is risky because it can be empty if the git describe command fails (e.g., not a git repo, or no tags). This would result in an empty version string for LIBOSAL_PACKAGE_VERSION and LIBOSAL_VERSION.

It's safer to use ${PROJECT_VERSION}, which is guaranteed to have a fallback value (e.g., 0.0.0) from the logic above. If you intend to keep the v prefix from the git tag, a more robust approach should be taken to construct a version variable with a proper fallback.

set(LIBOSAL_PACKAGE_VERSION "${PROJECT_VERSION}")
set(LIBOSAL_VERSION "${PROJECT_VERSION}")

set(LIBOSAL_PACKAGE_URL "${CMAKE_PROJECT_HOMEPAGE_URL}")
set(LIBOSAL_PACKAGE "${CMAKE_PROJECT_NAME}")
set(LIBOSAL_PACKAGE_NAME "${CMAKE_PROJECT_NAME}")
Expand Down