From 8821de2f11e4549de86823b3b6877b4e2713bbec Mon Sep 17 00:00:00 2001 From: "Evgeniy A. Dushistov" Date: Fri, 21 Aug 2020 00:18:38 +0300 Subject: [PATCH] speedup compilation with help of cmake 3.6 pch support Since cmake 3.16 there is cross-platform support of precompiled headers in cmake. So I add code to check version of cmake, and if it is recent enough build system switches to cmake's pch support. I tested with such command: touch qt/main.cpp && time ninja MAPS.ME Before: ~ 35 seconds on my machine (average) After: ~ 21 seconds What make this speedup possible: 1. With cmake's pch support generation of precompiled headers happens only if something changed, while current PCH script cause recompilation of headers every run of ninja. It takes ~ 4 seconds 2. Now it is possible to set precompiled headers per target, and I include map/framework.hpp for "qt" --- cmake/OmimHelpers.cmake | 137 +++++++++++++++++++---------------- qt/qt_precompiled_header.hpp | 30 ++++++++ 2 files changed, 105 insertions(+), 62 deletions(-) create mode 100644 qt/qt_precompiled_header.hpp diff --git a/cmake/OmimHelpers.cmake b/cmake/OmimHelpers.cmake index 3c5d2e7b7bb..65dec0507b8 100644 --- a/cmake/OmimHelpers.cmake +++ b/cmake/OmimHelpers.cmake @@ -273,74 +273,87 @@ function(add_pic_pch_target header pch_target_name endfunction() function(add_precompiled_headers header pch_target_name) - set(pch_flags_file "${CMAKE_BINARY_DIR}/${pch_target_name}_flags_file") - export_directory_flags("${pch_flags_file}") - set(compiler_flags "@${pch_flags_file}") - - # CMAKE_CXX_STANDARD 17 flags: - set(c_standard_flags "-std=c++17") - get_filename_component(pch_file_name ${header} NAME) - - add_pic_pch_target(${header} ${pch_target_name} ${pch_file_name} lib "-fPIC") - add_pic_pch_target(${header} ${pch_target_name} ${pch_file_name} exe "-fPIE") - - add_custom_target( - "${pch_target_name}" - COMMENT "Waiting for both lib and exe precompiled headers to build" - DEPENDS "${pch_target_name}_lib" "${pch_target_name}_exe" - ) - set_target_properties( - ${pch_target_name} - PROPERTIES - PCH_NAME - "${pch_file_name}" - ) + if(${CMAKE_VERSION} VERSION_LESS "3.16.7") + set(pch_flags_file "${CMAKE_BINARY_DIR}/${pch_target_name}_flags_file") + export_directory_flags("${pch_flags_file}") + set(compiler_flags "@${pch_flags_file}") + + # CMAKE_CXX_STANDARD 17 flags: + set(c_standard_flags "-std=c++17") + get_filename_component(pch_file_name ${header} NAME) + + add_pic_pch_target(${header} ${pch_target_name} ${pch_file_name} lib "-fPIC") + add_pic_pch_target(${header} ${pch_target_name} ${pch_file_name} exe "-fPIE") + + add_custom_target( + "${pch_target_name}" + COMMENT "Waiting for both lib and exe precompiled headers to build" + DEPENDS "${pch_target_name}_lib" "${pch_target_name}_exe" + ) + set_target_properties( + ${pch_target_name} + PROPERTIES + PCH_NAME + "${pch_file_name}" + ) + else() + message(STATUS "Use CMake embedded precompiled headers support") + set(OMIM_PCH_HEADER_NAME ${header} PARENT_SCOPE) + endif() endfunction() function(add_precompiled_headers_to_target target pch_target) - add_dependencies(${target} "${pch_target}") - get_property(sources TARGET ${target} PROPERTY SOURCES) - get_target_property(target_type ${target} TYPE) - get_target_property(pch_file_name ${pch_target} PCH_NAME) - - if (target_type STREQUAL "EXECUTABLE") - set(include_compiled_header_dir "${CMAKE_BINARY_DIR}/pch_exe") - # CMake automatically adds additional compile options after linking. - # For example '-fPIC' flag on skin_generator_tool, because it is linked to Qt libs. - # We force correct flag for executables. - set(additional_clang_flags "-fPIE") - endif() + if(${CMAKE_VERSION} VERSION_LESS "3.16.7") + add_dependencies(${target} "${pch_target}") + get_property(sources TARGET ${target} PROPERTY SOURCES) + get_target_property(target_type ${target} TYPE) + get_target_property(pch_file_name ${pch_target} PCH_NAME) + + if (target_type STREQUAL "EXECUTABLE") + set(include_compiled_header_dir "${CMAKE_BINARY_DIR}/pch_exe") + # CMake automatically adds additional compile options after linking. + # For example '-fPIC' flag on skin_generator_tool, because it is linked to Qt libs. + # We force correct flag for executables. + set(additional_clang_flags "-fPIE") + endif() - if (target_type MATCHES "LIBRARY") - set(include_compiled_header_dir "${CMAKE_BINARY_DIR}/pch_lib") - endif() + if (target_type MATCHES "LIBRARY") + set(include_compiled_header_dir "${CMAKE_BINARY_DIR}/pch_lib") + endif() - # Force gcc first search gch header in pch_exe/pch_lib: - target_include_directories( - ${target} - BEFORE - PUBLIC - ${include_compiled_header_dir} - ) + # Force gcc first search gch header in pch_exe/pch_lib: + target_include_directories( + ${target} + BEFORE + PUBLIC + ${include_compiled_header_dir} + ) - foreach(source ${sources}) - if(source MATCHES \\.\(cc|cpp|h|hpp\)$) - if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set_source_files_properties( - ${source} - PROPERTIES - COMPILE_FLAGS - "${additional_clang_flags} -include-pch \ + foreach(source ${sources}) + if(source MATCHES \\.\(cc|cpp|h|hpp\)$) + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set_source_files_properties( + ${source} + PROPERTIES + COMPILE_FLAGS + "${additional_clang_flags} -include-pch \ ${include_compiled_header_dir}/${pch_file_name}.${PCH_EXTENSION}" - ) - endif() - if (CMAKE_CXX_COMPILER_ID MATCHES "GNU") - set_source_files_properties( - ${source} - PROPERTIES - COMPILE_FLAGS "-include ${pch_file_name}" - ) + ) + endif() + if (CMAKE_CXX_COMPILER_ID MATCHES "GNU") + set_source_files_properties( + ${source} + PROPERTIES + COMPILE_FLAGS "-include ${pch_file_name}" + ) + endif() endif() - endif() - endforeach() + endforeach() + else() + if (target STREQUAL "desktop") + target_precompile_headers(${target} PRIVATE qt_precompiled_header.hpp) + else () + target_precompile_headers(${target} PUBLIC ${OMIM_PCH_HEADER_NAME}) + endif () + endif() endfunction() diff --git a/qt/qt_precompiled_header.hpp b/qt/qt_precompiled_header.hpp new file mode 100644 index 00000000000..505ce73b7df --- /dev/null +++ b/qt/qt_precompiled_header.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "testing/testing.hpp" + +#include "map/framework.hpp" + +#include "platform/platform.hpp" + +#include "geometry/point2d.hpp" + +#include "base/assert.hpp" +#include "base/logging.hpp" +#include "base/macros.hpp" +#include "base/stl_helpers.hpp" +#include "base/string_utils.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include