From e69755c2037318236d7db2a114417222a06560e6 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sun, 17 May 2020 19:41:02 +0200 Subject: [PATCH] CMake: check if the compiler supports using precompiled headers Build the cotire test project and check if it succeeds. Also check if the 'had text segment at different address' warning does not appear in the build output. If it does not succeed, disable usage of precompiled headers. If the PCH option was changed, clean the project and rebuild it again. Do not clean everytime the project is configured because (re)building the cotire test project takes some time. --- build/cmake/init.cmake | 34 +++++++++++++++++++ .../cmake/modules/cotire_test/CMakeLists.txt | 21 ++++++++++++ build/cmake/modules/cotire_test/license | 22 ++++++++++++ .../modules/cotire_test/src/CMakeLists.txt | 31 +++++++++++++++++ .../cmake/modules/cotire_test/src/example.cpp | 24 +++++++++++++ build/cmake/modules/cotire_test/src/example.h | 10 ++++++ build/cmake/modules/cotire_test/src/log.cpp | 17 ++++++++++ build/cmake/modules/cotire_test/src/log.h | 10 ++++++ build/cmake/modules/cotire_test/src/main.cpp | 12 +++++++ 9 files changed, 181 insertions(+) create mode 100644 build/cmake/modules/cotire_test/CMakeLists.txt create mode 100644 build/cmake/modules/cotire_test/license create mode 100644 build/cmake/modules/cotire_test/src/CMakeLists.txt create mode 100644 build/cmake/modules/cotire_test/src/example.cpp create mode 100644 build/cmake/modules/cotire_test/src/example.h create mode 100644 build/cmake/modules/cotire_test/src/log.cpp create mode 100644 build/cmake/modules/cotire_test/src/log.h create mode 100644 build/cmake/modules/cotire_test/src/main.cpp diff --git a/build/cmake/init.cmake b/build/cmake/init.cmake index 6709c371b8..2fdb787484 100644 --- a/build/cmake/init.cmake +++ b/build/cmake/init.cmake @@ -530,3 +530,37 @@ if(wxUSE_GUI) set(wxUSE_LIBGNOMEVFS OFF) endif() endif() + +# test if precompiled headers are supported using the cotire test project +if(DEFINED wxBUILD_PRECOMP_PREV AND NOT wxBUILD_PRECOMP STREQUAL wxBUILD_PRECOMP_PREV) + set(CLEAN_PRECOMP_TEST TRUE) +endif() +set(wxBUILD_PRECOMP_PREV ${wxBUILD_PRECOMP} CACHE INTERNAL "") + +if(wxBUILD_PRECOMP) + if (CLEAN_PRECOMP_TEST) + try_compile(RESULT_VAR_CLEAN + "${wxBINARY_DIR}/CMakeFiles/cotire_test" + "${wxSOURCE_DIR}/build/cmake/modules/cotire_test" + CotireExample clean_cotire + ) + endif() + try_compile(RESULT_VAR + "${wxBINARY_DIR}/CMakeFiles/cotire_test" + "${wxSOURCE_DIR}/build/cmake/modules/cotire_test" + CotireExample OUTPUT_VARIABLE OUTPUT_VAR + ) + + # check if output has precompiled header warnings. The build can still succeed, so check the output + # likely caused by gcc hardening: https://bugzilla.redhat.com/show_bug.cgi?id=1721553 + # cc1plus: warning /path/to/project/cotire/name_CXX_prefix.hxx.gch: had text segment at different address + string(FIND "${OUTPUT_VAR}" "had text segment at different address" HAS_MESSAGE) + if(${HAS_MESSAGE} GREATER -1) + set(RESULT_VAR FALSE) + endif() + + if(NOT RESULT_VAR) + message(WARNING "precompiled header (PCH) test failed, it will be turned off") + wx_option_force_value(wxBUILD_PRECOMP OFF) + endif() +endif(wxBUILD_PRECOMP) diff --git a/build/cmake/modules/cotire_test/CMakeLists.txt b/build/cmake/modules/cotire_test/CMakeLists.txt new file mode 100644 index 0000000000..dd1426208f --- /dev/null +++ b/build/cmake/modules/cotire_test/CMakeLists.txt @@ -0,0 +1,21 @@ +# cotire example project + +cmake_minimum_required(VERSION 2.8.12) + +if (POLICY CMP0058) + # Ninja requires custom command byproducts to be explicit + cmake_policy(SET CMP0058 NEW) +endif() + +project (CotireExample) + +set (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/..") + +if (NOT CMAKE_VERSION VERSION_LESS "3.1.0") + set (CMAKE_CXX_STANDARD "98") + set (CMAKE_CXX_EXTENSIONS OFF) +endif() + +include(cotire) + +add_subdirectory(src) diff --git a/build/cmake/modules/cotire_test/license b/build/cmake/modules/cotire_test/license new file mode 100644 index 0000000000..68c37ca2d5 --- /dev/null +++ b/build/cmake/modules/cotire_test/license @@ -0,0 +1,22 @@ +Copyright (c) 2012-2018 Sascha Kratky + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/build/cmake/modules/cotire_test/src/CMakeLists.txt b/build/cmake/modules/cotire_test/src/CMakeLists.txt new file mode 100644 index 0000000000..92df233550 --- /dev/null +++ b/build/cmake/modules/cotire_test/src/CMakeLists.txt @@ -0,0 +1,31 @@ +# cotire example project + +add_executable(example main.cpp example.cpp log.cpp log.h example.h) + +# enable warnings +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set_target_properties(example PROPERTIES COMPILE_FLAGS "-Weverything") +elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU") + set_target_properties(example PROPERTIES COMPILE_FLAGS "-Wall -Wextra") +endif() + +cotire(example) + +# cotire sets the following properties +get_target_property(_unitySource example COTIRE_CXX_UNITY_SOURCE) +get_target_property(_prefixHeader example COTIRE_CXX_PREFIX_HEADER) +get_target_property(_precompiledHeader example COTIRE_CXX_PRECOMPILED_HEADER) +get_target_property(_unityTargetName example COTIRE_UNITY_TARGET_NAME) + +if (_unitySource) + message(STATUS "example unity source: ${_unitySource}") +endif() +if (_prefixHeader) + message(STATUS "example prefix header: ${_prefixHeader}") +endif() +if (_precompiledHeader) + message(STATUS "example precompiled header: ${_precompiledHeader}") +endif() +if (TARGET ${_unityTargetName}) + message(STATUS "example unity target: ${_unityTargetName}") +endif() diff --git a/build/cmake/modules/cotire_test/src/example.cpp b/build/cmake/modules/cotire_test/src/example.cpp new file mode 100644 index 0000000000..a85731507a --- /dev/null +++ b/build/cmake/modules/cotire_test/src/example.cpp @@ -0,0 +1,24 @@ +// cotire example project + +#include "example.h" + +#ifndef NDEBUG +#include +#include +#endif + +namespace example { + +std::string get_message() { + char msg_chrs[] = { 'C', 'o', 't', 'i', 'r', 'e', 'd', '!' }; +#ifdef NDEBUG + return std::string(&msg_chrs[0], &msg_chrs[sizeof(msg_chrs)]); +#else + std::string msg; + msg.reserve(sizeof(msg_chrs)); + std::copy(msg_chrs, msg_chrs + sizeof(msg_chrs), std::back_inserter(msg)); + return msg; +#endif +} + +} diff --git a/build/cmake/modules/cotire_test/src/example.h b/build/cmake/modules/cotire_test/src/example.h new file mode 100644 index 0000000000..0fe3e235cc --- /dev/null +++ b/build/cmake/modules/cotire_test/src/example.h @@ -0,0 +1,10 @@ +// cotire example project + +#include + +namespace example { + +std::string get_message(); + +} + diff --git a/build/cmake/modules/cotire_test/src/log.cpp b/build/cmake/modules/cotire_test/src/log.cpp new file mode 100644 index 0000000000..5294adb827 --- /dev/null +++ b/build/cmake/modules/cotire_test/src/log.cpp @@ -0,0 +1,17 @@ +// cotire example project + +#include "log.h" + +#include + +namespace logging { + +void error(const std::string& msg) { + std::cerr << msg << std::endl; +} + +void info(const std::string& msg) { + std::cout << msg << std::endl; +} + +} diff --git a/build/cmake/modules/cotire_test/src/log.h b/build/cmake/modules/cotire_test/src/log.h new file mode 100644 index 0000000000..a6ce24a1e8 --- /dev/null +++ b/build/cmake/modules/cotire_test/src/log.h @@ -0,0 +1,10 @@ +// cotire example project + +#include + +namespace logging { + +void error(const std::string& msg); +void info(const std::string& msg); + +} diff --git a/build/cmake/modules/cotire_test/src/main.cpp b/build/cmake/modules/cotire_test/src/main.cpp new file mode 100644 index 0000000000..2ea1af6217 --- /dev/null +++ b/build/cmake/modules/cotire_test/src/main.cpp @@ -0,0 +1,12 @@ +// cotire example project main + +#include + +#include "example.h" +#include "log.h" + +int main() +{ + std::string msg = example::get_message(); + logging::info(msg); +}