cmake_modules/compilerchecks.cmake
author koda
Thu, 13 Jun 2013 22:01:41 +0200
changeset 9222 a19fa86509c2
parent 9221 c4289023ddae
child 9223 71fc5893071c
permissions -rw-r--r--
Backed out changeset c4289023ddae
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
9159
3e9d4bbc4053 set linker flags where appropriate
koda
parents: 9156
diff changeset
     6
# CMAKE_C{XX}_FLAGS is for compiler flags (c and c++)
3e9d4bbc4053 set linker flags where appropriate
koda
parents: 9156
diff changeset
     7
# CMAKE_EXE_LINKER_FLAGS is for linker flags (also add them to pascal_flags and haskell_flags)
9165
7b0d5388abc4 stack-protector flag needs to be passed to the linker as well
koda
parents: 9159
diff changeset
     8
# CMAKE_SHARED_LIBRARY_<lang>_FLAGS same but for shared libraries
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
     9
9156
6bf5359d5d14 make sure that also CXX sources pick up the flags (although I'm not 100% of the correctness of the tests)
koda
parents: 9155
diff changeset
    10
#TODO: should there be two different checks for C and CXX?
6bf5359d5d14 make sure that also CXX sources pick up the flags (although I'm not 100% of the correctness of the tests)
koda
parents: 9155
diff changeset
    11
9191
fc54667b1203 fix launching on windows
koda
parents: 9169
diff changeset
    12
#stack protection, when found it needs to go in the linker flags too
fc54667b1203 fix launching on windows
koda
parents: 9169
diff changeset
    13
#it is disabled on win32 because it adds a dll and messes with linker
fc54667b1203 fix launching on windows
koda
parents: 9169
diff changeset
    14
#(see 822312 654424 on bugzilla.redhat.com)
9222
a19fa86509c2 Backed out changeset c4289023ddae
koda
parents: 9221
diff changeset
    15
check_c_compiler_flag("-fstack-protector-all -fstack-protector" HAVE_STACKPROTECTOR)
a19fa86509c2 Backed out changeset c4289023ddae
koda
parents: 9221
diff changeset
    16
if(HAVE_STACKPROTECTOR AND (NOT WIN32))
9167
ecf19179b37e protect all types with -fstack-protector-all
koda
parents: 9165
diff changeset
    17
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-all -fstack-protector")
ecf19179b37e protect all types with -fstack-protector-all
koda
parents: 9165
diff changeset
    18
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-all -fstack-protector")
ecf19179b37e protect all types with -fstack-protector-all
koda
parents: 9165
diff changeset
    19
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fstack-protector-all -fstack-protector")
9217
992b7ef2eba3 apply linker flags to all relevant targets
koda
parents: 9211
diff changeset
    20
    set(CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS} -fstack-protector-all -fstack-protector")
992b7ef2eba3 apply linker flags to all relevant targets
koda
parents: 9211
diff changeset
    21
    set(CMAKE_SHARED_LIBRARY_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_CXX_FLAGS} -fstack-protector-all -fstack-protector")
9155
480f483de544 add fstack-protector and fvisibility=hidden to cmake tests
koda
parents: 9154
diff changeset
    22
endif()
480f483de544 add fstack-protector and fvisibility=hidden to cmake tests
koda
parents: 9154
diff changeset
    23
9222
a19fa86509c2 Backed out changeset c4289023ddae
koda
parents: 9221
diff changeset
    24
#symbol visibility, not supported on Windows (so we error out to avoid spam)
a19fa86509c2 Backed out changeset c4289023ddae
koda
parents: 9221
diff changeset
    25
check_c_compiler_flag("-fvisibility=hidden -Werror" HAVE_VISIBILITY)
9169
ef53201e95ec visibility unsupported on windows
koda
parents: 9167
diff changeset
    26
if(HAVE_VISIBILITY)
9155
480f483de544 add fstack-protector and fvisibility=hidden to cmake tests
koda
parents: 9154
diff changeset
    27
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
9156
6bf5359d5d14 make sure that also CXX sources pick up the flags (although I'm not 100% of the correctness of the tests)
koda
parents: 9155
diff changeset
    28
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
9155
480f483de544 add fstack-protector and fvisibility=hidden to cmake tests
koda
parents: 9154
diff changeset
    29
endif()
480f483de544 add fstack-protector and fvisibility=hidden to cmake tests
koda
parents: 9154
diff changeset
    30
480f483de544 add fstack-protector and fvisibility=hidden to cmake tests
koda
parents: 9154
diff changeset
    31
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    32
#check for noexecstack on ELF, Gentoo security
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    33
set(CMAKE_REQUIRED_FLAGS "-Wl,-z,noexecstack")
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    34
check_c_compiler_flag("" HAVE_NOEXECSTACK)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    35
if(HAVE_NOEXECSTACK)
9220
5e7db24f3489 gather up the linker flags in a macro
koda
parents: 9219
diff changeset
    36
    append_linker_flag("-znoexecstack")
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    37
endif()
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    38
9153
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    39
#check for full relro on ELF, Debian security
9220
5e7db24f3489 gather up the linker flags in a macro
koda
parents: 9219
diff changeset
    40
set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro,-znow")
9153
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    41
check_c_compiler_flag("" HAVE_RELROFULL)
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    42
if(HAVE_RELROFULL)
9220
5e7db24f3489 gather up the linker flags in a macro
koda
parents: 9219
diff changeset
    43
    append_linker_flag("-zrelro")
5e7db24f3489 gather up the linker flags in a macro
koda
parents: 9219
diff changeset
    44
    append_linker_flag("-znow")
9153
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    45
else()
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    46
    #if full relro is not available, try partial relro
9220
5e7db24f3489 gather up the linker flags in a macro
koda
parents: 9219
diff changeset
    47
    set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro")
9153
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    48
    check_c_compiler_flag("" HAVE_RELROPARTIAL)
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    49
    if(HAVE_RELROPARTIAL)
9220
5e7db24f3489 gather up the linker flags in a macro
koda
parents: 9219
diff changeset
    50
        append_linker_flag("-zrelro")
9153
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    51
    endif()
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    52
endif()
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    53
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    54
#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
    55
set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    56
check_c_compiler_flag("" HAVE_WINASLR)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    57
if(HAVE_WINASLR)
9220
5e7db24f3489 gather up the linker flags in a macro
koda
parents: 9219
diff changeset
    58
    append_linker_flag("--nxcompat")
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    59
endif()
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    60
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    61
#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
    62
set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    63
check_c_compiler_flag("" HAVE_WINDEP)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    64
if(HAVE_WINDEP)
9220
5e7db24f3489 gather up the linker flags in a macro
koda
parents: 9219
diff changeset
    65
    append_linker_flag("--dynamicbase")
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    66
endif()
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    67
9206
4788b24fee05 since shared libs are now our friends, let's optimise them
koda
parents: 9169
diff changeset
    68
#this is actually an optimisation
4788b24fee05 since shared libs are now our friends, let's optimise them
koda
parents: 9169
diff changeset
    69
set(CMAKE_REQUIRED_FLAGS "-Wl,--as-needed")
4788b24fee05 since shared libs are now our friends, let's optimise them
koda
parents: 9169
diff changeset
    70
check_c_compiler_flag("" HAVE_ASNEEDED)
4788b24fee05 since shared libs are now our friends, let's optimise them
koda
parents: 9169
diff changeset
    71
if(HAVE_ASNEEDED)
9220
5e7db24f3489 gather up the linker flags in a macro
koda
parents: 9219
diff changeset
    72
    append_linker_flag("--as-needed")
9206
4788b24fee05 since shared libs are now our friends, let's optimise them
koda
parents: 9169
diff changeset
    73
endif()
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    74
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    75
#always unset or these flags will be spread everywhere
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    76
unset(CMAKE_REQUIRED_FLAGS)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    77