cmake_modules/compilerchecks.cmake
author koda
Sat, 08 Jun 2013 02:03:02 +0200
changeset 9154 a43406054648
parent 9153 354a9803fe91
child 9155 480f483de544
permissions -rw-r--r--
also apply linker flags to haskell programs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
     1
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
     2
#TESTING TIME
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
     3
include(CheckCCompilerFlag)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
     4
#when you need to check for a linker flag, just leave the argument of "check_c_compiler_flag" empty
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
     5
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
     6
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
     7
#check for noexecstack on ELF, Gentoo security
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
     8
set(CMAKE_REQUIRED_FLAGS "-Wl,-z,noexecstack")
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
     9
check_c_compiler_flag("" HAVE_NOEXECSTACK)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    10
if(HAVE_NOEXECSTACK)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    11
    list(APPEND pascal_flags "-k-z" "-knoexecstack")
9154
a43406054648 also apply linker flags to haskell programs
koda
parents: 9153
diff changeset
    12
    list(APPEND haskell_flags "-optl" "-z" "-optl" "noexecstack")
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    13
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    14
endif()
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    15
9153
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    16
#check for full relro on ELF, Debian security
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    17
set(CMAKE_REQUIRED_FLAGS "-Wl,-z,relro,-z,now")
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    18
check_c_compiler_flag("" HAVE_RELROFULL)
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    19
if(HAVE_RELROFULL)
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    20
    list(APPEND pascal_flags "-k-z" "-krelro" "-k-z" "-know")
9154
a43406054648 also apply linker flags to haskell programs
koda
parents: 9153
diff changeset
    21
    list(APPEND haskell_flags "-optl" "-z" "-optl" "relro" "-optl" "-z" "-optl" "now")
9153
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    22
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    23
else()
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    24
    #if full relro is not available, try partial relro
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    25
    set(CMAKE_REQUIRED_FLAGS "-Wl,-z,relro")
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    26
    check_c_compiler_flag("" HAVE_RELROPARTIAL)
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    27
    if(HAVE_RELROPARTIAL)
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    28
        list(APPEND pascal_flags "-k-z" "-krelro")
9154
a43406054648 also apply linker flags to haskell programs
koda
parents: 9153
diff changeset
    29
        list(APPEND haskell_flags "-optl" "-z" "-optl" "relro")
9153
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    30
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    31
    endif()
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    32
endif()
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    33
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    34
#check for ASLR on Windows Vista or later, requires binutils >= 2.20
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    35
set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    36
check_c_compiler_flag("" HAVE_WINASLR)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    37
if(HAVE_WINASLR)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    38
    list(APPEND pascal_flags "-k--nxcompat")
9154
a43406054648 also apply linker flags to haskell programs
koda
parents: 9153
diff changeset
    39
    list(APPEND haskell_flags "-optl" "--nxcompat")
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    40
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    41
endif()
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    42
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    43
#check for DEP on Windows XP SP2 or later, requires binutils >= 2.20
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    44
set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    45
check_c_compiler_flag("" HAVE_WINDEP)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    46
if(HAVE_WINDEP)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    47
    list(APPEND pascal_flags "-k--dynamicbase")
9154
a43406054648 also apply linker flags to haskell programs
koda
parents: 9153
diff changeset
    48
    list(APPEND haskell_flags "-optl" "--dynamicbase")
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    49
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_REQUIRED_FLAGS}")
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    50
endif()
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    51
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    52
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    53
#always unset or these flags will be spread everywhere
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    54
unset(CMAKE_REQUIRED_FLAGS)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    55