Skip to content

Commit

Permalink
A few cmake build improvements
Browse files Browse the repository at this point in the history
Co-authored-by: AverageNerdz <[email protected]>
  • Loading branch information
hhyyrylainen and AverageNerdz committed Dec 4, 2024
1 parent ce409e5 commit 7b3481d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
28 changes: 20 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# Native code side of Thrive
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.13)

# Configuration types. Jolt requires a "Distribution" build type
# Need to be defined before project()
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo;Distribution")

project(Thrive)

# If you want to get compile commands run cmake with
# "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"

# Extra CMake module load path
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Scripts/CMake")

# Options
# Building either the faster variant with AVX or without for older CPU support
option(THRIVE_AVX "Create faster code that needs AVX2" ON)
Expand Down Expand Up @@ -39,17 +46,25 @@ option(USE_ATOMIC_COLLISION_WRITE
"If on uses atomic write to collision data that multiple threads might want to read/write"
OFF)

option(WARNINGS_AS_ERRORS "When on treat compiler warnings as errors" ON)
option(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" ON)

option(NULL_HAS_UNUSUAL_REPRESENTATION
"When on it is not assumed that null equals numeric 0" OFF)

# Extra CMake module load path if we ever need those for Thrive
# set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/CMake")

option(THRIVE_GODOT_API_FILE "Set to override folder Godot API file is looked for in"
"")

# End of options section

include(CMakeHelperFunctions)

# 32-bit detection
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
else()
message(WARNING
"32-bit build detected. Probably won't work correctly as this is untested")
endif()

# Also use a lib prefix on windows for consistency
if(WIN32)
set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "lib")
Expand All @@ -65,9 +80,6 @@ endif()
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
set(BUILD_SHARED_LIBS OFF)

# Configuration types. Jolt requires a "Distribution" build type
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo;Distribution")

# Set flags for Distribution configuration
# These are based on relwithdebinfo as we want symbols for eventual crash reporting
set(CMAKE_C_FLAGS_DISTRIBUTION "${CMAKE_C_FLAGS_RELWITHDEBINFO}" CACHE
Expand Down
50 changes: 50 additions & 0 deletions Scripts/CMake/CMakeHelperFunctions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# CPU core detection for parts of build that need to know that beforehand
include(ProcessorCount)
processorcount(CPU_CORES)
if(CPU_CORES EQUAL 0)
set(CPU_CORES 1)
elseif(CPU_CORES GREATER 4)
math(EXPR CPU_CORES "${CPU_CORES} - 1")
endif()

# Print source files for a target
function(print_source_files target)
get_target_property(target_sources ${target} SOURCES)
if(target_sources)
set(cpp_files "")
foreach(source ${target_sources})
if(source MATCHES ".*\\.(cpp|cxx|cc)$")
get_filename_component(filename ${source} NAME)
list(APPEND cpp_files ${filename})
endif()
endforeach()

list(LENGTH cpp_files total_files)
if(total_files GREATER 0)
list(SORT cpp_files)
message(STATUS "Files for ${target}:")
foreach(filename ${cpp_files})
message(STATUS " ${filename}")
endforeach()
endif()
endif()
endfunction()

# Unity build configuration based on CPU cores
function(configure_unity_build target)
if(WIN32 AND MSVC)
if(CPU_CORES LESS 4)
set(UNITY_BATCH_SIZE 10)
elseif(CPU_CORES LESS 8)
set(UNITY_BATCH_SIZE 20)
else()
set(UNITY_BATCH_SIZE 30)
endif()

set_target_properties(${target} PROPERTIES
UNITY_BUILD ON
UNITY_BUILD_MODE GROUP
UNITY_BUILD_BATCH_SIZE ${UNITY_BATCH_SIZE}
)
endif()
endfunction()

0 comments on commit 7b3481d

Please sign in to comment.