0002-cmake-add-check-for-explicit-linking-against-libatom.patch 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. From 71830c804be76cf6abe913ac2fe584947b7a91ea Mon Sep 17 00:00:00 2001
  2. From: Samuel Martin <s.martin49@gmail.com>
  3. Date: Tue, 24 May 2016 23:08:40 +0200
  4. Subject: [PATCH] cmake: add check for explicit linking against libatomic
  5. To use atomics functions, some toolchains requires to explicitly add
  6. -latomic to the linker flags (because they are not provided by libc,
  7. but libatomic).
  8. This change adds a helper function trying to build/link a test program
  9. using atomics, then calls it to:
  10. * first check if atomics are directly available in the libc;
  11. * if not and libatomic has been found, then run the same test with
  12. "-latomic" added to the linker flags.
  13. The pulseview link library list is updated according to the results of
  14. these tests.
  15. This issue was triggered by the Buildroot farms:
  16. http://autobuild.buildroot.org/results/1e3/1e3101261252d5f30fdf842cc99604e4f4c25eef/build-end.log
  17. Notes:
  18. 1- CMAKE_REQUIRED_* variables are only used in check functions. They
  19. are not automatically forwarded to/handled by the target commands
  20. (such as target_link_library), because the check functions are
  21. implemented as macro in CMake code, whereas many target commands
  22. are native.
  23. 2- Because of note #1, CMAKE_REQUIRED_LIBRARIES (or its value) must be
  24. explicitly passed to the target_link_library command when this is
  25. needed.
  26. 3- In this implementation, LIBATOMIC_LIBRARY is only set when it is
  27. needed; so, unconditionally appending it to PULSEVIEW_LINK_LIBS
  28. will produce the expected behavior.
  29. Signed-off-by: Samuel Martin <s.martin49@gmail.com>
  30. ---
  31. changes v1->v2:
  32. - use std::atomic_fetch_add_explicit function instead of
  33. __atomic_fetch_add_4;
  34. - rework code using cmake_*_check_state and find_library helpers;
  35. - quiet-ize checks and clean outputs
  36. - extend the commit log
  37. ---
  38. CMakeLists.txt | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
  39. 1 file changed, 50 insertions(+)
  40. diff --git a/CMakeLists.txt b/CMakeLists.txt
  41. index 9dac69f..44f810e 100644
  42. --- a/CMakeLists.txt
  43. +++ b/CMakeLists.txt
  44. @@ -107,6 +107,55 @@ endif()
  45. # This will set ${CMAKE_THREAD_LIBS_INIT} to the correct, OS-specific value.
  46. find_package(Threads REQUIRED)
  47. +
  48. +# Check for explicit link against libatomic
  49. +#
  50. +# Depending on the toolchain, linking a program using atomic functions may need
  51. +# "-latomic" explicitly passed to the linker
  52. +#
  53. +# This check first tests if atomics are available in the C-library, if not and
  54. +# libatomic exists, then it runs the same test with -latomic added to the
  55. +# linker flags.
  56. +
  57. +# Helper for checking for atomics
  58. +function(check_working_cxx_atomics varname additional_lib)
  59. + include(CheckCXXSourceCompiles)
  60. + include(CMakePushCheckState)
  61. + cmake_push_check_state()
  62. + set(CMAKE_REQUIRED_FLAGS "-std=c++11")
  63. + set(CMAKE_REQUIRED_LIBRARIES "${additional_lib}")
  64. + set(CMAKE_REQUIRED_QUIET 1)
  65. + CHECK_CXX_SOURCE_COMPILES("
  66. +#include <atomic>
  67. +std::atomic<int> x;
  68. +int main() {
  69. + return std::atomic_fetch_add_explicit(&x, 1, std::memory_order_seq_cst);
  70. +}
  71. +" ${varname})
  72. + cmake_pop_check_state()
  73. +endfunction(check_working_cxx_atomics)
  74. +
  75. +# First check if atomics work without the library.
  76. +# If not, check if the library exists, and atomics work with it.
  77. +check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB "")
  78. +if(HAVE_CXX_ATOMICS_WITHOUT_LIB)
  79. + message(STATUS "Atomics provided by the C-library - yes")
  80. +else()
  81. + message(STATUS "Atomics provided by the C-library - no")
  82. + find_library(LIBATOMIC_LIBRARY NAMES atomic PATH_SUFFIXES lib)
  83. + if(LIBATOMIC_LIBRARY)
  84. + check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB "${LIBATOMIC_LIBRARY}")
  85. + if (HAVE_CXX_ATOMICS_WITH_LIB)
  86. + message(STATUS "Atomics provided by libatomic - yes")
  87. + else()
  88. + message(STATUS "Atomics provided by libatomic - no")
  89. + message(FATAL_ERROR "Compiler must support std::atomic!")
  90. + endif()
  91. + else()
  92. + message(FATAL_ERROR "Compiler appears to require libatomic, but cannot find it.")
  93. + endif()
  94. +endif()
  95. +
  96. #===============================================================================
  97. #= System Introspection
  98. #-------------------------------------------------------------------------------
  99. @@ -387,6 +436,7 @@ set(PULSEVIEW_LINK_LIBS
  100. ${Boost_LIBRARIES}
  101. ${QT_LIBRARIES}
  102. ${CMAKE_THREAD_LIBS_INIT}
  103. + ${LIBATOMIC_LIBRARY}
  104. )
  105. if(STATIC_PKGDEPS_LIBS)
  106. --
  107. 2.8.3