blob: 753b10d30884f2c455a448ce21d2c5d4ed5209b2 [file] [log] [blame]
Michal Vaskod304a082021-05-19 16:36:10 +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>
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 Vasko5aa44c02020-06-29 11:47:02 +020017include(CheckSymbolExists)
Michal Vaskod304a082021-05-19 16:36:10 +020018include(CheckFunctionExists)
19include(CheckIncludeFile)
Michal Vasko5aa44c02020-06-29 11:47:02 +020020include(TestBigEndian)
Michal Vaskod304a082021-05-19 16:36:10 +020021if(POLICY CMP0075)
22 cmake_policy(SET CMP0075 NEW)
23endif()
Michal Vasko5aa44c02020-06-29 11:47:02 +020024
25macro(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 Vaskod304a082021-05-19 16:36:10 +020030 set(CMAKE_REQUIRED_LIBRARIES pthread)
31
Michal Vasko5aa44c02020-06-29 11:47:02 +020032 check_symbol_exists(vdprintf "stdio.h;stdarg.h" HAVE_VDPRINTF)
33 check_symbol_exists(asprintf "stdio.h" HAVE_ASPRINTF)
34 check_symbol_exists(vasprintf "stdio.h" HAVE_VASPRINTF)
Michal Vaskod304a082021-05-19 16:36:10 +020035 check_symbol_exists(getline "stdio.h" HAVE_GETLINE)
Michal Vasko5aa44c02020-06-29 11:47:02 +020036
37 check_symbol_exists(strndup "string.h" HAVE_STRNDUP)
38 check_symbol_exists(strnstr "string.h" HAVE_STRNSTR)
Michal Vaskod304a082021-05-19 16:36:10 +020039 check_symbol_exists(strdupa "string.h" HAVE_STRDUPA)
40 check_symbol_exists(strchrnul "string.h" HAVE_STRCHRNUL)
Michal Vasko5aa44c02020-06-29 11:47:02 +020041
42 check_symbol_exists(get_current_dir_name "unistd.h" HAVE_GET_CURRENT_DIR_NAME)
43
Michal Vaskod304a082021-05-19 16:36:10 +020044 check_function_exists(pthread_mutex_timedlock HAVE_PTHREAD_MUTEX_TIMEDLOCK)
45
Michal Vasko5aa44c02020-06-29 11:47:02 +020046 TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
47
Michal Vaskod304a082021-05-19 16:36:10 +020048 check_include_file("stdatomic.h" HAVE_STDATOMIC)
49
Jan Kundrátaac59c22021-12-09 23:25:15 +010050 include(CheckStructHasMember)
51 check_struct_has_member("struct tm" tm_gmtoff time.h HAVE_TM_GMTOFF)
52 check_symbol_exists(timezone time.h HAVE_TIME_H_TIMEZONE)
53
Jan Kundráta5b3b992021-12-10 16:13:57 +010054 check_symbol_exists(realpath "stdlib.h" HAVE_REALPATH)
Jan Kundrát447970d2021-12-14 16:18:16 +010055 check_symbol_exists(localtime_r "time.h" HAVE_LOCALTIME_R)
Jan Kundrát7f8fee02021-12-10 17:37:46 +010056 check_symbol_exists(strptime "time.h" HAVE_STRPTIME)
Jan Kundrát111ce782021-12-12 03:12:23 +010057 check_symbol_exists(mmap "sys/mman.h" HAVE_MMAP)
Jan Kundráta5b3b992021-12-10 16:13:57 +010058
Michal Vaskod304a082021-05-19 16:36:10 +020059 unset(CMAKE_REQUIRED_DEFINITIONS)
60 unset(CMAKE_REQUIRED_LIBRARIES)
61
Michal Vasko1b0b9a12021-05-06 08:38:34 +020062 # header and source file (adding the source directly allows for hiding its symbols)
Michal Vaskoc5a22832020-08-20 13:21:33 +020063 configure_file(${PROJECT_SOURCE_DIR}/compat/compat.h.in ${PROJECT_BINARY_DIR}/compat/compat.h @ONLY)
64 include_directories(${PROJECT_BINARY_DIR}/compat)
Michal Vasko1b0b9a12021-05-06 08:38:34 +020065 set(compatsrc ${PROJECT_SOURCE_DIR}/compat/compat.c)
Jan Kundrát099b7122021-12-09 23:51:15 +010066 if(WIN32)
67 include_directories(${PROJECT_SOURCE_DIR}/compat/posix-shims)
68 endif()
Jan Kundrát7f8fee02021-12-10 17:37:46 +010069 if(NOT HAVE_STRPTIME)
70 set(compatsrc ${compatsrc} ${PROJECT_SOURCE_DIR}/compat/strptime.c)
71 endif()
Michal Vasko5aa44c02020-06-29 11:47:02 +020072endmacro()