从autotool迁移到cmake:检测头文件,函数,结构体成员

autotool的检测是真的多
包含必要的CheckXXX后,就是对着生成的.h文件一个一个的改吧
目前遇到的问题还有如何检测库里的函数,比如pthread中的函数,
带宏标识的函数,比如__USE_GUN这种
获取类型大小 check_type_size
检测函数是否存在 check_function_exists
检测符号是否存在 check_symbol_exists, 这个要指定头文件,它可以检测宏,内联等
检测结构体是否有XXX成员变量 CHECK_STRUCT_HAS_MEMBER, 这个也要头文件
生成头文件

configure_file (${CMAKE_SOURCE_DIR}/config.h.cmake ${CMAKE_BINARY_DIR}/config.h)

这里用config.h.cmake而不是config.h.in,是因为config.h.in可能还需要改,但又不想影响到autotool的编译

一份检测代码,具体可以看 https://github.com/PikachuHy/guile/blob/1.8-cmake/CMakeLists.txt
这里贴一部分

include(CheckTypeSize)
check_type_size("char" SIZEOF_CHAR)
check_type_size("float" SIZEOF_FLOAT)
check_type_size("int" SIZEOF_INT)
check_type_size("intmax_t" SIZEOF_INTMAX_T)
check_type_size("intptr_t" SIZEOF_INTPTR_T)
check_type_size("long" SIZEOF_LONG)
check_type_size("long long" SIZEOF_LONG_LONG)
check_type_size("off_t" SIZEOF_OFF_T)
check_type_size("ptrdiff_t" SIZEOF_PTRDIFF_T)
check_type_size("short" SIZEOF_SHORT)
check_type_size("size_t" SIZEOF_SIZE_T)
check_type_size("uintptr_t" SIZEOF_UINTPTR_T)
check_type_size("unsigned char" SIZEOF_UNSIGNED_CHAR)
check_type_size("unsigned int" SIZEOF_UNSIGNED_INT)
check_type_size("unsigned long" SIZEOF_UNSIGNED_LONG)
check_type_size("unsigned long long" SIZEOF_UNSIGNED_LONG_LONG)
check_type_size("unsigned short" SIZEOF_UNSIGNED_SHORT)
check_type_size("unsigned __int64" SIZEOF_UNSIGNED___INT64)
check_type_size("void *" SIZEOF_VOID_P)
check_type_size("__int64" SIZEOF___INT64)
check_type_size("complex double" SIZEOF_COMPLEX_DOUBLE)
if(NOT SIZEOF___INT64)
    set(SIZEOF___INT64 0)
endif()
if(NOT SIZEOF_UNSIGNED___INT64)
    set(SIZEOF_UNSIGNED___INT64 0)
endif()
if(SIZEOF_COMPLEX_DOUBLE)
    set(HAVE_COMPLEX_DOUBLE 1)
endif()
include (CheckIncludeFile)
check_include_file (alloca.h HAVE_ALLOCA_H)
check_include_file (complex.h HAVE_COMPLEX_H)
check_include_file (crt_externs.h HAVE_CRT_EXTERNS_H)
check_include_file (crypt.h HAVE_CRYPT_H)
check_include_file (assert.h HAVE_ASSERT_H)
check_include_file (direct.h HAVE_DIRECT_H)
check_include_file (dirent.h HAVE_DIRENT_H)
check_include_file (dlfcn.h HAVE_DLFCN_H)
check_include_file (fenv.h HAVE_FENV_H)
check_include_file (floatingpoint.h HAVE_FLOATINGPOINT_H)
check_include_file (grp.h HAVE_GRP_H)
check_include_file (ieeefp.h HAVE_IEEEFP_H)
check_include_file (inttypes.h HAVE_INTTYPES_H)
check_include_file (io.h HAVE_IO_H)
check_include_file (libc.h HAVE_LIBC_H)
check_include_file (limits.h HAVE_LIMITS_H)
check_include_file (machine/fpu.h HAVE_MACHINE_FPU_H)
check_include_file (malloc.h HAVE_MALLOC_H)
check_include_file (memory.h HAVE_MEMORY_H)
check_include_file (nan.h HAVE_NAN_H)
check_include_file (ndir.h HAVE_NDIR_H)
check_include_file (netdb.h HAVE_NETDB_H)
check_include_file (process.h HAVE_PROCESS_H)
check_include_file (pthread.h HAVE_PTHREAD_H)
check_include_file (pwd.h HAVE_PWD_H)
check_include_file (regex.h HAVE_REGEX_H)
check_include_file (rxposix.h HAVE_RXPOSIX_H)
check_include_file (rx/rxposix.h HAVE_RX_RXPOSIX_H)
check_include_file (stdint.h HAVE_STDINT_H)
check_include_file (stdlib.h HAVE_STDLIB_H)
check_include_file (strings.h HAVE_STRINGS_H)
check_include_file (string.h HAVE_STRING_H)
check_include_file (sys/dir.h HAVE_SYS_DIR_H)
check_include_file (sys/file.h HAVE_SYS_FILE_H)
check_include_file (sys/ioctl.h HAVE_SYS_IOCTL_H)
check_include_file (sys/ndir.h HAVE_SYS_NDIR_H)
check_include_file (sys/param.h HAVE_SYS_PARAM_H)
check_include_file (sys/resource.h HAVE_SYS_RESOURCE_H)
check_include_file (sys/select.h HAVE_SYS_SELECT_H)
check_include_file (sys/stat.h HAVE_SYS_STAT_H)
check_include_file (sys/stdtypes.h HAVE_SYS_STDTYPES_H)
check_include_file (sys/timeb.h HAVE_SYS_TIMEB_H)
check_include_file (sys/times.h HAVE_SYS_TIMES_H)
check_include_file (sys/time.h HAVE_SYS_TIME_H)
check_include_file (sys/types.h HAVE_SYS_TYPES_H)
check_include_file (sys/utime.h HAVE_SYS_UTIME_H)
check_include_file (sys/utsname.h HAVE_SYS_UTSNAME_H)
check_include_file (sys/wait.h HAVE_SYS_WAIT_H)
check_include_file (time.h HAVE_TIME_H)
check_include_file (unistd.h HAVE_UNISTD_H)
check_include_file (utime.h HAVE_UTIME_H)
check_include_file (winsock2.h HAVE_WINSOCK2_H)
include (CheckFunctionExists)
check_function_exists (acosh HAVE_ACOSH)
check_function_exists (alloca HAVE_ALLOCA)
check_function_exists (asinh HAVE_ASINH)
check_function_exists (atanh HAVE_ATANH)
check_function_exists (atexit HAVE_ATEXIT)
check_function_exists (bcopy HAVE_BCOPY)
check_function_exists (cexp HAVE_CEXP)
check_function_exists (chown HAVE_CHOWN)
check_function_exists (chroot HAVE_CHROOT)
check_function_exists (chsize HAVE_CHSIZE)
check_function_exists (clog HAVE_CLOG)
check_function_exists (clog10 HAVE_CLOG10)
check_function_exists (connect HAVE_CONNECT)
check_function_exists (copysign HAVE_COPYSIGN)
check_function_exists (crypt HAVE_CRYPT)
check_function_exists (ctermid HAVE_CTERMID)
check_function_exists (cuserid HAVE_CUSERID)
check_function_exists (dcgettext HAVE_DCGETTEXT)
check_function_exists (cuserid HAVE_DECL_CUSERID)
check_function_exists (DINFINITY HAVE_DINFINITY)
check_function_exists (DQNAN HAVE_DQNAN)
check_function_exists (endhostent HAVE_ENDHOSTENT)
check_function_exists (endnetent HAVE_ENDNETENT)
check_function_exists (endprotoent HAVE_ENDPROTOENT)
check_function_exists (endservent HAVE_ENDSERVENT)
check_function_exists (fchown HAVE_FCHOWN)
check_function_exists (fcntl HAVE_FCNTL)
check_function_exists (fesetround HAVE_FESETROUND)
check_function_exists (finite HAVE_FINITE)
check_function_exists (flock HAVE_FLOCK)
check_function_exists (fork HAVE_FORK)
check_function_exists (ftime HAVE_FTIME)
check_function_exists (ftruncate HAVE_FTRUNCATE)
check_function_exists (getcwd HAVE_GETCWD)
check_function_exists (geteuid HAVE_GETEUID)
check_function_exists (getgrent HAVE_GETGRENT)
check_function_exists (getgroups HAVE_GETGROUPS)
check_function_exists (gethostbyname HAVE_GETHOSTBYNAME)
check_function_exists (gethostent HAVE_GETHOSTENT)
check_function_exists (gethostname HAVE_GETHOSTNAME)
check_function_exists (getitimer HAVE_GETITIMER)
check_function_exists (getnetbyaddr HAVE_GETNETBYADDR)
check_function_exists (getnetbyname HAVE_GETNETBYNAME)
check_function_exists (getnetent HAVE_GETNETENT)
check_function_exists (getpass HAVE_GETPASS)
check_function_exists (getpgrp HAVE_GETPGRP)
check_function_exists (getppid HAVE_GETPPID)
check_function_exists (getpriority HAVE_GETPRIORITY)
check_function_exists (getprotoent HAVE_GETPROTOENT)
check_function_exists (getpwent HAVE_GETPWENT)
check_function_exists (getservent HAVE_GETSERVENT)
check_function_exists (gettext HAVE_GETTEXT)
check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
check_function_exists (gmtime_r HAVE_GMTIME_R)
check_function_exists (hstrerror HAVE_HSTRERROR)
check_function_exists (iconv HAVE_ICONV)
check_function_exists (index HAVE_INDEX)
check_function_exists (inet_aton HAVE_INET_ATON)
check_function_exists (inet_lnaof HAVE_INET_LNAOF)
check_function_exists (inet_makeaddr HAVE_INET_MAKEADDR)
check_function_exists (inet_netof HAVE_INET_NETOF)
check_function_exists (inet_ntop HAVE_INET_NTOP)
check_function_exists (inet_pton HAVE_INET_PTON)
check_function_exists (ioctl HAVE_IOCTL)
check_function_exists (isblank HAVE_ISBLANK)
check_function_exists (kill HAVE_KILL)
check_function_exists (link HAVE_KILL)
check_function_exists (lstat HAVE_KILL)
check_function_exists (memcpy HAVE_MEMCPY)
check_function_exists (memmove HAVE_MEMMOVE)
check_function_exists (mkdir HAVE_MKDIR)
check_function_exists (mknod HAVE_MKNOD)
check_function_exists (mkstemp HAVE_MKSTEMP)
check_function_exists (nice HAVE_NICE)
check_function_exists (on_exit HAVE_ON_EXIT)
check_function_exists (pause HAVE_PAUSE)
check_function_exists (pipe HAVE_PIPE)
#check_function_exists (pthread_attr_getstack HAVE_PTHREAD_ATTR_GETSTACK)
#check_function_exists (pthread_getattr_np HAVE_PTHREAD_GETATTR_NP)
#check_function_exists (pthread_get_stackaddr_np HAVE_PTHREAD_GET_STACKADDR_NP)
#check_function_exists (pthread_sigmask HAVE_PTHREAD_SIGMASK)
check_function_exists (putenv HAVE_PUTENV)
check_function_exists (readdir64_r HAVE_READDIR64_R)
check_function_exists (readdir_r HAVE_READDIR_R)
check_function_exists (readlink HAVE_READLINK)
check_function_exists (rename HAVE_RENAME)
check_function_exists (rindex HAVE_RINDEX)
check_function_exists (rmdir HAVE_RMDIR)
check_function_exists (select HAVE_SELECT)
check_function_exists (setegid HAVE_SETEGID)
check_function_exists (seteuid HAVE_SETEUID)
check_function_exists (setgroups HAVE_SETGROUPS)
check_function_exists (sethostent HAVE_SETHOSTENT)
check_function_exists (sethostname HAVE_SETHOSTNAME)
check_function_exists (setitimer HAVE_SETITIMER)
check_function_exists (setlocale HAVE_SETLOCALE)
check_function_exists (setnetent HAVE_SETNETENT)
check_function_exists (setpgid HAVE_SETPGID)
check_function_exists (setpriority HAVE_SETPRIORITY)
check_function_exists (setprotoent HAVE_SETPROTOENT)
check_function_exists (setpwent HAVE_SETPWENT)
check_function_exists (setservent HAVE_SETSERVENT)
check_function_exists (setsid HAVE_SETSID)
check_function_exists (sigaction HAVE_SIGACTION)
check_function_exists (siginterrupt HAVE_SIGINTERRUPT)
check_function_exists (sincos HAVE_SINCOS)
check_function_exists (socketpair HAVE_SOCKETPAIR)
check_function_exists (stat64 HAVE_STAT64)
check_function_exists (strchr HAVE_STRCHR)
check_function_exists (strcmp HAVE_STRCMP)
check_function_exists (strdup HAVE_STRDUP)
check_function_exists (strerror HAVE_STRERROR)
check_function_exists (strftime HAVE_STRFTIME)
check_function_exists (strncasecmp HAVE_STRNCASECMP)
check_function_exists (strptime HAVE_STRPTIME)
check_function_exists (symlink HAVE_SYMLINK)
check_function_exists (sync HAVE_SYNC)
check_function_exists (sysconf HAVE_SYSCONF)
check_function_exists (system HAVE_SYSTEM)
check_function_exists (tcgetpgrp HAVE_TCGETPGRP)
check_function_exists (tcsetpgrp HAVE_TCSETPGRP)
check_function_exists (times HAVE_TIMES)
check_function_exists (trunc HAVE_TRUNC)
check_function_exists (truncate HAVE_TRUNCATE)
check_function_exists (ttyname HAVE_TTYNAME)
check_function_exists (tzset HAVE_TZSET)
check_function_exists (uname HAVE_UNAME)
check_function_exists (unsetenv HAVE_UNSETENV)
check_function_exists (usleep HAVE_USLEEP)
check_function_exists (waitpid HAVE_WAITPID)
check_function_exists (_NSGetEnviron HAVE__NSGETENVIRON)
check_function_exists (_pipe HAVE__PIPE)
include(CheckSymbolExists)
check_symbol_exists(cuserid "stdio.h" HAVE_DECL_CUSERID)
check_symbol_exists(flock "sys/file.h" HAVE_DECL_FLOCK)
check_symbol_exists(hstrerror "sys/socket.h" HAVE_DECL_HSTRERROR)
check_symbol_exists(sethostname "unistd.h" HAVE_DECL_SETHOSTNAME)
check_symbol_exists(strncasecmp "strings.h" HAVE_DECL_STRNCASECMP)
check_symbol_exists(strptime "time.h" HAVE_DECL_STRPTIME)
check_symbol_exists(tzname "time.h" HAVE_DECL_TZNAME)
check_symbol_exists(unsetenv "stdlib.h" HAVE_DECL_UNSETENV)
check_symbol_exists(vsnprintf "stdio.h" HAVE_DECL_VSNPRINTF)
check_symbol_exists(h_errno "netdb.h" HAVE_H_ERRNO)
check_symbol_exists(isinf "math.h" HAVE_ISINF)
check_symbol_exists(isnan "math.h" HAVE_ISNAN)
check_symbol_exists(timespec "time.h" "pthread.h" HAVE_STRUCT_TIMESPEC)
# FIXME: Looking for pthread_getattr_np - not found
check_symbol_exists(pthread_attr_getstack "pthread.h" HAVE_PTHREAD_ATTR_GETSTACK)
check_symbol_exists(pthread_getattr_np "pthread.h" HAVE_PTHREAD_GETATTR_NP)
check_symbol_exists(pthread_get_stackaddr_np "pthread.h" HAVE_PTHREAD_GET_STACKADDR_NP)
check_symbol_exists(pthread_sigmask "pthread.h" HAVE_PTHREAD_SIGMASK)
include(CheckStructHasMember)
CHECK_STRUCT_HAS_MEMBER("struct sockaddr_in6" sin6_scope_id sys/socket.h HAVE_SIN6_SCOPE_ID LANGUAGE C)
CHECK_STRUCT_HAS_MEMBER("struct sockaddr_in6" sin6_len sys/socket.h HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN LANGUAGE C)
CHECK_STRUCT_HAS_MEMBER("struct sockaddr" sin_len sys/socket.h HAVE_STRUCT_SOCKADDR_SIN_LEN LANGUAGE C)
CHECK_STRUCT_HAS_MEMBER("struct stat" st_blksize sys/stat.h HAVE_STRUCT_STAT_ST_BLKSIZE LANGUAGE C)
CHECK_STRUCT_HAS_MEMBER("struct stat" st_blocks sys/stat.h HAVE_STRUCT_STAT_ST_BLOCKS LANGUAGE C)
CHECK_STRUCT_HAS_MEMBER("struct stat" st_rdev sys/stat.h HAVE_STRUCT_STAT_ST_RDEV LANGUAGE C)
CHECK_STRUCT_HAS_MEMBER("struct tm" tm_gmtoff time.h HAVE_STRUCT_TM_TM_GMTOFF LANGUAGE C)
CHECK_STRUCT_HAS_MEMBER("struct tm" tm_zone time.h HAVE_STRUCT_TM_TM_ZONE LANGUAGE C)
CHECK_STRUCT_HAS_MEMBER("struct tm" tm_zone time.h HAVE_TM_ZONE LANGUAGE C)
CHECK_STRUCT_HAS_MEMBER("struct tm" tzname time.h HAVE_TZNAME LANGUAGE C)
include(CheckLibraryExists)

你可能感兴趣的:(工具,cmake,autotool)