-
-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimized CMake Build Process #5657
base: master
Are you sure you want to change the base?
Changes from all commits
2ca8153
8d8e41a
cf599f6
6efe70f
39c8d75
a816afc
16d087b
f2022e9
1c86977
b0893e0
f9c10bf
b5e3941
eac3748
ffc6ca7
6cecb91
8c3d3c7
5eee53a
b9204f5
d9a6e2d
d985ba6
33e2d9a
39791d2
d82992e
c93cfd6
b67d1c0
bba1d30
b50d783
4fd8316
514612b
8def9cd
449f381
476f5f9
2311a5c
dc72d67
2c4b723
78afd37
4a85d81
78657ef
9e405f1
4271c83
4bcfa91
b39f840
84ab0ec
f2ac07c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
# Native code side of Thrive | ||
cmake_minimum_required(VERSION 3.10) | ||
cmake_minimum_required(VERSION 3.13) | ||
|
||
project(Thrive) | ||
# Must be defined before project() call | ||
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo;Distribution" CACHE STRING "" FORCE) | ||
|
||
# If you want to get compile commands run cmake with | ||
# "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON" | ||
project(Thrive) | ||
|
||
# Options | ||
# Building either the faster variant with AVX or without for older CPU support | ||
|
@@ -39,121 +39,27 @@ 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" OFF) | ||
|
||
option(NULL_HAS_UNUSUAL_REPRESENTATION | ||
"When on it is not assumed that null equals numeric 0" OFF) | ||
"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 to print source files during build | ||
option(PRINT_SOURCE_FILES "Print source files during build" OFF) | ||
|
||
option(THRIVE_GODOT_API_FILE "Set to override folder Godot API file is looked for in" | ||
"") | ||
# Add our custom CMake scripts directory | ||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Scripts") | ||
include(CMakeConfiguration) | ||
include(CMakeHelperFunctions) | ||
|
||
# Also use a lib prefix on windows for consistency | ||
if(WIN32) | ||
set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "lib") | ||
endif() | ||
|
||
if(NOT THRIVE_AVX) | ||
message(STATUS "Building without AVX support") | ||
set(CMAKE_SHARED_LIBRARY_SUFFIX "_without_avx${CMAKE_SHARED_LIBRARY_SUFFIX}") | ||
set(CMAKE_STATIC_LIBRARY_SUFFIX "_without_avx${CMAKE_STATIC_LIBRARY_SUFFIX}") | ||
endif() | ||
|
||
# static building (on Linux) | ||
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 | ||
STRING "Flags used by the C compiler during Distribution builds." FORCE) | ||
set(CMAKE_CXX_FLAGS_DISTRIBUTION "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}" CACHE | ||
STRING "Flags used by the CXX compiler during Distribution builds." FORCE) | ||
set(CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}" CACHE | ||
STRING "Flags used for linking binaries during Distribution builds." FORCE) | ||
set(CMAKE_SHARED_LINKER_FLAGS_DISTRIBUTION "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}" CACHE | ||
STRING "Flags used by the shared libraries linker during Distribution builds." FORCE) | ||
set(CMAKE_STATIC_LINKER_FLAGS_DISTRIBUTION "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}" CACHE | ||
STRING "Flags used by the static libraries linker during Distribution builds." FORCE) | ||
|
||
mark_as_advanced( | ||
CMAKE_CXX_FLAGS_DISTRIBUTION | ||
CMAKE_C_FLAGS_DISTRIBUTION | ||
CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION | ||
CMAKE_SHARED_LINKER_FLAGS_DISTRIBUTION | ||
CMAKE_STATIC_LINKER_FLAGS_DISTRIBUTION | ||
) | ||
|
||
# If wanted, and extra safety check before LTO enable | ||
# if(THRIVE_LTO) | ||
# include(CheckIPOSupported) | ||
# check_ipo_supported(RESULT supported OUTPUT error) | ||
# if(supported) | ||
# set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) | ||
# else() | ||
# message(WARNING "LTO is not supported: ${error}") | ||
# endif() | ||
# endif() | ||
|
||
# Common options | ||
if(NOT CMAKE_BUILD_TYPE) | ||
set(CMAKE_BUILD_TYPE Release CACHE STRING | ||
"Set the build type, usually Debug or Distribution" FORCE) | ||
endif() | ||
|
||
if(CMAKE_BUILD_TYPE STREQUAL "Debug") | ||
set(CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/debug") | ||
else() | ||
set(CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/release") | ||
endif() | ||
|
||
# Standard library and other linking flags | ||
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") | ||
|
||
set(CLANG_DEFAULT_CXX_STDLIB libc++) | ||
set(CLANG_DEFAULT_RTLIB compiler-rt) | ||
|
||
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") | ||
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -stdlib=libc++") | ||
### apparently lld can't be specified here as llvm-ar will complain | ||
# set(CMAKE_STATIC_LINKER_FLAGS | ||
# "${CMAKE_STATIC_LINKER_FLAGS} -lc++abi -static-libstdc++ -fuse-ld=lld") | ||
#set(CMAKE_STATIC_LINKER_FLAGS | ||
# "${CMAKE_STATIC_LINKER_FLAGS} -static -lc++abi -pthread -fuse-ld=lld") | ||
#set(CMAKE_SHARED_LINKER_FLAGS | ||
# "${CMAKE_SHARED_LINKER_FLAGS} -static -lc++abi -pthread -fuse-ld=lld") | ||
|
||
# set(CMAKE_EXE_LINKER_FLAGS ) | ||
|
||
# static can't specify a linker, seems to use llvm-ar thankfully | ||
# set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -fuse-ld=lld") | ||
set(CMAKE_SHARED_LINKER_FLAGS | ||
"${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld") | ||
|
||
# fully static standard lib, seems to fail as libc is not relocatable code | ||
# set(CMAKE_SHARED_LINKER_FLAGS | ||
# "${CMAKE_SHARED_LINKER_FLAGS} -static") | ||
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static") | ||
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static") | ||
|
||
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") | ||
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} -fuse-ld=gold") | ||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold") | ||
else() | ||
set(CMAKE_GENERATOR_PLATFORM x64) | ||
endif() | ||
|
||
# Make better loading Linux binaries | ||
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON) | ||
set(CMAKE_INSTALL_RPATH "$ORIGIN") | ||
message(STATUS "Building for ${THRIVE_OS} on ${THRIVE_ARCH}") | ||
|
||
# Detect library version | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this code still very much needs to be after the user-configurable option variables which I think should be as close to the top of the file as possible. I'd put them even way before including any of the helper functions etc. |
||
file(READ "src/native/NativeConstants.cs" versionFile) | ||
|
||
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/src/native/NativeConstants.cs" versionFile) | ||
string(REGEX MATCH "Version = ([0-9]+);" _ "${versionFile}") | ||
set(NATIVE_LIBRARY_VERSION ${CMAKE_MATCH_1}) | ||
|
||
|
@@ -175,24 +81,51 @@ if(NOT THRIVE_EXTENSION_VERSION) | |
message(FATAL_ERROR "Failed to parse Thrive GDExtensions library version") | ||
endif() | ||
|
||
message(STATUS "Configured native library version ${NATIVE_LIBRARY_VERSION}") | ||
# Set default build type if not specified | ||
if(NOT CMAKE_BUILD_TYPE) | ||
set(CMAKE_BUILD_TYPE Debug) | ||
endif() | ||
|
||
# Handle RelWithDebInfo as Release for installation purposes | ||
if(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") | ||
set(INSTALL_BUILD_TYPE "release") | ||
else() | ||
string(TOLOWER "${CMAKE_BUILD_TYPE}" INSTALL_BUILD_TYPE) | ||
endif() | ||
|
||
# Set installation prefix | ||
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install/${THRIVE_OS}/${INSTALL_BUILD_TYPE}" | ||
CACHE PATH "Installation directory" FORCE) | ||
|
||
# Configure output directories | ||
foreach(CONFIG ${CMAKE_CONFIGURATION_TYPES}) | ||
string(TOUPPER ${CONFIG} CONFIG_UPPER) | ||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONFIG_UPPER} "${CMAKE_BINARY_DIR}/bin/${CONFIG}") | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONFIG_UPPER} "${CMAKE_BINARY_DIR}/lib/${CONFIG}") | ||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_UPPER} "${CMAKE_BINARY_DIR}/lib/${CONFIG}") | ||
endforeach() | ||
|
||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/${INSTALL_BUILD_TYPE}") | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${INSTALL_BUILD_TYPE}") | ||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${INSTALL_BUILD_TYPE}") | ||
|
||
# Configure include file | ||
configure_file("src/native/Include.h.in" "${PROJECT_BINARY_DIR}/Include.h") | ||
include_directories(${PROJECT_BINARY_DIR}) | ||
configure_file( | ||
"${CMAKE_CURRENT_SOURCE_DIR}/src/native/Include.h.in" | ||
"${CMAKE_BINARY_DIR}/Include.h" | ||
@ONLY | ||
) | ||
|
||
include_directories(${CMAKE_BINARY_DIR}) | ||
|
||
# Configure gdextension stuff | ||
if(THRIVE_GODOT_API_FILE) | ||
set(GODOT_GDEXTENSION_DIR "${THRIVE_GODOT_API_FILE}") | ||
include_directories("${THRIVE_GODOT_API_FILE}") | ||
message(STATUS "Using custom location of Godot API file") | ||
else() | ||
set(GODOT_GDEXTENSION_DIR "${CMAKE_BINARY_DIR}/api") | ||
include_directories("${CMAKE_BINARY_DIR}/api") | ||
endif() | ||
|
||
# Float precision configuration for Godot | ||
set(FLOAT_PRECISION "single") | ||
include_directories(${GODOT_GDEXTENSION_DIR}) | ||
|
||
# GODOT_CPP_SYSTEM_HEADERS | ||
# GODOT_CPP_WARNING_AS_ERROR | ||
|
@@ -208,3 +141,6 @@ endif() | |
add_subdirectory(src/native) | ||
add_subdirectory(src/extension) | ||
|
||
message(STATUS "Building Thrive version ${NATIVE_LIBRARY_VERSION}") | ||
AverageNerdz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") | ||
message(STATUS "Install prefix: ${CMAKE_INSTALL_PREFIX}") |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thisi file has wrong indentation and doesn't end with a newline, same with the other added cmake file in this folder. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Architecture detection | ||
if(CMAKE_SIZEOF_VOID_P EQUAL 8) | ||
set(THRIVE_ARCH "x64") | ||
set(FLOAT_PRECISION "double") | ||
add_definitions(-DTHRIVE_64_BIT) | ||
else() | ||
set(THRIVE_ARCH "x86") | ||
set(FLOAT_PRECISION "single") | ||
add_definitions(-DTHRIVE_32_BIT) | ||
message(WARNING "32-bit build detected. Some features may not work correctly.") | ||
endif() | ||
|
||
# OS Detection | ||
if(WIN32) | ||
set(THRIVE_OS "windows") | ||
else() | ||
set(THRIVE_OS "linux") | ||
endif() | ||
|
||
# CPU core optimization | ||
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() | ||
|
||
# Common compiler options | ||
if(WIN32 AND MSVC) | ||
add_compile_options( | ||
/MP${CPU_CORES} | ||
/bigobj | ||
/GR | ||
/EHsc | ||
/nologo | ||
) | ||
|
||
# Force consistent runtime | ||
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL") | ||
endif() | ||
|
||
# Target configuration functions | ||
function(configure_windows_target target) | ||
if(NOT WIN32 OR NOT MSVC) | ||
message(FATAL_ERROR "function called on non-Windows/MSVC platform for target ${target}") | ||
return() | ||
endif() | ||
|
||
target_compile_definitions(${target} PRIVATE | ||
NOMINMAX | ||
_CRT_SECURE_NO_WARNINGS | ||
_HAS_EXCEPTIONS=1 | ||
) | ||
|
||
# Create a Windows header that defines WIN32_LEAN_AND_MEAN | ||
set(WIN32_HEADER "${CMAKE_BINARY_DIR}/windows_lean_mean.h") | ||
file(WRITE ${WIN32_HEADER} "#ifndef WIN32_LEAN_AND_MEAN\n#define WIN32_LEAN_AND_MEAN\n#endif\n") | ||
|
||
# Force include the header before any other includes | ||
target_compile_options(${target} PRIVATE /FI"${WIN32_HEADER}") | ||
|
||
set_target_properties(${target} PROPERTIES | ||
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL" | ||
) | ||
|
||
configure_unity_build(${target}) | ||
endfunction() | ||
|
||
function(configure_linux_target target) | ||
if(WIN32) | ||
message(FATAL_ERROR "configure_linux_target called on Windows platform") | ||
return() | ||
endif() | ||
|
||
target_compile_options(${target} PRIVATE | ||
-Wall | ||
$<$<OR:$<CONFIG:Release>,$<CONFIG:Distribution>>:-O3> | ||
) | ||
|
||
if(WARNINGS_AS_ERRORS) | ||
target_compile_options(${target} PRIVATE -Werror) | ||
endif() | ||
|
||
set_target_properties(${target} PROPERTIES | ||
POSITION_INDEPENDENT_CODE ON | ||
) | ||
endfunction() | ||
|
||
# Common target configuration function | ||
function(configure_target_build target) | ||
if(WIN32 AND MSVC) | ||
configure_windows_target(${target}) | ||
|
||
target_include_directories(${target} BEFORE PRIVATE | ||
${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES} | ||
${PROJECT_BINARY_DIR} | ||
) | ||
else() | ||
configure_linux_target(${target}) | ||
endif() | ||
endfunction() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Print source files for a target (only if enabled) | ||
function(print_source_files target) | ||
if(NOT PRINT_SOURCE_FILES) | ||
return() | ||
endif() | ||
|
||
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should copy the comment from where you moved this code (the reason why the non-standard distribution type is here).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, i added something to it too :]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't see the comment text about Jolt requiring the Distribution type...