Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/cmake/TargetUtilities.cmake @ 7132

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

Bugfix in TargetUtilities.cmake: Always provide _taget_name_upper, not only in PCH case.

  • Property svn:eol-style set to native
File size: 10.5 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 #      STATIC/SHARED:     Inherited from ADD_LIBRARY
37 #      MODULE:            For dynamic module libraries
38 #      WIN32:             Inherited from ADD_EXECUTABLE
39 #      PCH_NO_DEFAULT:    Do not make precompiled header files default if
40 #                         specified with PCH_FILE
41 #      NO_INSTALL:        Do not install the target at all
42 #      NO_VERSION:        Prevents adding any version to a target
43 #
44 #    Lists:
45 #      LINK_LIBRARIES:    Redirects to TARGET_LINK_LIBRARIES
46 #      VERSION:           Set version to the binary
47 #      SOURCE_FILES:      Source files for the target
48 #      DEFINE_SYMBOL:     Sets the DEFINE_SYMBOL target property
49 #      TOLUA_FILES:       Files with tolua interface
50 #      PCH_FILE:          Precompiled header file
51 #      PCH_EXCLUDE:       Source files to be excluded from PCH support
52 #      OUTPUT_NAME:       If you want a different name than the target name
53 #  Note:
54 #    This function also installs the target!
55 #  Prerequisistes:
56 #    ORXONOX_DEFAULT_LINK, ORXONOX_CONFIG_FILES
57 #  Parameters:
58 #    _target_name, ARGN for the macro arguments
59 #
60
61INCLUDE(CMakeDependentOption)
62INCLUDE(CapitaliseName)
63INCLUDE(GenerateToluaBindings)
64INCLUDE(ParseMacroArguments)
65INCLUDE(SourceFileUtilities)
66IF(PCH_COMPILER_SUPPORT)
67  INCLUDE(PrecompiledHeaderFiles)
68ENDIF()
69
70MACRO(ORXONOX_ADD_LIBRARY _target_name)
71  TU_ADD_TARGET(${_target_name} LIBRARY "STATIC;SHARED" ${ARGN})
72ENDMACRO(ORXONOX_ADD_LIBRARY)
73
74MACRO(ORXONOX_ADD_EXECUTABLE _target_name)
75  TU_ADD_TARGET(${_target_name} EXECUTABLE "WIN32" ${ARGN})
76ENDMACRO(ORXONOX_ADD_EXECUTABLE)
77
78
79MACRO(TU_ADD_TARGET _target_name _target_type _additional_switches)
80  CAPITALISE_NAME(${_target_name} _target_name_capitalised)
81  STRING(TOUPPER "${_target_name}" _target_name_upper)
82
83  # Specify all possible options (either switch or with add. arguments)
84  SET(_switches   FIND_HEADER_FILES  EXCLUDE_FROM_ALL  ORXONOX_EXTERNAL
85                  NO_DLL_INTERFACE   NO_SOURCE_GROUPS  ${_additional_switches}
86                  PCH_NO_DEFAULT     NO_INSTALL        MODULE NO_VERSION)
87  SET(_list_names LINK_LIBRARIES  VERSION   SOURCE_FILES  DEFINE_SYMBOL
88                  TOLUA_FILES     PCH_FILE  PCH_EXCLUDE OUTPUT_NAME)
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  # Remove potential duplicates
109  SET(_${_target_name}_files ${_${_target_name}_header_files} ${_${_target_name}_source_files})
110  LIST(REMOVE_DUPLICATES _${_target_name}_files)
111
112  # Generate the source groups
113  IF(NOT _arg_NO_SOURCE_GROUPS)
114    GENERATE_SOURCE_GROUPS(${_${_target_name}_files})
115
116    IF(NOT _arg_ORXONOX_EXTERNAL)
117      # Move the prereqs.h file to the config section
118      IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_target_name_capitalised}Prereqs.h)
119        SOURCE_GROUP("Config" FILES ${_target_name_capitalised}Prereqs.h)
120      ENDIF()
121      # Add the config files in a special source group
122      LIST(APPEND _${_target_name}_files ${ORXONOX_CONFIG_FILES})
123      SOURCE_GROUP("Config" FILES ${ORXONOX_CONFIG_FILES})
124    ENDIF()
125  ENDIF(NOT _arg_NO_SOURCE_GROUPS)
126
127  # TOLUA_FILES
128  IF(_arg_TOLUA_FILES)
129    GENERATE_TOLUA_BINDINGS(${_target_name_capitalised} _${_target_name}_files
130                            INPUTFILES ${_arg_TOLUA_FILES})
131  ENDIF()
132
133  # First part (pre target) of precompiled header files
134  IF(PCH_COMPILER_SUPPORT AND _arg_PCH_FILE)
135    # Provide convenient option to control PCH
136    IF(_arg_PCH_NO_DEFAULT)
137      SET(PCH_DEFAULT FALSE)
138    ELSE()
139      SET(PCH_DEFAULT TRUE)
140    ENDIF()
141    CMAKE_DEPENDENT_OPTION(PCH_ENABLE_${_target_name_upper}
142      "Enable using precompiled header files for library ${_target_name}." ${PCH_DEFAULT} PCH_ENABLE OFF)
143
144    IF(PCH_ENABLE_${_target_name_upper})
145      PRECOMPILED_HEADER_FILES_PRE_TARGET(${_target_name} ${_arg_PCH_FILE} _${_target_name}_files EXCLUDE ${_arg_PCH_EXCLUDE})
146    ENDIF()
147  ENDIF()
148
149  # Certain libraries don't have dllexport/dllimport and can't be linked shared with msvc
150  IF(MSVC AND _arg_NO_DLL_INTERFACE)
151    SET(_arg_SHARED)
152    SET(_arg_STATIC STATIC)
153  ENDIF()
154
155  # No warnings needed from third party libraries
156  IF(_arg_ORXONOX_EXTERNAL)
157    REMOVE_COMPILER_FLAGS("-W3 -W4" MSVC)
158    ADD_COMPILER_FLAGS("-w")
159  ENDIF()
160
161  # Set default linking if required
162  IF(NOT _arg_SHARED AND NOT _arg_STATIC)
163    IF("${ORXONOX_DEFAULT_LINK}" STREQUAL "STATIC")
164      SET(_arg_STATIC STATIC)
165    ELSEIF("${ORXONOX_DEFAULT_LINK}" STREQUAL "SHARED")
166      SET(_arg_SHARED SHARED)
167    ENDIF()
168  ENDIF()
169
170  # MODULE A
171  # Always create shared libraries
172  IF(_arg_MODULE)
173    SET(_arg_SHARED SHARED)
174    SET(_arg_STATIC)
175  ENDIF()
176
177  # Don't compile header files
178  FOREACH(_file ${_${_target_name}_files})
179    IF(NOT _file MATCHES "\\.(c|cc|cpp)")
180      SET_SOURCE_FILES_PROPERTIES(${_file} PROPERTIES HEADER_FILE_ONLY TRUE)
181    ENDIF()
182  ENDFOREACH(_file)
183
184
185
186  # Add the library/executable
187  IF("${_target_type}" STREQUAL "LIBRARY")
188    ADD_LIBRARY(${_target_name} ${_arg_STATIC} ${_arg_SHARED}
189                ${_arg_EXCLUDE_FROM_ALL} ${_${_target_name}_files})
190  ELSE()
191    ADD_EXECUTABLE(${_target_name} ${_arg_WIN32} ${_arg_EXCLUDE_FROM_ALL}
192                   ${_${_target_name}_files})
193  ENDIF()
194
195
196
197  # Change library prefix to "lib"
198  IF(MSVC AND ${_target_type} STREQUAL "LIBRARY")
199    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES
200      PREFIX "lib"
201    )
202  ENDIF()
203
204  # MSVC hack to exclude external library sources from the intellisense database
205  # (IntelliSense stops working when adding "-Zm1000" as compile flag. "/Zm1000"
206  # would not work because of the slash)
207  IF(_arg_ORXONOX_EXTERNAL AND MSVC)
208    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES COMPILE_FLAGS "-Zm1000")
209  ENDIF()
210
211  # MODULE B
212  IF (_arg_MODULE)
213    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES
214      RUNTIME_OUTPUT_DIRECTORY ${CMAKE_MODULE_OUTPUT_DIRECTORY} # Windows
215      LIBRARY_OUTPUT_DIRECTORY ${CMAKE_MODULE_OUTPUT_DIRECTORY} # Unix
216    )
217    ADD_MODULE(${_target_name})
218  ENDIF()
219
220  # LINK_LIBRARIES
221  IF(_arg_LINK_LIBRARIES)
222    TARGET_LINK_LIBRARIES(${_target_name} ${_arg_LINK_LIBRARIES})
223  ENDIF()
224
225  # DEFINE_SYMBOL
226  IF(_arg_DEFINE_SYMBOL)
227    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES DEFINE_SYMBOL ${_arg_DEFINE_SYMBOL})
228  ENDIF()
229
230  # VERSION
231  IF(_arg_VERSION)
232    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES VERSION ${_arg_VERSION})
233  ELSEIF(NOT _arg_ORXONOX_EXTERNAL AND NOT _arg_NO_VERSION AND NOT ${_target_type} STREQUAL "EXECUTABLE")
234    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES VERSION ${ORXONOX_VERSION})
235  ENDIF()
236
237  # OUTPUT_NAME
238  IF(_arg_OUTPUT_NAME)
239    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES OUTPUT_NAME  ${_arg_OUTPUT_NAME})
240  ENDIF()
241
242  # Second part of precompiled header files
243  IF(PCH_COMPILER_SUPPORT AND PCH_ENABLE_${_target_name_upper} AND _arg_PCH_FILE)
244    PRECOMPILED_HEADER_FILES_POST_TARGET(${_target_name} ${_arg_PCH_FILE})
245  ENDIF()
246
247  IF((${_target_type} STREQUAL "EXECUTABLE" OR NOT _arg_STATIC) AND NOT _arg_NO_INSTALL)
248    IF(_arg_MODULE)
249      INSTALL(TARGETS ${_target_name}
250        RUNTIME DESTINATION ${MODULE_INSTALL_DIRECTORY}
251        LIBRARY DESTINATION ${MODULE_INSTALL_DIRECTORY}
252      )
253    ELSE()
254      INSTALL(TARGETS ${_target_name}
255        RUNTIME DESTINATION ${RUNTIME_INSTALL_DIRECTORY}
256        LIBRARY DESTINATION ${LIBRARY_INSTALL_DIRECTORY}
257      )
258    ENDIF()
259  ENDIF()
260
261ENDMACRO(TU_ADD_TARGET)
262
263
264# Creates a helper file with name <name_of_the_library>${ORXONOX_MODULE_EXTENSION}
265# This helps finding dynamically loadable modules at runtime
266
267FUNCTION(ADD_MODULE _target)
268  # We use the properties to get the name because the librarys name may differ from
269  # the target name (for example orxonox <-> liborxonox)
270
271  GET_TARGET_PROPERTY(_target_loc ${_target} LOCATION)
272  GET_FILENAME_COMPONENT(_target_name ${_target_loc} NAME_WE)
273
274  IF(CMAKE_CONFIGURATION_TYPES)
275    FOREACH(_config ${CMAKE_CONFIGURATION_TYPES})
276      SET(_module_filename ${CMAKE_MODULE_OUTPUT_DIRECTORY}/${_config}/${_target_name}${ORXONOX_MODULE_EXTENSION})
277
278      FILE(WRITE ${_module_filename})
279
280      INSTALL(
281        FILES ${_module_filename}
282        DESTINATION ${MODULE_INSTALL_DIRECTORY}
283        CONFIGURATIONS ${_config}
284      )
285    ENDFOREACH()
286  ELSE()
287    SET(_module_filename ${CMAKE_MODULE_OUTPUT_DIRECTORY}/${_target_name}${ORXONOX_MODULE_EXTENSION})
288
289    FILE(WRITE ${_module_filename})
290
291    INSTALL(
292      FILES ${_module_filename}
293      DESTINATION ${MODULE_INSTALL_DIRECTORY}
294    )
295  ENDIF()
296ENDFUNCTION(ADD_MODULE)
Note: See TracBrowser for help on using the repository browser.