cmake_modules/compilerchecks.cmake
author nemo
Mon, 24 Oct 2022 10:02:06 -0400
changeset 15884 f9e1a7c8a658
parent 15451 3102d95a870e
permissions -rw-r--r--
bump version
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?
9307
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    11
#TODO: can the various if(platform) be avoided in some way?
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
    12
9155
480f483de544 add fstack-protector and fvisibility=hidden to cmake tests
koda
parents: 9154
diff changeset
    13
9307
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    14
if(NOT (WIN32 OR (CMAKE_SYSTEM_NAME MATCHES BSD.OS) OR (CMAKE_SYSTEM_NAME MATCHES FreeBSD)))
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    15
    #stack protection, when found it needs to go in the linker flags too
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    16
    #it is disabled on win32 because it adds a dll and messes with linker
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    17
    #some bsd installations reported problems too
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    18
    #(see 822312 654424 on bugzilla.redhat.com)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    19
    check_c_compiler_flag("-fstack-protector-all -fstack-protector" HAVE_STACKPROTECTOR)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    20
    if(HAVE_STACKPROTECTOR)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    21
        add_flag_append(CMAKE_C_FLAGS "-fstack-protector-all -fstack-protector")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    22
        add_flag_append(CMAKE_CXX_FLAGS "-fstack-protector-all -fstack-protector")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    23
    endif()
9155
480f483de544 add fstack-protector and fvisibility=hidden to cmake tests
koda
parents: 9154
diff changeset
    24
endif()
480f483de544 add fstack-protector and fvisibility=hidden to cmake tests
koda
parents: 9154
diff changeset
    25
480f483de544 add fstack-protector and fvisibility=hidden to cmake tests
koda
parents: 9154
diff changeset
    26
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    27
9307
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    28
if(UNIX)
9315
15487f7fed42 as-needed only at release time
koda
parents: 9307
diff changeset
    29
    #symbol visibility
9307
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    30
    check_c_compiler_flag("-fvisibility=hidden" HAVE_VISIBILITY)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    31
    if(HAVE_VISIBILITY)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    32
        add_flag_append(CMAKE_C_FLAGS "-fvisibility=hidden")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    33
        add_flag_append(CMAKE_CXX_FLAGS "-fvisibility=hidden")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    34
    endif()
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    35
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    36
    #check for noexecstack on ELF, Gentoo security
9315
15487f7fed42 as-needed only at release time
koda
parents: 9307
diff changeset
    37
    set(CMAKE_REQUIRED_FLAGS "-Wl,-znoexecstack")
9307
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    38
    check_c_compiler_flag("" HAVE_NOEXECSTACK)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    39
    if(HAVE_NOEXECSTACK)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    40
        add_linker_flag("-znoexecstack")
9153
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    41
    endif()
354a9803fe91 add relro linker tests (issue 663)
koda
parents: 9152
diff changeset
    42
9339
14f5f3a1e2f7 some work on cmake_pascal files to better support flags we use, make engine compile as library again
koda
parents: 9315
diff changeset
    43
    #check for origin on ELF, BSD $ORIGIN support
14f5f3a1e2f7 some work on cmake_pascal files to better support flags we use, make engine compile as library again
koda
parents: 9315
diff changeset
    44
    set(CMAKE_REQUIRED_FLAGS "-Wl,-zorigin")
14f5f3a1e2f7 some work on cmake_pascal files to better support flags we use, make engine compile as library again
koda
parents: 9315
diff changeset
    45
    check_c_compiler_flag("" HAVE_ORIGIN)
14f5f3a1e2f7 some work on cmake_pascal files to better support flags we use, make engine compile as library again
koda
parents: 9315
diff changeset
    46
    if(HAVE_ORIGIN)
14f5f3a1e2f7 some work on cmake_pascal files to better support flags we use, make engine compile as library again
koda
parents: 9315
diff changeset
    47
        add_linker_flag("-zorigin")
14f5f3a1e2f7 some work on cmake_pascal files to better support flags we use, make engine compile as library again
koda
parents: 9315
diff changeset
    48
    endif()
14f5f3a1e2f7 some work on cmake_pascal files to better support flags we use, make engine compile as library again
koda
parents: 9315
diff changeset
    49
9307
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    50
    #check for full relro on ELF, Debian security
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    51
    set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro,-znow")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    52
    check_c_compiler_flag("" HAVE_RELROFULL)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    53
    if(HAVE_RELROFULL)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    54
        add_linker_flag("-zrelro")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    55
        add_linker_flag("-znow")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    56
    else()
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    57
        #if full relro is not available, try partial relro
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    58
        set(CMAKE_REQUIRED_FLAGS "-Wl,-zrelro")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    59
        check_c_compiler_flag("" HAVE_RELROPARTIAL)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    60
        if(HAVE_RELROPARTIAL)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    61
            add_linker_flag("-zrelro")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    62
        endif()
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    63
    endif()
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    64
15451
3102d95a870e CMAKE_BUILD_TYPE: Replace MATCHES with STREQUAL
Wuzzy <Wuzzy2@mail.ru>
parents: 15449
diff changeset
    65
    if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
9315
15487f7fed42 as-needed only at release time
koda
parents: 9307
diff changeset
    66
        set(CMAKE_REQUIRED_FLAGS "-Wl,--as-needed")
15487f7fed42 as-needed only at release time
koda
parents: 9307
diff changeset
    67
        check_c_compiler_flag("" HAVE_ASNEEDED)
15487f7fed42 as-needed only at release time
koda
parents: 9307
diff changeset
    68
        if(HAVE_ASNEEDED)
15487f7fed42 as-needed only at release time
koda
parents: 9307
diff changeset
    69
            add_linker_flag("--as-needed")
15487f7fed42 as-needed only at release time
koda
parents: 9307
diff changeset
    70
        endif()
9307
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    71
    endif()
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    72
else(UNIX)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    73
    #check for ASLR on Windows Vista or later, requires binutils >= 2.20
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    74
    set(CMAKE_REQUIRED_FLAGS "-Wl,--nxcompat")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    75
    check_c_compiler_flag("" HAVE_WINASLR)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    76
    if(HAVE_WINASLR)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    77
        add_linker_flag("--nxcompat")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    78
    endif()
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    79
9307
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    80
    #check for DEP on Windows XP SP2 or later, requires binutils >= 2.20
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    81
    set(CMAKE_REQUIRED_FLAGS "-Wl,--dynamicbase")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    82
    check_c_compiler_flag("" HAVE_WINDEP)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    83
    if(HAVE_WINDEP)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    84
        add_linker_flag("--dynamicbase")
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    85
    endif()
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    86
endif(UNIX)
a0fef7134ade tell ya what, let's just disable these tests on some platform for now
koda
parents: 9298
diff changeset
    87
9152
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    88
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    89
#always unset or these flags will be spread everywhere
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    90
unset(CMAKE_REQUIRED_FLAGS)
c884e40dca9e move compiler checks in a separate cmake module
koda
parents:
diff changeset
    91