r5sdk/r5dev/cmake/Macros.cmake
Kawe Mazidjatari 8dbc2024c6 CMake code improvements
Use the 'add_module' macro to add modules without creating duplicate code. This macro also takes a reuse PCH as parameter, so modules that need a precompiled header, could reuse those from different targets that compile them. This commit also restructures the group order of the generated solution files for easier code navigation.
2023-05-13 00:14:53 +02:00

78 lines
2.9 KiB
CMake

# -----------------------------------------------------------------------------
# Start the source file list
# -----------------------------------------------------------------------------
macro( start_sources )
add_sources( SOURCE_GROUP ""
# Add the CMakeLists file to the project filter root
"CMakeLists.txt"
)
endmacro()
# -----------------------------------------------------------------------------
# Add source files to target within a project filter
# -----------------------------------------------------------------------------
macro( add_sources )
set( options )
set( oneValueArgs SOURCE_GROUP )
set( multiValueArgs )
cmake_parse_arguments( ADD_SOURCES
"${options}"
"${oneValueArgs}"
"${multiValueArgs}" ${ARGN}
)
if( NOT ADD_SOURCES_SOURCE_GROUP )
message( FATAL_ERROR "SOURCE_GROUP must be provided" )
endif()
source_group( "${ADD_SOURCES_SOURCE_GROUP}" FILES ${ADD_SOURCES_UNPARSED_ARGUMENTS} )
target_sources( ${PROJECT_NAME} PRIVATE ${ADD_SOURCES_UNPARSED_ARGUMENTS} )
endmacro()
# -----------------------------------------------------------------------------
# End the source file list
# -----------------------------------------------------------------------------
macro( end_sources )
get_property( SRCS_LIST GLOBAL PROPERTY SRCS_LIST )
set_target_output_dirs( ${PROJECT_NAME} )
endmacro()
# -----------------------------------------------------------------------------
# Add modules to the project
# -----------------------------------------------------------------------------
macro( add_module MODULE_TYPE MODULE_NAME REUSE_PCH FOLDER_NAME )
project( ${MODULE_NAME} )
if( ${MODULE_TYPE} STREQUAL "lib" )
add_library( ${PROJECT_NAME} )
elseif( ${MODULE_TYPE} STREQUAL "shared_lib" )
add_library( ${PROJECT_NAME} SHARED )
elseif(${MODULE_TYPE} STREQUAL "exe")
add_executable( ${PROJECT_NAME} )
else()
message( FATAL_ERROR "Invalid module type: ${MODULE_TYPE}; expected 'lib', 'shared_lib', or 'exe'." )
endif()
if ( NOT "${REUSE_PCH}" STREQUAL "" )
target_precompile_headers( ${PROJECT_NAME} REUSE_FROM ${REUSE_PCH} )
endif()
set_target_properties( ${MODULE_NAME} PROPERTIES FOLDER ${FOLDER_NAME} )
endmacro()
# -----------------------------------------------------------------------------
# Initialize global compiler defines
# -----------------------------------------------------------------------------
macro( define_compiler_variables )
if( CMAKE_CXX_COMPILER_ID MATCHES "MSVC" )
add_definitions( -DCOMPILER_MSVC )
elseif( CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
add_definitions( -DCOMPILER_CLANG )
elseif( CMAKE_CXX_COMPILER_ID MATCHES "GNU" )
add_definitions( -DCOMPILER_GCC )
else()
message( FATAL_ERROR "Unsupported compiler: ${CMAKE_CXX_COMPILER_ID}" )
endif()
endmacro()