1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
# C O M P I L E R F L A G S . C M A K E
#
# Copyright (c) 2011-2014 United States Government as represented by
# the U.S. Army Research Laboratory.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# 3. The name of the author may not be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
###
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(CMakeParseArguments)
set(CMAKE_BUILD_TYPES DEBUG RELEASE)
# Debugging function to print all current flags
function(PRINT_BUILD_FLAGS)
message("Current Build Flags (${ARGV0}):\n")
message("CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")
message("CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
message("CMAKE_SHARED_LINKER_FLAGS: ${CMAKE_SHARED_LINKER_FLAGS}")
message("CMAKE_EXE_LINKER_FLAGS: ${CMAKE_EXE_LINKER_FLAGS}")
foreach(BTYPE ${CMAKE_BUILD_TYPES})
message(" ")
message("CMAKE_C_FLAGS_${BTYPE}: ${CMAKE_C_FLAGS_${BTYPE}}")
message("CMAKE_CXX_FLAGS_${BTYPE}: ${CMAKE_CXX_FLAGS_${BTYPE}}")
message("CMAKE_SHARED_LINKER_FLAGS_${BTYPE}: ${CMAKE_SHARED_LINKER_FLAGS_${BTYPE}}")
message("CMAKE_EXE_LINKER_FLAGS_${BTYPE}: ${CMAKE_EXE_LINKER_FLAGS_${BTYPE}}")
endforeach(BTYPE ${CMAKE_BUILD_TYPES})
message(" ")
endfunction(PRINT_BUILD_FLAGS)
# Cache current values of CMake build variables
macro(CACHE_BUILD_FLAGS suffix)
set(CMAKE_C_FLAGS_CACHED${suffix} "${CMAKE_C_FLAGS}" CACHE STRING "Cached build flag value" FORCE)
set(CMAKE_CXX_FLAGS_CACHED${suffix} "${CMAKE_CXX_FLAGS}" CACHE STRING "Cached build flag value" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS_CACHED${suffix} "${CMAKE_SHARED_LINKER_FLAGS}" CACHE STRING "Cached build flag value" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_CACHED${suffix} "${CMAKE_EXE_LINKER_FLAGS}" CACHE STRING "Cached build flag value" FORCE)
mark_as_advanced(CMAKE_C_FLAGS_CACHED${suffix})
mark_as_advanced(CMAKE_CXX_FLAGS_CACHED${suffix})
mark_as_advanced(CMAKE_SHARED_LINKER_FLAGS_CACHED${suffix})
mark_as_advanced(CMAKE_EXE_LINKER_FLAGS_CACHED${suffix})
foreach(BTYPE ${CMAKE_BUILD_TYPES})
set(CMAKE_C_FLAGS_${BTYPE}_CACHED${suffix} "${CMAKE_C_FLAGS_${BTYPE}}" CACHE STRING "Cached build flag value" FORCE)
set(CMAKE_CXX_FLAGS_${BTYPE}_CACHED${suffix} "${CMAKE_CXX_FLAGS_${BTYPE}}" CACHE STRING "Cached build flag value" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS_${BTYPE}_CACHED${suffix} "${CMAKE_SHARED_LINKER_FLAGS_${BTYPE}}" CACHE STRING "Cached build flag value" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_${BTYPE}_CACHED${suffix} "${CMAKE_EXE_LINKER_FLAGS_${BTYPE}}" CACHE STRING "Cached build flag value" FORCE)
mark_as_advanced(CMAKE_C_FLAGS_${BTYPE}_CACHED${suffix})
mark_as_advanced(CMAKE_CXX_FLAGS_${BTYPE}_CACHED${suffix})
mark_as_advanced(CMAKE_SHARED_LINKER_FLAGS_${BTYPE}_CACHED${suffix})
mark_as_advanced(CMAKE_EXE_LINKER_FLAGS_${BTYPE}_CACHED${suffix})
endforeach(BTYPE ${CMAKE_BUILD_TYPES})
set(CMAKE_BUILD_FLAGS_CACHED${suffix} TRUE CACHE BOOL "Have cached build flag values" FORCE)
mark_as_advanced(CMAKE_BUILD_FLAGS_CACHED${suffix})
endmacro(CACHE_BUILD_FLAGS)
# Restore cached values of CMake build variables
macro(RESTORE_CACHED_BUILD_FLAGS suffix)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS_CACHED${suffix}}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_CACHED${suffix}}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_CACHED${suffix}}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS_CACHED${suffix}}")
foreach(BTYPE ${CMAKE_BUILD_TYPES})
set(CMAKE_C_FLAGS_${BTYPE} "${CMAKE_C_FLAGS_${BTYPE}_CACHED${suffix}}")
set(CMAKE_CXX_FLAGS_${BTYPE} "${CMAKE_CXX_FLAGS_${BTYPE}_CACHED${suffix}}")
set(CMAKE_SHARED_LINKER_FLAGS_${BTYPE} "${CMAKE_SHARED_LINKER_FLAGS_${BTYPE}_CACHED${suffix}}")
set(CMAKE_EXE_LINKER_FLAGS_${BTYPE} "${CMAKE_EXE_LINKER_FLAGS_${BTYPE}_CACHED${suffix}}")
endforeach(BTYPE ${CMAKE_BUILD_TYPES})
endmacro(RESTORE_CACHED_BUILD_FLAGS)
# Clear all currently defined CMake compiler and linker flags
#
# TODO - currently, it seems most of our src/other subbuilds
# rely on CMake to specify Windows compilation flags - until
# that changes, we can't afford to strip flags out on MSVC
macro(CLEAR_BUILD_FLAGS)
set(BUILD_FLAGS_TO_CLEAR
CMAKE_C_FLAGS
CMAKE_CXX_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_EXE_LINKER_FLAGS
)
if (NOT MSVC)
foreach(bflag ${BUILD_FLAGS_TO_CLEAR})
set(${bflag} "")
unset(${bflag} CACHE)
foreach(BTYPE ${CMAKE_BUILD_TYPES})
set(${bflag}_${BTYPE} "")
unset(${bflag}_${BTYPE} CACHE)
endforeach(BTYPE ${CMAKE_BUILD_TYPES})
endforeach(bflag ${BUILD_FLAGS_TO_CLEAR})
set(CMAKE_C_FLAGS "$ENV{CFLAGS}")
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "$ENV{LDFLAGS}")
endif (NOT MSVC)
endmacro(CLEAR_BUILD_FLAGS)
# To reduce verbosity in this file, determine up front which
# build configuration type (if any) we are using and stash
# the variables we want to assign flags to into a common
# variable that will be used for all routines.
macro(ADD_NEW_FLAG FLAG_TYPE NEW_FLAG CONFIG_LIST)
if(${NEW_FLAG})
if("${CONFIG_LIST}" STREQUAL "ALL")
set(CMAKE_${FLAG_TYPE}_FLAGS "${CMAKE_${FLAG_TYPE}_FLAGS} ${${NEW_FLAG}}")
elseif("${CONFIG_LIST}" STREQUAL "Debug" AND NOT CMAKE_BUILD_TYPE)
set(CMAKE_${FLAG_TYPE}_FLAGS "${CMAKE_${FLAG_TYPE}_FLAGS} ${${NEW_FLAG}}")
else("${CONFIG_LIST}" STREQUAL "ALL")
foreach(CFG_TYPE ${CONFIG_LIST})
set(VALID_CONFIG 1)
if(CMAKE_CONFIGURATION_TYPES)
list(FIND CMAKE_CONFIGURATION_TYPES "${CFG_TYPE}" VALID_CONFIG)
else(CMAKE_CONFIGURATION_TYPES)
if(NOT "${CMAKE_BUILD_TYPE}" STREQUAL "${CFG_TYPE}")
set(VALID_CONFIG "-1")
endif(NOT "${CMAKE_BUILD_TYPE}" STREQUAL "${CFG_TYPE}")
endif(CMAKE_CONFIGURATION_TYPES)
if(NOT "${VALID_CONFIG}" STREQUAL "-1")
string(TOUPPER "${CFG_TYPE}" CFG_TYPE)
if(CMAKE_${FLAG_TYPE}_FLAGS_${CFG_TYPE})
set(CMAKE_${FLAG_TYPE}_FLAGS_${CFG_TYPE} "${CMAKE_${FLAG_TYPE}_FLAGS_${CFG_TYPE}} ${${NEW_FLAG}}")
else(CMAKE_${FLAG_TYPE}_FLAGS_${CFG_TYPE})
set(CMAKE_${FLAG_TYPE}_FLAGS_${CFG_TYPE} "${${NEW_FLAG}}")
endif(CMAKE_${FLAG_TYPE}_FLAGS_${CFG_TYPE})
endif(NOT "${VALID_CONFIG}" STREQUAL "-1")
endforeach(CFG_TYPE ${CMAKE_CONFIGURATION_TYPES})
endif("${CONFIG_LIST}" STREQUAL "ALL")
endif(${NEW_FLAG})
endmacro(ADD_NEW_FLAG)
# This macro tests for a specified C or C++ compiler flag, setting the
# result in the specified variable.
macro(CHECK_COMPILER_FLAG FLAG_LANG NEW_FLAG RESULTVAR)
if("${FLAG_LANG}" STREQUAL "C")
CHECK_C_COMPILER_FLAG(${NEW_FLAG} ${RESULTVAR})
endif("${FLAG_LANG}" STREQUAL "C")
if("${FLAG_LANG}" STREQUAL "CXX")
CHECK_CXX_COMPILER_FLAG(${NEW_FLAG} ${RESULTVAR})
endif("${FLAG_LANG}" STREQUAL "CXX")
endmacro(CHECK_COMPILER_FLAG LANG NEW_FLAG RESULTVAR)
# Synopsis: CHECK_FLAG(LANG flag [BUILD_TYPES type1 type2 ...] [GROUPS group1 group2 ...] [VARS var1 var2 ...] )
#
# CHECK_FLAG is BRL-CAD's core macro for C/C++ flag testing.
# The first value is the language to test (C or C++ currently). The
# second entry is the flag (without preliminary dash).
#
# If the first two mandatory options are the only ones provided, a
# successful test of the flag will result in its being assigned to
# *all* compilations using the appropriate global C/C++ CMake
# variable. If optional parameters are included, they tell the macro
# what to do with the test results instead of doing the default global
# assignment. Options include assigning the flag to one or more of
# the variable lists associated with build types (e.g. Debug or
# Release), appending the variable to a string that contains a group
# of variables, or assigning the flag to a variable if that variable
# does not already hold a value. The assignments are not mutually
# exclusive - any or all of them may be used in a given command.
#
# For example, to test a flag and add it to the C Debug configuration
# flags:
#
# CHECK_FLAG(C ggdb3 BUILD_TYPES Debug)
#
# To assign a C flag to a unique variable:
#
# CHECK_FLAG(C c99 VARS C99_FLAG)
#
# To do all assignments at once, for multiple configs and vars:
#
# CHECK_FLAG(C ggdb3
# BUILD_TYPES Debug Release
# GROUPS DEBUG_FLAGS
# VARS DEBUG1 DEBUG2)
macro(CHECK_FLAG)
# Set up some variables and names
set(FLAG_LANG ${ARGV0})
set(flag ${ARGV1})
string(TOUPPER ${flag} UPPER_FLAG)
string(REGEX REPLACE "[^a-zA-Z0-9]" "_" UPPER_FLAG ${UPPER_FLAG})
set(NEW_FLAG "-${flag}")
# Start processing arguments
if(${ARGC} LESS 3)
# Handle default (global) case
CHECK_COMPILER_FLAG(${FLAG_LANG} ${NEW_FLAG} ${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND)
if(${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND)
ADD_NEW_FLAG(${FLAG_LANG} NEW_FLAG ALL)
endif(${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND)
else(${ARGC} LESS 3)
# Parse extra arguments
CMAKE_PARSE_ARGUMENTS(FLAG "" "" "BUILD_TYPES;GROUPS;VARS" ${ARGN})
# Iterate over listed Build types and append the flag to them if successfully tested.
foreach(build_type ${FLAG_BUILD_TYPES})
CHECK_COMPILER_FLAG(${FLAG_LANG} ${NEW_FLAG} ${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND)
if(${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND)
ADD_NEW_FLAG(${FLAG_LANG} NEW_FLAG "${build_type}")
endif(${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND)
endforeach(build_type ${FLAG_BUILD_TYPES})
# Append flag to a group of flags (this apparently needs to be
# a string build, not a CMake list build. Do this for all supplied
# group variables.
foreach(flag_group ${FLAG_GROUPS})
CHECK_COMPILER_FLAG(${FLAG_LANG} ${NEW_FLAG} ${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND)
if(${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND)
if(${flag_group})
set(${flag_group} "${${flag_group}} ${NEW_FLAG}")
else(${flag_group})
set(${flag_group} "${NEW_FLAG}")
endif(${flag_group})
endif(${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND)
endforeach(flag_group ${FLAG_GROUPS})
# If a variable does not have a value, check the flag and if valid assign
# the flag as the variable's value. Do this for all supplied variables.
foreach(flag_var ${FLAG_VARS})
if(NOT ${flag_var})
CHECK_COMPILER_FLAG(${FLAG_LANG} ${NEW_FLAG} ${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND)
if(${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND AND NOT "${${flag_var}}")
set(${flag_var} "${NEW_FLAG}")
endif(${UPPER_FLAG}_${FLAG_LANG}_FLAG_FOUND AND NOT "${${flag_var}}")
endif(NOT ${flag_var})
endforeach(flag_var ${FLAG_VARS})
endif(${ARGC} LESS 3)
endmacro(CHECK_FLAG)
# This macro checks whether a specified C flag is available. See
# CHECK_FLAG() for arguments.
macro(CHECK_C_FLAG)
CHECK_FLAG(C ${ARGN})
endmacro(CHECK_C_FLAG)
# This macro checks whether a specified C++ flag is available. See
# CHECK_FLAG() for arguments.
macro(CHECK_CXX_FLAG)
CHECK_FLAG(CXX ${ARGN})
endmacro(CHECK_CXX_FLAG)
# Local Variables:
# tab-width: 8
# mode: cmake
# indent-tabs-mode: t
# End:
# ex: shiftwidth=2 tabstop=8
|