Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fixed problem with files directly supplied to ORXONOX_ADD_TARGET.

  • Property svn:eol-style set to native
File size: 11.8 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
128      # Prepare relative paths
129      IF(NOT _file MATCHES "^(.\\:|\\/)")
130        # Path can be relative to the current source directory if the file was
131        # not added with the source file macros. Otherwise there is a "./" at
132        # the beginning of each file and the filename is relative
133        # to the CMAKE_SOURCE_DIR
134        STRING(REGEX REPLACE "^\\.\\/(.+)$" "\\1" _temp ${_file})
135        IF(NOT ${_temp} STREQUAL ${_file})
136          SET(_file ${CMAKE_SOURCE_DIR}/${_temp})
137        ELSE()
138          SET(_file ${CMAKE_CURRENT_SOURCE_DIR}/${_file})
139        ENDIF()
140      ENDIF()
141
142      LIST(APPEND _${_target_name}_source_files ${_file})
143
144      # Handle compilations
145      IF(_add_to_compilation AND NOT DISABLE_COMPILATIONS)
146        IF(_file MATCHES "\\.(c|cc|cpp|cxx)$")
147          SET(_compilation_include_string "${_compilation_include_string}#include \"${_file}\"\n")
148        ENDIF()
149        # Don't compile these files, even if they are source files
150        SET_SOURCE_FILES_PROPERTIES(${_file} PROPERTIES HEADER_FILE_ONLY TRUE)
151      ENDIF()
152
153      # Handle Qt MOC and UI files
154      IF(_compile_qt_next)
155        GET_FILENAME_COMPONENT(_ext ${_file} EXT)
156        IF(_ext STREQUAL ".ui")
157          QT4_WRAP_UI(_${_target_name}_source_files ${_file})
158        ELSEIF(_ext STREQUAL ".qrc")
159          QT4_ADD_RESOURCES(_${_target_name}_source_files ${_file})
160        ELSEIF(_ext MATCHES "^\\.(h|hpp|hxx)$")
161          QT4_WRAP_CPP(_${_target_name}_source_files ${_file})
162        ENDIF()
163        SET(_compile_qt_next FALSE)
164      ENDIF()
165    ENDIF()
166  ENDFOREACH(_file)
167
168  # Assemble all header files of the library
169  IF(_arg_FIND_HEADER_FILES)
170    GET_ALL_HEADER_FILES(_${_target_name}_header_files)
171  ENDIF()
172
173  # Combine source and header files
174  SET(_${_target_name}_files
175    ${_${_target_name}_header_files}
176    ${_${_target_name}_source_files}
177  )
178  # Remove potential duplicates
179  LIST(REMOVE_DUPLICATES _${_target_name}_files)
180
181  # First part (pre target) of precompiled header files
182  IF(PCH_COMPILER_SUPPORT AND _arg_PCH_FILE)
183    # Provide convenient option to control PCH
184    IF(_arg_PCH_NO_DEFAULT)
185      SET(PCH_DEFAULT FALSE)
186    ELSE()
187      SET(PCH_DEFAULT TRUE)
188    ENDIF()
189    CMAKE_DEPENDENT_OPTION(PCH_ENABLE_${_target_name_upper}
190      "Enable using precompiled header files for library ${_target_name}." ${PCH_DEFAULT} PCH_ENABLE OFF)
191
192    IF(PCH_ENABLE_${_target_name_upper})
193      PRECOMPILED_HEADER_FILES_PRE_TARGET(${_target_name} ${_arg_PCH_FILE} _${_target_name}_files EXCLUDE ${_arg_PCH_EXCLUDE})
194    ENDIF()
195  ENDIF()
196
197  # Generate the source groups
198  IF(MSVC AND NOT _arg_NO_SOURCE_GROUPS)
199    GENERATE_SOURCE_GROUPS(${_${_target_name}_files})
200
201    IF(NOT _arg_ORXONOX_EXTERNAL)
202      # Move the prereqs.h file to the config section
203      IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_target_name_capitalised}Prereqs.h)
204        SOURCE_GROUP("Config" FILES ${_target_name_capitalised}Prereqs.h)
205      ENDIF()
206      # Add config files to the config section
207      LIST(APPEND _${_target_name}_files ${ORXONOX_CONFIG_FILES})
208      SOURCE_GROUP("Config" FILES ${ORXONOX_CONFIG_FILES})
209    ENDIF()
210  ENDIF()
211
212  # Set link mode (SHARED/STATIC)
213  IF(MSVC AND _arg_NO_DLL_INTERFACE)
214    # Certain libraries don't have dllexport/dllimport and can't be linked shared with MSVC
215    SET(_link_mode STATIC)
216  ELSEIF(_arg_ORXONOX_EXTERNAL)
217    # Externals can be linked shared or statically
218    SET(_link_mode ${ORXONOX_EXTERNAL_LINK_MODE})
219  ELSE()
220    # All our own libraries are linked dynamically because of static symbols
221    SET(_link_mode SHARED)
222  ENDIF()
223
224  # No warnings needed from third party libraries
225  IF(_arg_ORXONOX_EXTERNAL)
226    REMOVE_COMPILER_FLAGS("-W3 -W4" MSVC)
227    ADD_COMPILER_FLAGS("-w")
228  ENDIF()
229
230  # Don't compile header files
231  FOREACH(_file ${_${_target_name}_files})
232    IF(NOT _file MATCHES "\\.(c|cc|cpp|cxx)$")
233      SET_SOURCE_FILES_PROPERTIES(${_file} PROPERTIES HEADER_FILE_ONLY TRUE)
234    ENDIF()
235  ENDFOREACH(_file)
236
237
238
239  # Add the library/executable
240  IF("${_target_type}" STREQUAL "LIBRARY")
241    ADD_LIBRARY(${_target_name} ${_link_mode}
242                ${_arg_EXCLUDE_FROM_ALL} ${_${_target_name}_files})
243  ELSE()
244    ADD_EXECUTABLE(${_target_name} ${_arg_WIN32} ${_arg_EXCLUDE_FROM_ALL}
245                   ${_${_target_name}_files})
246  ENDIF()
247
248
249
250  # Change library prefix to "lib"
251  IF(MSVC AND ${_target_type} STREQUAL "LIBRARY")
252    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES
253      PREFIX "lib"
254    )
255  ENDIF()
256
257  # MSVC hack to exclude external library sources from the intellisense database
258  # (IntelliSense stops working when adding "-Zm1000" as compile flag. "/Zm1000"
259  # would not work because of the slash)
260  IF(_arg_ORXONOX_EXTERNAL AND MSVC)
261    GET_TARGET_PROPERTY(_compile_flags ${_target_name} COMPILE_FLAGS)
262    IF(NOT _compile_flags)
263      SET(_compile_flags)
264    ENDIF()
265    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES COMPILE_FLAGS "${_compile_flags} -Zm1000")
266  ENDIF()
267
268  # Static library flags are not globally available
269  IF(ORXONOX_STATIC_LINKER_FLAGS)
270    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES STATIC_LIBRARY_FLAGS ${ORXONOX_STATIC_LINKER_FLAGS})
271  ENDIF()
272
273  # LINK_LIBRARIES
274  IF(_arg_LINK_LIBRARIES)
275    TARGET_LINK_LIBRARIES(${_target_name} ${_arg_LINK_LIBRARIES})
276  ENDIF()
277
278  # DEFINE_SYMBOL
279  IF(_arg_DEFINE_SYMBOL)
280    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES DEFINE_SYMBOL ${_arg_DEFINE_SYMBOL})
281  ELSEIF(NOT _arg_ORXONOX_EXTERNAL)
282    # Automatically add the macro definitions for our own libraries
283    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES DEFINE_SYMBOL "${_target_name_upper}_SHARED_BUILD")
284  ENDIF()
285
286  # VERSION
287  IF(_arg_VERSION)
288    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES VERSION ${_arg_VERSION})
289  ELSEIF(NOT _arg_ORXONOX_EXTERNAL AND NOT _arg_NO_VERSION AND NOT ${_target_type} STREQUAL "EXECUTABLE")
290    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES VERSION ${ORXONOX_VERSION})
291  ENDIF()
292
293  # OUTPUT_NAME
294  IF(_arg_OUTPUT_NAME)
295    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES OUTPUT_NAME  ${_arg_OUTPUT_NAME})
296  ENDIF()
297
298  # Second part of precompiled header files
299  IF(PCH_COMPILER_SUPPORT AND PCH_ENABLE_${_target_name_upper} AND _arg_PCH_FILE)
300    PRECOMPILED_HEADER_FILES_POST_TARGET(${_target_name} ${_arg_PCH_FILE})
301  ENDIF()
302
303  # Install all targets except for static ones (executables also have SHARED in _link_mode)
304  IF(${_link_mode} STREQUAL "SHARED" AND NOT _arg_NO_INSTALL)
305    INSTALL(TARGETS ${_target_name}
306      RUNTIME DESTINATION ${RUNTIME_INSTALL_DIRECTORY}
307      LIBRARY DESTINATION ${LIBRARY_INSTALL_DIRECTORY}
308    )
309  ENDIF()
310
311ENDMACRO(TU_ADD_TARGET)
Note: See TracBrowser for help on using the repository browser.