Skip to content

Commit

Permalink
Separate build versions between engine and application projects
Browse files Browse the repository at this point in the history
  • Loading branch information
doanamo committed Jan 27, 2025
1 parent fd44f24 commit dc08fc0
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 50 deletions.
68 changes: 68 additions & 0 deletions CMakeShared.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,79 @@ function(setup_cmake_shared)
endif()
endfunction()

#
# Generated
#

find_package("Git")

function(setup_cmake_build_info target)
get_target_property(TARGET_BINARY_DIR ${target} BINARY_DIR)
get_target_property(ENGINE_SOURCE_DIR Engine SOURCE_DIR)

file(MAKE_DIRECTORY "${TARGET_BINARY_DIR}/Build")
file(TOUCH "${TARGET_BINARY_DIR}/Build/Info.cpp")
target_sources(${target} PRIVATE "${TARGET_BINARY_DIR}/Build/Info.cpp")

add_custom_target(${target}BuildInfo ALL
COMMENT "Generating build info for ${target}"
WORKING_DIRECTORY ${CURRENT_SOURCE_DIR}
DEPENDS "${ENGINE_SOURCE_DIR}/Build/Info.cpp.in"
BYPRODUCTS "${TARGET_BINARY_DIR}/Build/Info.cpp"
COMMAND ${CMAKE_COMMAND}
-D GIT_EXECUTABLE=${GIT_EXECUTABLE}
-D PROJECT_NAME=${CMAKE_PROJECT_NAME}
-D PROJECT_PATH="${PROJECT_SOURCE_DIR}/"
-D INPUT_FILE="${ENGINE_SOURCE_DIR}/Build/Info.cpp.in"
-D OUTPUT_FILE="${TARGET_BINARY_DIR}/Build/Info.cpp"
-P "${ENGINE_SOURCE_DIR}/Build/Info.cmake"
)

add_dependencies(${target} ${target}BuildInfo)
endfunction()

function(setup_cmake_build_version target)
if("${target}" STREQUAL "Engine")
set(VERSION_PREFIX "Engine")
else()
set(VERSION_PREFIX "Application")
endif()

get_target_property(TARGET_BINARY_DIR ${target} BINARY_DIR)
get_target_property(ENGINE_SOURCE_DIR Engine SOURCE_DIR)

file(MAKE_DIRECTORY "${TARGET_BINARY_DIR}/Build")
file(TOUCH "${TARGET_BINARY_DIR}/Build/Version.cpp")
target_sources(${target} PRIVATE "${TARGET_BINARY_DIR}/Build/Version.cpp")

add_custom_target(${target}BuildVersion ALL
COMMENT "Generating build version for ${target}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS "${ENGINE_SOURCE_DIR}/Build/Version.cpp.in"
BYPRODUCTS "${TARGET_BINARY_DIR}/Build/Version.cpp"
COMMAND ${CMAKE_COMMAND}
-D VERSION_PREFIX=${VERSION_PREFIX}
-D PROJECT_VERSION=${PROJECT_VERSION}
-D PROJECT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
-D PROJECT_VERSION_MINOR=${PROJECT_VERSION_MINOR}
-D PROJECT_VERSION_PATCH=${PROJECT_VERSION_PATCH}
-D INPUT_FILE="${ENGINE_SOURCE_DIR}/Build/Version.cpp.in"
-D OUTPUT_FILE="${TARGET_BINARY_DIR}/Build/Version.cpp"
-P "${ENGINE_SOURCE_DIR}/Build/Version.cmake"
)

add_dependencies(${target} ${target}BuildVersion)
endfunction()

#
# Targets
#

function(setup_cmake_executable target)
# Generate files needed for executable build.
setup_cmake_build_info(${target})
setup_cmake_build_version(${target})

# Use main() instead of WinMain() on Windows.
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set_target_properties(${target} PROPERTIES LINK_FLAGS "/ENTRY:mainCRTStartup")
Expand Down
8 changes: 4 additions & 4 deletions Engine/Build/Version.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Note: This file is generated by CMake.
// Any modifications will be overwritten!
const char* const EngineVersion::Readable = "${PROJECT_VERSION}";
const u32 EngineVersion::Major = ${PROJECT_VERSION_MAJOR};
const u32 EngineVersion::Minor = ${PROJECT_VERSION_MINOR};
const u32 EngineVersion::Patch = ${PROJECT_VERSION_PATCH};
const char* const ${VERSION_PREFIX}Version::Readable = "${PROJECT_VERSION}";
const u32 ${VERSION_PREFIX}Version::Major = ${PROJECT_VERSION_MAJOR};
const u32 ${VERSION_PREFIX}Version::Minor = ${PROJECT_VERSION_MINOR};
const u32 ${VERSION_PREFIX}Version::Patch = ${PROJECT_VERSION_PATCH};
8 changes: 8 additions & 0 deletions Engine/Build/Version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ namespace EngineVersion
extern const u32 Minor;
extern const u32 Patch;
}

namespace ApplicationVersion
{
extern const char* const Readable;
extern const u32 Major;
extern const u32 Minor;
extern const u32 Patch;
}
44 changes: 2 additions & 42 deletions Engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ add_library(Engine STATIC
"Engine.cpp"
)

setup_cmake_build_version(Engine)

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_sources(Engine PRIVATE
"Platform/Windows/Debug.cpp"
Expand Down Expand Up @@ -59,45 +61,3 @@ endif()
find_package(Vulkan REQUIRED)
target_include_directories(Engine PUBLIC ${Vulkan_INCLUDE_DIRS})
target_link_libraries(Engine PRIVATE Vulkan::Vulkan)

#
# Build
#

find_package("Git")

file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/Build")
file(TOUCH "${CMAKE_CURRENT_BINARY_DIR}/Build/Info.cpp")
file(TOUCH "${CMAKE_CURRENT_BINARY_DIR}/Build/Version.cpp")
target_sources(Engine PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/Build/Info.cpp")
target_sources(Engine PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/Build/Version.cpp")

add_custom_target(BuildInfo ALL
COMMENT "Generating build info source"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../"
BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/Build/Info.cpp"
COMMAND ${CMAKE_COMMAND}
-D GIT_EXECUTABLE="${GIT_EXECUTABLE}"
-D PROJECT_NAME="${CMAKE_PROJECT_NAME}"
-D PROJECT_PATH="${PROJECT_SOURCE_DIR}/"
-D INPUT_FILE="${CMAKE_CURRENT_SOURCE_DIR}/Build/Info.cpp.in"
-D OUTPUT_FILE="${CMAKE_CURRENT_BINARY_DIR}/Build/Info.cpp"
-P "${CMAKE_CURRENT_SOURCE_DIR}/Build/Info.cmake"
}
)

add_custom_target(BuildVersion ALL
COMMENT "Generating build version source"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../"
BYPRODUCTS "${CMAKE_CURRENT_BINARY_DIR}/Build/Version.cpp"
COMMAND ${CMAKE_COMMAND}
-D PROJECT_VERSION="${PROJECT_VERSION}"
-D PROJECT_VERSION_MAJOR="${PROJECT_VERSION_MAJOR}"
-D PROJECT_VERSION_MINOR="${PROJECT_VERSION_MINOR}"
-D PROJECT_VERSION_PATCH="${PROJECT_VERSION_PATCH}"
-D INPUT_FILE="${CMAKE_CURRENT_SOURCE_DIR}/Build/Version.cpp.in"
-D OUTPUT_FILE="${CMAKE_CURRENT_BINARY_DIR}/Build/Version.cpp"
-P "${CMAKE_CURRENT_SOURCE_DIR}/Build/Version.cmake"
)

add_dependencies(Engine BuildInfo BuildVersion)
2 changes: 2 additions & 0 deletions Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ void Engine::Setup(const Config& config)
ASSERT(config.applicationName, "Application name must be specified");
g_applicationName = config.applicationName;

LOG("Project: %s", BuildInfo::ProjectName);
LOG("Engine version: %s", EngineVersion::Readable);
LOG("Application version: %s", ApplicationVersion::Readable);
LOG("Build configuration: %s", CONFIG_NAME);
LOG("Build info: %s-%s-%s (%s)", BuildInfo::ChangeNumber,
BuildInfo::BranchName, BuildInfo::CommitHash, BuildInfo::CommitDate);
Expand Down
3 changes: 1 addition & 2 deletions Engine/Graphics/GraphicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ bool Graphics::System::CreateInstance()
applicationInfo.pEngineName = "Bourne Engine";
applicationInfo.engineVersion = VK_MAKE_VERSION(EngineVersion::Major, EngineVersion::Minor, EngineVersion::Patch);
applicationInfo.pApplicationName = Engine::GetApplicationName();
// #todo: Separate build version into engine and application versions
//applicationInfo.applicationVersion = VK_MAKE_VERSION(BuildVersion::Major, BuildVersion::Minor, BuildVersion::Patch);
applicationInfo.applicationVersion = VK_MAKE_VERSION(ApplicationVersion::Major, ApplicationVersion::Minor, ApplicationVersion::Patch);

VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Expand Down
2 changes: 1 addition & 1 deletion Example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.29)

project(Example)
project(Example VERSION ${CMAKE_PROJECT_VERSION})

#
# Executable
Expand Down
2 changes: 1 addition & 1 deletion Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.29)

project(Tests)
project(Tests VERSION ${CMAKE_PROJECT_VERSION})

#
# Executable
Expand Down

0 comments on commit dc08fc0

Please sign in to comment.