-
Notifications
You must be signed in to change notification settings - Fork 10
/
AddPCH.cmake
64 lines (52 loc) · 2.21 KB
/
AddPCH.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
# Alan Witkowski - https://github.com/jazztickets
# This macro adds a precompiled header to the project.
# It currently doesn't support mixing C/C++ files together
#
# target - name of target to apply pch to (e.g. ${CMAKE_PROJECT_NAME} )
# sourcecpp - path to pch source file (e.g. src/all.cpp)
# header - path to pch header file (e.g. src/all.h)
macro(add_pch target sourcecpp header)
# Extract header filename from path
get_filename_component(headerfile ${header} NAME)
# Handle visual studio.
if(MSVC)
set_target_properties(${target} PROPERTIES COMPILE_FLAGS "/Yu${headerfile}")
set_source_files_properties(${sourcecpp} PROPERTIES COMPILE_FLAGS "/Yc${headerfile}")
# Handle GCC
elseif(UNIX)
# Get compiler flags from build type
string(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" compiler_flags)
set(compiler_flags ${${compiler_flags}})
separate_arguments(compiler_flags)
# Build list of include dirs separated by -I
get_directory_property(include_dirs DIRECTORY ${CMAKE_SOURCE_DIR} INCLUDE_DIRECTORIES)
foreach(path ${include_dirs})
set(include_flag "${include_flag}-I${path} ")
endforeach()
separate_arguments(include_flag)
# Get definitions
get_directory_property(compile_defs DIRECTORY ${CMAKE_SOURCE_DIR} COMPILE_DEFINITIONS)
foreach(flag ${compile_defs})
set(compiledef_flag "${compiledef_flag}-D${flag} ")
endforeach()
separate_arguments(compiledef_flag)
# Add a dependency to main target
add_dependencies(${target} ${target}_pre)
# Create a target that depends on the pch
add_custom_target(${target}_pre DEPENDS ${EXECUTABLE_OUTPUT_PATH}/${headerfile}.gch)
# Create command to generate pch
add_custom_command(
OUTPUT ${EXECUTABLE_OUTPUT_PATH}/${headerfile}.gch
COMMAND ${CMAKE_CXX_COMPILER} ${compiledef_flag} ${include_flag} -xc++-header ${header} -o ${EXECUTABLE_OUTPUT_PATH}/${headerfile}.gch ${compiler_flags}
DEPENDS ${header}
)
# Create symlink for headerfile
add_custom_command(
TARGET ${target}_pre
COMMAND ${CMAKE_COMMAND} -E create_symlink ${header} ${EXECUTABLE_OUTPUT_PATH}/${headerfile}
DEPENDS ${header}
)
# Add pch directory to include path
include_directories(BEFORE ${EXECUTABLE_OUTPUT_PATH})
endif()
endmacro()