Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 1 | # - 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 Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 9 | # Copyright (c) 2021 - 2023 CESNET, z.s.p.o. |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 10 | # |
| 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 Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 17 | include(CheckSymbolExists) |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 18 | include(CheckIncludeFile) |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 19 | include(TestBigEndian) |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 20 | if(POLICY CMP0075) |
| 21 | cmake_policy(SET CMP0075 NEW) |
| 22 | endif() |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 23 | |
| 24 | macro(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 Vasko | 1ca3fa4 | 2020-05-29 08:43:45 +0200 | [diff] [blame] | 28 | list(APPEND CMAKE_REQUIRED_DEFINITIONS -D__BSD_VISIBLE=1) |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 29 | set(CMAKE_REQUIRED_LIBRARIES pthread) |
| 30 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 31 | 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 Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 51 | 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 Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 54 | 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 Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 60 | |
| 61 | check_symbol_exists(get_current_dir_name "unistd.h" HAVE_GET_CURRENT_DIR_NAME) |
| 62 | |
Michal Vasko | 652eaa6 | 2020-04-21 14:11:21 +0200 | [diff] [blame] | 63 | TEST_BIG_ENDIAN(IS_BIG_ENDIAN) |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 64 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 65 | 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 Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 74 | endmacro() |