FindPThreadGetSetName.cmake 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #
  2. # SRT - Secure, Reliable, Transport
  3. # Copyright (c) 2021 Haivision Systems Inc.
  4. #
  5. # This Source Code Form is subject to the terms of the Mozilla Public
  6. # License, v. 2.0. If a copy of the MPL was not distributed with this
  7. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. #
  9. # Check for pthread_getname_np(3) and pthread_setname_np(3)
  10. # used in srtcore/threadname.h.
  11. #
  12. # Some BSD distros need to include <pthread_np.h> for pthread_getname_np().
  13. #
  14. # TODO: Some BSD distros have pthread_get_name_np() and pthread_set_name_np()
  15. # instead of pthread_getname_np() and pthread_setname_np().
  16. #
  17. # Sets:
  18. # HAVE_PTHREAD_GETNAME_NP_IN_PTHREAD_NP_H
  19. # HAVE_PTHREAD_SETNAME_NP_IN_PTHREAD_NP_H
  20. # HAVE_PTHREAD_GETNAME_NP
  21. # HAVE_PTHREAD_SETNAME_NP
  22. # Sets as appropriate:
  23. # add_definitions(-DHAVE_PTHREAD_GETNAME_NP_IN_PTHREAD_NP_H=1)
  24. # add_definitions(-DHAVE_PTHREAD_SETNAME_NP_IN_PTHREAD_NP_H=1)
  25. # add_definitions(-DHAVE_PTHREAD_GETNAME_NP=1)
  26. # add_definitions(-DHAVE_PTHREAD_SETNAME_NP=1)
  27. include(CheckSymbolExists)
  28. function(FindPThreadGetSetName)
  29. unset(HAVE_PTHREAD_GETNAME_NP_IN_PTHREAD_NP_H CACHE)
  30. unset(HAVE_PTHREAD_SETNAME_NP_IN_PTHREAD_NP_H CACHE)
  31. unset(HAVE_PTHREAD_GETNAME_NP CACHE)
  32. unset(HAVE_PTHREAD_SETNAME_NP CACHE)
  33. set(CMAKE_REQUIRED_DEFINITIONS
  34. -D_GNU_SOURCE -D_DARWIN_C_SOURCE -D_POSIX_SOURCE=1)
  35. set(CMAKE_REQUIRED_FLAGS "-pthread")
  36. message(STATUS "Checking for pthread_(g/s)etname_np in 'pthread_np.h':")
  37. check_symbol_exists(
  38. pthread_getname_np "pthread_np.h" HAVE_PTHREAD_GETNAME_NP_IN_PTHREAD_NP_H)
  39. if (HAVE_PTHREAD_GETNAME_NP_IN_PTHREAD_NP_H)
  40. add_definitions(-DHAVE_PTHREAD_GETNAME_NP_IN_PTHREAD_NP_H=1)
  41. endif()
  42. check_symbol_exists(
  43. pthread_setname_np "pthread_np.h" HAVE_PTHREAD_SETNAME_NP_IN_PTHREAD_NP_H)
  44. if (HAVE_PTHREAD_SETNAME_NP_IN_PTHREAD_NP_H)
  45. add_definitions(-DHAVE_PTHREAD_SETNAME_NP_IN_PTHREAD_NP_H=1)
  46. endif()
  47. message(STATUS "Checking for pthread_(g/s)etname_np in 'pthread.h':")
  48. check_symbol_exists(pthread_getname_np "pthread.h" HAVE_PTHREAD_GETNAME_NP)
  49. if (HAVE_PTHREAD_GETNAME_NP_IN_PTHREAD_NP_H)
  50. set(HAVE_PTHREAD_GETNAME_NP 1 CACHE INTERNAL "" FORCE)
  51. endif()
  52. check_symbol_exists(pthread_setname_np "pthread.h" HAVE_PTHREAD_SETNAME_NP)
  53. if (HAVE_PTHREAD_SETNAME_NP_IN_PTHREAD_NP_H)
  54. set(HAVE_PTHREAD_SETNAME_NP 1 CACHE INTERNAL "" FORCE)
  55. endif()
  56. if (HAVE_PTHREAD_GETNAME_NP)
  57. add_definitions(-DHAVE_PTHREAD_GETNAME_NP=1)
  58. endif()
  59. if (HAVE_PTHREAD_SETNAME_NP)
  60. add_definitions(-DHAVE_PTHREAD_SETNAME_NP=1)
  61. endif()
  62. unset(CMAKE_REQUIRED_DEFINITIONS)
  63. unset(CMAKE_REQUIRED_FLAGS)
  64. endfunction(FindPThreadGetSetName)