blob: 144a6368e84b36c3255fb02e9316b0ee665f9c80 [file] [log] [blame]
Michal Vasko7a20d2e2021-05-19 16:40:23 +02001# - Use compat library providing various functions and macros that may be missing on some systems
2# Once done this will define
3#
4# compatsrc - sources to add to compilation
5#
6# Additionally, "compat.h" include directory is added and can be included.
7#
8# Author Michal Vasko <mvasko@cesnet.cz>
Michal Vaskod8a74192023-02-06 15:51:50 +01009# Copyright (c) 2021 - 2023 CESNET, z.s.p.o.
Michal Vasko7a20d2e2021-05-19 16:40:23 +020010#
11# This source code is licensed under BSD 3-Clause License (the "License").
12# You may not use this file except in compliance with the License.
13# You may obtain a copy of the License at
14#
15# https://opensource.org/licenses/BSD-3-Clause
16#
Michal Vasko9e8ac262020-04-07 13:06:45 +020017include(CheckSymbolExists)
Michal Vasko7a20d2e2021-05-19 16:40:23 +020018include(CheckIncludeFile)
Michal Vasko9e8ac262020-04-07 13:06:45 +020019include(TestBigEndian)
Michal Vasko7a20d2e2021-05-19 16:40:23 +020020if(POLICY CMP0075)
21 cmake_policy(SET CMP0075 NEW)
22endif()
Michal Vasko9e8ac262020-04-07 13:06:45 +020023
24macro(USE_COMPAT)
25 # compatibility checks
26 set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200809L)
27 list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
Michal Vasko1ca3fa42020-05-29 08:43:45 +020028 list(APPEND CMAKE_REQUIRED_DEFINITIONS -D__BSD_VISIBLE=1)
Michal Vasko7a20d2e2021-05-19 16:40:23 +020029 set(CMAKE_REQUIRED_LIBRARIES pthread)
30
Michal Vaskod8a74192023-02-06 15:51:50 +010031 check_symbol_exists(_POSIX_TIMERS "unistd.h" HAVE_CLOCK)
32 if(NOT HAVE_CLOCK)
33 message(FATAL_ERROR "Missing support for clock_gettime() and similar functions!")
34 endif()
35
36 check_symbol_exists(pthread_mutex_timedlock "pthread.h" HAVE_PTHREAD_MUTEX_TIMEDLOCK)
37 check_symbol_exists(pthread_mutex_clocklock "pthread.h" HAVE_PTHREAD_MUTEX_CLOCKLOCK)
Michal Vasko62ba1342023-10-17 08:54:26 +020038 check_symbol_exists(pthread_rwlock_timedrdlock "pthread.h" HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK)
Michal Vaskod8a74192023-02-06 15:51:50 +010039 check_symbol_exists(pthread_rwlock_clockrdlock "pthread.h" HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK)
Michal Vasko62ba1342023-10-17 08:54:26 +020040 check_symbol_exists(pthread_rwlock_timedwrlock "pthread.h" HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK)
Michal Vaskod8a74192023-02-06 15:51:50 +010041 check_symbol_exists(pthread_rwlock_clockwrlock "pthread.h" HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK)
42 check_symbol_exists(pthread_cond_clockwait "pthread.h" HAVE_PTHREAD_COND_CLOCKWAIT)
43 if(HAVE_PTHREAD_MUTEX_CLOCKLOCK)
44 # can use CLOCK_MONOTONIC only if we have pthread_mutex_clocklock()
45 check_symbol_exists(_POSIX_MONOTONIC_CLOCK "unistd.h" HAVE_CLOCK_MONOTONIC)
46 endif()
47 if(HAVE_CLOCK_MONOTONIC)
48 set(COMPAT_CLOCK_ID "CLOCK_MONOTONIC")
49 else()
50 set(COMPAT_CLOCK_ID "CLOCK_REALTIME")
51 endif()
52
Michal Vasko9e8ac262020-04-07 13:06:45 +020053 check_symbol_exists(vdprintf "stdio.h;stdarg.h" HAVE_VDPRINTF)
54 check_symbol_exists(asprintf "stdio.h" HAVE_ASPRINTF)
55 check_symbol_exists(vasprintf "stdio.h" HAVE_VASPRINTF)
Michal Vasko7a20d2e2021-05-19 16:40:23 +020056 check_symbol_exists(getline "stdio.h" HAVE_GETLINE)
57
58 check_symbol_exists(strndup "string.h" HAVE_STRNDUP)
59 check_symbol_exists(strnstr "string.h" HAVE_STRNSTR)
60 check_symbol_exists(strdupa "string.h" HAVE_STRDUPA)
61 check_symbol_exists(strchrnul "string.h" HAVE_STRCHRNUL)
Michal Vasko9e8ac262020-04-07 13:06:45 +020062
63 check_symbol_exists(get_current_dir_name "unistd.h" HAVE_GET_CURRENT_DIR_NAME)
64
Michal Vasko652eaa62020-04-21 14:11:21 +020065 TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
Michal Vasko9e8ac262020-04-07 13:06:45 +020066
Michal Vasko7a20d2e2021-05-19 16:40:23 +020067 check_include_file("stdatomic.h" HAVE_STDATOMIC)
68
69 unset(CMAKE_REQUIRED_DEFINITIONS)
70 unset(CMAKE_REQUIRED_LIBRARIES)
71
72 # header and source file (adding the source directly allows for hiding its symbols)
73 configure_file(${PROJECT_SOURCE_DIR}/compat/compat.h.in ${PROJECT_BINARY_DIR}/compat/compat.h @ONLY)
74 include_directories(${PROJECT_BINARY_DIR}/compat)
75 set(compatsrc ${PROJECT_SOURCE_DIR}/compat/compat.c)
Michal Vasko9e8ac262020-04-07 13:06:45 +020076endmacro()