Blame cmake/compilerFlags.cmake

Packit Service 21b5d1
# These flags applies to exiv2lib, the applications, and to the xmp code
Packit Service 21b5d1

Packit Service 21b5d1
if ( MINGW OR UNIX OR MSYS ) # MINGW, Linux, APPLE, CYGWIN
Packit Service 21b5d1
    if (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
Packit Service 21b5d1
        set(COMPILER_IS_GCC ON)
Packit Service 21b5d1
    elseif (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
Packit Service 21b5d1
        set(COMPILER_IS_CLANG ON)
Packit Service 21b5d1
    endif()
Packit Service 21b5d1

Packit Service 21b5d1
    set (CMAKE_CXX_FLAGS_DEBUG      "-g3 -gstrict-dwarf -O0")
Packit Service 21b5d1

Packit Service 21b5d1
    if (CMAKE_GENERATOR MATCHES "Xcode")
Packit Service 21b5d1
        set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
Packit Service 21b5d1
        if (EXIV2_ENABLE_EXTERNAL_XMP)
Packit Service 21b5d1
            # XMP SDK 2016 uses libstdc++ even when it is deprecated in modern versions of the OSX SDK.
Packit Service 21b5d1
            # The only way to make Exiv2 work with the external XMP SDK is to use the same standard library.
Packit Service 21b5d1
            set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libstdc++")
Packit Service 21b5d1
        else()
Packit Service 21b5d1
            set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
Packit Service 21b5d1
        endif()
Packit Service 21b5d1
    endif()
Packit Service 21b5d1

Packit Service 21b5d1
    if (COMPILER_IS_GCC OR COMPILER_IS_CLANG)
Packit Service 21b5d1

Packit Service 21b5d1
        if(BUILD_WITH_COVERAGE)
Packit Service 21b5d1
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g ")
Packit Service 21b5d1
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
Packit Service 21b5d1
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs")
Packit Service 21b5d1
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftest-coverage")
Packit Service 21b5d1
            set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
Packit Service 21b5d1
        endif()
Packit Service 21b5d1

Packit Service 21b5d1
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wcast-align -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Woverloaded-virtual -W")
Packit Service 21b5d1

Packit Service 21b5d1
        if ( EXIV2_TEAM_USE_SANITIZERS )
Packit Service 21b5d1
            # ASAN is available in gcc from 4.8 and UBSAN from 4.9
Packit Service 21b5d1
            # ASAN is available in clang from 3.1 and UBSAN from 3.3
Packit Service 21b5d1
            # UBSAN is not fatal by default, instead it only prints runtime errors to stderr
Packit Service 21b5d1
            # => make it fatal with -fno-sanitize-recover (gcc) or -fno-sanitize-recover=all (clang)
Packit Service 21b5d1
            # add -fno-omit-frame-pointer for better stack traces
Packit Service 21b5d1
            if ( COMPILER_IS_GCC )
Packit Service 21b5d1
                if ( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9 )
Packit Service 21b5d1
                    set(SANITIZER_FLAGS "-fno-omit-frame-pointer -fsanitize=address,undefined -fno-sanitize-recover")
Packit Service 21b5d1
                elseif( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8 )
Packit Service 21b5d1
                    set(SANITIZER_FLAGS "-fno-omit-frame-pointer -fsanitize=address")
Packit Service 21b5d1
                endif()
Packit Service 21b5d1
            elseif( COMPILER_IS_CLANG )
Packit Service 21b5d1
                if ( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9 )
Packit Service 21b5d1
                    set(SANITIZER_FLAGS "-fno-omit-frame-pointer -fsanitize=address,undefined -fno-sanitize-recover=all")
Packit Service 21b5d1
                elseif ( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.4 )
Packit Service 21b5d1
                    set(SANITIZER_FLAGS "-fno-omit-frame-pointer -fsanitize=address,undefined")
Packit Service 21b5d1
                elseif( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.1 )
Packit Service 21b5d1
                    set(SANITIZER_FLAGS "-fno-omit-frame-pointer -fsanitize=address")
Packit Service 21b5d1
                endif()
Packit Service 21b5d1
            endif()
Packit Service 21b5d1

Packit Service 21b5d1
            # sorry, ASAN does not work on Windows
Packit Service 21b5d1
            if ( NOT CYGWIN AND NOT MINGW AND NOT MSYS )
Packit Service 21b5d1
                set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SANITIZER_FLAGS}")
Packit Service 21b5d1
                set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SANITIZER_FLAGS}")
Packit Service 21b5d1
                set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${SANITIZER_FLAGS}")
Packit Service 21b5d1
                set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${SANITIZER_FLAGS}")
Packit Service 21b5d1
            endif()
Packit Service 21b5d1

Packit Service 21b5d1
        endif()
Packit Service 21b5d1

Packit Service 21b5d1
        if ( EXIV2_TEAM_EXTRA_WARNINGS )
Packit Service 21b5d1
            # Note that this is intended to be used only by Exiv2 developers/contributors.
Packit Service 21b5d1

Packit Service 21b5d1
            if ( COMPILER_IS_GCC )
Packit Service 21b5d1
                if ( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.0 )
Packit Service 21b5d1
                    string(CONCAT EXTRA_COMPILE_FLAGS ${EXTRA_COMPILE_FLAGS}
Packit Service 21b5d1
                        " -Wextra"
Packit Service 21b5d1
                        " -Wlogical-op"
Packit Service 21b5d1
                        " -Wdouble-promotion"
Packit Service 21b5d1
                        " -Wshadow"
Packit Service 21b5d1
                        " -Wuseless-cast"
Packit Service 21b5d1
                        " -Wpointer-arith" # This warning is also enabled by -Wpedantic
Packit Service 21b5d1
                        " -Wformat=2"
Packit Service 21b5d1
                        #" -Wold-style-cast"
Packit Service 21b5d1
                    )
Packit Service 21b5d1
                endif ()
Packit Service 21b5d1

Packit Service 21b5d1
                if ( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.0 )
Packit Service 21b5d1
                  string(CONCAT EXTRA_COMPILE_FLAGS ${EXTRA_COMPILE_FLAGS}
Packit Service 21b5d1
                    " -Warray-bounds=2"
Packit Service 21b5d1
                    )
Packit Service 21b5d1
                endif ()
Packit Service 21b5d1

Packit Service 21b5d1
                if ( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 6.0 )
Packit Service 21b5d1
                    string(CONCAT EXTRA_COMPILE_FLAGS ${EXTRA_COMPILE_FLAGS}
Packit Service 21b5d1
                        " -Wduplicated-cond"
Packit Service 21b5d1
                    )
Packit Service 21b5d1
                endif ()
Packit Service 21b5d1

Packit Service 21b5d1
                if ( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0 )
Packit Service 21b5d1
                    string(CONCAT EXTRA_COMPILE_FLAGS ${EXTRA_COMPILE_FLAGS}
Packit Service 21b5d1
                        " -Wduplicated-branches"
Packit Service 21b5d1
                        " -Wrestrict"
Packit Service 21b5d1
                    )
Packit Service 21b5d1
                endif ()
Packit Service 21b5d1
            endif ()
Packit Service 21b5d1

Packit Service 21b5d1
            if ( COMPILER_IS_CLANG )
Packit Service 21b5d1
                # https://clang.llvm.org/docs/DiagnosticsReference.html
Packit Service 21b5d1
                # These variables are at least available since clang 3.9.1
Packit Service 21b5d1
                string(CONCAT EXTRA_COMPILE_FLAGS "-Wextra"
Packit Service 21b5d1
                    " -Wshadow"
Packit Service 21b5d1
                    " -Wassign-enum"
Packit Service 21b5d1
                    " -Wmicrosoft"
Packit Service 21b5d1
                    " -Wcomments"
Packit Service 21b5d1
                    " -Wconditional-uninitialized"
Packit Service 21b5d1
                    " -Wdirect-ivar-access"
Packit Service 21b5d1
                    " -Weffc++"
Packit Service 21b5d1
                    " -Wpointer-arith"
Packit Service 21b5d1
                    " -Wformat=2"
Packit Service 21b5d1
                    #" -Warray-bounds" # Enabled by default
Packit Service 21b5d1
                    # These two raises lot of warnings. Use them wisely
Packit Service 21b5d1
                    #" -Wconversion"
Packit Service 21b5d1
                    #" -Wold-style-cast"
Packit Service 21b5d1
                )
Packit Service 21b5d1
                # -Wdouble-promotion flag is not available in clang 3.4.2
Packit Service 21b5d1
                if ( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.4.2 )
Packit Service 21b5d1
                    string(CONCAT EXTRA_COMPILE_FLAGS ${EXTRA_COMPILE_FLAGS}
Packit Service 21b5d1
                        " -Wdouble-promotion"
Packit Service 21b5d1
                    )
Packit Service 21b5d1
                endif ()
Packit Service 21b5d1
                # -Wcomma flag is not available in clang 3.8.1
Packit Service 21b5d1
                if ( CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.8.1 )
Packit Service 21b5d1
                    string(CONCAT EXTRA_COMPILE_FLAGS ${EXTRA_COMPILE_FLAGS}
Packit Service 21b5d1
                        " -Wcomma"
Packit Service 21b5d1
                    )
Packit Service 21b5d1
                endif ()
Packit Service 21b5d1
            endif ()
Packit Service 21b5d1

Packit Service 21b5d1

Packit Service 21b5d1
        endif ()
Packit Service 21b5d1
    endif()
Packit Service 21b5d1

Packit Service 21b5d1
endif ()
Packit Service 21b5d1

Packit Service 21b5d1
# http://stackoverflow.com/questions/10113017/setting-the-msvc-runtime-in-cmake
Packit Service 21b5d1
if(MSVC)
Packit Service 21b5d1
    find_program(CLCACHE name clcache.exe
Packit Service 21b5d1
        PATHS ENV CLCACHE_PATH
Packit Service 21b5d1
        PATH_SUFFIXES Scripts clcache-4.1.0
Packit Service 21b5d1
    )
Packit Service 21b5d1
    if (CLCACHE)
Packit Service 21b5d1
        message(STATUS "clcache found in ${CLCACHE}")
Packit Service 21b5d1
        if (CMAKE_BUILD_TYPE STREQUAL "Debug")
Packit Service 21b5d1
            message(WARNING "clcache only works for Release builds")
Packit Service 21b5d1
        else()
Packit Service 21b5d1
            set(CMAKE_CXX_COMPILER ${CLCACHE})
Packit Service 21b5d1
        endif()
Packit Service 21b5d1
    endif()
Packit Service 21b5d1

Packit Service 21b5d1
    set(variables
Packit Service 21b5d1
      CMAKE_CXX_FLAGS_DEBUG
Packit Service 21b5d1
      CMAKE_CXX_FLAGS_MINSIZEREL
Packit Service 21b5d1
      CMAKE_CXX_FLAGS_RELEASE
Packit Service 21b5d1
      CMAKE_CXX_FLAGS_RELWITHDEBINFO
Packit Service 21b5d1
    )
Packit Service 21b5d1

Packit Service 21b5d1
    if (NOT BUILD_SHARED_LIBS AND NOT EXIV2_ENABLE_DYNAMIC_RUNTIME)
Packit Service 21b5d1
         message(STATUS "MSVC -> forcing use of statically-linked runtime." )
Packit Service 21b5d1
         foreach(variable ${variables})
Packit Service 21b5d1
             if(${variable} MATCHES "/MD")
Packit Service 21b5d1
                 string(REGEX REPLACE "/MD" "/MT" ${variable} "${${variable}}")
Packit Service 21b5d1
             endif()
Packit Service 21b5d1
         endforeach()
Packit Service 21b5d1
    endif()
Packit Service 21b5d1

Packit Service 21b5d1
    # remove /Ob2 and /Ob1 - they cause linker issues
Packit Service 21b5d1
    set(obs /Ob2 /Ob1)
Packit Service 21b5d1
    foreach(ob ${obs})
Packit Service 21b5d1
        foreach(variable ${variables})
Packit Service 21b5d1
            if(${variable} MATCHES ${ob} )
Packit Service 21b5d1
                string(REGEX REPLACE ${ob} "" ${variable} "${${variable}}")
Packit Service 21b5d1
            endif()
Packit Service 21b5d1
      endforeach()
Packit Service 21b5d1
    endforeach()
Packit Service 21b5d1

Packit Service 21b5d1
    if ( EXIV2_EXTRA_WARNINGS )
Packit Service 21b5d1
        string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
Packit Service 21b5d1
    endif ()
Packit Service 21b5d1

Packit Service 21b5d1
    # Object Level Parallelism
Packit Service 21b5d1
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
Packit Service 21b5d1

Packit Service 21b5d1
    add_definitions(-DNOMINMAX -DWIN32_LEAN_AND_MEAN)
Packit Service 21b5d1

Packit Service 21b5d1
endif()