cmake_modules/compilerchecks.cmake
changeset 9156 6bf5359d5d14
parent 9155 480f483de544
child 9159 3e9d4bbc4053
equal deleted inserted replaced
9155:480f483de544 9156:6bf5359d5d14
     2 #TESTING TIME
     2 #TESTING TIME
     3 include(CheckCCompilerFlag)
     3 include(CheckCCompilerFlag)
     4 #when you need to check for a linker flag, just leave the argument of "check_c_compiler_flag" empty
     4 #when you need to check for a linker flag, just leave the argument of "check_c_compiler_flag" empty
     5 
     5 
     6 
     6 
       
     7 #TODO: should there be two different checks for C and CXX?
       
     8 
     7 #stack protection
     9 #stack protection
     8 check_c_compiler_flag("-fstack-protector" HAVE_STACKPROTECTOR)
    10 check_c_compiler_flag("-fstack-protector" HAVE_STACKPROTECTOR)
     9 if(HAVE_STACKPROTECTOR)
    11 if(HAVE_STACKPROTECTOR)
    10     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
    12     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
       
    13     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
    11 endif()
    14 endif()
    12 
    15 
    13 #symbol visibility
    16 #symbol visibility
    14 check_c_compiler_flag("-fvisibility=hidden" HAVE_VISIBILITYH)
    17 check_c_compiler_flag("-fvisibility=hidden" HAVE_VISIBILITYH)
    15 if(HAVE_VISIBILITYH)
    18 if(HAVE_VISIBILITYH)
    16     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
    19     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
       
    20     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
    17 endif()
    21 endif()
    18 
    22 
    19 
    23 
    20 #check for noexecstack on ELF, Gentoo security
    24 #check for noexecstack on ELF, Gentoo security
    21 set(CMAKE_REQUIRED_FLAGS "-Wl,-z,noexecstack")
    25 set(CMAKE_REQUIRED_FLAGS "-Wl,-z,noexecstack")
    22 check_c_compiler_flag("" HAVE_NOEXECSTACK)
    26 check_c_compiler_flag("" HAVE_NOEXECSTACK)
    23 if(HAVE_NOEXECSTACK)
    27 if(HAVE_NOEXECSTACK)
    24     list(APPEND pascal_flags "-k-z" "-knoexecstack")
    28     list(APPEND pascal_flags "-k-z" "-knoexecstack")
    25     list(APPEND haskell_flags "-optl" "-z" "-optl" "noexecstack")
    29     list(APPEND haskell_flags "-optl" "-z" "-optl" "noexecstack")
    26     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
    30     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
       
    31     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
    27 endif()
    32 endif()
    28 
    33 
    29 #check for full relro on ELF, Debian security
    34 #check for full relro on ELF, Debian security
    30 set(CMAKE_REQUIRED_FLAGS "-Wl,-z,relro,-z,now")
    35 set(CMAKE_REQUIRED_FLAGS "-Wl,-z,relro,-z,now")
    31 check_c_compiler_flag("" HAVE_RELROFULL)
    36 check_c_compiler_flag("" HAVE_RELROFULL)
    32 if(HAVE_RELROFULL)
    37 if(HAVE_RELROFULL)
    33     list(APPEND pascal_flags "-k-z" "-krelro" "-k-z" "-know")
    38     list(APPEND pascal_flags "-k-z" "-krelro" "-k-z" "-know")
    34     list(APPEND haskell_flags "-optl" "-z" "-optl" "relro" "-optl" "-z" "-optl" "now")
    39     list(APPEND haskell_flags "-optl" "-z" "-optl" "relro" "-optl" "-z" "-optl" "now")
    35     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
    40     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
       
    41     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
    36 else()
    42 else()
    37     #if full relro is not available, try partial relro
    43     #if full relro is not available, try partial relro
    38     set(CMAKE_REQUIRED_FLAGS "-Wl,-z,relro")
    44     set(CMAKE_REQUIRED_FLAGS "-Wl,-z,relro")
    39     check_c_compiler_flag("" HAVE_RELROPARTIAL)
    45     check_c_compiler_flag("" HAVE_RELROPARTIAL)
    40     if(HAVE_RELROPARTIAL)
    46     if(HAVE_RELROPARTIAL)
    41         list(APPEND pascal_flags "-k-z" "-krelro")
    47         list(APPEND pascal_flags "-k-z" "-krelro")
    42         list(APPEND haskell_flags "-optl" "-z" "-optl" "relro")
    48         list(APPEND haskell_flags "-optl" "-z" "-optl" "relro")
    43         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
    49         set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
       
    50         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
    44     endif()
    51     endif()
    45 endif()
    52 endif()
    46 
    53 
    47 #check for ASLR on Windows Vista or later, requires binutils >= 2.20
    54 #check for ASLR on Windows Vista or later, requires binutils >= 2.20
    48 set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
    55 set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
    49 check_c_compiler_flag("" HAVE_WINASLR)
    56 check_c_compiler_flag("" HAVE_WINASLR)
    50 if(HAVE_WINASLR)
    57 if(HAVE_WINASLR)
    51     list(APPEND pascal_flags "-k--nxcompat")
    58     list(APPEND pascal_flags "-k--nxcompat")
    52     list(APPEND haskell_flags "-optl" "--nxcompat")
    59     list(APPEND haskell_flags "-optl" "--nxcompat")
    53     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
    60     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
       
    61     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
    54 endif()
    62 endif()
    55 
    63 
    56 #check for DEP on Windows XP SP2 or later, requires binutils >= 2.20
    64 #check for DEP on Windows XP SP2 or later, requires binutils >= 2.20
    57 set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
    65 set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
    58 check_c_compiler_flag("" HAVE_WINDEP)
    66 check_c_compiler_flag("" HAVE_WINDEP)
    59 if(HAVE_WINDEP)
    67 if(HAVE_WINDEP)
    60     list(APPEND pascal_flags "-k--dynamicbase")
    68     list(APPEND pascal_flags "-k--dynamicbase")
    61     list(APPEND haskell_flags "-optl" "--dynamicbase")
    69     list(APPEND haskell_flags "-optl" "--dynamicbase")
    62     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
    70     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
       
    71     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
    63 endif()
    72 endif()
    64 
    73 
    65 
    74 
    66 #always unset or these flags will be spread everywhere
    75 #always unset or these flags will be spread everywhere
    67 unset(CMAKE_REQUIRED_FLAGS)
    76 unset(CMAKE_REQUIRED_FLAGS)