]> git.donarmstrong.com Git - flightcrew.git/blob - cmake_extras/CustomPCH.cmake
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / cmake_extras / CustomPCH.cmake
1 \r
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
4 \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
11 \r
12     # MSVC precompiled headers cmake code\r
13     if ( MSVC )\r
14         set_source_files_properties( ${header_name}.cpp PROPERTIES COMPILE_FLAGS "/Yc${header_name}.h" )\r
15             \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
19             endif()\r
20         endforeach()\r
21 \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
26 \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
31 \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
35         \r
36         # Add all the Qt include directories\r
37         foreach( item ${${includes}} )\r
38             list( APPEND compile_flags "-I${item}" )\r
39         endforeach()\r
40 \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
44 \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
49 \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
53         endforeach()\r
54         \r
55         list( APPEND compile_flags ${all_define_flags} ) \r
56         \r
57         # Prepare the compile flags var for passing to GCC\r
58         separate_arguments( compile_flags )\r
59         \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
67                          )\r
68         \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
73                             VERBATIM )\r
74     endif() \r
75 endmacro()\r
76 \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
81     if( APPLE )                   \r
82         set_target_properties(\r
83             ${PROJECT_NAME} \r
84             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
87         )\r
88     endif()\r
89 endmacro() \r