Skip to content

Commit

Permalink
Remove utility CMake code, add utility CMake files, remove runtime co…
Browse files Browse the repository at this point in the history
…nfig (default is acceptable)
  • Loading branch information
Solokiller committed Jul 13, 2017
1 parent 66bf8ad commit 3a19e5c
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 149 deletions.
154 changes: 5 additions & 149 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,134 +7,11 @@
###################################################
cmake_minimum_required( VERSION 3.6 )

# function to collect all the sources from sub-directories
# into a single list
function(add_sources)
get_property(is_defined GLOBAL PROPERTY SRCS_LIST DEFINED)
if(NOT is_defined)
define_property(GLOBAL PROPERTY SRCS_LIST
BRIEF_DOCS "List of source files"
FULL_DOCS "List of source files to be compiled in one library")
endif()
# make absolute paths
set(SRCS)
foreach(s IN LISTS ARGN)
if(NOT IS_ABSOLUTE "${s}")
get_filename_component(s "${s}" ABSOLUTE)
endif()
list(APPEND SRCS "${s}")
endforeach()
# append to global list
set_property(GLOBAL APPEND PROPERTY SRCS_LIST "${SRCS}")
endfunction(add_sources)

# preprocess sources
function( preprocess_sources )
set(PREP_SRCS PARENT_SCOPE)
get_property(SRCS GLOBAL PROPERTY SRCS_LIST)
foreach(s IN LISTS SRCS)
file(RELATIVE_PATH rs "${CMAKE_CURRENT_SOURCE_DIR}" "${s}")
string(REGEX REPLACE "r$" "" o "${CMAKE_CURRENT_BINARY_DIR}/${rs}")
add_custom_command(
OUTPUT "${o}"
COMMAND ${CMAKE_COMMAND} -E copy "${s}" "${o}"
DEPENDS "${s}"
COMMENT "Creating ${o}"
VERBATIM
)
list(APPEND PREP_SRCS "${s}")
endforeach()
set(PREP_SRCS ${PREP_SRCS} PARENT_SCOPE)
endfunction( preprocess_sources )

function( create_source_groups _src_root_path )
get_property(SRCS GLOBAL PROPERTY SRCS_LIST)
foreach(_source IN ITEMS ${SRCS})
get_filename_component(_source_path "${_source}" PATH)
file(RELATIVE_PATH _source_path_rel "${_src_root_path}" "${_source_path}")
string(REPLACE "/" "\\" _group_path "${_source_path_rel}")
source_group("${_group_path}" FILES "${_source}")
endforeach()
endfunction( create_source_groups )

#Function to clear the sources list.
function( clear_sources )
set_property( GLOBAL PROPERTY SRCS_LIST "" )
set( PREP_SRCS PARENT_SCOPE )
endfunction( clear_sources )

# Function to add include files.
function( add_includes )
get_property(is_defined GLOBAL PROPERTY INCLUDES_LIST DEFINED)
if(NOT is_defined)
define_property(GLOBAL PROPERTY INCLUDES_LIST
BRIEF_DOCS "List of include files"
FULL_DOCS "List of include files to be compiled in one library")
endif()
# make absolute paths
set(INCLUDES)
foreach(s IN LISTS ARGN)
if(NOT IS_ABSOLUTE "${s}")
get_filename_component(s "${s}" ABSOLUTE)
endif()
list(APPEND INCLUDES "${s}")
endforeach()
# append to global list
set_property(GLOBAL APPEND PROPERTY INCLUDES_LIST "${INCLUDES}")
endfunction( add_includes )

# Function to install includes.
function( install_includes _include_root_path )
get_property(INCLUDES GLOBAL PROPERTY INCLUDES_LIST)

foreach(_include IN ITEMS ${INCLUDES})
get_filename_component(_include_path "${_include}" PATH)
file(RELATIVE_PATH _include_path_rel "${_include_root_path}" "${_include_path}")
string(REPLACE "/" "\\" _group_path "${_include_path_rel}")
install( FILES "${_include}" DESTINATION "include/${_group_path}" )
endforeach()

set_property( GLOBAL PROPERTY INCLUDES_LIST "" )
endfunction( install_includes )

macro(configure_msvc_runtime)
if(MSVC)
# Default to statically-linked runtime.
if("${MSVC_RUNTIME}" STREQUAL "")
set(MSVC_RUNTIME "static")
endif()
# Set compiler options.
set(variables
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
)
if(${MSVC_RUNTIME} STREQUAL "static")
message(STATUS
"MSVC -> forcing use of statically-linked runtime."
)
foreach(variable ${variables})
if(${variable} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${variable} "${${variable}}")
endif()
endforeach()
else()
message(STATUS
"MSVC -> forcing use of dynamically-linked runtime."
)
foreach(variable ${variables})
if(${variable} MATCHES "/MT")
string(REGEX REPLACE "/MT" "/MD" ${variable} "${${variable}}")
endif()
endforeach()
endif()
endif()
endmacro()
include( cmake/InputFilesList.cmake )
include( cmake/MSVCRuntime.cmake )
include( cmake/PDBUtils.cmake )
include( cmake/Ternary.cmake )
include( cmake/WinXPSupport.cmake )

macro( copy_dependencies project_name base_dir )
foreach( lib ${ARGN} )
Expand All @@ -154,27 +31,6 @@ set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/bin )

project( HL_Tools )

#Needs static CRT to avoid alloc issues.
set( MSVC_RUNTIME "static" )

configure_msvc_runtime()

set(variables
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
)
message(STATUS "Initial build flags:")
foreach(variable ${variables})
message(STATUS " '${variable}': ${${variable}}")
endforeach()
message(STATUS "")

if( UNIX )
set( LINUX_32BIT_FLAG "-m32" )
else()
Expand Down
133 changes: 133 additions & 0 deletions cmake/InputFilesList.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
if( INPUTFILESLIST_INCLUDED )
return()
endif()
set( INPUTFILESLIST_INCLUDED true )

#
# This file defines functions to build a list of source and include files to be used to create libraries and executables, and install files, respectively.
#
# Call add_sources with one or more source/header files to add them to the list of files.
# Call preprocess_sources to generate a list of files to include in a library or executable.
# Use the PREP_SRCS variable to include the list.
# Call create_source_groups with the root directory for the files in the list after defining the library or executable to generate filters for Visual Studio that match the directory structure.
# Call add_includes to add files to the list of files to install.
# Call install_includes with the root directory for the files in the list after defining the library or executable to install the included files.
# Call clear_sources to clear the sources list and prepare it for the next library or executable.
#

#! Gets the list of source files
# \arg:dest_var Name of the variable that will contain the list
function( get_sources dest_var )
get_property( is_defined GLOBAL PROPERTY SRCS_LIST DEFINED )

if( is_defined )
get_property( srcs GLOBAL PROPERTY SRCS_LIST )
set( ${dest_var} ${srcs} PARENT_SCOPE )
else()
set( ${dest_var} PARENT_SCOPE )
endif()
endfunction( get_sources )

#! Function to collect all the sources from sub-directories into a single list
# Pass the file paths of source files relative to the CMakeLists that is calling this function
function( add_sources )
get_property( is_defined GLOBAL PROPERTY SRCS_LIST DEFINED )

if( NOT is_defined )
define_property( GLOBAL PROPERTY SRCS_LIST
BRIEF_DOCS "List of source files"
FULL_DOCS "List of source files to be compiled in one library"
)
endif()

# make absolute paths
set( SRCS )
foreach( s IN LISTS ARGN )
if( NOT IS_ABSOLUTE "${s}" )
get_filename_component( s "${s}" ABSOLUTE )
endif()
list( APPEND SRCS "${s}" )
endforeach()

# append to global list
set_property( GLOBAL APPEND PROPERTY SRCS_LIST "${SRCS}" )
endfunction( add_sources )

#! Preprocess source files
function( preprocess_sources )
set( PREP_SRCS PARENT_SCOPE )
get_property( SRCS GLOBAL PROPERTY SRCS_LIST )

foreach( s IN LISTS SRCS )
file( RELATIVE_PATH rs "${CMAKE_CURRENT_SOURCE_DIR}" "${s}" )
string( REGEX REPLACE "r$" "" o "${CMAKE_CURRENT_BINARY_DIR}/${rs}" )
add_custom_command(
OUTPUT "${o}"
COMMAND ${CMAKE_COMMAND} -E copy "${s}" "${o}"
DEPENDS "${s}"
COMMENT "Creating ${o}"
VERBATIM
)
list( APPEND PREP_SRCS "${s}" )
endforeach()

set( PREP_SRCS ${PREP_SRCS} PARENT_SCOPE )
endfunction( preprocess_sources )

#! Create the source groups for source files
# \arg:_src_root_path Root directory for the list of source files
function( create_source_groups _src_root_path )
get_property( SRCS GLOBAL PROPERTY SRCS_LIST )

foreach( _source IN ITEMS ${SRCS} )
get_filename_component( _source_path "${_source}" PATH )
file( RELATIVE_PATH _source_path_rel "${_src_root_path}" "${_source_path}" )
string( REPLACE "/" "\\" _group_path "${_source_path_rel}" )
source_group( "${_group_path}" FILES "${_source}" )
endforeach()
endfunction( create_source_groups )

#! Function to clear the sources list
# Call once sources have been added to a target and source groups have been created
function( clear_sources )
set_property( GLOBAL PROPERTY SRCS_LIST "" )
set( PREP_SRCS PARENT_SCOPE )
endfunction( clear_sources )

#! Function to add include files to a single list
function( add_includes )
get_property( is_defined GLOBAL PROPERTY INCLUDES_LIST DEFINED )
if( NOT is_defined )
define_property( GLOBAL PROPERTY INCLUDES_LIST
BRIEF_DOCS "List of include files"
FULL_DOCS "List of include files to be compiled in one library"
)
endif()

# make absolute paths
set( INCLUDES )
foreach( s IN LISTS ARGN )
if( NOT IS_ABSOLUTE "${s}" )
get_filename_component( s "${s}" ABSOLUTE )
endif()
list( APPEND INCLUDES "${s}" )
endforeach()

# append to global list
set_property( GLOBAL APPEND PROPERTY INCLUDES_LIST "${INCLUDES}" )
endfunction( add_includes )

#! Function to install includes
# \arg:_include_root_path Root directory for the list of include files
function( install_includes _include_root_path )
get_property( INCLUDES GLOBAL PROPERTY INCLUDES_LIST )

foreach( _include IN ITEMS ${INCLUDES} )
get_filename_component( _include_path "${_include}" PATH )
file( RELATIVE_PATH _include_path_rel "${_include_root_path}" "${_include_path}" )
string( REPLACE "/" "\\" _group_path "${_include_path_rel}" )
install( FILES "${_include}" DESTINATION "include/${_group_path}" )
endforeach()

set_property( GLOBAL PROPERTY INCLUDES_LIST "" )
endfunction( install_includes )
37 changes: 37 additions & 0 deletions cmake/MSVCRuntime.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
if( MSVCRUNTIME_INCLUDED )
return()
endif()
set( MSVCRUNTIME_INCLUDED true )

#! Configures the runtime for the given target
# Only one runtime can be provided
#
# \arg:target_name Name of the target whose runtime to configure
# \flag:STATIC Whether to use the static runtime
# \flag:DYNAMIC Whether to use the dynamic runtime
#
macro( configure_msvc_runtime target_name )
cmake_parse_arguments( ARG "STATIC;DYNAMIC" "" "" ${ARGN} )

if( ARG_UNPARSED_ARGUMENTS )
message( FATAL_ERROR "unrecognized arguments: ${ARG_UNPARSED_ARGUMENTS}" )
endif()

if( ARG_STATIC AND ARG_DYNAMIC )
message( FATAL_ERROR "The STATIC and DYNAMIC runtimes are mutually exclusive" )
endif()

if( MSVC )
# Set compiler options.
if( ARG_STATIC )
target_compile_options( ${target_name} PRIVATE $<$<CONFIG:DEBUG>:/MTd> )
target_compile_options( ${target_name} PRIVATE $<$<NOT:$<CONFIG:DEBUG>>:/MT> )
elseif( ARG_DYNAMIC )
target_compile_options( ${target_name} PRIVATE $<$<CONFIG:DEBUG>:/MDd> )
target_compile_options( ${target_name} PRIVATE $<$<NOT:$<CONFIG:DEBUG>>:/MD> )
else()
#Should never happen, but if more combinations are needed, this will cover edge cases.
message( FATAL_ERROR "Unknown runtime selected" )
endif()
endif()
endmacro()
37 changes: 37 additions & 0 deletions cmake/PDBUtils.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
if( PDBUTILS_INCLUDED )
return()
endif()
set( PDBUTILS_INCLUDED true )

#! Sets PDB file names to match the binaries
# \arg:target_name Name of the target to configure PDB names for
macro( set_pdb_names target_name )
foreach( config IN LISTS CMAKE_CONFIGURATION_TYPES )
#TODO: figure out if there's a way to check if a config is debug - Solokiller
set( POSTFIX )
if( ${config} STREQUAL "Debug" )
set( POSTFIX ${CMAKE_DEBUG_POSTFIX} )
endif()

string( TOUPPER ${config} PDB_POSTFIX )

set_target_properties( ${target_name} PROPERTIES COMPILE_PDB_NAME_${PDB_POSTFIX} "${target_name}${POSTFIX}" )
endforeach()
endmacro( set_pdb_names )

#! Installs PDB files for a given target
# \arg:target_name Name of the target to install PDB files for
# \arg:destination Destination to install files to
macro( install_pdbs target_name destination )
if( WIN32 )
foreach( config IN LISTS CMAKE_CONFIGURATION_TYPES )
#TODO: figure out if there's a way to check if a config is debug - Solokiller
set( POSTFIX )
if( ${config} STREQUAL "Debug" )
set( POSTFIX ${CMAKE_DEBUG_POSTFIX} )
endif()

install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${config}/${target_name}${POSTFIX}.pdb DESTINATION ${destination} CONFIGURATIONS ${config} )
endforeach()
endif()
endmacro( install_pdbs )
18 changes: 18 additions & 0 deletions cmake/Ternary.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
if( TERNARY_INCLUDED )
return()
endif()
set( TERNARY_INCLUDED true )

#! Ternary operator
#
# \arg:result_var_name Name of the variable to assign the value to
# \arg:condition Condition to evaluate
# \arg:value_if_true Value to set if the condition is true
# \arg:value_if_false Value to set if the condition is false
macro( ternary result_var_name condition value_if_true value_if_false )
if( ${condition} )
set( ${result_var_name} ${value_if_true} )
else()
set( ${result_var_name} ${value_if_false} )
endif()
endmacro( ternary )
Loading

0 comments on commit 3a19e5c

Please sign in to comment.