CMakeLists.txt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. include(CheckTypeSize)
  2. include(CheckSymbolExists)
  3. include(CheckFunctionExists)
  4. include(CheckIncludeFiles)
  5. include(CheckCSourceRuns)
  6. include(CheckCSourceCompiles)
  7. check_type_size("int" SIZEOF_INT)
  8. check_type_size("long" SIZEOF_LONG)
  9. check_type_size("intmax_t" SIZEOF_INTMAX_T)
  10. check_type_size("size_t" SIZEOF_SIZE_T)
  11. check_type_size("long long" SIZEOF_LONG_LONG)
  12. check_type_size("void *" SIZEOF_VOID_PTR)
  13. if (CMAKE_HOST_SYSTEM_VERSION MATCHES ".*-Microsoft")
  14. # Windows Subsystem for Linux
  15. set(HAVE_WSL 1)
  16. endif()
  17. check_symbol_exists(_NSGetEnviron crt_externs.h HAVE__NSGETENVIRON)
  18. # Headers
  19. check_include_files(langinfo.h HAVE_LANGINFO_H)
  20. check_include_files(locale.h HAVE_LOCALE_H)
  21. check_include_files(pwd.h HAVE_PWD_H)
  22. check_include_files(strings.h HAVE_STRINGS_H)
  23. check_include_files(sys/wait.h HAVE_SYS_WAIT_H)
  24. if(NOT HAVE_SYS_WAIT_H AND UNIX)
  25. # See if_cscope.c
  26. message(SEND_ERROR "header sys/wait.h is required for Unix")
  27. endif()
  28. check_include_files(sys/utsname.h HAVE_SYS_UTSNAME_H)
  29. check_include_files(termios.h HAVE_TERMIOS_H)
  30. check_include_files(sys/uio.h HAVE_SYS_UIO_H)
  31. # Functions
  32. check_function_exists(fseeko HAVE_FSEEKO)
  33. check_function_exists(getpwent HAVE_GETPWENT)
  34. check_function_exists(getpwnam HAVE_GETPWNAM)
  35. check_function_exists(getpwuid HAVE_GETPWUID)
  36. check_function_exists(readv HAVE_READV)
  37. if(Iconv_FOUND)
  38. set(HAVE_ICONV 1)
  39. endif()
  40. check_function_exists(opendir HAVE_OPENDIR)
  41. check_function_exists(readlink HAVE_READLINK)
  42. check_function_exists(setpgid HAVE_SETPGID)
  43. check_function_exists(setsid HAVE_SETSID)
  44. check_function_exists(sigaction HAVE_SIGACTION)
  45. check_function_exists(strcasecmp HAVE_STRCASECMP)
  46. check_function_exists(strncasecmp HAVE_STRNCASECMP)
  47. # Symbols
  48. check_symbol_exists(FD_CLOEXEC "fcntl.h" HAVE_FD_CLOEXEC)
  49. if(HAVE_LANGINFO_H)
  50. check_symbol_exists(CODESET "langinfo.h" HAVE_NL_LANGINFO_CODESET)
  51. endif()
  52. check_include_files("endian.h" HAVE_ENDIAN_H)
  53. check_include_files("sys/endian.h" HAVE_SYS_ENDIAN_H)
  54. set(ENDIAN_INCLUDE_FILE "endian.h")
  55. if(HAVE_SYS_ENDIAN_H AND NOT HAVE_ENDIAN_H)
  56. set(ENDIAN_INCLUDE_FILE "sys/endian.h")
  57. endif()
  58. set(SI "#include <stdint.h>\n")
  59. set(MS "int main(int argc,char**argv)\n{\n uint64_t i=0x0102030405060708ULL;")
  60. set(ME "}")
  61. check_c_source_compiles("
  62. #define _BSD_SOURCE 1
  63. #define _DEFAULT_SOURCE 1
  64. ${SI}
  65. #include <${ENDIAN_INCLUDE_FILE}>
  66. #ifndef be64toh
  67. # error No be64toh macros
  68. #endif
  69. ${MS}
  70. uint64_t j = be64toh(i);
  71. return (j == 0); // j must not be zero
  72. ${ME}"
  73. HAVE_BE64TOH_MACROS)
  74. if(NOT "${HAVE_BE64TOH_MACROS}")
  75. check_function_exists(be64toh HAVE_BE64TOH_FUNC)
  76. endif()
  77. if("${HAVE_BE64TOH_MACROS}" OR "${HAVE_BE64TOH_FUNC}")
  78. set(HAVE_BE64TOH 1)
  79. endif()
  80. if (NOT "${HAVE_BE64TOH}")
  81. if (NOT "${CMAKE_CROSSCOMPILING}")
  82. # It is safe to make ORDER_BIG_ENDIAN not defined if
  83. # - HAVE_BE64TOH is true. In this case be64toh will be used unconditionally in
  84. # any case and ORDER_BIG_ENDIAN will not be examined.
  85. # - CMAKE_CROSSCOMPILING *and* HAVE_BE64TOH are both false. In this case
  86. # be64toh function which uses cycle and arithmetic operations is used which
  87. # will work regardless of endianess. Function is sub-optimal though.
  88. check_c_source_runs("
  89. ${SI}
  90. ${MS}
  91. char *s = (char *) &i;
  92. return (
  93. s[0] == 0x01
  94. && s[1] == 0x02
  95. && s[2] == 0x03
  96. && s[3] == 0x04
  97. && s[4] == 0x05
  98. && s[5] == 0x06
  99. && s[6] == 0x07
  100. && s[7] == 0x08) ? 0 : 1;
  101. ${ME}"
  102. ORDER_BIG_ENDIAN)
  103. endif()
  104. endif()
  105. # generate configuration header and update include directories
  106. configure_file (
  107. "${PROJECT_SOURCE_DIR}/config/config.h.in"
  108. "${PROJECT_BINARY_DIR}/config/auto/config.h"
  109. )
  110. # generate version definitions
  111. configure_file (
  112. "${PROJECT_SOURCE_DIR}/config/versiondef.h.in"
  113. "${PROJECT_BINARY_DIR}/config/auto/versiondef.h"
  114. )
  115. # generate pathdef.c
  116. find_program(WHOAMI_PROG whoami)
  117. find_program(HOSTNAME_PROG hostname)
  118. if (DEFINED ENV{USERNAME})
  119. set(USERNAME $ENV{USERNAME})
  120. elseif (NOT DEFINED USERNAME AND EXISTS ${WHOAMI_PROG})
  121. execute_process(COMMAND ${WHOAMI_PROG}
  122. OUTPUT_STRIP_TRAILING_WHITESPACE
  123. OUTPUT_VARIABLE USERNAME)
  124. endif()
  125. if (DEFINED ENV{HOSTNAME})
  126. set(HOSTNAME $ENV{HOSTNAME})
  127. elseif (EXISTS ${HOSTNAME_PROG})
  128. execute_process(COMMAND ${HOSTNAME_PROG}
  129. OUTPUT_STRIP_TRAILING_WHITESPACE
  130. OUTPUT_VARIABLE HOSTNAME)
  131. endif()
  132. configure_file (
  133. "${PROJECT_SOURCE_DIR}/config/pathdef.c.in"
  134. "${PROJECT_BINARY_DIR}/config/auto/pathdef.c"
  135. ESCAPE_QUOTES)