Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/forks/sandbox_qt/cmake/tools/TargetUtilities.cmake @ 7805

Last change on this file since 7805 was 7805, checked in by rgrieder, 13 years ago

Changed source file handling procedure, especially for Qt moc files.

  • Property svn:eol-style set to native
File size: 11.0 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 #  Description:
23 #    Adds a library or an executable like ADD_LIBRARY/ADD_EXECUTABLE, but
24 #    accepts a lot more input information. Simply supply the keywords
25 #    described below in any order you wish.
26 #    The output is then stored in "_arg_ARGNAME" where ARGNAME is the the
27 #    name of the switch or list.
28 #
29 #    Switches: (when given --> TRUE, FALSE otherwise)
30 #      FIND_HEADER_FILES: Searches the current directory for all header files
31 #                         and adds them to the target.
32 #      EXCLUDE_FROM_ALL:  Inherited from ADD_LIBRARY/ADD_EXECUTABLE
33 #      ORXONOX_EXTERNAL:  Specify this for third party libraries
34 #      NO_DLL_INTERFACE:  Link statically with MSVC
35 #      NO_SOURCE_GROUPS:  Don't create msvc source groups
36 #      WIN32:             Inherited from ADD_EXECUTABLE (executables only)
37 #      PCH_NO_DEFAULT:    Do not make precompiled header files default if
38 #                         specified with PCH_FILE
39 #      NO_INSTALL:        Do not install the target at all
40 #      NO_VERSION:        Prevents adding any version to a target
41 #
42 #    Lists:
43 #      LINK_LIBRARIES:    Redirects to TARGET_LINK_LIBRARIES
44 #      VERSION:           Set version to the binary
45 #      SOURCE_FILES:      Source files for the target
46 #      DEFINE_SYMBOL:     Sets the DEFINE_SYMBOL target property
47 #      PCH_FILE:          Precompiled header file
48 #      PCH_EXCLUDE:       Source files to be excluded from PCH support
49 #      OUTPUT_NAME:       If you want a different name than the target name
50 #  Note:
51 #    This function also installs the target!
52 #  Prerequisistes:
53 #    ORXONOX_DEFAULT_LINK, ORXONOX_CONFIG_FILES
54 #  Parameters:
55 #    _target_name, ARGN for the macro arguments
56 #
57
58INCLUDE(CMakeDependentOption)
59INCLUDE(CapitaliseName)
60INCLUDE(ParseMacroArguments)
61INCLUDE(SourceFileUtilities)
62IF(PCH_COMPILER_SUPPORT)
63  INCLUDE(PrecompiledHeaderFiles)
64ENDIF()
65
66MACRO(ORXONOX_ADD_LIBRARY _target_name)
67  TU_ADD_TARGET(${_target_name} LIBRARY "" ${ARGN})
68ENDMACRO(ORXONOX_ADD_LIBRARY)
69
70MACRO(ORXONOX_ADD_EXECUTABLE _target_name)
71  TU_ADD_TARGET(${_target_name} EXECUTABLE "WIN32" ${ARGN})
72ENDMACRO(ORXONOX_ADD_EXECUTABLE)
73
74
75MACRO(TU_ADD_TARGET _target_name _target_type _additional_switches)
76  CAPITALISE_NAME(${_target_name} _target_name_capitalised)
77  STRING(TOUPPER "${_target_name}" _target_name_upper)
78
79  # Specify all possible options (either switch or with add. arguments)
80  SET(_switches   FIND_HEADER_FILES  EXCLUDE_FROM_ALL  ORXONOX_EXTERNAL
81                  NO_DLL_INTERFACE   NO_SOURCE_GROUPS  PCH_NO_DEFAULT 
82                  NO_INSTALL         NO_VERSION        ${_additional_switches})
83  SET(_list_names LINK_LIBRARIES     VERSION           SOURCE_FILES
84                  DEFINE_SYMBOL      PCH_FILE          PCH_EXCLUDE
85                  OUTPUT_NAME)
86
87  PARSE_MACRO_ARGUMENTS("${_switches}" "${_list_names}" ${ARGN})
88
89
90  # Process source files with support for compilations and QT special files
91  # Note: All file paths are relative to the root source directory, even the
92  #       name of the compilation file.
93  SET(_${_target_name}_source_files)
94  SET(_get_compilation_file FALSE)
95  SET(_add_to_compilation FALSE)
96  SET(_compile_qt_next FALSE)
97  FOREACH(_file ${_arg_SOURCE_FILES})
98    IF(_file STREQUAL "COMPILATION_BEGIN")
99      # Next file is the name of the compilation
100      SET(_get_compilation_file TRUE)
101    ELSEIF(_file STREQUAL "COMPILATION_END")
102      IF(NOT _compilation_file)
103        MESSAGE(FATAL_ERROR "No name provided for source file compilation")
104      ENDIF()
105      IF(NOT _compilation_include_string)
106        MESSAGE(STATUS "Warning: Empty source file compilation!")
107      ENDIF()
108      IF(NOT DISABLE_COMPILATIONS)
109        IF(EXISTS ${_compilation_file})
110          FILE(READ ${_compilation_file} _include_string_file)
111        ENDIF()
112        IF(NOT _compilation_include_string STREQUAL "${_include_string_file}")
113          FILE(WRITE ${_compilation_file} "${_compilation_include_string}")
114        ENDIF()
115        LIST(APPEND _${_target_name}_source_files ${_compilation_file})
116      ENDIF()
117      SET(_add_to_compilation FALSE)
118    ELSEIF(_get_compilation_file)
119      SET(_compilation_file ${CMAKE_BINARY_DIR}/${_file})
120      SET(_get_compilation_file FALSE)
121      SET(_add_to_compilation TRUE)
122      SET(_compilation_include_string)
123    ELSEIF(_file STREQUAL "QT")
124      SET(_compile_qt_next TRUE)
125    ELSE()
126      # Default, add source file
127      SET(_file ${CMAKE_SOURCE_DIR}/${_file})
128      LIST(APPEND _${_target_name}_source_files ${_file})
129
130      # Handle compilations
131      IF(_add_to_compilation AND NOT DISABLE_COMPILATIONS)
132        IF(_file MATCHES "\\.(c|cc|cpp|cxx)$")
133          SET(_compilation_include_string "${_compilation_include_string}#include \"${_file}\"\n")
134        ENDIF()
135        # Don't compile these files, even if they are source files
136        SET_SOURCE_FILES_PROPERTIES(${_file} PROPERTIES HEADER_FILE_ONLY TRUE)
137      ENDIF()
138
139      # Handle Qt MOC and UI files
140      IF(_compile_qt_next)
141        GET_FILENAME_COMPONENT(_ext ${_file} EXT)
142        IF(_ext STREQUAL ".ui")
143          QT4_WRAP_UI(_${_target_name}_source_files ${_file})
144        ELSEIF(_ext STREQUAL ".qrc")
145          QT4_ADD_RESOURCES(_${_target_name}_source_files ${_file})
146        ELSEIF(_ext MATCHES "^\\.(h|hpp|hxx)$")
147          QT4_WRAP_CPP(_${_target_name}_source_files ${_file})
148        ENDIF()
149        SET(_compile_qt_next FALSE)
150      ENDIF()
151    ENDIF()
152  ENDFOREACH(_file)
153
154  # Assemble all header files of the library
155  IF(_arg_FIND_HEADER_FILES)
156    GET_ALL_HEADER_FILES(_${_target_name}_header_files)
157  ENDIF()
158
159  # Combine source and header files
160  SET(_${_target_name}_files
161    ${_${_target_name}_header_files}
162    ${_${_target_name}_source_files}
163  )
164  # Remove potential duplicates
165  LIST(REMOVE_DUPLICATES _${_target_name}_files)
166
167  # First part (pre target) of precompiled header files
168  IF(PCH_COMPILER_SUPPORT AND _arg_PCH_FILE)
169    # Provide convenient option to control PCH
170    IF(_arg_PCH_NO_DEFAULT)
171      SET(PCH_DEFAULT FALSE)
172    ELSE()
173      SET(PCH_DEFAULT TRUE)
174    ENDIF()
175    CMAKE_DEPENDENT_OPTION(PCH_ENABLE_${_target_name_upper}
176      "Enable using precompiled header files for library ${_target_name}." ${PCH_DEFAULT} PCH_ENABLE OFF)
177
178    IF(PCH_ENABLE_${_target_name_upper})
179      PRECOMPILED_HEADER_FILES_PRE_TARGET(${_target_name} ${_arg_PCH_FILE} _${_target_name}_files EXCLUDE ${_arg_PCH_EXCLUDE})
180    ENDIF()
181  ENDIF()
182
183  # Generate the source groups
184  IF(MSVC AND NOT _arg_NO_SOURCE_GROUPS)
185    GENERATE_SOURCE_GROUPS(${_${_target_name}_files})
186
187    IF(NOT _arg_ORXONOX_EXTERNAL)
188      # Move the prereqs.h file to the config section
189      IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_target_name_capitalised}Prereqs.h)
190        SOURCE_GROUP("Config" FILES ${_target_name_capitalised}Prereqs.h)
191      ENDIF()
192      # Add config files to the config section
193      LIST(APPEND _${_target_name}_files ${ORXONOX_CONFIG_FILES})
194      SOURCE_GROUP("Config" FILES ${ORXONOX_CONFIG_FILES})
195    ENDIF()
196  ENDIF()
197
198  # Set link mode (SHARED/STATIC)
199  IF(MSVC AND _arg_NO_DLL_INTERFACE)
200    # Certain libraries don't have dllexport/dllimport and can't be linked shared with MSVC
201    SET(_link_mode STATIC)
202  ELSEIF(_arg_ORXONOX_EXTERNAL)
203    # Externals can be linked shared or statically
204    SET(_link_mode ${ORXONOX_EXTERNAL_LINK_MODE})
205  ELSE()
206    # All our own libraries are linked dynamically because of static symbols
207    SET(_link_mode SHARED)
208  ENDIF()
209
210  # No warnings needed from third party libraries
211  IF(_arg_ORXONOX_EXTERNAL)
212    REMOVE_COMPILER_FLAGS("-W3 -W4" MSVC)
213    ADD_COMPILER_FLAGS("-w")
214  ENDIF()
215
216  # Don't compile header files
217  FOREACH(_file ${_${_target_name}_files})
218    IF(NOT _file MATCHES "\\.(c|cc|cpp|cxx)$")
219      SET_SOURCE_FILES_PROPERTIES(${_file} PROPERTIES HEADER_FILE_ONLY TRUE)
220    ENDIF()
221  ENDFOREACH(_file)
222
223
224
225  # Add the library/executable
226  IF("${_target_type}" STREQUAL "LIBRARY")
227    ADD_LIBRARY(${_target_name} ${_link_mode}
228                ${_arg_EXCLUDE_FROM_ALL} ${_${_target_name}_files})
229  ELSE()
230    ADD_EXECUTABLE(${_target_name} ${_arg_WIN32} ${_arg_EXCLUDE_FROM_ALL}
231                   ${_${_target_name}_files})
232  ENDIF()
233
234
235
236  # Change library prefix to "lib"
237  IF(MSVC AND ${_target_type} STREQUAL "LIBRARY")
238    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES
239      PREFIX "lib"
240    )
241  ENDIF()
242
243  # MSVC hack to exclude external library sources from the intellisense database
244  # (IntelliSense stops working when adding "-Zm1000" as compile flag. "/Zm1000"
245  # would not work because of the slash)
246  IF(_arg_ORXONOX_EXTERNAL AND MSVC)
247    GET_TARGET_PROPERTY(_compile_flags ${_target_name} COMPILE_FLAGS)
248    IF(NOT _compile_flags)
249      SET(_compile_flags)
250    ENDIF()
251    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES COMPILE_FLAGS "${_compile_flags} -Zm1000")
252  ENDIF()
253
254  # LINK_LIBRARIES
255  IF(_arg_LINK_LIBRARIES)
256    TARGET_LINK_LIBRARIES(${_target_name} ${_arg_LINK_LIBRARIES})
257  ENDIF()
258
259  # DEFINE_SYMBOL
260  IF(_arg_DEFINE_SYMBOL)
261    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES DEFINE_SYMBOL ${_arg_DEFINE_SYMBOL})
262  ELSEIF(NOT _arg_ORXONOX_EXTERNAL)
263    # Automatically add the macro definitions for our own libraries
264    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES DEFINE_SYMBOL "${_target_name_upper}_SHARED_BUILD")
265  ENDIF()
266
267  # VERSION
268  IF(_arg_VERSION)
269    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES VERSION ${_arg_VERSION})
270  ELSEIF(NOT _arg_ORXONOX_EXTERNAL AND NOT _arg_NO_VERSION AND NOT ${_target_type} STREQUAL "EXECUTABLE")
271    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES VERSION ${ORXONOX_VERSION})
272  ENDIF()
273
274  # OUTPUT_NAME
275  IF(_arg_OUTPUT_NAME)
276    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES OUTPUT_NAME  ${_arg_OUTPUT_NAME})
277  ENDIF()
278
279  # Second part of precompiled header files
280  IF(PCH_COMPILER_SUPPORT AND PCH_ENABLE_${_target_name_upper} AND _arg_PCH_FILE)
281    PRECOMPILED_HEADER_FILES_POST_TARGET(${_target_name} ${_arg_PCH_FILE})
282  ENDIF()
283
284  # Install all targets except for static ones (executables also have SHARED in _link_mode)
285  IF(${_link_mode} STREQUAL "SHARED" AND NOT _arg_NO_INSTALL)
286    INSTALL(TARGETS ${_target_name}
287      RUNTIME DESTINATION ${RUNTIME_INSTALL_DIRECTORY}
288      LIBRARY DESTINATION ${LIBRARY_INSTALL_DIRECTORY}
289    )
290  ENDIF()
291
292ENDMACRO(TU_ADD_TARGET)
Note: See TracBrowser for help on using the repository browser.