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.
This commit is contained in:
Maarten Bent
2020-05-17 19:41:02 +02:00
parent fb5c13ed00
commit e69755c203
9 changed files with 181 additions and 0 deletions

View File

@@ -530,3 +530,37 @@ if(wxUSE_GUI)
set(wxUSE_LIBGNOMEVFS OFF) set(wxUSE_LIBGNOMEVFS OFF)
endif() endif()
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)

View File

@@ -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)

View File

@@ -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.

View File

@@ -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()

View File

@@ -0,0 +1,24 @@
// cotire example project
#include "example.h"
#ifndef NDEBUG
#include <algorithm>
#include <iterator>
#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
}
}

View File

@@ -0,0 +1,10 @@
// cotire example project
#include <string>
namespace example {
std::string get_message();
}

View File

@@ -0,0 +1,17 @@
// cotire example project
#include "log.h"
#include <iostream>
namespace logging {
void error(const std::string& msg) {
std::cerr << msg << std::endl;
}
void info(const std::string& msg) {
std::cout << msg << std::endl;
}
}

View File

@@ -0,0 +1,10 @@
// cotire example project
#include <string>
namespace logging {
void error(const std::string& msg);
void info(const std::string& msg);
}

View File

@@ -0,0 +1,12 @@
// cotire example project main
#include <string>
#include "example.h"
#include "log.h"
int main()
{
std::string msg = example::get_message();
logging::info(msg);
}