Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/spaceNavigation/cmake/tools/TargetUtilities.cmake @ 10035

Last change on this file since 10035 was 8729, checked in by rgrieder, 13 years ago

Merged unity_build branch back to trunk.

Features:

  • Implemented fully automatic build units to speed up compilation if requested
  • Added DOUT macro for quick debug output
  • Activated text colouring in the POSIX IOConsole
  • DeclareToluaInterface is not necessary anymore

Improvements:

  • Output levels now change appropriately when switch back and forth from dev mode
  • Log level for the file output is now also correct during startup
  • Removed some header file dependencies in core and tools to speed up compilation

no more file for command line options

  • Improved util::tribool by adapting some concepts from boost::tribool

Regressions:

  • It is not possible anymore to specify command line arguments in an extra file because we've got config values for that purpose.
  • Property svn:eol-style set to native
File size: 17.0 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 #      NO_BUILD_UNITS:    Disables automatic (full) build units
43 #
44 #    Lists:
45 #      LINK_LIBRARIES:    Redirects to TARGET_LINK_LIBRARIES
46 #      LINK_LIBS_LINUX:   Redirects to TARGET_LINK_LIBRARIES only on Linux
47 #      LINK_LIBS_WIN32:   Redirects to TARGET_LINK_LIBRARIES only on Windows
48 #      LINK_LIBS_APPLE:   Redirects to TARGET_LINK_LIBRARIES only on Apple
49 #      LINK_LIBS_UNIX:    Redirects to TARGET_LINK_LIBRARIES only on UNIX
50 #      VERSION:           Set version to the binary
51 #      SOURCE_FILES:      Source files for the target
52 #      DEFINE_SYMBOL:     Sets the DEFINE_SYMBOL target property
53 #      TOLUA_FILES:       Files with tolua interface
54 #      PCH_FILE:          Precompiled header file
55 #      PCH_EXCLUDE:       Source files to be excluded from PCH support
56 #      OUTPUT_NAME:       If you want a different name than the target name
57 #      EXCLUDE_FROM_BUILD_UNITS: Specifies files that are not put into
58 #                         automatic (full) build units. They can still
59 #                         explicitely be included in a BUILD_UNIT (partial)
60 #  Note:
61 #    This function also installs the target!
62 #  Prerequisistes:
63 #    ORXONOX_DEFAULT_LINK
64 #  Parameters:
65 #    _target_name, ARGN for the macro arguments
66 #
67
68INCLUDE(BuildUnits)
69INCLUDE(CMakeDependentOption)
70INCLUDE(CapitaliseName)
71INCLUDE(GenerateToluaBindings)
72INCLUDE(ParseMacroArguments)
73INCLUDE(SourceFileUtilities)
74IF(PCH_COMPILER_SUPPORT)
75  INCLUDE(PrecompiledHeaderFiles)
76ENDIF()
77
78MACRO(ORXONOX_ADD_LIBRARY _target_name)
79  TU_ADD_TARGET(${_target_name} LIBRARY "MODULE" ${ARGN})
80ENDMACRO(ORXONOX_ADD_LIBRARY)
81
82MACRO(ORXONOX_ADD_EXECUTABLE _target_name)
83  TU_ADD_TARGET(${_target_name} EXECUTABLE "WIN32" ${ARGN})
84ENDMACRO(ORXONOX_ADD_EXECUTABLE)
85
86
87MACRO(TU_ADD_TARGET _target_name _target_type _additional_switches)
88  CAPITALISE_NAME(${_target_name} _target_name_capitalised)
89  STRING(TOUPPER "${_target_name}" _target_name_upper)
90
91  # Specify all possible options (either switch or with add. arguments)
92  SET(_switches   FIND_HEADER_FILES  EXCLUDE_FROM_ALL  ORXONOX_EXTERNAL
93                  NO_DLL_INTERFACE   NO_SOURCE_GROUPS  PCH_NO_DEFAULT 
94                  NO_INSTALL         NO_VERSION        NO_BUILD_UNITS
95                  ${_additional_switches})
96  SET(_list_names LINK_LIBRARIES     VERSION           SOURCE_FILES
97                  DEFINE_SYMBOL      TOLUA_FILES       PCH_FILE
98                  PCH_EXCLUDE        OUTPUT_NAME       LINK_LIBS_LINUX
99                  LINK_LIBS_WIN32    LINK_LIBS_APPLE   LINK_LIBS_UNIX
100                  EXCLUDE_FROM_BUILD_UNITS)
101
102  PARSE_MACRO_ARGUMENTS("${_switches}" "${_list_names}" ${ARGN})
103
104  # Process source files with support for build units
105  # Note: All file paths are relative to the root source directory, even the
106  #       name of the build unit.
107  SET(_${_target_name}_source_files)
108  SET(_get_build_unit_file FALSE)
109  SET(_add_to_build_unit FALSE)
110  FOREACH(_file ${_arg_SOURCE_FILES})
111    IF(_file STREQUAL "BUILD_UNIT")
112      # Next file is the name of the build unit
113      SET(_get_build_unit_file TRUE)
114    ELSEIF(_file STREQUAL "END_BUILD_UNIT")
115      IF(NOT _build_unit_file)
116        MESSAGE(FATAL_ERROR "No name provided for build unit")
117      ENDIF()
118      IF(ENABLE_BUILD_UNITS)
119        IF(NOT _build_unit_include_string)
120          MESSAGE(STATUS "Warning: Empty build unit!")
121        ENDIF()
122        IF(EXISTS ${_build_unit_file})
123          FILE(READ ${_build_unit_file} _include_string_file)
124        ENDIF()
125        IF(NOT _build_unit_include_string STREQUAL "${_include_string_file}")
126          FILE(WRITE ${_build_unit_file} "${_build_unit_include_string}")
127        ENDIF()
128        LIST(APPEND _${_target_name}_source_files ${_build_unit_file})
129        LIST(APPEND _${_target_name}_build_units ${_build_unit_file})
130        # Store the number of files included. May be used for full build units.
131        SET_SOURCE_FILES_PROPERTIES(${_build_unit_file}
132          PROPERTIES BUILD_UNIT_SIZE "${_build_unit_count}")
133      ENDIF()
134      SET(_add_to_build_unit FALSE)
135    ELSEIF(_get_build_unit_file)
136      # Note: ${_file} is relative to the binary directory
137      SET(_build_unit_file ${CMAKE_BINARY_DIR}/${_file})
138      SET(_get_build_unit_file FALSE)
139      SET(_add_to_build_unit TRUE)
140      SET(_build_unit_include_string)
141      SET(_build_unit_count "0")
142    ELSE()
143      # Default, add source file
144
145      # Prepare relative paths
146      IF(NOT _file MATCHES "^(.\\:|\\/)")
147        # Path can be relative to the current source directory if the file was
148        # not added with the source file macros. Otherwise there is a "./" at
149        # the beginning of each file and the filename is relative
150        # to the CMAKE_SOURCE_DIR
151        STRING(REGEX REPLACE "^\\.\\/(.+)$" "\\1" _temp ${_file})
152        IF(NOT ${_temp} STREQUAL ${_file})
153          SET(_file ${CMAKE_SOURCE_DIR}/${_temp})
154        ELSE()
155          SET(_file ${CMAKE_CURRENT_SOURCE_DIR}/${_file})
156        ENDIF()
157      ENDIF()
158
159      LIST(APPEND _${_target_name}_source_files ${_file})
160
161      # Handle build units
162      IF(_add_to_build_unit AND ENABLE_BUILD_UNITS)
163        IF(_file MATCHES "\\.(c|cc|cpp|cxx)$")
164          SET(_build_unit_include_string "${_build_unit_include_string}#include \"${_file}\"\n")
165          MATH(EXPR _build_unit_count "1 + ${_build_unit_count}")
166        ENDIF()
167        # Don't compile these files, even if they are source files
168        SET_SOURCE_FILES_PROPERTIES(${_file} PROPERTIES HEADER_FILE_ONLY TRUE)
169      ENDIF()
170    ENDIF()
171  ENDFOREACH(_file)
172
173  # Assemble all header files of the library
174  IF(_arg_FIND_HEADER_FILES)
175    GET_ALL_HEADER_FILES(_${_target_name}_header_files)
176  ENDIF()
177
178  # Combine source and header files
179  SET(_${_target_name}_files
180    ${_${_target_name}_header_files}
181    ${_${_target_name}_source_files}
182  )
183  # Remove potential duplicates
184  LIST(REMOVE_DUPLICATES _${_target_name}_files)
185
186  # TOLUA_FILES
187  IF(_arg_TOLUA_FILES)
188    GENERATE_TOLUA_BINDINGS(${_target_name_capitalised} _${_target_name}_files
189                            INPUTFILES ${_arg_TOLUA_FILES})
190    # Workaround for XCode: The folder where the bind files are written to has
191    # to be present beforehand.
192    IF(CMAKE_CONFIGURATION_TYPES)
193      FOREACH(_dir ${CMAKE_CONFIGURATION_TYPES})
194        FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${_dir})
195      ENDFOREACH(_dir)
196    ENDIF()
197  ENDIF()
198
199  # Mark files to be excluded from build units
200  IF(_arg_EXCLUDE_FROM_BUILD_UNITS)
201    SET_SOURCE_FILES_PROPERTIES(${_arg_EXCLUDE_FROM_BUILD_UNITS}
202      PROPERTIES EXCLUDE_FROM_BUILD_UNITS TRUE)
203  ENDIF()
204
205  # Full build units
206  IF(ENABLE_BUILD_UNITS AND NOT _arg_NO_BUILD_UNITS)
207    # Use full build units even in partial mode for externals
208    IF(ENABLE_BUILD_UNITS MATCHES "full" OR _arg_ORXONOX_EXTERNAL)
209      GENERATE_BUILD_UNITS(${_target_name} _${_target_name}_files)
210    ENDIF()
211  ENDIF()
212
213  # First part (pre target) of precompiled header files
214  IF(PCH_COMPILER_SUPPORT AND _arg_PCH_FILE)
215    # Provide convenient option to control PCH
216    IF(_arg_PCH_NO_DEFAULT)
217      SET(PCH_DEFAULT FALSE)
218    ELSE()
219      SET(PCH_DEFAULT TRUE)
220    ENDIF()
221    CMAKE_DEPENDENT_OPTION(PCH_ENABLE_${_target_name_upper}
222      "Enable using precompiled header files for library ${_target_name}." ${PCH_DEFAULT} PCH_ENABLE OFF)
223    # Almost never used individually, but produces a lot of options --> hide
224    MARK_AS_ADVANCED(PCH_ENABLE_${_target_name_upper})
225
226    IF(PCH_ENABLE_${_target_name_upper} AND NOT PCH_DISABLE_${_target_name})
227      PRECOMPILED_HEADER_FILES_PRE_TARGET(${_target_name} ${_arg_PCH_FILE} _${_target_name}_files EXCLUDE ${_arg_PCH_EXCLUDE})
228    ENDIF()
229  ENDIF()
230
231  # Generate the source groups
232  IF(MSVC AND NOT _arg_NO_SOURCE_GROUPS)
233    GENERATE_SOURCE_GROUPS(${_${_target_name}_files})
234
235    IF(NOT _arg_ORXONOX_EXTERNAL)
236      # Move the ...Prereqs.h and the PCH files to the 'Config' section
237      IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_target_name_capitalised}Prereqs.h)
238        SOURCE_GROUP("Config" FILES ${_target_name_capitalised}Prereqs.h)
239      ENDIF()
240      IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_arg_PCH_FILE})
241        SOURCE_GROUP("Config" FILES ${CMAKE_CURRENT_SOURCE_DIR}/${_arg_PCH_FILE})
242      ENDIF()
243    ENDIF()
244  ENDIF()
245
246  # Set link mode (SHARED/STATIC)
247  IF(MSVC AND _arg_NO_DLL_INTERFACE)
248    # Certain libraries don't have dllexport/dllimport and can't be linked shared with MSVC
249    SET(_link_mode STATIC)
250  ELSEIF(_arg_ORXONOX_EXTERNAL)
251    # Externals can be linked shared or statically
252    SET(_link_mode ${ORXONOX_EXTERNAL_LINK_MODE})
253  ELSE()
254    # All our own libraries are linked dynamically because of static symbols
255    SET(_link_mode SHARED)
256  ENDIF()
257
258  # No warnings needed from third party libraries
259  IF(_arg_ORXONOX_EXTERNAL)
260    REMOVE_COMPILER_FLAGS("-W3 -W4" MSVC)
261    ADD_COMPILER_FLAGS("-w" NOT MSVC)
262    ADD_COMPILER_FLAGS("-W0" MSVC)
263  ENDIF()
264
265  # Don't compile header files
266  FOREACH(_file ${_${_target_name}_files})
267    IF(NOT _file MATCHES "\\.(c|cc|cpp|cxx|mm)$")
268      SET_SOURCE_FILES_PROPERTIES(${_file} PROPERTIES HEADER_FILE_ONLY TRUE)
269    ENDIF()
270  ENDFOREACH(_file)
271
272
273
274  # Add the library/executable
275  IF("${_target_type}" STREQUAL "LIBRARY")
276    ADD_LIBRARY(${_target_name} ${_link_mode}
277                ${_arg_EXCLUDE_FROM_ALL} ${_${_target_name}_files})
278  ELSE()
279    ADD_EXECUTABLE(${_target_name} ${_arg_WIN32} ${_arg_EXCLUDE_FROM_ALL}
280                   ${_${_target_name}_files})
281  ENDIF()
282
283
284
285  # Change library prefix to "lib"
286  IF(MSVC AND ${_target_type} STREQUAL "LIBRARY")
287    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES
288      PREFIX "lib"
289    )
290  ENDIF()
291
292  # MSVC hack to exclude external library sources from the intellisense database
293  # (IntelliSense stops working when adding "-Zm1000" as compile flag. "/Zm1000"
294  # would not work because of the slash)
295  IF(_arg_ORXONOX_EXTERNAL AND MSVC)
296    GET_TARGET_PROPERTY(_compile_flags ${_target_name} COMPILE_FLAGS)
297    IF(NOT _compile_flags)
298      SET(_compile_flags)
299    ENDIF()
300    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES COMPILE_FLAGS "${_compile_flags} -Zm1000")
301  ENDIF()
302
303  # Configure modules
304  IF (_arg_MODULE)
305    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES
306      RUNTIME_OUTPUT_DIRECTORY ${CMAKE_MODULE_OUTPUT_DIRECTORY} # Windows
307      LIBRARY_OUTPUT_DIRECTORY ${CMAKE_MODULE_OUTPUT_DIRECTORY} # Unix
308    )
309    ADD_MODULE(${_target_name})
310    # Ensure that the main program depends on the module
311    SET(ORXONOX_MODULES ${ORXONOX_MODULES} ${_target_name} CACHE INTERNAL "")
312  ENDIF()
313
314  # Static library flags are not globally available
315  IF(ORXONOX_STATIC_LINKER_FLAGS)
316    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES STATIC_LIBRARY_FLAGS ${ORXONOX_STATIC_LINKER_FLAGS})
317  ENDIF()
318
319  # LINK_LIBRARIES
320  IF(_arg_LINK_LIBRARIES)
321    TARGET_LINK_LIBRARIES(${_target_name} ${_arg_LINK_LIBRARIES})
322  ENDIF()
323  IF(_arg_LINK_LIBS_LINUX AND LINUX)
324    TARGET_LINK_LIBRARIES(${_target_name} ${_arg_LINK_LIBS_LINUX})
325  ENDIF()
326  IF(_arg_LINK_LIBS_WIN32 AND WIN32)
327    TARGET_LINK_LIBRARIES(${_target_name} ${_arg_LINK_LIBS_WIN32})
328  ENDIF()
329  IF(_arg_LINK_LIBS_APPLE AND APPLE)
330    TARGET_LINK_LIBRARIES(${_target_name} ${_arg_LINK_LIBS_APPLE})
331  ENDIF()
332  IF(_arg_LINK_LIBS_UNIX AND UNIX)
333    TARGET_LINK_LIBRARIES(${_target_name} ${_arg_LINK_LIBS_UNIX})
334  ENDIF()
335
336  # Visual Leak Detector specific stuff (avoids the include)
337  IF(VISUAL_LEAK_DETECTOR_ENABLE)
338    TARGET_LINK_LIBRARIES(${_target_name} debug ${VLD_LIBRARY})
339  ENDIF()
340
341  # RPATH settings for the installation
342  IF("${_target_type}" STREQUAL "LIBRARY")
343    IF(_arg_MODULE)
344      SET(_rpath "${MODULE_RPATH}")
345    ELSE()
346      SET(_rpath "${LIBRARY_RPATH}")
347    ENDIF()
348  ELSE()
349    SET(_rpath "${RUNTIME_RPATH}")
350  ENDIF()
351  SET_TARGET_PROPERTIES(${_target_name} PROPERTIES INSTALL_RPATH "${_rpath}")
352
353  # DEFINE_SYMBOL
354  IF(_arg_DEFINE_SYMBOL)
355    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES DEFINE_SYMBOL ${_arg_DEFINE_SYMBOL})
356  ELSEIF(NOT _arg_ORXONOX_EXTERNAL)
357    # Automatically add the macro definitions for our own libraries
358    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES DEFINE_SYMBOL "${_target_name_upper}_SHARED_BUILD")
359  ENDIF()
360
361  # VERSION
362  IF(_arg_VERSION)
363    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES VERSION ${_arg_VERSION})
364  ELSEIF(NOT _arg_ORXONOX_EXTERNAL AND NOT _arg_NO_VERSION AND NOT ${_target_type} STREQUAL "EXECUTABLE")
365    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES VERSION ${ORXONOX_VERSION})
366  ENDIF()
367
368  # OUTPUT_NAME
369  IF(_arg_OUTPUT_NAME)
370    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES OUTPUT_NAME  ${_arg_OUTPUT_NAME})
371  ENDIF()
372
373  # Second part of precompiled header files
374  IF(PCH_COMPILER_SUPPORT AND PCH_ENABLE_${_target_name_upper} AND _arg_PCH_FILE AND NOT PCH_DISABLE_${_target_name})
375    PRECOMPILED_HEADER_FILES_POST_TARGET(${_target_name} ${_arg_PCH_FILE})
376  ENDIF()
377
378  # Install all targets except for static ones (executables also have SHARED in _link_mode)
379  IF(${_link_mode} STREQUAL "SHARED" AND NOT _arg_NO_INSTALL)
380    IF(_arg_MODULE)
381      INSTALL(TARGETS ${_target_name}
382        RUNTIME DESTINATION ${MODULE_INSTALL_DIRECTORY}
383        LIBRARY DESTINATION ${MODULE_INSTALL_DIRECTORY}
384      )
385    ELSE()
386      INSTALL(TARGETS ${_target_name}
387        RUNTIME DESTINATION ${RUNTIME_INSTALL_DIRECTORY}
388        LIBRARY DESTINATION ${LIBRARY_INSTALL_DIRECTORY}
389      )
390    ENDIF()
391    IF(INSTALL_PDB_FILES) # MSVC specific: install debug symbols files
392      FOREACH(_config RelForDevs RelWithDebInfo)
393        GET_TARGET_PROPERTY(_location ${_target_name} LOCATION_${_config})
394        # Get absolute location without dll/exe extension
395        STRING(REGEX REPLACE "^(.+)\\.(dll|exe)$" "\\1" _location_we ${_location})
396        IF(_arg_MODULE)
397          INSTALL(FILES ${_location_we}.pdb
398            DESTINATION ${MODULE_INSTALL_DIRECTORY}
399            CONFIGURATIONS ${_config}
400          )
401        ELSE()
402          INSTALL(FILES ${_location_we}.pdb
403            DESTINATION ${RUNTIME_INSTALL_DIRECTORY}
404            CONFIGURATIONS ${_config}
405          )
406        ENDIF()
407      ENDFOREACH(_config)
408    ENDIF()
409  ENDIF()
410
411ENDMACRO(TU_ADD_TARGET)
412
413
414# Creates a helper file with name <name_of_the_library>${ORXONOX_MODULE_EXTENSION}
415# This helps finding dynamically loadable modules at runtime
416
417FUNCTION(ADD_MODULE _target)
418  # We use the properties to get the name because the librarys name may differ from
419  # the target name (for example orxonox <-> liborxonox)
420
421  GET_TARGET_PROPERTY(_target_loc ${_target} LOCATION)
422  GET_FILENAME_COMPONENT(_target_name ${_target_loc} NAME_WE)
423
424  IF(CMAKE_CONFIGURATION_TYPES)
425    FOREACH(_config ${CMAKE_CONFIGURATION_TYPES})
426      SET(_module_filename ${CMAKE_MODULE_OUTPUT_DIRECTORY}/${_config}/${_target_name}${ORXONOX_MODULE_EXTENSION})
427
428      FILE(WRITE ${_module_filename})
429
430      INSTALL(
431        FILES ${_module_filename}
432        DESTINATION ${MODULE_INSTALL_DIRECTORY}
433        CONFIGURATIONS ${_config}
434      )
435    ENDFOREACH()
436  ELSE()
437    SET(_module_filename ${CMAKE_MODULE_OUTPUT_DIRECTORY}/${_target_name}${ORXONOX_MODULE_EXTENSION})
438
439    FILE(WRITE ${_module_filename})
440
441    INSTALL(
442      FILES ${_module_filename}
443      DESTINATION ${MODULE_INSTALL_DIRECTORY}
444    )
445  ENDIF()
446ENDFUNCTION(ADD_MODULE)
Note: See TracBrowser for help on using the repository browser.