From e431218618d6d7387ab53a53ff2b0f74f9c76531 Mon Sep 17 00:00:00 2001 From: nihui Date: Wed, 25 Dec 2024 11:06:23 +0800 Subject: [PATCH] define empty assertion termination function for fixing linking with old android ndk (#5847) --- CMakeLists.txt | 10 ++++++++ cmake/glslang_drop_ostream.cmake | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 cmake/glslang_drop_ostream.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b532d7c245..df135be52c6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -723,6 +723,12 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten") endif() endif() +if(ANDROID_NDK_MAJOR AND (ANDROID_NDK_MAJOR GREATER_EQUAL 27)) + # llvm 18 in ndk-27 introduce __libcpp_verbose_abort for assertion output + # define empty body for fixing linking issue with old ndk + add_definitions("-D_LIBCPP_VERBOSE_ABORT\\(format,args...\\)=") +endif() + if(NCNN_VULKAN) if(NCNN_SYSTEM_GLSLANG) find_package(Threads) @@ -766,6 +772,10 @@ if(NCNN_VULKAN) # glslang requires c++11 set(CMAKE_CXX_STANDARD 11) + # remove ostream usage in glslang for better libcpp abi compatibility + include(cmake/glslang_drop_ostream.cmake) + glslang_drop_ostream(${CMAKE_CURRENT_LIST_DIR}/glslang) + option(BUILD_EXTERNAL "" OFF) option(ENABLE_SPVREMAPPER "" OFF) option(ENABLE_GLSLANG_BINARIES "" OFF) diff --git a/cmake/glslang_drop_ostream.cmake b/cmake/glslang_drop_ostream.cmake new file mode 100644 index 00000000000..192a524241e --- /dev/null +++ b/cmake/glslang_drop_ostream.cmake @@ -0,0 +1,42 @@ +# this cmake script remove ostream usage in glslang + +function(glslang_drop_ostream GLSLANG_ROOT) + + # patch glslang/SPIRV/GlslangToSpv.h + file(STRINGS ${GLSLANG_ROOT}/SPIRV/GlslangToSpv.h GlslangToSpv_HDR) + file(WRITE ${GLSLANG_ROOT}/SPIRV/GlslangToSpv.h "") + foreach(LINE IN LISTS GlslangToSpv_HDR) + if(LINE MATCHES "^bool OutputSpvBin(.*);" OR LINE MATCHES "^bool OutputSpvHex(.*);") + continue() + endif() + + file(APPEND ${GLSLANG_ROOT}/SPIRV/GlslangToSpv.h "${LINE}\n") + endforeach() + + # patch glslang/SPIRV/GlslangToSpv.cpp + file(STRINGS ${GLSLANG_ROOT}/SPIRV/GlslangToSpv.cpp GlslangToSpv_SRC) + file(WRITE ${GLSLANG_ROOT}/SPIRV/GlslangToSpv.cpp "") + foreach(LINE IN LISTS GlslangToSpv_SRC) + if(LINE MATCHES "^bool OutputSpvBin(.*)") + set(GlslangToSpv_SRC_OutputSpvBin TRUE) + endif() + if(LINE MATCHES "^bool OutputSpvHex(.*)") + set(GlslangToSpv_SRC_OutputSpvHex TRUE) + endif() + + if(GlslangToSpv_SRC_OutputSpvBin OR GlslangToSpv_SRC_OutputSpvHex) + if(LINE MATCHES "^}$") + if(GlslangToSpv_SRC_OutputSpvBin) + unset(GlslangToSpv_SRC_OutputSpvBin) + endif() + if(GlslangToSpv_SRC_OutputSpvHex) + unset(GlslangToSpv_SRC_OutputSpvHex) + endif() + endif() + continue() + endif() + + file(APPEND ${GLSLANG_ROOT}/SPIRV/GlslangToSpv.cpp "${LINE}\n") + endforeach() + +endfunction()