2020-01-02 06:44:10 +01:00
# This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
2016-03-14 15:45:39 +01:00
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
2025-11-12 22:53:54 +01:00
# Adds all found source files to a given target
2016-03-14 15:45:39 +01:00
#
# Use it like:
2025-11-12 22:53:54 +01:00
# CollectAndAddSourceFiles(
# common
2016-03-14 15:45:39 +01:00
# ${CMAKE_CURRENT_SOURCE_DIR}
2025-11-12 22:53:54 +01:00
# EXCLUDE
2016-03-14 15:45:39 +01:00
# ${CMAKE_CURRENT_SOURCE_DIR}/PrecompiledHeaders
2026-02-21 15:08:51 +01:00
# ${CMAKE_CURRENT_SOURCE_DIR}/Platform
# HEADER_VISIBILITY PRIVATE) # default is PUBLIC, this controls whether other targets that have this target as dependency will be able to access its headers
2016-03-14 15:45:39 +01:00
#
2025-11-12 22:53:54 +01:00
function ( CollectAndAddSourceFiles target_name current_dir )
2026-02-21 15:08:51 +01:00
cmake_parse_arguments ( PARSE_ARGV 2 arg "" "BASE_DIR;HEADER_VISIBILITY" "EXCLUDE" )
2025-11-12 22:53:54 +01:00
if ( NOT arg_BASE_DIR )
set ( arg_BASE_DIR "${current_dir}" )
endif ()
2026-02-21 15:08:51 +01:00
if ( NOT arg_HEADER_VISIBILITY )
set ( arg_HEADER_VISIBILITY "PUBLIC" )
endif ()
2025-11-12 22:53:54 +01:00
list ( FIND arg_EXCLUDE "${current_dir}" IS_EXCLUDED )
2016-03-14 15:45:39 +01:00
if ( IS_EXCLUDED EQUAL -1 )
2025-11-12 22:53:54 +01:00
cmake_path ( RELATIVE_PATH current_dir BASE_DIRECTORY "${arg_BASE_DIR}" OUTPUT_VARIABLE fileset_name )
# normalize file set name
string ( REGEX REPLACE "[./\\]" "_" fileset_name "${fileset_name}" )
file ( GLOB private_source_files
2016-03-14 15:45:39 +01:00
${ current_dir } /*.c
${ current_dir } /*.cc
2025-11-12 22:53:54 +01:00
${ current_dir } /*.cpp )
file ( GLOB public_header_files
2016-03-14 15:45:39 +01:00
${ current_dir } /*.inl
${ current_dir } /*.h
${ current_dir } /*.hh
${ current_dir } /*.hpp )
2025-11-12 22:53:54 +01:00
target_sources ( ${ target_name } PRIVATE ${ private_source_files } )
2026-02-21 15:08:51 +01:00
target_sources ( ${ target_name } ${ arg_HEADER_VISIBILITY } FILE_SET "headers_${fileset_name}" TYPE HEADERS BASE_DIRS ${ current_dir } FILES ${ public_header_files } )
2016-03-14 15:45:39 +01:00
file ( GLOB SUB_DIRECTORIES ${ current_dir } /* )
foreach ( SUB_DIRECTORY ${ SUB_DIRECTORIES } )
2020-06-22 15:27:03 +03:00
if ( IS_DIRECTORY ${ SUB_DIRECTORY } )
2026-02-21 15:08:51 +01:00
CollectAndAddSourceFiles ( "${target_name}" "${SUB_DIRECTORY}" BASE_DIR ${ arg_BASE_DIR } HEADER_VISIBILITY ${ arg_HEADER_VISIBILITY } EXCLUDE ${ arg_EXCLUDE } )
2016-03-14 15:45:39 +01:00
endif ()
endforeach ()
endif ()
endfunction ()
# Collects all subdirectoroies into the given variable,
# which is useful to include all subdirectories.
# Ignores full qualified directories listed in the variadic arguments.
#
# Use it like:
# CollectIncludeDirectories(
# ${CMAKE_CURRENT_SOURCE_DIR}
# COMMON_PUBLIC_INCLUDES
2025-11-12 22:53:54 +01:00
# EXCLUDE
2016-03-14 15:45:39 +01:00
# ${CMAKE_CURRENT_SOURCE_DIR}/PrecompiledHeaders
# ${CMAKE_CURRENT_SOURCE_DIR}/Platform)
#
2025-11-12 22:53:54 +01:00
function ( CollectIncludeDirectories current_dir sources_variable )
cmake_parse_arguments ( PARSE_ARGV 2 arg "" "" "EXCLUDE" )
list ( FIND arg_EXCLUDE "${current_dir}" IS_EXCLUDED )
2016-03-14 15:45:39 +01:00
if ( IS_EXCLUDED EQUAL -1 )
2025-11-12 22:53:54 +01:00
list ( APPEND ${ sources_variable } ${ current_dir } )
2016-03-14 15:45:39 +01:00
file ( GLOB SUB_DIRECTORIES ${ current_dir } /* )
foreach ( SUB_DIRECTORY ${ SUB_DIRECTORIES } )
2020-06-22 15:27:03 +03:00
if ( IS_DIRECTORY ${ SUB_DIRECTORY } )
2025-11-12 22:53:54 +01:00
CollectIncludeDirectories ( "${SUB_DIRECTORY}" "${sources_variable}" EXCLUDE ${ arg_EXCLUDE } )
2016-03-14 15:45:39 +01:00
endif ()
endforeach ()
2025-11-12 22:53:54 +01:00
set ( ${ sources_variable } ${ ${sources_variable } } PARENT_SCOPE )
2016-03-14 15:45:39 +01:00
endif ()
endfunction ()