Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/cmake/FlagUtilities.cmake @ 6140

Last change on this file since 6140 was 3196, checked in by rgrieder, 15 years ago

Merged pch branch back to trunk.

  • Property svn:eol-style set to native
File size: 6.4 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 #    Sets the compiler/linker flags. After the flags you can specify more args:
24 #    Release, Debug, RelWithDebInfo, MinSizeRel: Build configs (inclusive)
25 #    ReleaseAll: Sets the flags of all three release builds
26 #    CACHE: Values are witten with SET_CACHE_ADVANCED
27 #    FORCE: When writing to the cache, the values are set anyway
28 #    Any variable names (like WIN32, MSVC, etc.): Condition (combined with AND)
29 #    You can suffix the condition with a NOT if you wish
30 #  Function names:
31 #    [ADD/SET/REMOVE]_[COMPILER/LINKER]_FLAGS
32 #  Caution: -If you use CACHE after calling the macro without CACHE, the value
33 #            Will not be written unless FORCE is specified.
34 #          - Also be aware to always specify the flags in quotes.
35 #  Example:
36 #    REMOVE_COMPILER_FLAGS("/Gm "asdf" -q"test -foo" CXX ReleaseAll NOT UNIX)
37 #    This will only remove the CXX (C++) flags on a non Unix system for the
38 #    Release, RelWithDebInfo and MinSizeRel configurations. The macros should
39 #    be able to cope with "test -foo" as string argument for a flag.
40 #
41
42INCLUDE(SeparateFlags)
43INCLUDE(SetCacheAdvanced)
44
45# Compiler flags, additional arguments:
46# C, CXX: Specify a language, default is both
47MACRO(SET_COMPILER_FLAGS _flags)
48  _INTERNAL_PARSE_FLAGS_ARGS(SET "C;CXX" "" "${_flags}" "${ARGN}")
49ENDMACRO(SET_COMPILER_FLAGS)
50# Add flags (flags don't get added twice)
51MACRO(ADD_COMPILER_FLAGS _flags)
52  _INTERNAL_PARSE_FLAGS_ARGS(APPEND "C;CXX" "" "${_flags}" "${ARGN}")
53ENDMACRO(ADD_COMPILER_FLAGS)
54# Remove flags
55MACRO(REMOVE_COMPILER_FLAGS _flags)
56  _INTERNAL_PARSE_FLAGS_ARGS(REMOVE_ITEM "C;CXX" "" "${_flags}" "${ARGN}")
57ENDMACRO(REMOVE_COMPILER_FLAGS)
58
59
60# Linker flags, additional arguments:
61# EXE, SHARED, MODULE: Specify a linker mode, default is all three
62MACRO(SET_LINKER_FLAGS _flags)
63  _INTERNAL_PARSE_FLAGS_ARGS(SET "EXE;SHARED;MODULE" "_LINKER" "${_flags}" "${ARGN}")
64ENDMACRO(SET_LINKER_FLAGS)
65# Add flags (flags don't get added twice)
66MACRO(ADD_LINKER_FLAGS _flags)
67  _INTERNAL_PARSE_FLAGS_ARGS(APPEND "EXE;SHARED;MODULE" "_LINKER" "${_flags}" "${ARGN}")
68ENDMACRO(ADD_LINKER_FLAGS)
69# Remove flags
70MACRO(REMOVE_LINKER_FLAGS _flags)
71  _INTERNAL_PARSE_FLAGS_ARGS(REMOVE_ITEM "EXE;SHARED;MODULE" "_LINKER" "${_flags}" "${ARGN}")
72ENDMACRO(REMOVE_LINKER_FLAGS)
73
74
75# Internal macro, do not use
76# Parses the given additional arguments and sets the flags to the
77# corresponding variables.
78MACRO(_INTERNAL_PARSE_FLAGS_ARGS _mode _keys _key_postfix _flags)
79  SET(_langs)
80  SET(_build_types)
81  SET(_cond TRUE)
82  SET(_invert_condition FALSE)
83  SET(_write_to_cache FALSE)
84  SET(_force FALSE)
85  STRING(REPLACE ";" "|" _key_regex "${_keys}")
86  SET(_key_regex "^(${_key_regex})$")
87
88  FOREACH(_arg ${ARGN})
89    IF(_arg MATCHES "${_key_regex}")
90      LIST(APPEND _langs "${_arg}")
91    ELSEIF(   _arg MATCHES "^(Debug|Release|MinSizeRel|RelWithDebInfo)$"
92           OR _arg MATCHES "^(DEBUG|RELEASE|MINSIZEREL|RELWITHDEBINFO)$")
93      STRING(TOUPPER "${_arg}" _upper_arg)
94      LIST(APPEND _build_types ${_upper_arg})
95    ELSEIF(_arg STREQUAL "ReleaseAll")
96      LIST(APPEND _build_types RELEASE MINSIZEREL RELWITHDEBINFO)
97    ELSEIF(_arg STREQUAL "CACHE")
98      SET(_write_to_cache TRUE)
99    ELSEIF(_arg STREQUAL "FORCE")
100      SET(_force TRUE)
101    ELSEIF(_arg MATCHES "^[Nn][Oo][Tt]$")
102      SET(_invert_condition TRUE)
103    ELSE()
104      IF(_invert_condition)
105        SET(_invert_condition FALSE)
106        IF(${_arg})
107          SET(_arg_cond FALSE)
108        ELSE()
109          SET(_arg_cond TRUE)
110       ENDIF()
111      ELSE()
112        SET(_arg_cond ${${_arg}})
113      ENDIF()
114      IF(_cond AND _arg_cond)
115        SET(_cond TRUE)
116      ELSE()
117        SET(_cond FALSE)
118      ENDIF()
119    ENDIF()
120  ENDFOREACH(_arg)
121
122  # No language specified, use all: C and CXX or EXE, SHARED and MODULE
123  IF(NOT DEFINED _langs)
124    SET(_langs ${_keys})
125  ENDIF()
126
127  IF(_cond)
128    FOREACH(_lang ${_langs})
129      SET(_varname "CMAKE_${_lang}${_key_postfix}_FLAGS")
130      IF(DEFINED _build_types)
131        FOREACH(_build_type ${_build_types})
132          _INTERNAL_PARSE_FLAGS(${_mode} "${_flags}" ${_varname}_${_build_type} ${_write_to_cache} ${_force})
133        ENDFOREACH(_build_type)
134      ELSE()
135        _INTERNAL_PARSE_FLAGS(${_mode} "${_flags}" ${_varname} ${_write_to_cache} ${_force})
136      ENDIF()
137    ENDFOREACH(_lang ${_langs})
138  ENDIF(_cond)
139ENDMACRO(_INTERNAL_PARSE_FLAGS_ARGS)
140
141
142# Internal macro, do not use
143# Modifies the flags according to the mode: set, add or remove
144# Also sets flags according to the CACHE and FORCE parameter.
145# If only CACHE is specified, SET_CACHE_ADVANCED() is used.
146MACRO(_INTERNAL_PARSE_FLAGS _mode _flags _varname _write_to_cache _force)
147  SEPARATE_FLAGS("${_flags}" _arg_flag_list)
148
149  IF("${_mode}" STREQUAL "SET")
150    # SET
151    SET(_flag_list "${_arg_flag_list}")
152  ELSE()
153    # ADD or REMOVE
154    SEPARATE_FLAGS("${${_varname}}" _flag_list)
155    IF(NOT _flag_list)
156      SET(_flag_list "") # LIST command requires a list in any case
157    ENDIF()
158    FOREACH(_flag ${_arg_flag_list})
159      LIST(${_mode} _flag_list "${_flag}")
160    ENDFOREACH(_flag)
161  ENDIF()
162
163  LIST(REMOVE_DUPLICATES _flag_list)
164  LIST(SORT _flag_list)
165  STRING(REPLACE ";" " " _flag_list "${_flag_list}")
166
167  IF(_write_to_cache)
168    IF(_force)
169      SET(${_varname} "${_flag_list}" CACHE STRING "${${_varname}}" FORCE)
170      SET(${_varname} "${_flag_list}" CACHE STRING "${${_varname}}" FORCE)
171    ELSE()
172      SET_CACHE_ADVANCED(${_varname} STRING "${${_varname}}" "${_flag_list}")
173    ENDIF()
174  ELSE()
175    SET(${_varname} "${_flag_list}")
176  ENDIF()
177ENDMACRO(_INTERNAL_PARSE_FLAGS)
Note: See TracBrowser for help on using the repository browser.