Skip to content

Commit

Permalink
fix link openmp flags (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui authored Aug 30, 2023
1 parent bdea6c9 commit ba622ab
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 12 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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/
Expand Down
36 changes: 32 additions & 4 deletions opencv-2.4.13.7-link-openmp.patch
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
56 changes: 48 additions & 8 deletions opencv-3.4.20-link-openmp.patch
Original file line number Diff line number Diff line change
@@ -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()
54 changes: 54 additions & 0 deletions opencv-4.8.0-link-openmp.patch
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit ba622ab

Please sign in to comment.