Rapid spam filtering system https://rspamd.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.5 KiB

  1. # Function to check for atomic builtins, CPU features, and other specialized symbols
  2. function(CheckSymbols)
  3. # Check for atomic builtins
  4. check_c_source_runs("
  5. #include <stdbool.h>
  6. int main(int argc, char **argv) {
  7. int a = 0, b = 0;
  8. if (__atomic_compare_exchange_n(&a, &b, 1, false, __ATOMIC_RELEASE, __ATOMIC_RELAXED)) {
  9. return 0;
  10. }
  11. return -1;
  12. }
  13. " HAVE_ATOMIC_BUILTINS)
  14. if (NOT HAVE_ATOMIC_BUILTINS)
  15. message(STATUS "atomic builtins are -NOT- supported")
  16. else ()
  17. message(STATUS "atomic builtins are supported")
  18. endif ()
  19. # Check for libatomic
  20. check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC)
  21. if (HAVE_LIBATOMIC)
  22. list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
  23. list(APPEND RSPAMD_REQUIRED_LIBRARIES "atomic")
  24. set(RSPAMD_REQUIRED_LIBRARIES "${RSPAMD_REQUIRED_LIBRARIES}" PARENT_SCOPE)
  25. endif ()
  26. # Check for CPU feature intrinsics
  27. check_c_source_runs("
  28. #include <stdio.h>
  29. int main() {
  30. __builtin_cpu_init();
  31. printf(\"%d\", __builtin_cpu_supports(\"avx\"));
  32. return 0;
  33. }" HAVE_BUILTIN_CPU_SUPPORTS)
  34. if (HAVE_BUILTIN_CPU_SUPPORTS)
  35. message(STATUS "CPU feature detection via __builtin_cpu_supports is supported")
  36. else ()
  37. message(STATUS "CPU feature detection via __builtin_cpu_supports is NOT supported")
  38. endif ()
  39. # Check for RDTSC
  40. check_c_source_runs("
  41. #include <x86intrin.h>
  42. int main(int argc, char **argv) {
  43. __builtin_ia32_lfence();
  44. if (__builtin_ia32_rdtsc()) {
  45. return 0;
  46. }
  47. return -1;
  48. }
  49. " HAVE_RDTSC)
  50. if (NOT HAVE_RDTSC)
  51. message(STATUS "rdtsc intrinsic is -NOT- supported")
  52. else ()
  53. message(STATUS "rdtsc intrinsic is supported")
  54. endif ()
  55. # Check for POSIX shared memory support
  56. if (NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
  57. # For non-Linux systems, test shmem capability
  58. check_c_source_runs("
  59. #include <sys/mman.h>
  60. #include <fcntl.h>
  61. #include <unistd.h>
  62. #define TEST_NAME \"/test-shmem-work\"
  63. int
  64. main(int argc, char **argv)
  65. {
  66. int fd;
  67. fd = shm_open(TEST_NAME, O_RDWR | O_CREAT | O_EXCL, 00600);
  68. if (fd == -1) {
  69. return -1;
  70. }
  71. if (ftruncate(fd, 100) == -1) {
  72. shm_unlink(TEST_NAME);
  73. close(fd);
  74. return -1;
  75. }
  76. if (ftruncate(fd, 200) == -1) {
  77. shm_unlink(TEST_NAME);
  78. close(fd);
  79. return -1;
  80. }
  81. if (ftruncate(fd, 300) == -1) {
  82. shm_unlink(TEST_NAME);
  83. close(fd);
  84. return -1;
  85. }
  86. close(fd);
  87. shm_unlink(TEST_NAME);
  88. return 0;
  89. }
  90. " HAVE_SANE_SHMEM)
  91. if (NOT HAVE_SANE_SHMEM)
  92. message(STATUS "shmem support is NOT compatible with POSIX")
  93. else ()
  94. message(STATUS "shmem support is compatible with POSIX")
  95. endif ()
  96. endif ()
  97. # Check for pthread shared mutexes and robust mutexes
  98. file(WRITE ${CMAKE_BINARY_DIR}/pthread_setpshared.c "
  99. #include <pthread.h>
  100. #include <stdlib.h>
  101. int main(void)
  102. {
  103. pthread_mutexattr_t mattr;
  104. if (pthread_mutexattr_init(&mattr) != 0) return 0;
  105. if (pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED) != 0) return 0;
  106. if (pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST) != 0) return 0;
  107. return 1;
  108. }
  109. ")
  110. try_run(_CAN_RUN _CAN_COMPILE
  111. "${CMAKE_BINARY_DIR}" "${CMAKE_BINARY_DIR}/pthread_setpshared.c"
  112. CMAKE_FLAGS CMAKE_C_FLAGS="-pthread")
  113. if (_CAN_RUN EQUAL 1)
  114. set(HAVE_PTHREAD_PROCESS_SHARED 1 CACHE INTERNAL "")
  115. endif ()
  116. if (HAVE_PTHREAD_PROCESS_SHARED)
  117. message(STATUS "pthread_mutexattr_setpshared is supported")
  118. else ()
  119. message(STATUS "pthread_mutexattr_setpshared is -NOT- supported")
  120. endif ()
  121. # Propagate variables to parent scope
  122. set(HAVE_ATOMIC_BUILTINS ${HAVE_ATOMIC_BUILTINS} PARENT_SCOPE)
  123. set(HAVE_LIBATOMIC ${HAVE_LIBATOMIC} PARENT_SCOPE)
  124. set(HAVE_BUILTIN_CPU_SUPPORTS ${HAVE_BUILTIN_CPU_SUPPORTS} PARENT_SCOPE)
  125. set(HAVE_RDTSC ${HAVE_RDTSC} PARENT_SCOPE)
  126. set(HAVE_SANE_SHMEM ${HAVE_SANE_SHMEM} PARENT_SCOPE)
  127. set(HAVE_PTHREAD_PROCESS_SHARED ${HAVE_PTHREAD_PROCESS_SHARED} PARENT_SCOPE)
  128. endfunction()
  129. CheckSymbols()