Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 1 | From e20a5d13935a41a856e8f71c49f2cc9d81b1d92c Mon Sep 17 00:00:00 2001 |
| 2 | From: Changqing Li <changqing.li@windriver.com> |
| 3 | Date: Fri, 13 Nov 2020 17:07:00 +0800 |
| 4 | Subject: [PATCH] support link against libatomic if no built-in atomic exist |
| 5 | |
| 6 | fix error: |
| 7 | | framework/lib/ppc/libframework.a(device.cpp.o): in function `std::__atomic_base<unsigned long long>::load(std::memory_order) const': |
| 8 | | /usr/include/c++/10.2.0/bits/atomic_base.h:426: undefined reference to `__atomic_load_8' |
| 9 | |
| 10 | Upstream-Status: Submitted [https://github.com/KhronosGroup/Vulkan-Samples/pull/212] |
| 11 | |
| 12 | Signed-off-by: Changqing Li <changqing.li@windriver.com> |
| 13 | --- |
| 14 | CMakeLists.txt | 1 + |
| 15 | bldsys/cmake/check_atomic.cmake | 62 +++++++++++++++++++++++++++++++++ |
| 16 | framework/CMakeLists.txt | 4 +++ |
| 17 | 3 files changed, 67 insertions(+) |
| 18 | create mode 100644 bldsys/cmake/check_atomic.cmake |
| 19 | |
| 20 | diff --git a/CMakeLists.txt b/CMakeLists.txt |
| 21 | index e72e829..466f51d 100644 |
| 22 | --- a/CMakeLists.txt |
| 23 | +++ b/CMakeLists.txt |
| 24 | @@ -42,6 +42,7 @@ endmacro(vulkan_samples_pch) |
| 25 | include(utils) |
| 26 | include(global_options) |
| 27 | include(sample_helper) |
| 28 | +include(check_atomic) |
| 29 | |
| 30 | # Add third party libraries |
| 31 | add_subdirectory(third_party) |
| 32 | diff --git a/bldsys/cmake/check_atomic.cmake b/bldsys/cmake/check_atomic.cmake |
| 33 | new file mode 100644 |
| 34 | index 0000000..6b47a7a |
| 35 | --- /dev/null |
| 36 | +++ b/bldsys/cmake/check_atomic.cmake |
| 37 | @@ -0,0 +1,62 @@ |
| 38 | +# check weither need to link atomic library explicitly |
| 39 | +INCLUDE(CheckCXXSourceCompiles) |
| 40 | +INCLUDE(CheckLibraryExists) |
| 41 | + |
| 42 | +if(NOT DEFINED VULKAN_COMPILER_IS_GCC_COMPATIBLE) |
| 43 | + if(CMAKE_COMPILER_IS_GNUCXX) |
| 44 | + set(VULKAN_COMPILER_IS_GCC_COMPATIBLE ON) |
| 45 | + elseif( MSVC ) |
| 46 | + set(VULKAN_COMPILER_IS_GCC_COMPATIBLE OFF) |
| 47 | + elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" ) |
| 48 | + set(VULKAN_COMPILER_IS_GCC_COMPATIBLE ON) |
| 49 | + elseif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel" ) |
| 50 | + set(VULKAN_COMPILER_IS_GCC_COMPATIBLE ON) |
| 51 | + endif() |
| 52 | +endif() |
| 53 | + |
| 54 | +# Sometimes linking against libatomic is required for atomic ops, if |
| 55 | +# the platform doesn't support lock-free atomics. |
| 56 | + |
| 57 | +function(check_working_cxx_atomics varname) |
| 58 | + set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) |
| 59 | + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11") |
| 60 | + CHECK_CXX_SOURCE_COMPILES(" |
| 61 | +#include <atomic> |
| 62 | +std::atomic<int> x; |
| 63 | +std::atomic<short> y; |
| 64 | +std::atomic<char> z; |
| 65 | +int main() { |
| 66 | + ++z; |
| 67 | + ++y; |
| 68 | + return ++x; |
| 69 | +} |
| 70 | +" ${varname}) |
| 71 | + set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) |
| 72 | +endfunction(check_working_cxx_atomics) |
| 73 | + |
| 74 | +function(check_working_cxx_atomics64 varname) |
| 75 | + set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) |
| 76 | + set(CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}") |
| 77 | + CHECK_CXX_SOURCE_COMPILES(" |
| 78 | +#include <atomic> |
| 79 | +#include <cstdint> |
| 80 | +std::atomic<uint64_t> x (0); |
| 81 | +int main() { |
| 82 | + uint64_t i = x.load(std::memory_order_relaxed); |
| 83 | + (void)i; |
| 84 | + return 0; |
| 85 | +} |
| 86 | +" ${varname}) |
| 87 | + set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) |
| 88 | +endfunction(check_working_cxx_atomics64) |
| 89 | + |
| 90 | +set(NEED_LINK_ATOMIC OFF CACHE BOOL "weither need to link against atomic library") |
| 91 | +if(VULKAN_COMPILER_IS_GCC_COMPATIBLE) |
| 92 | + # check if non-64-bit atomics work without the library. |
| 93 | + check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB) |
| 94 | + # check 64-bit atomics work without the library. |
| 95 | + check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB) |
| 96 | + if (NOT HAVE_CXX_ATOMICS_WITHOUT_LIB OR NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB) |
| 97 | + set(NEED_LINK_ATOMIC ON CACHE BOOL "weither need to link to atomic library" FORCE) |
| 98 | + endif() |
| 99 | +endif() |
| 100 | diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt |
| 101 | index bf26786..322526e 100644 |
| 102 | --- a/framework/CMakeLists.txt |
| 103 | +++ b/framework/CMakeLists.txt |
| 104 | @@ -412,6 +412,10 @@ target_link_libraries(${PROJECT_NAME} |
| 105 | ctpl |
| 106 | docopt) |
| 107 | |
| 108 | +if(${NEED_LINK_ATOMIC}) |
| 109 | + target_link_libraries(${PROJECT_NAME} atomic) |
| 110 | +endif() |
| 111 | + |
| 112 | # Link platform specific libraries |
| 113 | if(ANDROID) |
| 114 | target_link_libraries(${PROJECT_NAME} log android native_app_glue) |
| 115 | -- |
| 116 | 2.17.1 |
| 117 | |