Add PCH builds via 'SFML_ENABLE_PCH' CMake option

This commit is contained in:
Vittorio Romeo 2022-10-07 23:38:45 +02:00 committed by Lukas Dürrenberger
parent 7a5b862fd0
commit fd5c358c98
4 changed files with 121 additions and 9 deletions

View File

@ -288,6 +288,17 @@ if(SFML_INSTALL_PKGCONFIG_FILES)
endforeach() endforeach()
endif() endif()
# option to enable precompiled headers
sfml_set_option(SFML_ENABLE_PCH FALSE BOOL "TRUE to enable precompiled headers for SFML builds -- only supported on Windows/Linux and for static library builds")
if(SFML_ENABLE_PCH AND BUILD_SHARED_LIBS)
message(FATAL_ERROR "Precompiled headers are currently not supported for shared library builds")
endif()
if(SFML_ENABLE_PCH AND SFML_OS_MACOSX)
message(FATAL_ERROR "Precompiled headers are currently not supported in MacOS builds")
endif()
# setup the install rules # setup the install rules
if(NOT SFML_BUILD_FRAMEWORKS) if(NOT SFML_BUILD_FRAMEWORKS)
install(DIRECTORY include install(DIRECTORY include

View File

@ -3,6 +3,14 @@ include(CMakeParseArguments)
# include the compiler warnings helpers # include the compiler warnings helpers
include(${CMAKE_CURRENT_LIST_DIR}/CompilerWarnings.cmake) include(${CMAKE_CURRENT_LIST_DIR}/CompilerWarnings.cmake)
# helper function to tweak visibility of public symbols
function(set_public_symbols_hidden target)
# ensure public symbols are hidden by default (exported ones are explicitly marked)
set_target_properties(${target} PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES)
endfunction()
# This little macro lets you set any Xcode specific property # This little macro lets you set any Xcode specific property
macro(sfml_set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE) macro(sfml_set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
set_property (TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY} ${XCODE_VALUE}) set_property (TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY} ${XCODE_VALUE})
@ -80,6 +88,13 @@ macro(sfml_add_library module)
endif() endif()
set_target_warnings(${target}) set_target_warnings(${target})
set_public_symbols_hidden(${target})
# enable precompiled headers
if (SFML_ENABLE_PCH AND (NOT ${target} STREQUAL "sfml-system"))
message(VERBOSE "enabling PCH for SFML library '${target}'")
target_precompile_headers(${target} REUSE_FROM sfml-system)
endif()
# define the export symbol of the module # define the export symbol of the module
string(REPLACE "-" "_" NAME_UPPER "${target}") string(REPLACE "-" "_" NAME_UPPER "${target}")
@ -163,6 +178,14 @@ macro(sfml_add_library module)
set_target_properties(${target} PROPERTIES set_target_properties(${target} PROPERTIES
PDB_NAME "${target}${SFML_PDB_POSTFIX}" PDB_NAME "${target}${SFML_PDB_POSTFIX}"
PDB_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib") PDB_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
else()
if(SFML_ENABLE_PCH)
message(VERBOSE "overriding PDB name for '${target}' with \"sfml-s${SFML_PDB_POSTFIX}\" due to PCH being enabled")
# For PCH builds with PCH reuse, the PDB name must be the same as the target that's being reused
set_target_properties(${target} PROPERTIES
COMPILE_PDB_NAME "sfml-system"
COMPILE_PDB_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
else() else()
# Static libraries have no linker PDBs, thus the compiler PDBs are relevant # Static libraries have no linker PDBs, thus the compiler PDBs are relevant
set_target_properties(${target} PROPERTIES set_target_properties(${target} PROPERTIES
@ -170,11 +193,7 @@ macro(sfml_add_library module)
COMPILE_PDB_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib") COMPILE_PDB_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
endif() endif()
endif() endif()
endif()
# ensure public symbols are hidden by default (exported ones are explicitly marked)
set_target_properties(${target} PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES)
# build frameworks or dylibs # build frameworks or dylibs
if(SFML_OS_MACOSX AND BUILD_SHARED_LIBS AND NOT THIS_STATIC) if(SFML_OS_MACOSX AND BUILD_SHARED_LIBS AND NOT THIS_STATIC)
@ -285,7 +304,14 @@ macro(sfml_add_example target)
set_property(TARGET ${target} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") set_property(TARGET ${target} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif() endif()
# enable precompiled headers
if (SFML_ENABLE_PCH)
message(VERBOSE "enabling PCH for SFML example '${target}'")
target_precompile_headers(${target} REUSE_FROM sfml-system)
endif()
set_target_warnings(${target}) set_target_warnings(${target})
set_public_symbols_hidden(${target})
# set the debug suffix # set the debug suffix
set_target_properties(${target} PROPERTIES DEBUG_POSTFIX -d) set_target_properties(${target} PROPERTIES DEBUG_POSTFIX -d)
@ -322,6 +348,12 @@ function(sfml_add_test target SOURCES DEPENDS)
# create the target # create the target
add_executable(${target} ${SOURCES}) add_executable(${target} ${SOURCES})
# enable precompiled headers
if (SFML_ENABLE_PCH)
message(VERBOSE "enabling PCH for SFML test '${target}'")
target_precompile_headers(${target} REUSE_FROM sfml-system)
endif()
# set the target's folder (for IDEs that support it, e.g. Visual Studio) # set the target's folder (for IDEs that support it, e.g. Visual Studio)
set_target_properties(${target} PROPERTIES FOLDER "Tests") set_target_properties(${target} PROPERTIES FOLDER "Tests")
@ -329,6 +361,7 @@ function(sfml_add_test target SOURCES DEPENDS)
target_link_libraries(${target} PRIVATE ${DEPENDS} sfml-test-main) target_link_libraries(${target} PRIVATE ${DEPENDS} sfml-test-main)
set_target_warnings(${target}) set_target_warnings(${target})
set_public_symbols_hidden(${target})
# If coverage is enabled for MSVC and we are linking statically, use /WHOLEARCHIVE # If coverage is enabled for MSVC and we are linking statically, use /WHOLEARCHIVE
# to make sure the linker doesn't discard unused code sections before coverage can be measured # to make sure the linker doesn't discard unused code sections before coverage can be measured

62
src/SFML/PCH.hpp Normal file
View File

@ -0,0 +1,62 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2022 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_SFML_PCH_HPP
#define SFML_SFML_PCH_HPP
////////////////////////////////////////////////////////////
// Precompiled Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#ifdef SFML_SYSTEM_WINDOWS
#define UNICODE 1
#define _UNICODE 1
#include <SFML/System/Win32/WindowsHeader.hpp>
#endif // SFML_SYSTEM_WINDOWS
#include <SFML/System/Err.hpp>
#include <SFML/System/String.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/System/Vector2.hpp>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <filesystem>
#include <iostream>
#include <memory>
#include <mutex>
#include <ostream>
#include <string>
#include <unordered_map>
#include <vector>
#endif // SFML_SFML_PCH_HPP

View File

@ -67,6 +67,12 @@ endif()
sfml_add_library(System sfml_add_library(System
SOURCES ${SRC} ${PLATFORM_SRC}) SOURCES ${SRC} ${PLATFORM_SRC})
# enable precompiled headers
if (SFML_ENABLE_PCH)
message(VERBOSE "enabling PCH for SFML library 'sfml-system' (reused as the PCH for other SFML libraries)")
target_precompile_headers(sfml-system PRIVATE ${PROJECT_SOURCE_DIR}/src/SFML/PCH.hpp)
endif()
if(SFML_OS_ANDROID) if(SFML_OS_ANDROID)
# glad sources # glad sources
target_include_directories(sfml-system PRIVATE "${PROJECT_SOURCE_DIR}/extlibs/headers/glad/include") target_include_directories(sfml-system PRIVATE "${PROJECT_SOURCE_DIR}/extlibs/headers/glad/include")