diff --git a/CheckAtomic.cmake b/CheckAtomic.cmake new file mode 100644 index 0000000..17721cf --- /dev/null +++ b/CheckAtomic.cmake @@ -0,0 +1,29 @@ +include(CheckCXXSourceCompiles) + +# Some toolchains require explicitly linking against libatomic +# +# If such linking is required, then this function sets the +# value of result to "atomic". Otherwise it remains untouched. +# This makes it so that the function only needs to be called once +# per project. +# +# It can be used as follows: +# +# check_cxx_needs_atomic(LINK_ATOMIC) +# target_link_libraries(foo PRIVATE ${LINK_ATOMIC}) +# ... +# target_link_libraries(bar PRIVATE ${LINK_ATOMIC}) +# +function(check_cxx_needs_atomic result) + check_cxx_source_compiles(" +#include +#include + int main() { + return std::atomic(0).load(); + } + " success) + + if(NOT success) + set(${result} atomic PARENT_SCOPE) + endif() +endfunction()