cmake_modules/compilerchecks.cmake
author Simon McVittie <smcv@debian.org>
Mon, 12 Sep 2022 10:40:53 -0400
branch1.0.0
changeset 15859 7b1d6dfa3173
parent 15451 3102d95a870e
permissions -rw-r--r--
Remove FindSDL2 find-module, use sdl2-config.cmake instead This requires SDL >= 2.0.4. Since <https://bugzilla.libsdl.org/show_bug.cgi?id=2464> was fixed in SDL 2.0.4, SDL behaves as a CMake "config-file package", even if it was not itself built using CMake: it installs a sdl2-config.cmake file to ${libdir}/cmake/SDL2, which tells CMake where to find SDL's headers and library, analogous to a pkg-config .pc file. As a result, we no longer need to copy/paste a "find-module package" to be able to find a system copy of SDL >= 2.0.4 with find_package(SDL2). Find-module packages are now discouraged by the CMake developers, in favour of having upstream projects behave as config-file packages. This results in a small API change: FindSDL2 used to set SDL2_INCLUDE_DIR and SDL2_LIBRARY, but the standard behaviour for config-file packages is to set <name>_INCLUDE_DIRS and <name>_LIBRARIES. Use the CONFIG keyword to make sure we search in config-file package mode, and will not find a FindSDL2.cmake in some other directory that implements the old interface. In addition to deleting redundant code, this avoids some assumptions in FindSDL2 about the layout of a SDL installation. The current libsdl2-dev package in Debian breaks those assumptions; this is considered a bug and will hopefully be fixed soon, but it illustrates how fragile these assumptions can be. We can be more robust against different installation layouts by relying on SDL's own CMake integration. When linking to a copy of CMake in a non-standard location, users can now set the SDL2_DIR or CMAKE_PREFIX_PATH environment variable to point to it; previously, these users would have used the SDL2DIR environment variable. This continues to be unnecessary if using matching system-wide installations of CMake and SDL2, for example both from Debian.


#TESTING TIME
include(CheckCCompilerFlag)
#when you need to check for a linker flag, just leave the argument of "check_c_compiler_flag" empty

# CMAKE_C{XX}_FLAGS is for compiler flags (c and c++)
# CMAKE_EXE_LINKER_FLAGS is for linker flags (also add them to pascal_flags and haskell_flags)
# CMAKE_SHARED_LIBRARY_<lang>_FLAGS same but for shared libraries

#TODO: should there be two different checks for C and CXX?
#TODO: can the various if(platform) be avoided in some way?


if(NOT (WIN32 OR (CMAKE_SYSTEM_NAME MATCHES BSD.OS) OR (CMAKE_SYSTEM_NAME MATCHES FreeBSD)))
    #stack protection, when found it needs to go in the linker flags too
    #it is disabled on win32 because it adds a dll and messes with linker
    #some bsd installations reported problems too
    #(see 822312 654424 on bugzilla.redhat.com)
    check_c_compiler_flag("-fstack-protector-all -fstack-protector" HAVE_STACKPROTECTOR)
    if(HAVE_STACKPROTECTOR)
        add_flag_append(CMAKE_C_FLAGS "-fstack-protector-all -fstack-protector")
        add_flag_append(CMAKE_CXX_FLAGS "-fstack-protector-all -fstack-protector")
    endif()
endif()



if(UNIX)
    #symbol visibility
    check_c_compiler_flag("-fvisibility=hidden" HAVE_VISIBILITY)
    if(HAVE_VISIBILITY)
        add_flag_append(CMAKE_C_FLAGS "-fvisibility=hidden")
        add_flag_append(CMAKE_CXX_FLAGS "-fvisibility=hidden")
    endif()

    #check for noexecstack on ELF, Gentoo security
    set(CMAKE_REQUIRED_FLAGS "-Wl,-znoexecstack")
    check_c_compiler_flag("" HAVE_NOEXECSTACK)
    if(HAVE_NOEXECSTACK)
        add_linker_flag("-znoexecstack")
    endif()

    #check for origin on ELF, BSD $ORIGIN support
    set(CMAKE_REQUIRED_FLAGS "-Wl,-zorigin")
    check_c_compiler_flag("" HAVE_ORIGIN)
    if(HAVE_ORIGIN)
        add_linker_flag("-zorigin")
    endif()

    #check for full relro on ELF, Debian security
    set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro,-znow")
    check_c_compiler_flag("" HAVE_RELROFULL)
    if(HAVE_RELROFULL)
        add_linker_flag("-zrelro")
        add_linker_flag("-znow")
    else()
        #if full relro is not available, try partial relro
        set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro")
        check_c_compiler_flag("" HAVE_RELROPARTIAL)
        if(HAVE_RELROPARTIAL)
            add_linker_flag("-zrelro")
        endif()
    endif()

    if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
        set(CMAKE_REQUIRED_FLAGS "-Wl,--as-needed")
        check_c_compiler_flag("" HAVE_ASNEEDED)
        if(HAVE_ASNEEDED)
            add_linker_flag("--as-needed")
        endif()
    endif()
else(UNIX)
    #check for ASLR on Windows Vista or later, requires binutils >= 2.20
    set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
    check_c_compiler_flag("" HAVE_WINASLR)
    if(HAVE_WINASLR)
        add_linker_flag("--nxcompat")
    endif()

    #check for DEP on Windows XP SP2 or later, requires binutils >= 2.20
    set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
    check_c_compiler_flag("" HAVE_WINDEP)
    if(HAVE_WINDEP)
        add_linker_flag("--dynamicbase")
    endif()
endif(UNIX)


#always unset or these flags will be spread everywhere
unset(CMAKE_REQUIRED_FLAGS)