cmake_modules/compilerchecks.cmake
changeset 9307 a0fef7134ade
parent 9298 2d88d01bad07
child 9315 15487f7fed42
equal deleted inserted replaced
9306:c9978ada9a3d 9307:a0fef7134ade
     6 # CMAKE_C{XX}_FLAGS is for compiler flags (c and c++)
     6 # CMAKE_C{XX}_FLAGS is for compiler flags (c and c++)
     7 # CMAKE_EXE_LINKER_FLAGS is for linker flags (also add them to pascal_flags and haskell_flags)
     7 # CMAKE_EXE_LINKER_FLAGS is for linker flags (also add them to pascal_flags and haskell_flags)
     8 # CMAKE_SHARED_LIBRARY_<lang>_FLAGS same but for shared libraries
     8 # CMAKE_SHARED_LIBRARY_<lang>_FLAGS same but for shared libraries
     9 
     9 
    10 #TODO: should there be two different checks for C and CXX?
    10 #TODO: should there be two different checks for C and CXX?
       
    11 #TODO: can the various if(platform) be avoided in some way?
    11 
    12 
    12 #stack protection, when found it needs to go in the linker flags too
       
    13 #it is disabled on win32 because it adds a dll and messes with linker
       
    14 #some bsd installations reported problems too
       
    15 #(see 822312 654424 on bugzilla.redhat.com)
       
    16 check_c_compiler_flag("-fstack-protector-all -fstack-protector" HAVE_STACKPROTECTOR)
       
    17 if(HAVE_STACKPROTECTOR AND ((NOT WIN32) OR (CMAKE_SYSTEM_NAME MATCHES BSD.OS)))
       
    18     add_flag_append(CMAKE_C_FLAGS "-fstack-protector-all -fstack-protector")
       
    19     add_flag_append(CMAKE_CXX_FLAGS "-fstack-protector-all -fstack-protector")
       
    20     add_flag_append(CMAKE_EXE_LINKER_FLAGS "-fstack-protector-all -fstack-protector")
       
    21     add_flag_append(CMAKE_SHARED_LIBRARY_C_FLAGS "-fstack-protector-all -fstack-protector")
       
    22     add_flag_append(CMAKE_SHARED_LIBRARY_CXX_FLAGS "-fstack-protector-all -fstack-protector")
       
    23 endif()
       
    24 
    13 
    25 #symbol visibility, not supported on Windows
    14 if(NOT (WIN32 OR (CMAKE_SYSTEM_NAME MATCHES BSD.OS) OR (CMAKE_SYSTEM_NAME MATCHES FreeBSD)))
    26 check_c_compiler_flag("-fvisibility=hidden" HAVE_VISIBILITY)
    15     #stack protection, when found it needs to go in the linker flags too
    27 if(HAVE_VISIBILITY AND (NOT WIN32))
    16     #it is disabled on win32 because it adds a dll and messes with linker
    28     add_flag_append(CMAKE_C_FLAGS "-fvisibility=hidden")
    17     #some bsd installations reported problems too
    29     add_flag_append(CMAKE_CXX_FLAGS "-fvisibility=hidden")
    18     #(see 822312 654424 on bugzilla.redhat.com)
       
    19     check_c_compiler_flag("-fstack-protector-all -fstack-protector" HAVE_STACKPROTECTOR)
       
    20     if(HAVE_STACKPROTECTOR)
       
    21         add_flag_append(CMAKE_C_FLAGS "-fstack-protector-all -fstack-protector")
       
    22         add_flag_append(CMAKE_CXX_FLAGS "-fstack-protector-all -fstack-protector")
       
    23         add_flag_append(CMAKE_EXE_LINKER_FLAGS "-fstack-protector-all -fstack-protector")
       
    24         add_flag_append(CMAKE_SHARED_LIBRARY_C_FLAGS "-fstack-protector-all -fstack-protector")
       
    25         add_flag_append(CMAKE_SHARED_LIBRARY_CXX_FLAGS "-fstack-protector-all -fstack-protector")
       
    26     endif()
    30 endif()
    27 endif()
    31 
    28 
    32 
    29 
    33 #check for noexecstack on ELF, Gentoo security
       
    34 set(CMAKE_REQUIRED_FLAGS "-Wl,-z,noexecstack")
       
    35 check_c_compiler_flag("" HAVE_NOEXECSTACK)
       
    36 if(HAVE_NOEXECSTACK)
       
    37     add_linker_flag("-znoexecstack")
       
    38 endif()
       
    39 
    30 
    40 #check for full relro on ELF, Debian security
    31 if(UNIX)
    41 set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro,-znow")
    32     #symbol visibility, not supported on Windows (error out then)
    42 check_c_compiler_flag("" HAVE_RELROFULL)
    33     check_c_compiler_flag("-fvisibility=hidden" HAVE_VISIBILITY)
    43 if(HAVE_RELROFULL)
    34     if(HAVE_VISIBILITY)
    44     add_linker_flag("-zrelro")
    35         add_flag_append(CMAKE_C_FLAGS "-fvisibility=hidden")
    45     add_linker_flag("-znow")
    36         add_flag_append(CMAKE_CXX_FLAGS "-fvisibility=hidden")
    46 else()
    37     endif()
    47     #if full relro is not available, try partial relro
    38 
    48     set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro")
    39     #check for noexecstack on ELF, Gentoo security
    49     check_c_compiler_flag("" HAVE_RELROPARTIAL)
    40     set(CMAKE_REQUIRED_FLAGS "-Wl,-z,noexecstack")
    50     if(HAVE_RELROPARTIAL)
    41     check_c_compiler_flag("" HAVE_NOEXECSTACK)
       
    42     if(HAVE_NOEXECSTACK)
       
    43         add_linker_flag("-znoexecstack")
       
    44     endif()
       
    45 
       
    46     #check for full relro on ELF, Debian security
       
    47     set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro,-znow")
       
    48     check_c_compiler_flag("" HAVE_RELROFULL)
       
    49     if(HAVE_RELROFULL)
    51         add_linker_flag("-zrelro")
    50         add_linker_flag("-zrelro")
       
    51         add_linker_flag("-znow")
       
    52     else()
       
    53         #if full relro is not available, try partial relro
       
    54         set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro")
       
    55         check_c_compiler_flag("" HAVE_RELROPARTIAL)
       
    56         if(HAVE_RELROPARTIAL)
       
    57             add_linker_flag("-zrelro")
       
    58         endif()
    52     endif()
    59     endif()
    53 endif()
       
    54 
    60 
    55 #check for ASLR on Windows Vista or later, requires binutils >= 2.20
    61     #this is actually an optimisation
    56 set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
    62     set(CMAKE_REQUIRED_FLAGS "-Wl,--as-needed")
    57 check_c_compiler_flag("" HAVE_WINASLR)
    63     check_c_compiler_flag("" HAVE_ASNEEDED)
    58 if(HAVE_WINASLR)
    64     if(HAVE_ASNEEDED)
    59     add_linker_flag("--nxcompat")
    65         add_linker_flag("--as-needed")
    60 endif()
    66     endif()
       
    67 else(UNIX)
       
    68     #check for ASLR on Windows Vista or later, requires binutils >= 2.20
       
    69     set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
       
    70     check_c_compiler_flag("" HAVE_WINASLR)
       
    71     if(HAVE_WINASLR)
       
    72         add_linker_flag("--nxcompat")
       
    73     endif()
    61 
    74 
    62 #check for DEP on Windows XP SP2 or later, requires binutils >= 2.20
    75     #check for DEP on Windows XP SP2 or later, requires binutils >= 2.20
    63 set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
    76     set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
    64 check_c_compiler_flag("" HAVE_WINDEP)
    77     check_c_compiler_flag("" HAVE_WINDEP)
    65 if(HAVE_WINDEP)
    78     if(HAVE_WINDEP)
    66     add_linker_flag("--dynamicbase")
    79         add_linker_flag("--dynamicbase")
    67 endif()
    80     endif()
       
    81 endif(UNIX)
    68 
    82 
    69 #this is actually an optimisation
       
    70 set(CMAKE_REQUIRED_FLAGS "-Wl,--as-needed")
       
    71 check_c_compiler_flag("" HAVE_ASNEEDED)
       
    72 if(HAVE_ASNEEDED)
       
    73     add_linker_flag("--as-needed")
       
    74 endif()
       
    75 
    83 
    76 #always unset or these flags will be spread everywhere
    84 #always unset or these flags will be spread everywhere
    77 unset(CMAKE_REQUIRED_FLAGS)
    85 unset(CMAKE_REQUIRED_FLAGS)
    78 
    86