Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentationHS14merge/cmake/tools/PrecompiledHeaderFiles.cmake @ 10256

Last change on this file since 10256 was 10187, checked in by landauf, 10 years ago

fixed dependency issue in cmake script (which also fixes a bunch of warnings with cmake 3.1). I hope this works because I don't really understand why it was done like that before.

  • Property svn:eol-style set to native
File size: 6.2 KB
Line 
1 #
2 #             ORXONOX - the hottest 3D action shooter ever to exist
3 #                             > www.orxonox.net <
4 #
5 #        This program is free software; you can redistribute it and/or
6 #         modify it under the terms of the GNU General Public License
7 #        as published by the Free Software Foundation; either version 2
8 #            of the License, or (at your option) any later version.
9 #
10 #       This program is distributed in the hope that it will be useful,
11 #        but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #                 GNU General Public License for more details.
14 #
15 #   You should have received a copy of the GNU General Public License along
16 #      with this program; if not, write to the Free Software Foundation,
17 #     Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 #
19 #
20 #  Author:
21 #    Reto Grieder
22 #
23
24INCLUDE(GetGCCCompilerFlags)
25
26MACRO(PRECOMPILED_HEADER_FILES_PRE_TARGET _target_name _header_file_arg _sourcefile_var)
27
28  GET_FILENAME_COMPONENT(_pch_header_file ${_header_file_arg} ABSOLUTE)
29  GET_FILENAME_COMPONENT(_pch_header_filename ${_pch_header_file} NAME)
30  GET_FILENAME_COMPONENT(_pch_header_filename_we ${_pch_header_file} NAME_WE)
31
32  IF(NOT EXISTS ${_pch_header_file})
33    MESSAGE(FATAL_ERROR "Specified precompiled headerfile '${_header_file_arg}' does not exist.")
34  ENDIF()
35
36  # Extract arguments from ARGN
37  FOREACH(_arg ${ARGN})
38    IF(NOT "${_arg}" STREQUAL "EXCLUDE")
39      IF(NOT _arg_second)
40        # Source files with PCH support
41        SET(_included_files ${_included_files} ${_arg})
42      ELSE()
43        # Source files to be excluded from PCH support (easier syntax this way)
44        SET(_excluded files ${_excluded_files} ${_arg})
45      ENDIF()
46    ELSE()
47      SET(_arg_second TRUE)
48    ENDIF()
49  ENDFOREACH(_arg)
50
51  # Use ${_sourcefile_var} if no files were specified explicitely
52  IF(NOT _included_files)
53    SET(_source_files ${${_sourcefile_var}})
54  ELSE()
55    SET(_source_files ${_included_files})
56  ENDIF()
57
58  # Exclude files (if specified)
59  FOREACH(_file ${_excluded_files})
60    LIST(FIND _source_files ${_file} _list_index)
61    IF(_list_index GREATER -1)
62      LIST(REMOVE_AT _source_files _list_index)
63    ELSE()
64      MESSAGE(FATAL_ERROR "Could not exclude file ${_file} in target ${_target_name}")
65    ENDIF()
66  ENDFOREACH(_file)
67
68  LIST(FIND ${_sourcefile_var} ${_pch_header_file} _list_index)
69  IF(_list_index EQUAL -1) # Header file could already be included with GET_ALL_HEADER_FILES
70    LIST(APPEND ${_sourcefile_var} ${_pch_header_file})
71  ENDIF()
72
73  IF(MSVC)
74
75    # Write and add one source file, which generates the precompiled header file
76    SET(_pch_source_file "${CMAKE_CURRENT_BINARY_DIR}/${_pch_header_filename_we}.cc")
77    IF(NOT EXISTS ${_pch_source_file})
78      FILE(WRITE ${_pch_source_file} "#include \"${_pch_header_file}\"\n")
79    ENDIF()
80    SET_SOURCE_FILES_PROPERTIES(_pch_source_file PROPERTIES GENERATED TRUE)
81    LIST(APPEND ${_sourcefile_var} ${_pch_source_file})
82
83    SET(_pch_file "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${_pch_header_filename}.pch")
84    # Set compile flags for generated source file
85    SET_SOURCE_FILES_PROPERTIES(${_pch_source_file} PROPERTIES COMPILE_FLAGS "/c /Yc\"${_pch_header_file}\" /Fp\"${_pch_file}\"")
86    # Set Compile flags for the other source files
87    FOREACH(_file ${_source_files})
88      GET_SOURCE_FILE_PROPERTY(_is_header ${_file} HEADER_FILE_ONLY)
89      IF(NOT _is_header)
90        GET_SOURCE_FILE_PROPERTY(_old_flags ${_file} COMPILE_FLAGS)
91        IF(NOT _old_flags)
92          SET(_old_flags "")
93        ENDIF()
94        SET_SOURCE_FILES_PROPERTIES(${_file} PROPERTIES COMPILE_FLAGS "${_old_flags} /FI\"${_pch_header_file}\" /Yu\"${_pch_header_file}\" /Fp\"${_pch_file}\"")
95      ENDIF(NOT _is_header)
96    ENDFOREACH(_file)
97
98  ELSEIF(CMAKE_COMPILER_IS_GNU)
99
100    # Append the gch-dir to make sure gcc finds the pch file
101    INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
102
103    # Set Compile flags for the other source files
104    FOREACH(_file ${_source_files})
105      GET_SOURCE_FILE_PROPERTY(_is_header ${_file} HEADER_FILE_ONLY)
106      IF(NOT _is_header)
107        GET_SOURCE_FILE_PROPERTY(_old_flags ${_file} COMPILE_FLAGS)
108        IF(NOT _old_flags)
109          SET(_old_flags "")
110        ENDIF()
111        SET_SOURCE_FILES_PROPERTIES(${_file} PROPERTIES
112          COMPILE_FLAGS "${_old_flags} -I ${CMAKE_CURRENT_SOURCE_DIR} -include ${_pch_header_filename}"
113          OBJECT_DEPENDS "${_pch_header_file}"
114        )
115      ENDIF(NOT _is_header)
116    ENDFOREACH(_file)
117
118  ENDIF()
119
120ENDMACRO(PRECOMPILED_HEADER_FILES_PRE_TARGET)
121
122FUNCTION(PRECOMPILED_HEADER_FILES_POST_TARGET _target_name)
123    # This macro is only necessary for GCC
124    IF(CMAKE_COMPILER_IS_GNU)
125
126      SET(_pch_file "${CMAKE_CURRENT_BINARY_DIR}/${_pch_header_filename}.gch")
127      SET(_pch_dep_helper_file "${CMAKE_CURRENT_BINARY_DIR}/${_target_name}PCHDependencyHelper.h")
128
129      # Workaround for distcc
130      IF(CMAKE_CXX_COMPILER_ARG1)
131        # remove leading space in compiler argument
132        STRING(REGEX REPLACE "^ +" "" _pch_cmake_cxx_compiler_arg1 "${CMAKE_CXX_COMPILER_ARG1}")
133      ENDIF()
134
135      # Get compiler flags of the source files
136      GET_GCC_COMPILER_FLAGS(${_target_name} _pch_gcc_flags)
137
138      # Make sure we recompile the pch file even if only the flags change
139      IF(NOT "${_pch_gcc_flags}" STREQUAL "${_INTERNAL_${_target_name}_PCH_GCC_FLAGS}" OR NOT EXISTS "${_pch_dep_helper_file}")
140        SET(_INTERNAL_${_target_name}_PCH_GCC_FLAGS "${_pch_gcc_flags}" CACHE INTERNAL "")
141        FILE(WRITE ${_pch_dep_helper_file} "/* ${_pch_gcc_flags} */")
142      ENDIF()
143
144      # Compile the header file
145      ADD_CUSTOM_COMMAND(
146        OUTPUT ${_pch_file}
147        COMMAND ${CMAKE_CXX_COMPILER}
148        ARGS ${pchsupport_compiler_cxx_arg1} ${_pch_gcc_flags} -c -x c++-header -o ${_pch_file} ${_pch_header_file}
149        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
150        DEPENDS ${_pch_dep_helper_file}
151        IMPLICIT_DEPENDS CXX ${_pch_header_file}
152        VERBATIM
153      )
154      ADD_CUSTOM_TARGET(${_target_name}PrecompiledHeader DEPENDS ${_pch_file})
155      ADD_DEPENDENCIES(${_target_name} ${_target_name}PrecompiledHeader)
156
157    ENDIF(CMAKE_COMPILER_IS_GNU)
158ENDFUNCTION(PRECOMPILED_HEADER_FILES_POST_TARGET)
Note: See TracBrowser for help on using the repository browser.