introspection.cmake raw

   1  # Copyright (c) 2023-present The Bitcoin Core developers
   2  # Distributed under the MIT software license, see the accompanying
   3  # file COPYING or https://opensource.org/license/mit/.
   4  
   5  include(CheckCXXSourceCompiles)
   6  include(CheckCXXSymbolExists)
   7  
   8  check_cxx_symbol_exists(O_CLOEXEC "fcntl.h" HAVE_O_CLOEXEC)
   9  check_cxx_symbol_exists(fdatasync "unistd.h" HAVE_FDATASYNC)
  10  check_cxx_symbol_exists(fork "unistd.h" HAVE_DECL_FORK)
  11  check_cxx_symbol_exists(pipe2 "unistd.h" HAVE_DECL_PIPE2)
  12  check_cxx_symbol_exists(setsid "unistd.h" HAVE_DECL_SETSID)
  13  
  14  if(NOT WIN32)
  15    include(TestAppendRequiredLibraries)
  16    test_append_socket_library(core_interface)
  17  endif()
  18  
  19  include(TestAppendRequiredLibraries)
  20  if(NOT CMAKE_SYSTEM_NAME STREQUAL "Generic")
  21  test_append_atomic_library(core_interface)
  22  endif()
  23  
  24  # Even though ::system is part of the standard library, we still check
  25  # for it, to support building targets that don't have it, such as iOS.
  26  check_cxx_symbol_exists(std::system "cstdlib" HAVE_STD_SYSTEM)
  27  check_cxx_symbol_exists(::_wsystem "stdlib.h" HAVE__WSYSTEM)
  28  if(HAVE_STD_SYSTEM OR HAVE__WSYSTEM)
  29    set(HAVE_SYSTEM 1)
  30  endif()
  31  
  32  check_cxx_source_compiles("
  33    #include <string.h>
  34  
  35    int main()
  36    {
  37      char buf[100];
  38      char* p{strerror_r(0, buf, sizeof buf)};
  39      (void)p;
  40    }
  41    " STRERROR_R_CHAR_P
  42  )
  43  
  44  # Check for malloc_info (for memory statistics information in getmemoryinfo).
  45  check_cxx_symbol_exists(malloc_info "malloc.h" HAVE_MALLOC_INFO)
  46  
  47  # Check for mallopt(M_ARENA_MAX) (to set glibc arenas).
  48  check_cxx_source_compiles("
  49    #include <malloc.h>
  50  
  51    int main()
  52    {
  53      mallopt(M_ARENA_MAX, 1);
  54    }
  55    " HAVE_MALLOPT_ARENA_MAX
  56  )
  57  
  58  # Check for posix_fallocate().
  59  check_cxx_source_compiles("
  60    #include <fcntl.h>
  61  
  62    int main()
  63    {
  64      return posix_fallocate(0, 0, 0);
  65    }
  66    " HAVE_POSIX_FALLOCATE
  67  )
  68  
  69  # Check for strong getauxval() support in the system headers.
  70  check_cxx_source_compiles("
  71    #include <sys/auxv.h>
  72  
  73    int main()
  74    {
  75      getauxval(AT_HWCAP);
  76    }
  77    " HAVE_STRONG_GETAUXVAL
  78  )
  79  
  80  # Check for UNIX sockets.
  81  check_cxx_source_compiles("
  82    #include <sys/socket.h>
  83    #include <sys/un.h>
  84  
  85    int main()
  86    {
  87      struct sockaddr_un addr;
  88      addr.sun_family = AF_UNIX;
  89    }
  90    " HAVE_SOCKADDR_UN
  91  )
  92  
  93  # Check for different ways of gathering OS randomness:
  94  # - Linux getrandom()
  95  check_cxx_source_compiles("
  96    #include <sys/random.h>
  97  
  98    int main()
  99    {
 100      getrandom(nullptr, 32, 0);
 101    }
 102    " HAVE_GETRANDOM
 103  )
 104  
 105  # - BSD getentropy()
 106  check_cxx_source_compiles("
 107    #include <sys/random.h>
 108  
 109    int main()
 110    {
 111      getentropy(nullptr, 32);
 112    }
 113    " HAVE_GETENTROPY_RAND
 114  )
 115  
 116  
 117  # - BSD sysctl()
 118  check_cxx_source_compiles("
 119    #include <sys/types.h>
 120    #include <sys/sysctl.h>
 121  
 122    #ifdef __linux__
 123    #error Don't use sysctl on Linux, it's deprecated even when it works
 124    #endif
 125  
 126    int main()
 127    {
 128      sysctl(nullptr, 2, nullptr, nullptr, nullptr, 0);
 129    }
 130    " HAVE_SYSCTL
 131  )
 132  
 133  # - BSD sysctl(KERN_ARND)
 134  check_cxx_source_compiles("
 135    #include <sys/types.h>
 136    #include <sys/sysctl.h>
 137  
 138    #ifdef __linux__
 139    #error Don't use sysctl on Linux, it's deprecated even when it works
 140    #endif
 141  
 142    int main()
 143    {
 144      static int name[2] = {CTL_KERN, KERN_ARND};
 145      sysctl(name, 2, nullptr, nullptr, nullptr, 0);
 146    }
 147    " HAVE_SYSCTL_ARND
 148  )
 149  
 150  if(NOT MSVC)
 151    include(CheckSourceCompilesWithFlags)
 152  
 153    # Check for SSE4.1 intrinsics.
 154    set(SSE41_CXXFLAGS -msse4.1)
 155    check_cxx_source_compiles_with_flags("
 156      #include <immintrin.h>
 157  
 158      int main()
 159      {
 160        __m128i a = _mm_set1_epi32(0);
 161        __m128i b = _mm_set1_epi32(1);
 162        __m128i r = _mm_blend_epi16(a, b, 0xFF);
 163        return _mm_extract_epi32(r, 3);
 164      }
 165      " HAVE_SSE41
 166      CXXFLAGS ${SSE41_CXXFLAGS}
 167    )
 168  
 169    # Check for AVX2 intrinsics.
 170    set(AVX2_CXXFLAGS -mavx -mavx2)
 171    check_cxx_source_compiles_with_flags("
 172      #include <immintrin.h>
 173  
 174      int main()
 175      {
 176        __m256i l = _mm256_set1_epi32(0);
 177        return _mm256_extract_epi32(l, 7);
 178      }
 179      " HAVE_AVX2
 180      CXXFLAGS ${AVX2_CXXFLAGS}
 181    )
 182  
 183    # Check for x86 SHA-NI intrinsics.
 184    set(X86_SHANI_CXXFLAGS -msse4 -msha)
 185    check_cxx_source_compiles_with_flags("
 186      #include <immintrin.h>
 187  
 188      int main()
 189      {
 190        __m128i i = _mm_set1_epi32(0);
 191        __m128i j = _mm_set1_epi32(1);
 192        __m128i k = _mm_set1_epi32(2);
 193        return _mm_extract_epi32(_mm_sha256rnds2_epu32(i, j, k), 0);
 194      }
 195      " HAVE_X86_SHANI
 196      CXXFLAGS ${X86_SHANI_CXXFLAGS}
 197    )
 198  
 199    # Check for ARMv8 SHA-NI intrinsics.
 200    set(ARM_SHANI_CXXFLAGS -march=armv8-a+crypto)
 201    check_cxx_source_compiles_with_flags("
 202      #include <arm_neon.h>
 203  
 204      int main()
 205      {
 206        uint32x4_t a, b, c;
 207        vsha256h2q_u32(a, b, c);
 208        vsha256hq_u32(a, b, c);
 209        vsha256su0q_u32(a, b);
 210        vsha256su1q_u32(a, b, c);
 211      }
 212      " HAVE_ARM_SHANI
 213      CXXFLAGS ${ARM_SHANI_CXXFLAGS}
 214    )
 215  endif()
 216