Changed: #880 Use PCH under Linux with CMake

This commit is contained in:
kervala 2010-06-25 13:42:21 +02:00
parent 79eb834080
commit 1bbfcbf53f

View file

@ -1,4 +1,4 @@
# - Try to find precompiled headers support for GCC 3.4 and 4.x
# - Try to find precompiled headers support for GCC 3.4 and 4.x (and MSVC)
# Once done this will define:
#
# Variable:
@ -12,51 +12,50 @@
IF(CMAKE_COMPILER_IS_GNUCXX)
EXEC_PROGRAM(
${CMAKE_CXX_COMPILER}
ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
OUTPUT_VARIABLE gcc_compiler_version)
#MESSAGE("GCC Version: ${gcc_compiler_version}")
IF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
SET(PCHSupport_FOUND TRUE)
ELSE(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
IF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
SET(PCHSupport_FOUND TRUE)
ENDIF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
ENDIF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
EXEC_PROGRAM(
${CMAKE_CXX_COMPILER}
ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
OUTPUT_VARIABLE gcc_compiler_version)
#MESSAGE("GCC Version: ${gcc_compiler_version}")
IF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
SET(PCHSupport_FOUND TRUE)
ELSE(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
IF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
SET(PCHSupport_FOUND TRUE)
ENDIF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
ENDIF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
SET(_PCH_include_prefix "-I")
ELSE(CMAKE_COMPILER_IS_GNUCXX)
IF(WIN32)
SET(PCHSupport_FOUND TRUE) # for experimental msvc support
SET(_PCH_include_prefix "/I")
ELSE(WIN32)
SET(PCHSupport_FOUND FALSE)
ENDIF(WIN32)
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags)
STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
SET(${_out_compile_flags} ${${_flags_var_name}} )
IF(CMAKE_COMPILER_IS_GNUCXX)
GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
IF(${_targetType} STREQUAL SHARED_LIBRARY)
LIST(APPEND ${_out_compile_flags} "${${_out_compile_flags}} -fPIC")
ENDIF(${_targetType} STREQUAL SHARED_LIBRARY)
GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
IF(${_targetType} STREQUAL SHARED_LIBRARY)
LIST(APPEND ${_out_compile_flags} "${${_out_compile_flags}} -fPIC")
ENDIF(${_targetType} STREQUAL SHARED_LIBRARY)
ELSE(CMAKE_COMPILER_IS_GNUCXX)
## TODO ... ? or does it work out of the box
## TODO ... ? or does it work out of the box
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES )
FOREACH(item ${DIRINC})
LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}${item}")
LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}${item}")
ENDFOREACH(item)
GET_DIRECTORY_PROPERTY(_directory_flags DEFINITIONS)
@ -68,18 +67,17 @@ MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags)
ENDMACRO(_PCH_GET_COMPILE_FLAGS)
MACRO(_PCH_WRITE_PCHDEP_CXX _targetName _include_file _dephelp)
SET(${_dephelp} ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch_dephelp.cxx)
FILE(WRITE ${${_dephelp}}
SET(${_dephelp} ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch_dephelp.cxx)
FILE(WRITE ${${_dephelp}}
"#include \"${_include_file}\"
int testfunction()
{
return 0;
return 0;
}
"
)
)
ENDMACRO(_PCH_WRITE_PCHDEP_CXX )
@ -90,18 +88,18 @@ MACRO(_PCH_GET_COMPILE_COMMAND out_command _input _output)
IF(CMAKE_COMPILER_IS_GNUCXX)
IF(CMAKE_CXX_COMPILER_ARG1)
# remove leading space in compiler argument
STRING(REGEX REPLACE "^ +" "" pchsupport_compiler_cxx_arg1 ${CMAKE_CXX_COMPILER_ARG1})
IF(CMAKE_CXX_COMPILER_ARG1)
# remove leading space in compiler argument
STRING(REGEX REPLACE "^ +" "" pchsupport_compiler_cxx_arg1 ${CMAKE_CXX_COMPILER_ARG1})
SET(${out_command}
${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
)
ELSE(CMAKE_CXX_COMPILER_ARG1)
SET(${out_command}
${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
)
ENDIF(CMAKE_CXX_COMPILER_ARG1)
SET(${out_command}
${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
)
ELSE(CMAKE_CXX_COMPILER_ARG1)
SET(${out_command}
${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}
)
ENDIF(CMAKE_CXX_COMPILER_ARG1)
ELSE(CMAKE_COMPILER_IS_GNUCXX)
SET(_dummy_str "#include <${_input}>")
@ -120,32 +118,32 @@ ENDMACRO(_PCH_GET_COMPILE_COMMAND )
MACRO(_PCH_GET_TARGET_COMPILE_FLAGS _cflags _header_name _pch_path _dowarn )
FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path)
FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path)
IF(CMAKE_COMPILER_IS_GNUCXX)
# for use with distcc and gcc >4.0.1 if preprocessed files are accessible
# on all remote machines set
# PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess
# if you want warnings for invalid header files (which is very inconvenient
# if you have different versions of the headers for different build types
# you may set _pch_dowarn
IF (_dowarn)
SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include ${CMAKE_CURRENT_BINARY_DIR}/${_header_name} -Winvalid-pch " )
ELSE (_dowarn)
SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include ${CMAKE_CURRENT_BINARY_DIR}/${_header_name} " )
ENDIF (_dowarn)
ELSE(CMAKE_COMPILER_IS_GNUCXX)
IF(CMAKE_COMPILER_IS_GNUCXX)
# for use with distcc and gcc >4.0.1 if preprocessed files are accessible
# on all remote machines set
# PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess
# if you want warnings for invalid header files (which is very inconvenient
# if you have different versions of the headers for different build types
# you may set _pch_dowarn
IF (_dowarn)
SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include ${CMAKE_CURRENT_BINARY_DIR}/${_header_name} -Winvalid-pch " )
ELSE (_dowarn)
SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -include ${CMAKE_CURRENT_BINARY_DIR}/${_header_name} " )
ENDIF (_dowarn)
ELSE(CMAKE_COMPILER_IS_GNUCXX)
set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" )
set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" )
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
ENDMACRO(_PCH_GET_TARGET_COMPILE_FLAGS )
MACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input _output)
GET_FILENAME_COMPONENT(_name ${_input} NAME)
GET_FILENAME_COMPONENT(_path ${_input} PATH)
SET(_output "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch/${_targetName}_${CMAKE_BUILD_TYPE}.h++")
SET(_output "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch/${_targetName}_${CMAKE_BUILD_TYPE}.gch")
ENDMACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input)
@ -156,22 +154,22 @@ MACRO(ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use )
GET_FILENAME_COMPONENT(_name ${_input} NAME)
IF( "${ARGN}" STREQUAL "0")
SET(_dowarn 0)
SET(_dowarn 0)
ELSE( "${ARGN}" STREQUAL "0")
SET(_dowarn 1)
SET(_dowarn 1)
ENDIF("${ARGN}" STREQUAL "0")
_PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_pch_output_to_use} ${_dowarn})
# MESSAGE("Add flags ${_target_cflags} to ${_targetName} " )
SET_TARGET_PROPERTIES(${_targetName}
PROPERTIES
COMPILE_FLAGS ${_target_cflags}
)
_PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_pch_output_to_use} ${_dowarn})
# MESSAGE("Add flags ${_target_cflags} to ${_targetName} " )
SET_TARGET_PROPERTIES(${_targetName}
PROPERTIES
COMPILE_FLAGS ${_target_cflags}
)
ADD_CUSTOM_TARGET(pch_Generate_${_targetName}
DEPENDS ${_pch_output_to_use}
)
ADD_CUSTOM_TARGET(pch_Generate_${_targetName}
DEPENDS ${_pch_output_to_use}
)
ADD_DEPENDENCIES(${_targetName} pch_Generate_${_targetName} )
@ -182,19 +180,18 @@ MACRO(ADD_PRECOMPILED_HEADER _targetName _input)
SET(_PCH_current_target ${_targetName})
IF(NOT CMAKE_BUILD_TYPE)
MESSAGE(FATAL_ERROR
"This is the ADD_PRECOMPILED_HEADER macro. "
"You must set CMAKE_BUILD_TYPE!"
)
MESSAGE(FATAL_ERROR
"This is the ADD_PRECOMPILED_HEADER macro. "
"You must set CMAKE_BUILD_TYPE!"
)
ENDIF(NOT CMAKE_BUILD_TYPE)
IF( "${ARGN}" STREQUAL "0")
SET(_dowarn 0)
SET(_dowarn 0)
ELSE( "${ARGN}" STREQUAL "0")
SET(_dowarn 1)
SET(_dowarn 1)
ENDIF("${ARGN}" STREQUAL "0")
GET_FILENAME_COMPONENT(_name ${_input} NAME)
GET_FILENAME_COMPONENT(_path ${_input} PATH)
GET_PRECOMPILED_HEADER_OUTPUT( ${_targetName} ${_input} _output)
@ -205,14 +202,13 @@ MACRO(ADD_PRECOMPILED_HEADER _targetName _input)
_PCH_WRITE_PCHDEP_CXX(${_targetName} ${_input} _pch_dephelp_cxx)
IF(${_targetType} STREQUAL SHARED_LIBRARY)
ADD_LIBRARY(${_targetName}_pch_dephelp SHARED ${_pch_dephelp_cxx} )
ADD_LIBRARY(${_targetName}_pch_dephelp SHARED ${_pch_dephelp_cxx} )
ELSE(${_targetType} STREQUAL SHARED_LIBRARY)
ADD_LIBRARY(${_targetName}_pch_dephelp STATIC ${_pch_dephelp_cxx})
ADD_LIBRARY(${_targetName}_pch_dephelp STATIC ${_pch_dephelp_cxx})
ENDIF(${_targetType} STREQUAL SHARED_LIBRARY)
FILE(MAKE_DIRECTORY ${_outdir})
_PCH_GET_COMPILE_FLAGS(_compile_FLAGS)
#MESSAGE("_compile_FLAGS: ${_compile_FLAGS}")
@ -231,16 +227,14 @@ MACRO(ADD_PRECOMPILED_HEADER _targetName _input)
#message("_output ${_output}")
ADD_CUSTOM_COMMAND(
OUTPUT ${_output}
COMMAND ${_command}
DEPENDS ${_input} ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_targetName}_pch_dephelp
)
OUTPUT ${_output}
COMMAND ${_command}
DEPENDS ${_input} ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_targetName}_pch_dephelp
)
ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_input} ${_output} ${_dowarn})
ENDMACRO(ADD_PRECOMPILED_HEADER)
# Generates the use of precompiled in a target,
# without using depency targets (2 extra for each target)
# Using Visual, must also add ${_targetName}_pch to sources
@ -251,8 +245,8 @@ MACRO(GET_NATIVE_PRECOMPILED_HEADER _targetName _input)
if(CMAKE_GENERATOR MATCHES Visual*)
SET(_dummy_str "#include \"${_input}\"\n"
"// This is required to suppress LNK4221. Very annoying.\n"
"void *g_${_targetName}Dummy = 0\;\n")
"// This is required to suppress LNK4221. Very annoying.\n"
"void *g_${_targetName}Dummy = 0\;\n")
# Use of cxx extension for generated files (as Qt does)
SET(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cxx)
@ -266,7 +260,6 @@ MACRO(GET_NATIVE_PRECOMPILED_HEADER _targetName _input)
ENDMACRO(GET_NATIVE_PRECOMPILED_HEADER)
MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _input)
IF( "${ARGN}" STREQUAL "0")