Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/forks/sandbox_light/cmake/tools/TargetUtilities.cmake @ 10525

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

Stripped down trunk to form a new light sandbox.

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