2 # DON'T FORGET to include ${CMAKE_CURRENT_SOURCE_DIR} in include_directories for the compiler
\r
3 # to see the header, and ${CMAKE_CURRENT_BINARY_DIR} for the compiler to see the GCC PCH
\r
5 # "sources" - unexpanded cmake variable holding all the source files
\r
6 # "includes" - unexpanded cmake variable holding all include paths the PCH needs to know about
\r
7 # "target_name" - the name of the a special target used to build the PCH for GCC
\r
8 # "header_name" - the name of the PCH header, without the extension; "stdafx" or something similar;
\r
9 # note that the source file compiling the header needs to have the same name
\r
10 macro( precompiled_header sources includes target_name header_name )
\r
12 # MSVC precompiled headers cmake code
\r
14 set_source_files_properties( ${header_name}.cpp PROPERTIES COMPILE_FLAGS "/Yc${header_name}.h" )
\r
16 foreach( src_file ${${sources}} )
\r
17 if( ${src_file} MATCHES ".*cpp$" )
\r
18 set_source_files_properties( ${src_file} PROPERTIES COMPILE_FLAGS "/Yu${header_name}.h" )
\r
22 # ${header_name}.cpp has to come before ${header_name}.h,
\r
23 # otherwise we get a linker error...
\r
24 list( INSERT ${sources} 0 ${header_name}.h )
\r
25 list( INSERT ${sources} 0 ${header_name}.cpp )
\r
27 # GCC precompiled headers cmake code
\r
28 # We don't do this on Macs since GCC there goes haywire
\r
29 # when you try to generate a PCH with two "-arch" flags
\r
30 elseif( CMAKE_COMPILER_IS_GNUCXX AND NOT APPLE )
\r
32 # Get the compiler flags for this build type
\r
33 string( TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" flags_for_build_name )
\r
34 set( compile_flags ${${flags_for_build_name}} )
\r
36 # Add all the Qt include directories
\r
37 foreach( item ${${includes}} )
\r
38 list( APPEND compile_flags "-I${item}" )
\r
41 # Get the list of all build-independent preprocessor definitions
\r
42 get_directory_property( defines_global COMPILE_DEFINITIONS )
\r
43 list( APPEND defines ${defines_global} )
\r
45 # Get the list of all build-dependent preprocessor definitions
\r
46 string( TOUPPER "COMPILE_DEFINITIONS_${CMAKE_BUILD_TYPE}" defines_for_build_name )
\r
47 get_directory_property( defines_build ${defines_for_build_name} )
\r
48 list( APPEND defines ${defines_build} )
\r
50 # Add the "-D" prefix to all of them
\r
51 foreach( item ${defines} )
\r
52 list( APPEND all_define_flags "-D${item}" )
\r
55 list( APPEND compile_flags ${all_define_flags} )
\r
57 # Prepare the compile flags var for passing to GCC
\r
58 separate_arguments( compile_flags )
\r
60 # Finally, build the precompiled header.
\r
61 # We don't add the buil command to add_custom_target
\r
62 # because that would force a PCH rebuild even when
\r
63 # the ${header_name}.h file hasn't changed. We add it to
\r
64 # a special add_custom_command to work around this problem.
\r
65 add_custom_target( ${target_name} ALL
\r
66 DEPENDS ${header_name}.h.gch
\r
69 add_custom_command( OUTPUT ${header_name}.h.gch
\r
70 COMMAND ${CMAKE_CXX_COMPILER} ${compile_flags} ${CMAKE_CURRENT_SOURCE_DIR}/${header_name}.h -o ${header_name}.h.gch
\r
71 MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${header_name}.h
\r
72 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
\r
77 # Xcode PCH support. Has to be called *AFTER* the target is created.
\r
78 # "header_name" - the name of the PCH header, without the extension; "stdafx" or something similar;
\r
79 # note that the source file compiling the header needs to have the same name
\r
80 macro( xcode_pch header_name )
\r
82 set_target_properties(
\r
85 XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/${PCH_NAME}.h"
\r
86 XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES"
\r