From ba622ab520838b3b53406adb80ac71d897e46182 Mon Sep 17 00:00:00 2001 From: nihui Date: Wed, 30 Aug 2023 13:17:41 +0800 Subject: [PATCH] fix link openmp flags (#58) --- .github/workflows/release.yml | 16 +++++++++ opencv-2.4.13.7-link-openmp.patch | 36 +++++++++++++++++--- opencv-3.4.20-link-openmp.patch | 56 ++++++++++++++++++++++++++----- opencv-4.8.0-link-openmp.patch | 54 +++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+), 12 deletions(-) create mode 100644 opencv-4.8.0-link-openmp.patch diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index aa27ca3e..d36c6107 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -49,6 +49,7 @@ jobs: rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-rtti.patch patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch rm -rf modules/highgui cp -r ../highgui modules/ - name: build-armeabi-v7a @@ -371,6 +372,7 @@ jobs: rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-rtti.patch patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch rm -rf modules/highgui cp -r ../highgui modules/ - name: build-armv7 @@ -640,6 +642,7 @@ jobs: rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-rtti.patch patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch rm -rf modules/highgui cp -r ../highgui modules/ - name: build-armv7 @@ -909,6 +912,7 @@ jobs: rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-rtti.patch patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch rm -rf modules/highgui cp -r ../highgui modules/ - name: build-i386 @@ -1178,6 +1182,7 @@ jobs: rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-rtti.patch patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch rm -rf modules/highgui cp -r ../highgui modules/ - name: build-i386 @@ -1452,6 +1457,7 @@ jobs: rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-rtti.patch patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch rm -rf modules/highgui cp -r ../highgui modules/ - name: build-arm-linux-gnueabi @@ -1741,6 +1747,7 @@ jobs: echo -n > cmake/OpenCVFindLibsGrfmt.cmake rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch rm -rf modules/highgui cp -r ../highgui modules/ - name: build-x86_64 @@ -1974,6 +1981,7 @@ jobs: echo -n > cmake/OpenCVFindLibsGrfmt.cmake rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch rm -rf modules/highgui cp -r ../highgui modules/ - name: build-x86_64 @@ -2183,6 +2191,7 @@ jobs: echo -n > cmake/OpenCVFindLibsGrfmt.cmake rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch rm -rf modules/highgui cp -r ../highgui modules/ - name: build-x86_64 @@ -2393,6 +2402,7 @@ jobs: Clear-Content "cmake\OpenCVFindLibsGrfmt.cmake" Remove-Item "modules\gapi" -Recurse C:\msys64\usr\bin\patch.exe -p1 -i ..\opencv-4.8.0-no-zlib.patch + C:\msys64\usr\bin\patch.exe -p1 -i ..\opencv-4.8.0-link-openmp.patch Remove-Item "modules\highgui" -Recurse Copy-Item -Recurse -Path "..\highgui" -Destination "modules\" - name: build-x86 @@ -2576,6 +2586,7 @@ jobs: Clear-Content "cmake\OpenCVFindLibsGrfmt.cmake" Remove-Item "modules\gapi" -Recurse C:\msys64\usr\bin\patch.exe -p1 -i ..\opencv-4.8.0-no-zlib.patch + C:\msys64\usr\bin\patch.exe -p1 -i ..\opencv-4.8.0-link-openmp.patch Remove-Item "modules\highgui" -Recurse Copy-Item -Recurse -Path "..\highgui" -Destination "modules\" - name: build-x86 @@ -2759,6 +2770,7 @@ jobs: Clear-Content "cmake\OpenCVFindLibsGrfmt.cmake" Remove-Item "modules\gapi" -Recurse C:\msys64\usr\bin\patch.exe -p1 -i ..\opencv-4.8.0-no-zlib.patch + C:\msys64\usr\bin\patch.exe -p1 -i ..\opencv-4.8.0-link-openmp.patch Remove-Item "modules\highgui" -Recurse Copy-Item -Recurse -Path "..\highgui" -Destination "modules\" - name: build-x86 @@ -2947,6 +2959,7 @@ jobs: Clear-Content "cmake\OpenCVFindLibsGrfmt.cmake" Remove-Item "modules\gapi" -Recurse C:\msys64\usr\bin\patch.exe -p1 -i ..\opencv-4.8.0-no-zlib.patch + C:\msys64\usr\bin\patch.exe -p1 -i ..\opencv-4.8.0-link-openmp.patch Remove-Item "modules\highgui" -Recurse Copy-Item -Recurse -Path "..\highgui" -Destination "modules\" - name: build-x86 @@ -3139,6 +3152,7 @@ jobs: truncate -s 0 cmake/OpenCVFindLibsGrfmt.cmake rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch rm -rf modules/highgui cp -r ../highgui modules/ - name: build @@ -3279,6 +3293,7 @@ jobs: truncate -s 0 cmake/OpenCVFindLibsGrfmt.cmake rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch rm -rf modules/highgui cp -r ../highgui modules/ - name: build @@ -3426,6 +3441,7 @@ jobs: rm -rf modules/gapi patch -p1 -i ../opencv-4.8.0-no-rtti.patch patch -p1 -i ../opencv-4.8.0-no-zlib.patch + patch -p1 -i ../opencv-4.8.0-link-openmp.patch patch -p1 -i ../opencv-4.8.0-fix-emscripten-include.patch rm -rf modules/highgui cp -r ../highgui modules/ diff --git a/opencv-2.4.13.7-link-openmp.patch b/opencv-2.4.13.7-link-openmp.patch index b6c1ae01..76c2e4fd 100644 --- a/opencv-2.4.13.7-link-openmp.patch +++ b/opencv-2.4.13.7-link-openmp.patch @@ -1,13 +1,41 @@ +diff -Nuarp opencv-2.4.13.7.orig/cmake/templates/OpenCVConfig.cmake.in opencv-2.4.13.7/cmake/templates/OpenCVConfig.cmake.in +--- opencv-2.4.13.7.orig/cmake/templates/OpenCVConfig.cmake.in 2018-07-02 20:41:56.000000000 +0800 ++++ opencv-2.4.13.7/cmake/templates/OpenCVConfig.cmake.in 2023-08-30 11:25:24.227478073 +0800 +@@ -80,6 +80,7 @@ set(OpenCV_CUDA_VERSION @OpenCV_CUDA_VER + set(OpenCV_USE_CUBLAS @HAVE_CUBLAS@) + set(OpenCV_USE_CUFFT @HAVE_CUFFT@) + set(OpenCV_USE_NVCUVID @HAVE_NVCUVID@) ++set(OpenCV_USE_OPENMP @HAVE_OPENMP@) + + # Android API level from which OpenCV has been compiled is remembered + if(ANDROID) +@@ -291,6 +292,10 @@ if(OpenCV_CUDA_VERSION) + endforeach() + endif() + ++if(OpenCV_USE_OPENMP) ++ find_package(OpenMP) ++endif() ++ + # ============================================================== + # Android camera helper macro + # ============================================================== diff -Nuarp opencv-2.4.13.7.orig/modules/core/CMakeLists.txt opencv-2.4.13.7/modules/core/CMakeLists.txt --- opencv-2.4.13.7.orig/modules/core/CMakeLists.txt 2018-07-02 20:41:56.000000000 +0800 -+++ opencv-2.4.13.7/modules/core/CMakeLists.txt 2023-08-13 19:26:51.073833956 +0800 -@@ -52,7 +52,11 @@ else() ++++ opencv-2.4.13.7/modules/core/CMakeLists.txt 2023-08-30 11:41:25.374790126 +0800 +@@ -52,7 +52,17 @@ else() HEADERS ${lib_cuda_hdrs} ${lib_cuda_hdrs_detail}) endif() -ocv_create_module() -+if(HAVE_OPENMP AND DEFINED OpenMP_CXX_LIBRARIES AND OpenMP_CXX_LIBRARIES) -+ ocv_create_module(${OpenMP_CXX_LIBRARIES}) ++if(HAVE_OPENMP) ++ if(ANDROID_NDK_MAJOR AND (ANDROID_NDK_MAJOR GREATER 20)) ++ ocv_create_module(-fopenmp -static-openmp) ++ elseif(OpenMP_CXX_FOUND) ++ ocv_create_module(OpenMP::OpenMP_CXX) ++ else() ++ ocv_create_module(${OpenMP_CXX_FLAGS}) ++ endif() +else() + ocv_create_module() +endif() diff --git a/opencv-3.4.20-link-openmp.patch b/opencv-3.4.20-link-openmp.patch index 6768161f..6792efc9 100644 --- a/opencv-3.4.20-link-openmp.patch +++ b/opencv-3.4.20-link-openmp.patch @@ -1,14 +1,54 @@ +diff -Nuarp opencv-3.4.20.orig/cmake/OpenCVGenConfig.cmake opencv-3.4.20/cmake/OpenCVGenConfig.cmake +--- opencv-3.4.20.orig/cmake/OpenCVGenConfig.cmake 2023-06-27 19:29:13.000000000 +0800 ++++ opencv-3.4.20/cmake/OpenCVGenConfig.cmake 2023-08-30 11:59:03.095856713 +0800 +@@ -15,6 +15,10 @@ if(HAVE_CUDA) + ocv_cmake_configure("${CMAKE_CURRENT_LIST_DIR}/templates/OpenCVConfig-CUDA.cmake.in" CUDA_CONFIGCMAKE @ONLY) + endif() + ++if(HAVE_OPENMP) ++ ocv_cmake_configure("${CMAKE_CURRENT_LIST_DIR}/templates/OpenCVConfig-OPENMP.cmake.in" OPENMP_CONFIGCMAKE @ONLY) ++endif() ++ + if(ANDROID) + if(NOT ANDROID_NATIVE_API_LEVEL) + set(OpenCV_ANDROID_NATIVE_API_LEVEL_CONFIGCMAKE 0) +diff -Nuarp opencv-3.4.20.orig/cmake/templates/OpenCVConfig.cmake.in opencv-3.4.20/cmake/templates/OpenCVConfig.cmake.in +--- opencv-3.4.20.orig/cmake/templates/OpenCVConfig.cmake.in 2023-06-27 19:29:13.000000000 +0800 ++++ opencv-3.4.20/cmake/templates/OpenCVConfig.cmake.in 2023-08-30 11:59:19.223817499 +0800 +@@ -94,6 +94,7 @@ endif() + + + @CUDA_CONFIGCMAKE@ ++@OPENMP_CONFIGCMAKE@ + @ANDROID_CONFIGCMAKE@ + + @IPPICV_CONFIGCMAKE@ +diff -Nuarp opencv-3.4.20.orig/cmake/templates/OpenCVConfig-OPENMP.cmake.in opencv-3.4.20/cmake/templates/OpenCVConfig-OPENMP.cmake.in +--- opencv-3.4.20.orig/cmake/templates/OpenCVConfig-OPENMP.cmake.in 1970-01-01 08:00:00.000000000 +0800 ++++ opencv-3.4.20/cmake/templates/OpenCVConfig-OPENMP.cmake.in 2023-08-30 11:58:12.298980226 +0800 +@@ -0,0 +1,5 @@ ++set(OpenCV_USE_OPENMP "@HAVE_OPENMP@") ++ ++if(OpenCV_USE_OPENMP) ++ find_package(OpenMP) ++endif() diff -Nuarp opencv-3.4.20.orig/modules/core/CMakeLists.txt opencv-3.4.20/modules/core/CMakeLists.txt --- opencv-3.4.20.orig/modules/core/CMakeLists.txt 2023-06-27 19:29:13.000000000 +0800 -+++ opencv-3.4.20/modules/core/CMakeLists.txt 2023-08-13 19:39:56.659037366 +0800 -@@ -31,6 +31,10 @@ if(HAVE_TBB) - list(APPEND extra_libs tbb) - endif() ++++ opencv-3.4.20/modules/core/CMakeLists.txt 2023-08-30 12:13:56.534064133 +0800 +@@ -113,6 +113,16 @@ ocv_target_link_libraries(${the_module} + "${OPENCV_HAL_LINKER_LIBS}" + ) -+if(HAVE_OPENMP AND DEFINED OpenMP_CXX_LIBRARIES AND OpenMP_CXX_LIBRARIES) -+ list(APPEND extra_libs ${OpenMP_CXX_LIBRARIES}) ++if(HAVE_OPENMP) ++ if(ANDROID_NDK_MAJOR AND (ANDROID_NDK_MAJOR GREATER 20)) ++ ocv_target_link_libraries(${the_module} PUBLIC "-fopenmp" "-static-openmp") ++ elseif(OpenMP_CXX_FOUND) ++ ocv_target_link_libraries(${the_module} PUBLIC OpenMP::OpenMP_CXX) ++ else() ++ ocv_target_link_libraries(${the_module} PUBLIC "${OpenMP_CXX_FLAGS}") ++ endif() +endif() + - if(DEFINED WINRT AND NOT DEFINED ENABLE_WINRT_MODE_NATIVE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /ZW") + if(OPENCV_CORE_EXCLUDE_C_API) + ocv_target_compile_definitions(${the_module} PRIVATE "OPENCV_EXCLUDE_C_API=1") endif() diff --git a/opencv-4.8.0-link-openmp.patch b/opencv-4.8.0-link-openmp.patch new file mode 100644 index 00000000..dec1bc49 --- /dev/null +++ b/opencv-4.8.0-link-openmp.patch @@ -0,0 +1,54 @@ +diff -Nuarp opencv-4.8.0.orig/cmake/OpenCVGenConfig.cmake opencv-4.8.0/cmake/OpenCVGenConfig.cmake +--- opencv-4.8.0.orig/cmake/OpenCVGenConfig.cmake 2023-06-28 19:53:33.000000000 +0800 ++++ opencv-4.8.0/cmake/OpenCVGenConfig.cmake 2023-08-30 12:46:00.413783565 +0800 +@@ -15,6 +15,10 @@ if(HAVE_CUDA) + ocv_cmake_configure("${CMAKE_CURRENT_LIST_DIR}/templates/OpenCVConfig-CUDA.cmake.in" CUDA_CONFIGCMAKE @ONLY) + endif() + ++if(HAVE_OPENMP) ++ ocv_cmake_configure("${CMAKE_CURRENT_LIST_DIR}/templates/OpenCVConfig-OPENMP.cmake.in" OPENMP_CONFIGCMAKE @ONLY) ++endif() ++ + if(ANDROID) + if(NOT ANDROID_NATIVE_API_LEVEL) + set(OpenCV_ANDROID_NATIVE_API_LEVEL_CONFIGCMAKE 0) +diff -Nuarp opencv-4.8.0.orig/cmake/templates/OpenCVConfig.cmake.in opencv-4.8.0/cmake/templates/OpenCVConfig.cmake.in +--- opencv-4.8.0.orig/cmake/templates/OpenCVConfig.cmake.in 2023-06-28 19:53:33.000000000 +0800 ++++ opencv-4.8.0/cmake/templates/OpenCVConfig.cmake.in 2023-08-30 12:46:16.641712071 +0800 +@@ -94,6 +94,7 @@ endif() + + + @CUDA_CONFIGCMAKE@ ++@OPENMP_CONFIGCMAKE@ + @ANDROID_CONFIGCMAKE@ + + @IPPICV_CONFIGCMAKE@ +diff -Nuarp opencv-4.8.0.orig/cmake/templates/OpenCVConfig-OPENMP.cmake.in opencv-4.8.0/cmake/templates/OpenCVConfig-OPENMP.cmake.in +--- opencv-4.8.0.orig/cmake/templates/OpenCVConfig-OPENMP.cmake.in 1970-01-01 08:00:00.000000000 +0800 ++++ opencv-4.8.0/cmake/templates/OpenCVConfig-OPENMP.cmake.in 2023-08-30 11:58:12.298980226 +0800 +@@ -0,0 +1,5 @@ ++set(OpenCV_USE_OPENMP "@HAVE_OPENMP@") ++ ++if(OpenCV_USE_OPENMP) ++ find_package(OpenMP) ++endif() +diff -Nuarp opencv-4.8.0.orig/modules/core/CMakeLists.txt opencv-4.8.0/modules/core/CMakeLists.txt +--- opencv-4.8.0.orig/modules/core/CMakeLists.txt 2023-06-28 19:53:33.000000000 +0800 ++++ opencv-4.8.0/modules/core/CMakeLists.txt 2023-08-30 12:45:06.680020296 +0800 +@@ -169,8 +169,14 @@ if(HAVE_HPX) + ocv_target_link_libraries(${the_module} LINK_PRIVATE "${HPX_LIBRARIES}") + endif() + +-if(HAVE_OPENMP AND DEFINED OpenMP_CXX_LIBRARIES AND OpenMP_CXX_LIBRARIES) +- ocv_target_link_libraries(${the_module} LINK_PRIVATE "${OpenMP_CXX_LIBRARIES}") ++if(HAVE_OPENMP) ++ if(ANDROID_NDK_MAJOR AND (ANDROID_NDK_MAJOR GREATER 20)) ++ ocv_target_link_libraries(${the_module} PUBLIC "-fopenmp" "-static-openmp") ++ elseif(OpenMP_CXX_FOUND) ++ ocv_target_link_libraries(${the_module} PUBLIC OpenMP::OpenMP_CXX) ++ else() ++ ocv_target_link_libraries(${the_module} PUBLIC "${OpenMP_CXX_FLAGS}") ++ endif() + endif() + + ocv_add_accuracy_tests()