Fixed: #1447 PCH don't work anymore on Mac OS X

This commit is contained in:
kervala 2012-03-09 09:31:13 +01:00
parent 68fd549142
commit b7281e5cbe

View file

@ -8,10 +8,12 @@
# ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use # ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use
# ADD_NATIVE_PRECOMPILED_HEADER _targetName _inputh _inputcpp # ADD_NATIVE_PRECOMPILED_HEADER _targetName _inputh _inputcpp
IF(CMAKE_COMPILER_IS_GNUCXX) IF(MSVC)
SET(PCHSupport_FOUND TRUE)
EXEC_PROGRAM( SET(_PCH_include_prefix "/I")
${CMAKE_CXX_COMPILER} ELSE(MSVC)
IF(CMAKE_COMPILER_IS_GNUCXX)
EXEC_PROGRAM(${CMAKE_CXX_COMPILER}
ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion ARGS ${CMAKE_CXX_COMPILER_ARG1} -dumpversion
OUTPUT_VARIABLE gcc_compiler_version) OUTPUT_VARIABLE gcc_compiler_version)
@ -22,30 +24,24 @@ IF(CMAKE_COMPILER_IS_GNUCXX)
SET(PCHSupport_FOUND TRUE) SET(PCHSupport_FOUND TRUE)
ENDIF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]") ENDIF(gcc_compiler_version MATCHES "3\\.4\\.[0-9]")
ENDIF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]") ENDIF(gcc_compiler_version MATCHES "4\\.[0-9]\\.[0-9]")
ELSE(CMAKE_COMPILER_IS_GNUCXX)
# TODO: make tests for other compilers than GCC
SET(PCHSupport_FOUND TRUE)
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
SET(_PCH_include_prefix "-I") SET(_PCH_include_prefix "-I")
ENDIF(MSVC)
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) MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags)
STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name) STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
SET(${_out_compile_flags} ${${_flags_var_name}} ) SET(${_out_compile_flags} ${${_flags_var_name}} )
IF(CMAKE_COMPILER_IS_GNUCXX) IF(NOT MSVC)
GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE) GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
IF(${_targetType} STREQUAL SHARED_LIBRARY OR ${_targetType} STREQUAL MODULE_LIBRARY) IF(${_targetType} STREQUAL SHARED_LIBRARY OR ${_targetType} STREQUAL MODULE_LIBRARY)
LIST(APPEND ${_out_compile_flags} "-fPIC") LIST(APPEND ${_out_compile_flags} "-fPIC")
ENDIF(${_targetType} STREQUAL SHARED_LIBRARY OR ${_targetType} STREQUAL MODULE_LIBRARY) ENDIF(${_targetType} STREQUAL SHARED_LIBRARY OR ${_targetType} STREQUAL MODULE_LIBRARY)
ENDIF(CMAKE_COMPILER_IS_GNUCXX) ENDIF(NOT MSVC)
GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES ) GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES )
FOREACH(item ${DIRINC}) FOREACH(item ${DIRINC})
@ -100,17 +96,13 @@ MACRO(_PCH_GET_COMPILE_COMMAND out_command _input _inputcpp _output)
SET(pchsupport_compiler_cxx_arg1 "") SET(pchsupport_compiler_cxx_arg1 "")
ENDIF(CMAKE_CXX_COMPILER_ARG1) ENDIF(CMAKE_CXX_COMPILER_ARG1)
IF(CMAKE_COMPILER_IS_GNUCXX) IF(MSVC)
SET(${out_command}
${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} -c ${_input}
)
ELSE(CMAKE_COMPILER_IS_GNUCXX)
_PCH_GET_PDB_FILENAME(PDB_FILE ${_PCH_current_target}) _PCH_GET_PDB_FILENAME(PDB_FILE ${_PCH_current_target})
SET(${out_command} SET(${out_command} ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} /Yc /Fp\"${_output}\" ${_inputcpp} /c /Fd\"${PDB_FILE}\")
${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} /Yc /Fp\"${_output}\" ${_inputcpp} /c /Fd\"${PDB_FILE}\" ELSE(MSVC)
) SET(${out_command} ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} -c ${_input})
ENDIF(CMAKE_COMPILER_IS_GNUCXX) ENDIF(MSVC)
ENDMACRO(_PCH_GET_COMPILE_COMMAND ) ENDMACRO(_PCH_GET_COMPILE_COMMAND)
MACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input _output) MACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input _output)
IF(MSVC) IF(MSVC)
@ -128,7 +120,9 @@ MACRO(ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use )
SET(oldProps "") SET(oldProps "")
ENDIF(${oldProps} MATCHES NOTFOUND) ENDIF(${oldProps} MATCHES NOTFOUND)
IF(CMAKE_COMPILER_IS_GNUCXX) IF(MSVC)
SET(_target_cflags "${oldProps} /Yu\"${_input}\" /FI\"${_input}\" /Fp\"${_pch_output_to_use}\"")
ELSE(MSVC)
# to do: test whether compiler flags match between target _targetName # to do: test whether compiler flags match between target _targetName
# and _pch_output_to_use # and _pch_output_to_use
FILE(TO_NATIVE_PATH ${_pch_output_to_use} _native_pch_path) FILE(TO_NATIVE_PATH ${_pch_output_to_use} _native_pch_path)
@ -137,11 +131,7 @@ MACRO(ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use )
# on all remote machines set # on all remote machines set
# PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess # PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess
SET(_target_cflags "${oldProps} ${PCH_ADDITIONAL_COMPILER_FLAGS}-include ${_input} -Winvalid-pch") SET(_target_cflags "${oldProps} ${PCH_ADDITIONAL_COMPILER_FLAGS}-include ${_input} -Winvalid-pch")
ELSE(CMAKE_COMPILER_IS_GNUCXX)
IF(MSVC)
SET(_target_cflags "${oldProps} /Yu\"${_input}\" /FI\"${_input}\" /Fp\"${_pch_output_to_use}\"")
ENDIF(MSVC) ENDIF(MSVC)
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS ${_target_cflags}) SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS ${_target_cflags})
IF(oldProps) IF(oldProps)
@ -184,8 +174,31 @@ MACRO(ADD_PRECOMPILED_HEADER _targetName _inputh _inputcpp)
ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_inputh} ${_output}) ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_inputh} ${_output})
ENDMACRO(ADD_PRECOMPILED_HEADER) ENDMACRO(ADD_PRECOMPILED_HEADER)
# Macro to move PCH creation file to the front of files list
MACRO(FIX_PRECOMPILED_HEADER _files _pch)
# Remove .cpp creating PCH from the list
LIST(REMOVE_ITEM ${_files} ${_pch})
# Prepend .cpp creating PCH to the list
LIST(INSERT ${_files} 0 ${_pch})
ENDMACRO(FIX_PRECOMPILED_HEADER)
MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _inputh _inputcpp) MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _inputh _inputcpp)
IF(CMAKE_GENERATOR MATCHES Visual*) SET(PCH_METHOD 0)
# 0 => creating a new target for PCH, works for all makefiles
# 1 => setting PCH for VC++ project, works for VC++ projects
# 2 => setting PCH for XCode project, works for XCode projects
IF(CMAKE_GENERATOR MATCHES "Visual Studio")
SET(PCH_METHOD 1)
ELSEIF(CMAKE_GENERATOR MATCHES "NMake Makefiles" AND MFC_FOUND AND CMAKE_MFC_FLAG)
# To fix a bug with MFC
# Don't forget to use FIX_PRECOMPILED_HEADER before creating the target
# SET(PCH_METHOD 1)
ELSEIF(CMAKE_GENERATOR MATCHES "Xcode")
SET(PCH_METHOD 2)
ENDIF(CMAKE_GENERATOR MATCHES "Visual Studio")
IF(PCH_METHOD EQUAL 1)
# Auto include the precompile (useful for moc processing, since the use of # Auto include the precompile (useful for moc processing, since the use of
# precompiled is specified at the target level # precompiled is specified at the target level
# and I don't want to specifiy /F- for each moc/res/ui generated files (using Qt) # and I don't want to specifiy /F- for each moc/res/ui generated files (using Qt)
@ -200,8 +213,7 @@ MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _inputh _inputcpp)
#also inlude ${oldProps} to have the same compile options #also inlude ${oldProps} to have the same compile options
SET_SOURCE_FILES_PROPERTIES(${_inputcpp} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_inputh}\"") SET_SOURCE_FILES_PROPERTIES(${_inputcpp} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_inputh}\"")
ELSE(CMAKE_GENERATOR MATCHES Visual*) ELSEIF(PCH_METHOD EQUAL 2)
IF(CMAKE_GENERATOR MATCHES Xcode)
# For Xcode, cmake needs my patch to process # For Xcode, cmake needs my patch to process
# GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties # GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties
@ -216,10 +228,9 @@ MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _inputh _inputcpp)
SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}") SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}")
SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES") SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
ELSE(CMAKE_GENERATOR MATCHES Xcode) ELSE(PCH_METHOD EQUAL 1)
#Fallback to the "old" precompiled suppport #Fallback to the "old" precompiled suppport
ADD_PRECOMPILED_HEADER(${_targetName} ${_inputh} ${_inputcpp}) ADD_PRECOMPILED_HEADER(${_targetName} ${_inputh} ${_inputcpp})
ENDIF(CMAKE_GENERATOR MATCHES Xcode) ENDIF(PCH_METHOD EQUAL 1)
ENDIF(CMAKE_GENERATOR MATCHES Visual*)
ENDMACRO(ADD_NATIVE_PRECOMPILED_HEADER) ENDMACRO(ADD_NATIVE_PRECOMPILED_HEADER)