Michal Vasko | d304a08 | 2021-05-19 16:36:10 +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> |
| 9 | # Copyright (c) 2021 CESNET, z.s.p.o. |
| 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 | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 17 | include(CheckSymbolExists) |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 18 | include(CheckFunctionExists) |
| 19 | include(CheckIncludeFile) |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 20 | include(TestBigEndian) |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 21 | if(POLICY CMP0075) |
| 22 | cmake_policy(SET CMP0075 NEW) |
| 23 | endif() |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 24 | |
| 25 | macro(USE_COMPAT) |
| 26 | # compatibility checks |
| 27 | set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=200809L) |
| 28 | list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) |
| 29 | list(APPEND CMAKE_REQUIRED_DEFINITIONS -D__BSD_VISIBLE=1) |
Michal Vasko | 762fbcf | 2023-02-10 11:34:06 +0100 | [diff] [blame] | 30 | list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_DEFAULT_SOURCE) |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 31 | set(CMAKE_REQUIRED_LIBRARIES pthread) |
| 32 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 33 | check_symbol_exists(vdprintf "stdio.h;stdarg.h" HAVE_VDPRINTF) |
| 34 | check_symbol_exists(asprintf "stdio.h" HAVE_ASPRINTF) |
| 35 | check_symbol_exists(vasprintf "stdio.h" HAVE_VASPRINTF) |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 36 | check_symbol_exists(getline "stdio.h" HAVE_GETLINE) |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 37 | |
| 38 | check_symbol_exists(strndup "string.h" HAVE_STRNDUP) |
| 39 | check_symbol_exists(strnstr "string.h" HAVE_STRNSTR) |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 40 | check_symbol_exists(strdupa "string.h" HAVE_STRDUPA) |
| 41 | check_symbol_exists(strchrnul "string.h" HAVE_STRCHRNUL) |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 42 | |
| 43 | check_symbol_exists(get_current_dir_name "unistd.h" HAVE_GET_CURRENT_DIR_NAME) |
| 44 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 45 | check_function_exists(pthread_mutex_timedlock HAVE_PTHREAD_MUTEX_TIMEDLOCK) |
| 46 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 47 | TEST_BIG_ENDIAN(IS_BIG_ENDIAN) |
| 48 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 49 | check_include_file("stdatomic.h" HAVE_STDATOMIC) |
| 50 | |
Jan Kundrát | 147a72c | 2021-12-10 16:13:57 +0100 | [diff] [blame] | 51 | check_symbol_exists(realpath "stdlib.h" HAVE_REALPATH) |
Jan Kundrát | 9190763 | 2021-12-14 16:18:16 +0100 | [diff] [blame] | 52 | check_symbol_exists(localtime_r "time.h" HAVE_LOCALTIME_R) |
Jan Kundrát | a9caaf7 | 2022-02-14 18:29:43 +0100 | [diff] [blame] | 53 | check_symbol_exists(gmtime_r "time.h" HAVE_GMTIME_R) |
Michal Vasko | cc68224 | 2023-02-10 12:02:43 +0100 | [diff] [blame] | 54 | check_function_exists(timegm HAVE_TIMEGM) |
Jan Kundrát | a5a7e29 | 2021-12-10 17:37:46 +0100 | [diff] [blame] | 55 | check_symbol_exists(strptime "time.h" HAVE_STRPTIME) |
Jan Kundrát | f1960dc | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 56 | check_symbol_exists(mmap "sys/mman.h" HAVE_MMAP) |
Jan Kundrát | c880530 | 2021-12-11 23:40:26 +0100 | [diff] [blame] | 57 | check_symbol_exists(setenv "stdlib.h" HAVE_SETENV) |
Jan Kundrát | 147a72c | 2021-12-10 16:13:57 +0100 | [diff] [blame] | 58 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 59 | unset(CMAKE_REQUIRED_DEFINITIONS) |
| 60 | unset(CMAKE_REQUIRED_LIBRARIES) |
| 61 | |
Michal Vasko | 1b0b9a1 | 2021-05-06 08:38:34 +0200 | [diff] [blame] | 62 | # header and source file (adding the source directly allows for hiding its symbols) |
Michal Vasko | c5a2283 | 2020-08-20 13:21:33 +0200 | [diff] [blame] | 63 | configure_file(${PROJECT_SOURCE_DIR}/compat/compat.h.in ${PROJECT_BINARY_DIR}/compat/compat.h @ONLY) |
| 64 | include_directories(${PROJECT_BINARY_DIR}/compat) |
Michal Vasko | 1b0b9a1 | 2021-05-06 08:38:34 +0200 | [diff] [blame] | 65 | set(compatsrc ${PROJECT_SOURCE_DIR}/compat/compat.c) |
Jan Kundrát | 178ff71 | 2021-12-09 23:51:15 +0100 | [diff] [blame] | 66 | if(WIN32) |
| 67 | include_directories(${PROJECT_SOURCE_DIR}/compat/posix-shims) |
| 68 | endif() |
Jan Kundrát | a5a7e29 | 2021-12-10 17:37:46 +0100 | [diff] [blame] | 69 | if(NOT HAVE_STRPTIME) |
| 70 | set(compatsrc ${compatsrc} ${PROJECT_SOURCE_DIR}/compat/strptime.c) |
| 71 | endif() |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 72 | endmacro() |