Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/cmake/tools/TargetUtilities.cmake @ 7221

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

Static linking to the libraries in src/external should work now (at least on Windows with MSVC).
There are no STATIC/SHARED options anymore. Instead all of our own libraries get linked dynamically anyway because it simply doesn't work otherwise (there is really no way to link them statically).
The linking behaviour for external libraries can be changed with the CMake cache variable ORXONOX_EXTERNAL_LINK_MODE, but it defaults to SHARED.
Also, I had to revert the DEFINE_SYMBOL changes.

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