-
Notifications
You must be signed in to change notification settings - Fork 8
/
precompiled_headers.cmake
91 lines (74 loc) · 3.5 KB
/
precompiled_headers.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#
# Christoph Heindl 2010
# Precompiled Headers Demo
# http://cheind.wordpress.com
#
# Instructs the MSVC toolset to use the precompiled header PRECOMPILED_HEADER
# for each source file given in the collection named by SOURCE_VARIABLE_NAME.
function(enable_precompiled_headers_msvc PRECOMPILED_HEADER SOURCE_VARIABLE_NAME)
if(WITH_HEADER_PRECOMP)
if(MSVC_IDE)
set(files ${${SOURCE_VARIABLE_NAME}})
# Generate precompiled header translation unit
get_filename_component(pch_basename ${PRECOMPILED_HEADER} NAME_WE)
set(pch_abs ${PRECOMPILED_HEADER})
set(pch_unity ${pch_basename}.cpp)
FILE(WRITE ${pch_unity} "// Precompiled header unity generated by CMake\n")
FILE(APPEND ${pch_unity} "#include <${pch_abs}>\n")
set_source_files_properties(${pch_unity} PROPERTIES COMPILE_FLAGS "/Yc\"${pch_abs}\"")
# Update properties of source files to use the precompiled header.
# Additionally, force the inclusion of the precompiled header at beginning of each source file.
foreach(source_file ${files} )
set_source_files_properties(
${source_file}
PROPERTIES COMPILE_FLAGS
"/Yu\"${pch_abs}\" /FI\"${pch_abs}\""
)
endforeach(source_file)
# Finally, update the source file collection to contain the precompiled header translation unit
set(${SOURCE_VARIABLE_NAME} ${${SOURCE_VARIABLE_NAME}} ${pch_unity} PARENT_SCOPE)
endif(MSVC_IDE)
endif(WITH_HEADER_PRECOMP)
endfunction(enable_precompiled_headers_msvc)
function(enable_precompiled_headers_GCC PRECOMPILED_HEADER TARGET_NAME EXTRA_CXX_FLAGS)
if(WITH_HEADER_PRECOMP)
#ne marche pas avec Clang
# if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
# SET(EXT_HP pth)
# SET(OPTION_HP "-ccl -emit-pth")
# endif()
if(CMAKE_COMPILER_IS_GNUCXX)
#Message("Gcc")
SET(EXT_HP gch)
SET(OPTION_HP "-x c++-header")
endif()
# IF(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
IF(CMAKE_COMPILER_IS_GNUCXX)
GET_FILENAME_COMPONENT(_name ${PRECOMPILED_HEADER} NAME)
#SET(_source "${CMAKE_CURRENT_SOURCE_DIR}/${PRECOMPILED_HEADER}")
SET(_source "${PROJECT_SOURCE_DIR}/include/${PRECOMPILED_HEADER}")
#SET(_outdir "${CMAKE_CURRENT_SOURCE_DIR}/${_name}.${EXT_HP}")
SET(_outdir "${PROJECT_SOURCE_DIR}/include/${_name}.${EXT_HP}")
MAKE_DIRECTORY(${_outdir})
SET(_output "${_outdir}/.c++")
STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
SET(_compiler_FLAGS ${${_flags_var_name}} ${EXTRA_CXX_FLAGS})
GET_DIRECTORY_PROPERTY(_directory_flags INCLUDE_DIRECTORIES)
FOREACH(item ${_directory_flags})
LIST(APPEND _compiler_FLAGS "-I${item}")
ENDFOREACH(item)
GET_DIRECTORY_PROPERTY(_directory_flags DEFINITIONS)
LIST(APPEND _compiler_FLAGS ${_directory_flags})
SEPARATE_ARGUMENTS(_compiler_FLAGS)
#MESSAGE("${CMAKE_CXX_COMPILER} ${_compiler_FLAGS} ${OPTION_HP} -o ${_output} ${_source}")
ADD_CUSTOM_COMMAND(
OUTPUT ${_output}
COMMAND ${CMAKE_CXX_COMPILER} ${_compiler_FLAGS} -x c++-header -o ${_output} ${_source}
DEPENDS ${_source} IMPLICIT_DEPENDS CXX ${_source})
ADD_CUSTOM_TARGET(${TARGET_NAME}_${EXT_HP} DEPENDS ${_output})
ADD_DEPENDENCIES(${TARGET_NAME} ${TARGET_NAME}_${EXT_HP})
SET_TARGET_PROPERTIES(${TARGET_NAME}_${EXT_HP} PROPERTIES COMPILE_FLAGS "-include ${_name} -Winvalid-pch")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
#endif()
endif(WITH_HEADER_PRECOMP)
endfunction(enable_precompiled_headers_GCC)