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

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