cmake: replace CheckFunctionExists with CheckSymbolExists

Checking functions with `CheckFunctionExists` may
get wrong results, we should replace it with
`CheckSymbolExists`, which is recommended by the cmake
official documentation.
Before using `CheckSymbolExists`, we use
`CheckIncludeFiles` to check header files and save the
available header files in a variable that guarantees
`CheckSymbolExists` and `CheckTypeSize` to work correctly.
This approach is modeled after the cmake scripts of `curl`.

The following functions or files were not found before
modification, they can now be found:
- msys2 + mingw-8.1.0 on Windows10 or mingw-7.3.0 on Ubuntu-18.04
timerclear
timercmp
timerisset

- windows10
getaddrinfo
getnameinfo
getprotobynumber
getservbyname
putenv
strtoll
timerclear
timercmp
timerisset

- ubuntu-18.04
sys/sysctl.h
timeradd
timerclear
timercmp
timerisset

- MacOS 10.13
sys/random.h
timeradd
timerclear
timercmp
timerisset
This commit is contained in:
yuangongji 2020-03-20 17:49:37 +08:00
parent 1675a55620
commit 41d1d75a84
4 changed files with 181 additions and 176 deletions

View File

@ -52,12 +52,9 @@ string(REGEX MATCH "SunOS" SOLARIS "${CMAKE_SYSTEM_NAME}")
include(CheckTypeSize) include(CheckTypeSize)
include(CheckFileOffsetBits) include(CheckFileOffsetBits)
include(CheckFunctionExists) include(Macros)
include(CheckIncludeFile)
include(CheckIncludeFiles)
include(CheckVariableExists) include(CheckVariableExists)
include(CheckSymbolExists) include(CheckSymbolExists)
include(CheckSymbolsExist)
include(CheckStructHasMember) include(CheckStructHasMember)
include(CheckCSourceCompiles) include(CheckCSourceCompiles)
include(CheckPrototypeDefinition) include(CheckPrototypeDefinition)
@ -335,10 +332,13 @@ if (APPLE)
) )
endif() endif()
if (MINGW OR CYGWIN)
set(WIN32 TRUE)
endif()
# Winsock. # Winsock.
if(WIN32) if(WIN32)
set(CMAKE_EXTRA_INCLUDE_FILES winsock2.h ws2tcpip.h) set(CMAKE_REQUIRED_LIBRARIES ws2_32 shell32 advapi32)
set(CMAKE_REQUIRED_LIBRARIES ws2_32.lib shell32.lib advapi32.lib)
set(CMAKE_REQUIRED_DEFINITIONS -FIwinsock2.h -FIws2tcpip.h -D_WIN32_WINNT=0x0600) set(CMAKE_REQUIRED_DEFINITIONS -FIwinsock2.h -FIws2tcpip.h -D_WIN32_WINNT=0x0600)
endif() endif()
if (SOLARIS) if (SOLARIS)
@ -361,161 +361,161 @@ endif()
if (_GNU_SOURCE) if (_GNU_SOURCE)
add_definitions(-D_GNU_SOURCE=1) add_definitions(-D_GNU_SOURCE=1)
set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
endif() endif()
CHECK_INCLUDE_FILE(sys/types.h EVENT__HAVE_SYS_TYPES_H) # Check if header files exist
if(EVENT__HAVE_SYS_TYPES_H) list(APPEND FILES_TO_CHECK
list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/types.h) fcntl.h
endif() inttypes.h
memory.h
CHECK_INCLUDE_FILE(sys/socket.h EVENT__HAVE_SYS_SOCKET_H) signal.h
if(EVENT__HAVE_SYS_SOCKET_H) stdarg.h
list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/socket.h) stddef.h
endif() stdint.h
stdlib.h
CHECK_INCLUDE_FILE(sys/random.h EVENT__HAVE_SYS_RANDOM_H) string.h
if(EVENT__HAVE_SYS_RANDOM_H) errno.h
list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/random.h) unistd.h
endif() time.h
sys/types.h
CHECK_INCLUDE_FILE(netinet/in.h EVENT__HAVE_NETINET_IN_H) sys/stat.h
if(EVENT__HAVE_NETINET_IN_H) sys/time.h
list(APPEND CMAKE_EXTRA_INCLUDE_FILES netinet/in.h) sys/param.h
endif() )
CHECK_INCLUDE_FILE(sys/un.h EVENT__HAVE_SYS_UN_H)
if(EVENT__HAVE_SYS_UN_H)
list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/un.h)
endif()
if(WIN32)
CHECK_INCLUDE_FILE(afunix.h EVENT__HAVE_AFUNIX_H)
if(EVENT__HAVE_AFUNIX_H)
list(APPEND CMAKE_EXTRA_INCLUDE_FILES afunix.h)
endif()
endif()
CHECK_TYPE_SIZE("struct sockaddr_un" EVENT__HAVE_STRUCT_SOCKADDR_UN)
CHECK_INCLUDE_FILE(netinet/in6.h EVENT__HAVE_NETINET_IN6_H)
if(EVENT__HAVE_NETINET_IN6_H)
list(APPEND CMAKE_EXTRA_INCLUDE_FILES netinet/in6.h)
endif()
CHECK_INCLUDE_FILE(unistd.h EVENT__HAVE_UNISTD_H)
CHECK_INCLUDE_FILE(netdb.h EVENT__HAVE_NETDB_H)
CHECK_INCLUDE_FILE(dlfcn.h EVENT__HAVE_DLFCN_H)
CHECK_INCLUDE_FILE(arpa/inet.h EVENT__HAVE_ARPA_INET_H)
CHECK_INCLUDE_FILE(fcntl.h EVENT__HAVE_FCNTL_H)
if(EVENT__HAVE_FCNTL_H)
list(APPEND CMAKE_EXTRA_INCLUDE_FILES fcntl.h)
endif()
CHECK_INCLUDE_FILE(inttypes.h EVENT__HAVE_INTTYPES_H)
CHECK_INCLUDE_FILE(memory.h EVENT__HAVE_MEMORY_H)
CHECK_INCLUDE_FILE(poll.h EVENT__HAVE_POLL_H)
CHECK_INCLUDE_FILE(port.h EVENT__HAVE_PORT_H)
if(EVENT__HAVE_PORT_H)
list(APPEND CMAKE_EXTRA_INCLUDE_FILES port.h)
endif()
CHECK_INCLUDE_FILE(signal.h EVENT__HAVE_SIGNAL_H)
CHECK_INCLUDE_FILE(stdarg.h EVENT__HAVE_STDARG_H)
CHECK_INCLUDE_FILE(stddef.h EVENT__HAVE_STDDEF_H)
CHECK_INCLUDE_FILE(stdint.h EVENT__HAVE_STDINT_H)
CHECK_INCLUDE_FILE(stdlib.h EVENT__HAVE_STDLIB_H)
CHECK_INCLUDE_FILE(strings.h EVENT__HAVE_STRINGS_H)
CHECK_INCLUDE_FILE(string.h EVENT__HAVE_STRING_H)
CHECK_INCLUDE_FILE(sys/devpoll.h EVENT__HAVE_DEVPOLL)
CHECK_INCLUDE_FILE(sys/epoll.h EVENT__HAVE_SYS_EPOLL_H)
CHECK_INCLUDE_FILE(sys/eventfd.h EVENT__HAVE_SYS_EVENTFD_H)
CHECK_INCLUDE_FILE(sys/event.h EVENT__HAVE_SYS_EVENT_H)
CHECK_INCLUDE_FILE(sys/ioctl.h EVENT__HAVE_SYS_IOCTL_H)
CHECK_INCLUDE_FILE(sys/mman.h EVENT__HAVE_SYS_MMAN_H)
CHECK_INCLUDE_FILE(sys/param.h EVENT__HAVE_SYS_PARAM_H)
CHECK_INCLUDE_FILE(sys/queue.h EVENT__HAVE_SYS_QUEUE_H)
CHECK_INCLUDE_FILE(sys/select.h EVENT__HAVE_SYS_SELECT_H)
CHECK_INCLUDE_FILE(sys/sendfile.h EVENT__HAVE_SYS_SENDFILE_H)
CHECK_INCLUDE_FILE(sys/stat.h EVENT__HAVE_SYS_STAT_H)
CHECK_INCLUDE_FILE(sys/time.h EVENT__HAVE_SYS_TIME_H)
if(EVENT__HAVE_SYS_TIME_H)
list(APPEND CMAKE_EXTRA_INCLUDE_FILES sys/time.h)
endif()
CHECK_INCLUDE_FILE(sys/uio.h EVENT__HAVE_SYS_UIO_H)
CHECK_INCLUDE_FILES("sys/types.h;ifaddrs.h" EVENT__HAVE_IFADDRS_H)
CHECK_INCLUDE_FILE(mach/mach_time.h EVENT__HAVE_MACH_MACH_TIME_H)
CHECK_INCLUDE_FILE(mach/mach.h EVENT__HAVE_MACH_MACH_H)
CHECK_INCLUDE_FILE(netinet/tcp.h EVENT__HAVE_NETINET_TCP_H)
CHECK_INCLUDE_FILE(sys/wait.h EVENT__HAVE_SYS_WAIT_H)
CHECK_INCLUDE_FILE(sys/resource.h EVENT__HAVE_SYS_RESOURCE_H)
if (NOT "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux")
CHECK_INCLUDE_FILE(sys/sysctl.h EVENT__HAVE_SYS_SYSCTL_H)
endif()
CHECK_INCLUDE_FILE(sys/timerfd.h EVENT__HAVE_SYS_TIMERFD_H)
CHECK_INCLUDE_FILE(errno.h EVENT__HAVE_ERRNO_H)
if (WIN32) if (WIN32)
list(APPEND CHECK_SYMBOL_HEADERS winsock2.h ws2tcpip.h) list(APPEND FILES_TO_CHECK
io.h
winsock2.h
ws2tcpip.h
afunix.h
)
else()
list(APPEND FILES_TO_CHECK
netdb.h
dlfcn.h
arpa/inet.h
poll.h
port.h
sys/socket.h
sys/random.h
sys/un.h
sys/devpoll.h
sys/epoll.h
sys/eventfd.h
sys/event.h
sys/ioctl.h
sys/mman.h
sys/queue.h
sys/select.h
sys/sendfile.h
sys/uio.h
sys/wait.h
sys/resource.h
sys/timerfd.h
netinet/in.h
netinet/in6.h
netinet/tcp.h
ifaddrs.h
)
endif() endif()
if (NOT "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux")
list(APPEND FILES_TO_CHECK sys/sysctl.h)
endif()
if (APPLE)
list(APPEND FILES_TO_CHECK
mach/mach_time.h
mach/mach.h
)
endif()
foreach(FILE ${FILES_TO_CHECK})
CHECK_INCLUDE_FILE_CONCAT(${FILE} "EVENT")
endforeach()
unset(FILES_TO_CHECK)
# Check if functions exist
list(APPEND SYMBOLS_TO_CHECK list(APPEND SYMBOLS_TO_CHECK
getaddrinfo getaddrinfo
getnameinfo getnameinfo
getprotobynumber getprotobynumber
getservbyname
gethostbyname
inet_ntop inet_ntop
inet_pton inet_pton
gettimeofday
signal
strtoll strtoll
splice
strlcpy
strsep
strtok_r
vasprintf
timerclear timerclear
timercmp timercmp
timerisset timerisset
timeradd
nanosleep
putenv putenv
umask
) )
CHECK_SYMBOLS_EXIST("${SYMBOLS_TO_CHECK}" "${CHECK_SYMBOL_HEADERS}" "EVENT") if (NOT EVENT__DISABLE_CLOCK_GETTIME)
unset(CHECK_SYMBOL_HEADERS) list(APPEND SYMBOLS_TO_CHECK clock_gettime)
unset(SYMBOLS_TO_CHECK)
CHECK_FUNCTION_EXISTS(epoll_create EVENT__HAVE_EPOLL)
CHECK_FUNCTION_EXISTS(epoll_ctl EVENT__HAVE_EPOLL_CTL)
CHECK_FUNCTION_EXISTS(eventfd EVENT__HAVE_EVENTFD)
if(NOT EVENT__DISABLE_CLOCK_GETTIME)
CHECK_FUNCTION_EXISTS(clock_gettime EVENT__HAVE_CLOCK_GETTIME)
endif() endif()
CHECK_FUNCTION_EXISTS(fcntl EVENT__HAVE_FCNTL)
CHECK_FUNCTION_EXISTS(gettimeofday EVENT__HAVE_GETTIMEOFDAY) if (WIN32)
CHECK_FUNCTION_EXISTS(kqueue EVENT__HAVE_KQUEUE) list(APPEND SYMBOLS_TO_CHECK
CHECK_FUNCTION_EXISTS(mmap EVENT__HAVE_MMAP) _gmtime64_s
CHECK_FUNCTION_EXISTS(pipe EVENT__HAVE_PIPE) _gmtime64
CHECK_FUNCTION_EXISTS(pipe2 EVENT__HAVE_PIPE2) )
CHECK_FUNCTION_EXISTS(poll EVENT__HAVE_POLL) else()
CHECK_FUNCTION_EXISTS(port_create EVENT__HAVE_PORT_CREATE) list(APPEND SYMBOLS_TO_CHECK
CHECK_FUNCTION_EXISTS(sendfile EVENT__HAVE_SENDFILE) getifaddrs
CHECK_FUNCTION_EXISTS(sigaction EVENT__HAVE_SIGACTION) select
CHECK_FUNCTION_EXISTS(signal EVENT__HAVE_SIGNAL) epoll_create
CHECK_FUNCTION_EXISTS(strsignal EVENT__HAVE_STRSIGNAL) epoll_create1
CHECK_FUNCTION_EXISTS(splice EVENT__HAVE_SPLICE) epoll_ctl
CHECK_FUNCTION_EXISTS(strlcpy EVENT__HAVE_STRLCPY) eventfd
CHECK_FUNCTION_EXISTS(strsep EVENT__HAVE_STRSEP) poll
CHECK_FUNCTION_EXISTS(strtok_r EVENT__HAVE_STRTOK_R) port_create
CHECK_FUNCTION_EXISTS(vasprintf EVENT__HAVE_VASPRINTF) kqueue
CHECK_FUNCTION_EXISTS(sysctl EVENT__HAVE_SYSCTL) fcntl
CHECK_FUNCTION_EXISTS(accept4 EVENT__HAVE_ACCEPT4) mmap
CHECK_FUNCTION_EXISTS(arc4random EVENT__HAVE_ARC4RANDOM) pipe
CHECK_FUNCTION_EXISTS(arc4random_buf EVENT__HAVE_ARC4RANDOM_BUF) pipe2
CHECK_FUNCTION_EXISTS(arc4random_addrandom EVENT__HAVE_ARC4RANDOM_ADDRANDOM) sendfile
CHECK_FUNCTION_EXISTS(epoll_create1 EVENT__HAVE_EPOLL_CREATE1) sigaction
CHECK_FUNCTION_EXISTS(getegid EVENT__HAVE_GETEGID) strsignal
CHECK_FUNCTION_EXISTS(geteuid EVENT__HAVE_GETEUID) sysctl
CHECK_FUNCTION_EXISTS(getifaddrs EVENT__HAVE_GETIFADDRS) accept4
CHECK_FUNCTION_EXISTS(issetugid EVENT__HAVE_ISSETUGID) arc4random
CHECK_FUNCTION_EXISTS(mach_absolute_time EVENT__HAVE_MACH_ABSOLUTE_TIME) arc4random_buf
CHECK_FUNCTION_EXISTS(nanosleep EVENT__HAVE_NANOSLEEP) arc4random_addrandom
CHECK_FUNCTION_EXISTS(usleep EVENT__HAVE_USLEEP) getrandom
CHECK_FUNCTION_EXISTS(timeradd EVENT__HAVE_TIMERADD) getegid
CHECK_FUNCTION_EXISTS(timerfd_create EVENT__HAVE_TIMERFD_CREATE) geteuid
CHECK_FUNCTION_EXISTS(setenv EVENT__HAVE_SETENV) issetugid
CHECK_FUNCTION_EXISTS(setrlimit EVENT__HAVE_SETRLIMIT) usleep
CHECK_FUNCTION_EXISTS(umask EVENT__HAVE_UMASK) timerfd_create
CHECK_FUNCTION_EXISTS(unsetenv EVENT__HAVE_UNSETENV) setenv
unsetenv
setrlimit
gethostbyname_r
)
if (APPLE)
list(APPEND SYMBOLS_TO_CHECK mach_absolute_time)
endif()
endif()
# Add stdio.h for vasprintf
set(EVENT_INCLUDES ${EVENT_INCLUDES} stdio.h)
CHECK_SYMBOLS_EXIST("${SYMBOLS_TO_CHECK}" "${EVENT_INCLUDES}" "EVENT")
unset(SYMBOLS_TO_CHECK)
set(EVENT__HAVE_EPOLL ${EVENT__HAVE_EPOLL_CREATE})
# Get the gethostbyname_r prototype. # Get the gethostbyname_r prototype.
CHECK_FUNCTION_EXISTS(gethostbyname_r EVENT__HAVE_GETHOSTBYNAME_R)
if(EVENT__HAVE_GETHOSTBYNAME_R) if(EVENT__HAVE_GETHOSTBYNAME_R)
CHECK_PROTOTYPE_DEFINITION(gethostbyname_r CHECK_PROTOTYPE_DEFINITION(gethostbyname_r
"int gethostbyname_r(const char *name, struct hostent *hp, struct hostent_data *hdata)" "int gethostbyname_r(const char *name, struct hostent *hp, struct hostent_data *hdata)"
@ -540,17 +540,10 @@ if(HAVE_PORT_H AND HAVE_PORT_CREATE)
set(EVENT__HAVE_EVENT_PORTS 1) set(EVENT__HAVE_EVENT_PORTS 1)
endif() endif()
if(NOT WIN32) # Only `CHECK_TYPE_SIZE()' will use `CMAKE_EXTRA_INCLUDE_FILES'
CHECK_FUNCTION_EXISTS(select EVENT__HAVE_SELECT) set(CMAKE_EXTRA_INCLUDE_FILES ${EVENT_INCLUDES})
endif()
if(WIN32)
CHECK_FUNCTION_EXISTS(_gmtime64_s EVENT__HAVE__GMTIME64_S)
if (NOT EVENT__HAVE__GMTIME64_S)
CHECK_FUNCTION_EXISTS(_gmtime64 EVENT__HAVE__GMTIME64)
endif()
endif()
CHECK_TYPE_SIZE("struct sockaddr_un" EVENT__HAVE_STRUCT_SOCKADDR_UN)
CHECK_TYPE_SIZE("uint8_t" EVENT__HAVE_UINT8_T) CHECK_TYPE_SIZE("uint8_t" EVENT__HAVE_UINT8_T)
CHECK_TYPE_SIZE("uint16_t" EVENT__HAVE_UINT16_T) CHECK_TYPE_SIZE("uint16_t" EVENT__HAVE_UINT16_T)
CHECK_TYPE_SIZE("uint32_t" EVENT__HAVE_UINT32_T) CHECK_TYPE_SIZE("uint32_t" EVENT__HAVE_UINT32_T)
@ -590,7 +583,6 @@ CHECK_SYMBOL_EXISTS(TAILQ_FOREACH sys/queue.h EVENT__HAVE_TAILQFOREACH)
CHECK_CONST_EXISTS(CTL_KERN sys/sysctl.h EVENT__HAVE_DECL_CTL_KERN) CHECK_CONST_EXISTS(CTL_KERN sys/sysctl.h EVENT__HAVE_DECL_CTL_KERN)
CHECK_CONST_EXISTS(KERN_ARND sys/sysctl.h EVENT__HAVE_DECL_KERN_ARND) CHECK_CONST_EXISTS(KERN_ARND sys/sysctl.h EVENT__HAVE_DECL_KERN_ARND)
CHECK_SYMBOL_EXISTS(F_SETFD fcntl.h EVENT__HAVE_SETFD) CHECK_SYMBOL_EXISTS(F_SETFD fcntl.h EVENT__HAVE_SETFD)
CHECK_FUNCTION_EXISTS(getrandom EVENT__HAVE_GETRANDOM)
CHECK_TYPE_SIZE(fd_mask EVENT__HAVE_FD_MASK) CHECK_TYPE_SIZE(fd_mask EVENT__HAVE_FD_MASK)

View File

@ -1,20 +0,0 @@
# Check if each symbol in the symbol list exists,
# and define PREFIX__HAVE_SYMNAME to 1 if yes.
#
include(CheckSymbolExists)
# SYMLIST: list of symbols to check
# HEADERS: header files to be included in check code
# PREFIX: the prefix of definition
macro(CHECK_SYMBOLS_EXIST SYMLIST HEADERS PREFIX)
foreach(SYMNAME ${SYMLIST})
string(TOUPPER "${SYMNAME}" SYMNAME_UPPER)
if("${PREFIX}" STREQUAL "")
set(HAVE_SYM_DEF "HAVE_${SYMNAME_UPPER}")
else()
set(HAVE_SYM_DEF "${PREFIX}__HAVE_${SYMNAME_UPPER}")
endif()
CHECK_SYMBOL_EXISTS(${SYMNAME} "${HEADERS}" ${HAVE_SYM_DEF})
endforeach()
endmacro()

36
cmake/Macros.cmake Normal file
View File

@ -0,0 +1,36 @@
include(CheckSymbolExists)
include(CheckIncludeFiles)
# Check if each symbol in the symbol list exists,
# and define PREFIX__HAVE_SYMNAME to 1 if yes.
#
# SYMLIST: list of symbols to check
# HEADERS: header files to be included in check code
# PREFIX: the prefix of definition
macro(CHECK_SYMBOLS_EXIST SYMLIST HEADERS PREFIX)
foreach(SYMNAME ${SYMLIST})
string(TOUPPER "${SYMNAME}" SYMNAME_UPPER)
if ("${PREFIX}" STREQUAL "")
set(HAVE_SYM_DEF "HAVE_${SYMNAME_UPPER}")
else()
set(HAVE_SYM_DEF "${PREFIX}__HAVE_${SYMNAME_UPPER}")
endif()
CHECK_SYMBOL_EXISTS(${SYMNAME} "${HEADERS}" ${HAVE_SYM_DEF})
endforeach()
endmacro()
# Check if file exists, define PREFIX__HAVE_FILE to 1 if yes,
# and collect file to EVENT_INCLUDES
macro(CHECK_INCLUDE_FILE_CONCAT FILE PREFIX)
string(REGEX REPLACE "[./]" "_" FILE_UL ${FILE})
string(TOUPPER "${FILE_UL}" FILE_UL_UPPER)
if ("${PREFIX}" STREQUAL "")
set(HAVE_FILE_DEF "HAVE_${FILE_UL_UPPER}")
else()
set(HAVE_FILE_DEF "${PREFIX}__HAVE_${FILE_UL_UPPER}")
endif()
CHECK_INCLUDE_FILES("${EVENT_INCLUDES};${FILE}" ${HAVE_FILE_DEF})
if(${HAVE_FILE_DEF})
set(EVENT_INCLUDES ${EVENT_INCLUDES} ${FILE})
endif()
endmacro()

View File

@ -277,9 +277,6 @@
/* Define to 1 if you have the <stdlib.h> header file. */ /* Define to 1 if you have the <stdlib.h> header file. */
#cmakedefine EVENT__HAVE_STDLIB_H 1 #cmakedefine EVENT__HAVE_STDLIB_H 1
/* Define to 1 if you have the <strings.h> header file. */
#cmakedefine EVENT__HAVE_STRINGS_H 1
/* Define to 1 if you have the <string.h> header file. */ /* Define to 1 if you have the <string.h> header file. */
#cmakedefine EVENT__HAVE_STRING_H 1 #cmakedefine EVENT__HAVE_STRING_H 1