blob: 3d8ad7ac56f5b2c44fea883f79dbf7f77db79cdf [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)
38 check_symbol_exists(pthread_rwlock_clockrdlock "pthread.h" HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK)
39 check_symbol_exists(pthread_rwlock_clockwrlock "pthread.h" HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK)
40 check_symbol_exists(pthread_cond_clockwait "pthread.h" HAVE_PTHREAD_COND_CLOCKWAIT)
41 if(HAVE_PTHREAD_MUTEX_CLOCKLOCK)
42 # can use CLOCK_MONOTONIC only if we have pthread_mutex_clocklock()
43 check_symbol_exists(_POSIX_MONOTONIC_CLOCK "unistd.h" HAVE_CLOCK_MONOTONIC)
44 endif()
45 if(HAVE_CLOCK_MONOTONIC)
46 set(COMPAT_CLOCK_ID "CLOCK_MONOTONIC")
47 else()
48 set(COMPAT_CLOCK_ID "CLOCK_REALTIME")
49 endif()
50
Michal Vasko9e8ac262020-04-07 13:06:45 +020051 check_symbol_exists(vdprintf "stdio.h;stdarg.h" HAVE_VDPRINTF)
52 check_symbol_exists(asprintf "stdio.h" HAVE_ASPRINTF)
53 check_symbol_exists(vasprintf "stdio.h" HAVE_VASPRINTF)
Michal Vasko7a20d2e2021-05-19 16:40:23 +020054 check_symbol_exists(getline "stdio.h" HAVE_GETLINE)
55
56 check_symbol_exists(strndup "string.h" HAVE_STRNDUP)
57 check_symbol_exists(strnstr "string.h" HAVE_STRNSTR)
58 check_symbol_exists(strdupa "string.h" HAVE_STRDUPA)
59 check_symbol_exists(strchrnul "string.h" HAVE_STRCHRNUL)
Michal Vasko9e8ac262020-04-07 13:06:45 +020060
61 check_symbol_exists(get_current_dir_name "unistd.h" HAVE_GET_CURRENT_DIR_NAME)
62
Michal Vasko652eaa62020-04-21 14:11:21 +020063 TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
Michal Vasko9e8ac262020-04-07 13:06:45 +020064
Michal Vasko7a20d2e2021-05-19 16:40:23 +020065 check_include_file("stdatomic.h" HAVE_STDATOMIC)
66
67 unset(CMAKE_REQUIRED_DEFINITIONS)
68 unset(CMAKE_REQUIRED_LIBRARIES)
69
70 # header and source file (adding the source directly allows for hiding its symbols)
71 configure_file(${PROJECT_SOURCE_DIR}/compat/compat.h.in ${PROJECT_BINARY_DIR}/compat/compat.h @ONLY)
72 include_directories(${PROJECT_BINARY_DIR}/compat)
73 set(compatsrc ${PROJECT_SOURCE_DIR}/compat/compat.c)
Michal Vasko9e8ac262020-04-07 13:06:45 +020074endmacro()