summaryrefslogtreecommitdiffstats
path: root/CMakeModules/UseCompat.cmake
blob: c1befd7a45c14a891ff1dfa12fd976884afb9ab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# - Use compat library providing various functions and macros that may be missing on some systems
# Once done this will define
#
# compatsrc - sources to add to compilation
#
# Additionally, "compat.h" include directory is added and can be included.
#
# Author Michal Vasko <mvasko@cesnet.cz>
#  Copyright (c) 2021 CESNET, z.s.p.o.
#
# This source code is licensed under BSD 3-Clause License (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://opensource.org/licenses/BSD-3-Clause
#
include(CheckSymbolExists)
include(CheckFunctionExists)
include(CheckIncludeFile)
include(TestBigEndian)
if(POLICY CMP0075)
    cmake_policy(SET CMP0075 NEW)
endif()

macro(USE_COMPAT)
    # compatibility checks
    set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200809L)
    list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
    list(APPEND CMAKE_REQUIRED_DEFINITIONS -D__BSD_VISIBLE=1)
    set(CMAKE_REQUIRED_LIBRARIES pthread)

    check_symbol_exists(vdprintf "stdio.h;stdarg.h" HAVE_VDPRINTF)
    check_symbol_exists(asprintf "stdio.h" HAVE_ASPRINTF)
    check_symbol_exists(vasprintf "stdio.h" HAVE_VASPRINTF)
    check_symbol_exists(getline "stdio.h" HAVE_GETLINE)

    check_symbol_exists(strndup "string.h" HAVE_STRNDUP)
    check_symbol_exists(strnstr "string.h" HAVE_STRNSTR)
    check_symbol_exists(strdupa "string.h" HAVE_STRDUPA)
    check_symbol_exists(strchrnul "string.h" HAVE_STRCHRNUL)

    check_symbol_exists(get_current_dir_name "unistd.h" HAVE_GET_CURRENT_DIR_NAME)

    check_function_exists(pthread_mutex_timedlock HAVE_PTHREAD_MUTEX_TIMEDLOCK)

    TEST_BIG_ENDIAN(IS_BIG_ENDIAN)

    check_include_file("stdatomic.h" HAVE_STDATOMIC)

    include(CheckStructHasMember)
    check_struct_has_member("struct tm" tm_gmtoff time.h HAVE_TM_GMTOFF)
    check_symbol_exists(timezone time.h HAVE_TIME_H_TIMEZONE)

    check_symbol_exists(realpath "stdlib.h" HAVE_REALPATH)
    check_symbol_exists(localtime_r "time.h" HAVE_LOCALTIME_R)
    check_symbol_exists(gmtime_r "time.h" HAVE_GMTIME_R)
    check_symbol_exists(strptime "time.h" HAVE_STRPTIME)
    check_symbol_exists(mmap "sys/mman.h" HAVE_MMAP)
    check_symbol_exists(dirname "libgen.h" HAVE_DIRNAME)
    check_symbol_exists(setenv "stdlib.h" HAVE_SETENV)

    unset(CMAKE_REQUIRED_DEFINITIONS)
    unset(CMAKE_REQUIRED_LIBRARIES)

    # header and source file (adding the source directly allows for hiding its symbols)
    configure_file(${PROJECT_SOURCE_DIR}/compat/compat.h.in ${PROJECT_BINARY_DIR}/compat/compat.h @ONLY)
    include_directories(${PROJECT_BINARY_DIR}/compat)
    set(compatsrc ${PROJECT_SOURCE_DIR}/compat/compat.c)
    if(WIN32)
        include_directories(${PROJECT_SOURCE_DIR}/compat/posix-shims)
    endif()
    if(NOT HAVE_STRPTIME)
        set(compatsrc ${compatsrc} ${PROJECT_SOURCE_DIR}/compat/strptime.c)
    endif()
endmacro()