Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7588 was 7421, checked in by rgrieder, 14 years ago

Basic stuff up and running for the Qt sandbox.
No GUI support yet.

  • Property svn:eol-style set to native
File size: 9.3 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 #      QT_MOC_FILES:      List of files to be processed by Qt MOC
51 #      QT_UIC_FILES:      List of files to be processed by Qt UIC
52 #  Note:
53 #    This function also installs the target!
54 #  Prerequisistes:
55 #    ORXONOX_DEFAULT_LINK, ORXONOX_CONFIG_FILES
56 #  Parameters:
57 #    _target_name, ARGN for the macro arguments
58 #
59
60INCLUDE(CMakeDependentOption)
61INCLUDE(CapitaliseName)
62INCLUDE(ParseMacroArguments)
63INCLUDE(SourceFileUtilities)
64IF(PCH_COMPILER_SUPPORT)
65  INCLUDE(PrecompiledHeaderFiles)
66ENDIF()
67
68MACRO(ORXONOX_ADD_LIBRARY _target_name)
69  TU_ADD_TARGET(${_target_name} LIBRARY "" ${ARGN})
70ENDMACRO(ORXONOX_ADD_LIBRARY)
71
72MACRO(ORXONOX_ADD_EXECUTABLE _target_name)
73  TU_ADD_TARGET(${_target_name} EXECUTABLE "WIN32" ${ARGN})
74ENDMACRO(ORXONOX_ADD_EXECUTABLE)
75
76
77MACRO(TU_ADD_TARGET _target_name _target_type _additional_switches)
78  CAPITALISE_NAME(${_target_name} _target_name_capitalised)
79  STRING(TOUPPER "${_target_name}" _target_name_upper)
80
81  # Specify all possible options (either switch or with add. arguments)
82  SET(_switches   FIND_HEADER_FILES  EXCLUDE_FROM_ALL  ORXONOX_EXTERNAL
83                  NO_DLL_INTERFACE   NO_SOURCE_GROUPS  PCH_NO_DEFAULT 
84                  NO_INSTALL         NO_VERSION        ${_additional_switches})
85  SET(_list_names LINK_LIBRARIES     VERSION           SOURCE_FILES
86                  DEFINE_SYMBOL      PCH_FILE          PCH_EXCLUDE
87                  OUTPUT_NAME        QT_MOC_FILES      QT_UIC_FILES)
88
89  PARSE_MACRO_ARGUMENTS("${_switches}" "${_list_names}" ${ARGN})
90
91
92  # Workaround: Source file properties get lost when leaving a subdirectory
93  # Therefore an "H" after a file means we have to set it as HEADER_FILE_ONLY
94  FOREACH(_file ${_arg_SOURCE_FILES})
95    IF(_file STREQUAL "H")
96      SET_SOURCE_FILES_PROPERTIES(${_last_file} PROPERTIES HEADER_FILE_ONLY TRUE)
97    ELSE()
98      SET(_last_file ${_file})
99      LIST(APPEND _${_target_name}_source_files ${_file})
100    ENDIF()
101  ENDFOREACH(_file)
102
103  # Assemble all header files of the library
104  IF(_arg_FIND_HEADER_FILES)
105    GET_ALL_HEADER_FILES(_${_target_name}_header_files)
106  ENDIF()
107
108  # Combine source, header and QT designer files
109  SET(_${_target_name}_files
110    ${_${_target_name}_header_files}
111    ${_${_target_name}_source_files}
112    ${_arg_QT_UIC_FILES}
113  )
114  # Remove potential duplicates
115  LIST(REMOVE_DUPLICATES _${_target_name}_files)
116
117  # QT MOC and UIC preprocessing
118  IF(_arg_QT_MOC_FILES)
119      QT4_WRAP_CPP(_${_target_name}_files ${_arg_QT_MOC_FILES})
120  ENDIF()
121  IF(_arg_QT_UIC_FILES)
122      QT4_WRAP_UI(_${_target_name}_files ${_arg_QT_UIC_FILES})
123  ENDIF()
124
125  # First part (pre target) of precompiled header files
126  IF(PCH_COMPILER_SUPPORT AND _arg_PCH_FILE)
127    # Provide convenient option to control PCH
128    IF(_arg_PCH_NO_DEFAULT)
129      SET(PCH_DEFAULT FALSE)
130    ELSE()
131      SET(PCH_DEFAULT TRUE)
132    ENDIF()
133    CMAKE_DEPENDENT_OPTION(PCH_ENABLE_${_target_name_upper}
134      "Enable using precompiled header files for library ${_target_name}." ${PCH_DEFAULT} PCH_ENABLE OFF)
135
136    IF(PCH_ENABLE_${_target_name_upper})
137      PRECOMPILED_HEADER_FILES_PRE_TARGET(${_target_name} ${_arg_PCH_FILE} _${_target_name}_files EXCLUDE ${_arg_PCH_EXCLUDE})
138    ENDIF()
139  ENDIF()
140
141  # Generate the source groups
142  IF(MSVC AND NOT _arg_NO_SOURCE_GROUPS)
143    GENERATE_SOURCE_GROUPS(${_${_target_name}_files})
144
145    IF(NOT _arg_ORXONOX_EXTERNAL)
146      # Move the prereqs.h file to the config section
147      IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_target_name_capitalised}Prereqs.h)
148        SOURCE_GROUP("Config" FILES ${_target_name_capitalised}Prereqs.h)
149      ENDIF()
150      # Add config files to the config section
151      LIST(APPEND _${_target_name}_files ${ORXONOX_CONFIG_FILES})
152      SOURCE_GROUP("Config" FILES ${ORXONOX_CONFIG_FILES})
153    ENDIF()
154  ENDIF()
155
156  # Set link mode (SHARED/STATIC)
157  IF(MSVC AND _arg_NO_DLL_INTERFACE)
158    # Certain libraries don't have dllexport/dllimport and can't be linked shared with MSVC
159    SET(_link_mode STATIC)
160  ELSEIF(_arg_ORXONOX_EXTERNAL)
161    # Externals can be linked shared or statically
162    SET(_link_mode ${ORXONOX_EXTERNAL_LINK_MODE})
163  ELSE()
164    # All our own libraries are linked dynamically because of static symbols
165    SET(_link_mode SHARED)
166  ENDIF()
167
168  # No warnings needed from third party libraries
169  IF(_arg_ORXONOX_EXTERNAL)
170    REMOVE_COMPILER_FLAGS("-W3 -W4" MSVC)
171    ADD_COMPILER_FLAGS("-w")
172  ENDIF()
173
174  # Don't compile header files
175  FOREACH(_file ${_${_target_name}_files})
176    IF(NOT _file MATCHES "\\.(c|cc|cpp)")
177      SET_SOURCE_FILES_PROPERTIES(${_file} PROPERTIES HEADER_FILE_ONLY TRUE)
178    ENDIF()
179  ENDFOREACH(_file)
180
181
182
183  # Add the library/executable
184  IF("${_target_type}" STREQUAL "LIBRARY")
185    ADD_LIBRARY(${_target_name} ${_link_mode}
186                ${_arg_EXCLUDE_FROM_ALL} ${_${_target_name}_files})
187  ELSE()
188    ADD_EXECUTABLE(${_target_name} ${_arg_WIN32} ${_arg_EXCLUDE_FROM_ALL}
189                   ${_${_target_name}_files})
190  ENDIF()
191
192
193
194  # Change library prefix to "lib"
195  IF(MSVC AND ${_target_type} STREQUAL "LIBRARY")
196    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES
197      PREFIX "lib"
198    )
199  ENDIF()
200
201  # MSVC hack to exclude external library sources from the intellisense database
202  # (IntelliSense stops working when adding "-Zm1000" as compile flag. "/Zm1000"
203  # would not work because of the slash)
204  IF(_arg_ORXONOX_EXTERNAL AND MSVC)
205    GET_TARGET_PROPERTY(_compile_flags ${_target_name} COMPILE_FLAGS)
206    IF(NOT _compile_flags)
207      SET(_compile_flags)
208    ENDIF()
209    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES COMPILE_FLAGS "${_compile_flags} -Zm1000")
210  ENDIF()
211
212  # LINK_LIBRARIES
213  IF(_arg_LINK_LIBRARIES)
214    TARGET_LINK_LIBRARIES(${_target_name} ${_arg_LINK_LIBRARIES})
215  ENDIF()
216
217  # DEFINE_SYMBOL
218  IF(_arg_DEFINE_SYMBOL)
219    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES DEFINE_SYMBOL ${_arg_DEFINE_SYMBOL})
220  ELSEIF(NOT _arg_ORXONOX_EXTERNAL)
221    # Automatically add the macro definitions for our own libraries
222    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES DEFINE_SYMBOL "${_target_name_upper}_SHARED_BUILD")
223  ENDIF()
224
225  # VERSION
226  IF(_arg_VERSION)
227    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES VERSION ${_arg_VERSION})
228  ELSEIF(NOT _arg_ORXONOX_EXTERNAL AND NOT _arg_NO_VERSION AND NOT ${_target_type} STREQUAL "EXECUTABLE")
229    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES VERSION ${ORXONOX_VERSION})
230  ENDIF()
231
232  # OUTPUT_NAME
233  IF(_arg_OUTPUT_NAME)
234    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES OUTPUT_NAME  ${_arg_OUTPUT_NAME})
235  ENDIF()
236
237  # Second part of precompiled header files
238  IF(PCH_COMPILER_SUPPORT AND PCH_ENABLE_${_target_name_upper} AND _arg_PCH_FILE)
239    PRECOMPILED_HEADER_FILES_POST_TARGET(${_target_name} ${_arg_PCH_FILE})
240  ENDIF()
241
242  # Install all targets except for static ones (executables also have SHARED in _link_mode)
243  IF(${_link_mode} STREQUAL "SHARED" AND NOT _arg_NO_INSTALL)
244    INSTALL(TARGETS ${_target_name}
245      RUNTIME DESTINATION ${RUNTIME_INSTALL_DIRECTORY}
246      LIBRARY DESTINATION ${LIBRARY_INSTALL_DIRECTORY}
247    )
248  ENDIF()
249
250ENDMACRO(TU_ADD_TARGET)
Note: See TracBrowser for help on using the repository browser.