From 089405b354636caddea7a590b935472e19701db2 Mon Sep 17 00:00:00 2001 From: dutor <440396+dutor@users.noreply.github.com> Date: Tue, 31 Dec 2019 06:36:32 +0800 Subject: [PATCH] Fixed use of precompiled header (#1553) Currently, PCH does not take effect when building in the out-of-source way, since the .gch file, by design, must be at the same directory with the original header. This PR resolves this problem by generating the .gch file in the source tree, i.e. ${CMAKE_CURRENT_SOURCE_DIR}. At the mean time, the former implementation of FindPCHSupport does not handle the compile options properly, so that the options used for PCH generation and source compilation are different, which is erroneous. This PR also addresses this issue. --- cmake/FindPCHSupport.cmake | 36 +++++++++++++++++++----------- src/common/time/detail/TscHelper.h | 4 ++-- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/cmake/FindPCHSupport.cmake b/cmake/FindPCHSupport.cmake index 73072dd4690..955134adfdc 100644 --- a/cmake/FindPCHSupport.cmake +++ b/cmake/FindPCHSupport.cmake @@ -13,7 +13,7 @@ IF(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") EXEC_PROGRAM( ${CMAKE_CXX_COMPILER} - ARGS --version + ARGS --version OUTPUT_VARIABLE _compiler_output) STRING(REGEX REPLACE ".* ([0-9]\\.[0-9]\\.[0-9]) .*" "\\1" gcc_compiler_version ${_compiler_output}) @@ -36,9 +36,11 @@ MACRO(ADD_PRECOMPILED_HEADER _targetName _input) GET_FILENAME_COMPONENT(_name ${_input} NAME) SET(_source "${CMAKE_CURRENT_SOURCE_DIR}/${_input}") - SET(_output "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch") + SET(_output "${CMAKE_CURRENT_SOURCE_DIR}/${_name}.gch") STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name) SET(_compiler_FLAGS ${${_flags_var_name}}) + GET_DIRECTORY_PROPERTY(_directory_compile_options COMPILE_OPTIONS) + LIST(APPEND _compiler_FLAGS ${_directory_compile_options}) GET_DIRECTORY_PROPERTY(_directory_flags INCLUDE_DIRECTORIES) SET(_system_INCLUDE_DIRS "/usr/include" "/usr/local/include") @@ -47,36 +49,44 @@ MACRO(ADD_PRECOMPILED_HEADER _targetName _input) LIST(FIND _system_INCLUDE_DIRS ${item} _index) IF(NOT ${_index} EQUAL -1) continue() - ENDIF(NOT ${_index} EQUAL -1) + ENDIF() IF(item MATCHES "^${PROJECT_SOURCE_DIR}.*") # Directories in this project - IF(item MATCHES "^${PROJECT_SOURCE_DIR}/third-party.*") + IF(item MATCHES "^${PROJECT_SOURCE_DIR}/.*third-party.*") # third-party LIST(APPEND _compiler_FLAGS "-isystem ${item}") - ELSE(item MATCHES "^${PROJECT_SOURCE_DIR}/third-party.*") + ELSE() # Our own directories LIST(APPEND _compiler_FLAGS "-I ${item}") - ENDIF(item MATCHES "^${PROJECT_SOURCE_DIR}/third-party.*") - ELSE(item MATCHES "^${PROJECT_SOURCE_DIR}.*") + ENDIF() + ELSE() # All of others LIST(APPEND _compiler_FLAGS "-isystem ${item}") - ENDIF(item MATCHES "^${PROJECT_SOURCE_DIR}.*") + ENDIF() ENDFOREACH(item) - GET_DIRECTORY_PROPERTY(_directory_flags COMPILE_DEFINITIONS) + GET_DIRECTORY_PROPERTY(_directory_flags COMPILE_DEFINITIONS) FOREACH(item ${_directory_flags}) - LIST(APPEND _compiler_FLAGS "-D${item}") + IF(NOT ${item} MATCHES "^GIT_INFO_SHA") + LIST(APPEND _compiler_FLAGS "-D${item}") + ENDIF() ENDFOREACH(item) SEPARATE_ARGUMENTS(_compiler_FLAGS) MESSAGE("Precompile header file " ${_source} " into " ${_output}) + IF(CMAKE_CXX_EXTENSIONS OR NOT DEFINED CMAKE_CXX_EXTENSIONS) + SET(_cxx_standard "gnu++${CMAKE_CXX_STANDARD}") + ELSE() + SET(_cxx_standard "c++${CMAKE_CXX_STANDARD}") + ENDIF() ADD_CUSTOM_COMMAND( OUTPUT ${_output} COMMAND ${CMAKE_CXX_COMPILER} - ${_compiler_FLAGS} - -x c++-header - -o ${_output} ${_source} + ${_compiler_FLAGS} + -x c++-header + -std=${_cxx_standard} + -o ${_output} ${_source} MAIN_DEPENDENCY ${_source}) ADD_CUSTOM_TARGET(${_targetName}_gch DEPENDS ${_output}) ADD_DEPENDENCIES(${_targetName} ${_targetName}_gch) diff --git a/src/common/time/detail/TscHelper.h b/src/common/time/detail/TscHelper.h index 136c28cd3d3..27f3dc310d0 100644 --- a/src/common/time/detail/TscHelper.h +++ b/src/common/time/detail/TscHelper.h @@ -4,11 +4,11 @@ * attached with Common Clause Condition 1.0, found in the LICENSES directory. */ -#include "base/Base.h" - #ifndef COMMON_TIME_DETAIL_TSCHELPER_H_ #define COMMON_TIME_DETAIL_TSCHELPER_H_ +#include "base/Base.h" + namespace nebula { namespace time {