Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/cmake/TargetUtilities.cmake @ 5910

Last change on this file since 5910 was 5872, checked in by rgrieder, 15 years ago

Removing redundancy: Specifying ORXONOX_EXTERAL for a target should already suppress all warnings.

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