SetCompilerFlags.cmake 5.07 KB
Newer Older
1
# Copyright (c) 2014-2015, Siemens AG. All rights reserved.
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
#
# 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.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.

## DETERMINE COMPILER AND LINKER FLAGS
#
function(SetGNUCompilerFlags compiler_libs)
  if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
    set(compiler_libs pthread rt PARENT_SCOPE)
    # -Wall   -> All warnings
    # -Wextra -> Even more warnings
    # -Werror -> Warnings are errors
    set(warning_flags "-Wall -Wextra")
34
    if (WARNINGS_ARE_ERRORS STREQUAL ON)
35
      set(warning_flags "${warning_flags} -Werror")
36
    endif()
37 38 39 40 41 42 43
    if(CMAKE_COMPILER_IS_GNUCC)
      set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -std=c99 ${warning_flags}"
          PARENT_SCOPE)
      set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DEMBB_DEBUG"
          PARENT_SCOPE)
      set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -DNDEBUG"
          PARENT_SCOPE)
44 45 46
      set(CMAKE_C_FLAGS_COVERAGE
         "${CMAKE_C_FLAGS_COVERAGE} -O0 -fprofile-arcs -ftest-coverage"
         PARENT_SCOPE)
47 48 49 50 51 52 53 54
    endif()
    if(CMAKE_COMPILER_IS_GNUCXX)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++03 ${warning_flags}"
          PARENT_SCOPE)
      set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DEMBB_DEBUG"
          PARENT_SCOPE)
      set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DNDEBUG"
          PARENT_SCOPE)
55 56 57
      set(CMAKE_CXX_FLAGS_COVERAGE
         "${CMAKE_CXX_FLAGS_COVERAGE} -O0 -fprofile-arcs -ftest-coverage"
         PARENT_SCOPE)
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
    endif()
  endif()
endfunction()

function(SetVisualStudioCompilerFlags)
  if(MSVC)
    # /Wall   -> All warnings
    # /WX     -> Warnings as errors
    #
    # Globally deactivated warning numbers (by flag \wd#):
    # 4820 -> Deactivates warning "2/4/... bytes padding added after some type"
    # 4514 -> Deactivates warning "'fct': unreferenced inline function has
    #         been removed"
    # 4668 -> Deactivates warning "'macro' is not defined as preprocessor macro,
    #         replacing with '0' in #if/#elif"
    # 4710 -> Deactivates warning "Function not inlined"
    # 4350 -> Deactivates warning "Behavior change ...", which warns a
    #         behavior change since VS C++ 2002, when using R-values as
    #         L-value arguments. This warning occurs a lot in the VC libs.
    # 4571 -> Deactivates warning that when compiling with /EHs,
    #         a catch(...) block will not catch a structured exception.
    # 4625 -> Deactivates warning for derived classes
    #         when copy constructor could not be generated because
    #         a base class copy constructor is inaccessible
    # 4626 -> Deactivates warning for derived classes
    #         when assignment operator could not be generated because
    #         a base class assignment operator is inaccessible
    # 4711 -> Deactivates warning for inline functions
    #         This is only an informational warning about which functions
    #         have been inlined by the compiler.
    # 4255 -> Deactivates warning "no function prototype given converting () to (void)"
89
    #
90 91 92
    # Locally suppressed warnings (should not be globally suppressed):
    # 4640 -> Information that local static variable initialization is not
    #         thread-safe.
93
    #
94 95
    # VS 2015 specific warnings:
    # 5026 -> Move constructor was implicitly defined as deleted
96
    # 5027 -> Move assignment operator was implicitly defined as deleted
97 98 99
    #
    set(warning_flags "/Wall /wd4820 /wd4514 /wd4668 /wd4710 /wd4350 /wd4571 /wd4625 /wd4626 /wd4711 /wd4255")
    if (WARNINGS_ARE_ERRORS STREQUAL ON)
100
      set(warning_flags "${warning_flags} /WX")
101 102 103 104 105
    endif()
    string(FIND "${CMAKE_GENERATOR}" "Visual Studio 14 2015" vs2015_state)
    if (vs2015_state EQUAL 0)
      set(warning_flags "${warning_flags} /wd5026 /wd5027")
    endif()
106 107 108 109
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${warning_flags}" PARENT_SCOPE)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${warning_flags}" PARENT_SCOPE)
  endif()
endfunction()