cmake_modules/compilerchecks.cmake
branchwebgl
changeset 9521 8054d9d775fd
parent 9339 14f5f3a1e2f7
child 10783 8e742c9ffdf8
equal deleted inserted replaced
9282:92af50454cf2 9521:8054d9d775fd
     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 #(see 822312 654424 on bugzilla.redhat.com)
       
    15 check_c_compiler_flag("-fstack-protector-all -fstack-protector" HAVE_STACKPROTECTOR)
       
    16 if(HAVE_STACKPROTECTOR AND (NOT WIN32))
       
    17     add_flag_append(CMAKE_C_FLAGS "-fstack-protector-all -fstack-protector")
       
    18     add_flag_append(CMAKE_CXX_FLAGS "-fstack-protector-all -fstack-protector")
       
    19     add_flag_append(CMAKE_EXE_LINKER_FLAGS "-fstack-protector-all -fstack-protector")
       
    20     add_flag_append(CMAKE_SHARED_LIBRARY_C_FLAGS "-fstack-protector-all -fstack-protector")
       
    21     add_flag_append(CMAKE_SHARED_LIBRARY_CXX_FLAGS "-fstack-protector-all -fstack-protector")
       
    22 endif()
       
    23 
    13 
    24 #symbol visibility, not supported on Windows
    14 if(NOT (WIN32 OR (CMAKE_SYSTEM_NAME MATCHES BSD.OS) OR (CMAKE_SYSTEM_NAME MATCHES FreeBSD)))
    25 check_c_compiler_flag("-fvisibility=hidden" HAVE_VISIBILITY)
    15     #stack protection, when found it needs to go in the linker flags too
    26 if(HAVE_VISIBILITY AND (NOT WIN32))
    16     #it is disabled on win32 because it adds a dll and messes with linker
    27     add_flag_append(CMAKE_C_FLAGS "-fvisibility=hidden")
    17     #some bsd installations reported problems too
    28     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     endif()
    29 endif()
    24 endif()
    30 
    25 
    31 
    26 
    32 #check for noexecstack on ELF, Gentoo security
       
    33 set(CMAKE_REQUIRED_FLAGS "-Wl,-z,noexecstack")
       
    34 check_c_compiler_flag("" HAVE_NOEXECSTACK)
       
    35 if(HAVE_NOEXECSTACK)
       
    36     add_linker_flag("-znoexecstack")
       
    37 endif()
       
    38 
    27 
    39 #check for full relro on ELF, Debian security
    28 if(UNIX)
    40 set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro,-znow")
    29     #symbol visibility
    41 check_c_compiler_flag("" HAVE_RELROFULL)
    30     check_c_compiler_flag("-fvisibility=hidden" HAVE_VISIBILITY)
    42 if(HAVE_RELROFULL)
    31     if(HAVE_VISIBILITY)
    43     add_linker_flag("-zrelro")
    32         add_flag_append(CMAKE_C_FLAGS "-fvisibility=hidden")
    44     add_linker_flag("-znow")
    33         add_flag_append(CMAKE_CXX_FLAGS "-fvisibility=hidden")
    45 else()
    34     endif()
    46     #if full relro is not available, try partial relro
    35 
    47     set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro")
    36     #check for noexecstack on ELF, Gentoo security
    48     check_c_compiler_flag("" HAVE_RELROPARTIAL)
    37     set(CMAKE_REQUIRED_FLAGS "-Wl,-znoexecstack")
    49     if(HAVE_RELROPARTIAL)
    38     check_c_compiler_flag("" HAVE_NOEXECSTACK)
       
    39     if(HAVE_NOEXECSTACK)
       
    40         add_linker_flag("-znoexecstack")
       
    41     endif()
       
    42 
       
    43     #check for origin on ELF, BSD $ORIGIN support
       
    44     set(CMAKE_REQUIRED_FLAGS "-Wl,-zorigin")
       
    45     check_c_compiler_flag("" HAVE_ORIGIN)
       
    46     if(HAVE_ORIGIN)
       
    47         add_linker_flag("-zorigin")
       
    48     endif()
       
    49 
       
    50     #check for full relro on ELF, Debian security
       
    51     set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro,-znow")
       
    52     check_c_compiler_flag("" HAVE_RELROFULL)
       
    53     if(HAVE_RELROFULL)
    50         add_linker_flag("-zrelro")
    54         add_linker_flag("-zrelro")
       
    55         add_linker_flag("-znow")
       
    56     else()
       
    57         #if full relro is not available, try partial relro
       
    58         set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro")
       
    59         check_c_compiler_flag("" HAVE_RELROPARTIAL)
       
    60         if(HAVE_RELROPARTIAL)
       
    61             add_linker_flag("-zrelro")
       
    62         endif()
    51     endif()
    63     endif()
    52 endif()
       
    53 
    64 
    54 #check for ASLR on Windows Vista or later, requires binutils >= 2.20
    65     if(CMAKE_BUILD_TYPE MATCHES "RELEASE")
    55 set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
    66         set(CMAKE_REQUIRED_FLAGS "-Wl,--as-needed")
    56 check_c_compiler_flag("" HAVE_WINASLR)
    67         check_c_compiler_flag("" HAVE_ASNEEDED)
    57 if(HAVE_WINASLR)
    68         if(HAVE_ASNEEDED)
    58     add_linker_flag("--nxcompat")
    69             add_linker_flag("--as-needed")
    59 endif()
    70         endif()
       
    71     endif()
       
    72 else(UNIX)
       
    73     #check for ASLR on Windows Vista or later, requires binutils >= 2.20
       
    74     set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
       
    75     check_c_compiler_flag("" HAVE_WINASLR)
       
    76     if(HAVE_WINASLR)
       
    77         add_linker_flag("--nxcompat")
       
    78     endif()
    60 
    79 
    61 #check for DEP on Windows XP SP2 or later, requires binutils >= 2.20
    80     #check for DEP on Windows XP SP2 or later, requires binutils >= 2.20
    62 set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
    81     set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
    63 check_c_compiler_flag("" HAVE_WINDEP)
    82     check_c_compiler_flag("" HAVE_WINDEP)
    64 if(HAVE_WINDEP)
    83     if(HAVE_WINDEP)
    65     add_linker_flag("--dynamicbase")
    84         add_linker_flag("--dynamicbase")
    66 endif()
    85     endif()
       
    86 endif(UNIX)
    67 
    87 
    68 #this is actually an optimisation
       
    69 set(CMAKE_REQUIRED_FLAGS "-Wl,--as-needed")
       
    70 check_c_compiler_flag("" HAVE_ASNEEDED)
       
    71 if(HAVE_ASNEEDED)
       
    72     add_linker_flag("--as-needed")
       
    73 endif()
       
    74 
    88 
    75 #always unset or these flags will be spread everywhere
    89 #always unset or these flags will be spread everywhere
    76 unset(CMAKE_REQUIRED_FLAGS)
    90 unset(CMAKE_REQUIRED_FLAGS)
    77 
    91