Skip to content
Merged
Show file tree
Hide file tree
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
36 changes: 35 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
cmake_minimum_required(VERSION 3.20)
project(soft_render VERSION 1.0.0 LANGUAGES CXX)
project(soft_render LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Version from git tags (e.g. 0.0.2-17-g9f8a5e9 → 0.0.2)
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --always
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE SR_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
endif()
Comment on lines +9 to +18
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

For better robustness and portability, it's good practice to use CMake's find_package to locate Git before attempting to execute it. This ensures that the build system doesn't fail unexpectedly in environments where git is not in the default PATH, and uses the executable found by CMake's search logic.

find_package(Git QUIET)
if(Git_FOUND)
    execute_process(
        COMMAND ${GIT_EXECUTABLE} describe --tags --always
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        OUTPUT_VARIABLE SR_VERSION
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
    )
endif()
if(NOT SR_VERSION)
    set(SR_VERSION "0.0.0-unknown")
endif()

if(NOT SR_VERSION)
set(SR_VERSION "0.0.0-unknown")
endif()

string(REGEX MATCH "^v?([0-9]+)\\.([0-9]+)\\.([0-9]+)" _ver_match "${SR_VERSION}")
if(_ver_match)
set(SR_VERSION_MAJOR ${CMAKE_MATCH_1})
set(SR_VERSION_MINOR ${CMAKE_MATCH_2})
set(SR_VERSION_PATCH ${CMAKE_MATCH_3})
else()
set(SR_VERSION_MAJOR 0)
set(SR_VERSION_MINOR 0)
set(SR_VERSION_PATCH 0)
endif()
Comment on lines +23 to +32
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The current regex for parsing major, minor, and patch versions (^v?([0-9]+)\.([0-9]+)\.([0-9]+)) strictly requires all three components (major.minor.patch). If a Git tag is in a format like v1.0 or 1.0 (missing the patch component), the regex will not match, causing the major, minor, and patch versions to incorrectly default to 0 instead of 1, 0, 0. This can lead to misleading version information in the generated header. Consider making the minor and patch components optional in the regex to handle such cases more robustly.

string(REGEX MATCH "^v?([0-9]+)\\.?([0-9]*)\\.?([0-9]*)" _ver_match "${SR_VERSION}")
if(_ver_match)
    set(SR_VERSION_MAJOR ${CMAKE_MATCH_1})
    if(CMAKE_MATCH_2)
        set(SR_VERSION_MINOR ${CMAKE_MATCH_2})
    else()
        set(SR_VERSION_MINOR 0)
    endif()
    if(CMAKE_MATCH_3)
        set(SR_VERSION_PATCH ${CMAKE_MATCH_3})
    else()
        set(SR_VERSION_PATCH 0)
    endif()
else()
    set(SR_VERSION_MAJOR 0)
    set(SR_VERSION_MINOR 0)
    set(SR_VERSION_PATCH 0)
endif()


message(STATUS "soft_render version: ${SR_VERSION}")

configure_file(
${CMAKE_SOURCE_DIR}/include/soft_render/version.hpp.in
${CMAKE_BINARY_DIR}/generated/soft_render/version.hpp
)

option(ENABLE_SIMD "Enable SIMD optimizations" ON)
option(ENABLE_THREADS "Enable multithreading" ON)

Expand Down Expand Up @@ -35,6 +68,7 @@ add_library(soft_render STATIC

target_include_directories(soft_render PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/generated
)

if(ENABLE_SIMD)
Expand Down
3 changes: 3 additions & 0 deletions examples/demo/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "soft_render/render/renderer.hpp"
#include "soft_render/math/mat4.hpp"
#include "soft_render/version.hpp"
#include "obj_loader.hpp"
#include <cstdio>
#include <cmath>
Expand All @@ -12,6 +13,8 @@ using namespace sr::pipeline;
static const float PI = 3.14159265358979f;

int main(int argc, char** argv) {
std::printf("soft_render %s\n", SR_VERSION);

const int W = 800, H = 600;
const int FRAMES = 60;

Expand Down
6 changes: 6 additions & 0 deletions include/soft_render/version.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#define SR_VERSION "@SR_VERSION@"
#define SR_VERSION_MAJOR @SR_VERSION_MAJOR@
#define SR_VERSION_MINOR @SR_VERSION_MINOR@
#define SR_VERSION_PATCH @SR_VERSION_PATCH@