fix openmp include and link issue on macos#23095
Conversation
|
There is the same problems like: #21427 (comment) |
Sure, let me take a look into the old version. By the way, by "old version" do you mean versions <= 3.5? |
|
OpenCV 4.0 has been released with CMake 3.5.1 support (version from Ubuntu 16.04). BTW, we need to check for existence/definition of new variables ( |
Latest commit should fix this issue. Tested with |
This is a regression "by definition". Because you broke OpenMP on configurations similar to Ubuntu 16.04. |
|
I found that gnu gcc links But this is not the case for clang on macOS. clang on macOS treats |
No.
|
|
I need more information about the return of |
|
In CMake 3.5.1 Again, refer to comments from previous PR: #21427 (comment) To track behavior changes of CMake scripts you need to check history of CMake. |
|
I found |
|
It shows OpenMP is used on openmp:16.04 with latest commit: https://pullrequest.opencv.org/buildbot/builders/precommit_custom_linux/builds/100176/steps/cmake/logs/stdio. Let me take a look at |
This is actually caused by a typo I made to the CMake option. It is good now. Feel free to review this pull request @alalek |
modules/core/CMakeLists.txt
Outdated
| ocv_target_link_libraries(${the_module} LINK_PRIVATE "${HPX_LIBRARIES}") | ||
| endif() | ||
|
|
||
| if(WITH_OPENMP AND DEFINED OpenMP_CXX_INCLUDE_DIRS AND OpenMP_CXX_INCLUDE_DIRS) |
There was a problem hiding this comment.
WITH_OPENMP
HAVE_OPENMP
OpenMP_CXX_INCLUDE_DIRS
Empty OpenMP_CXX_LIBRARIES would trigger CMake error below with missing argument, so check should use OpenMP_CXX_LIBRARIES instead.
There was a problem hiding this comment.
Okay, let me bake another commit for this.
There was a problem hiding this comment.
Empty OpenMP_CXX_LIBRARIES would trigger CMake error below with missing argument, so check should use OpenMP_CXX_LIBRARIES instead.
I found that OpenMP_CXX_LIBRARIES is defined since CMake>=3.9.0, while OpenMP_CXX_INCLUDE_DIRS is defined since CMake>=3.16.0. So OpenMP_CXX_LIBRARIES is never empty when OpenMP_CXX_INCLUDE_DIRS has value. It actually behaves exactly like this in my experiences below:
| Linux (Ubuntu 20.04) | |
|---|---|
| cmake-3.5.1 | inc: no def, lib: no def |
| cmake-3.9.6 | inc: no def, lib: def +value |
| cmake-3.15.7 | inc: no def, lib: def + value |
| cmake-3.16.0 | inc: def (no value), lib: def + value |
Update:
I use HAVE_OPENMP and OpenMP_CXX_LIBRARIES as condition for linking in the latest commit. Basically this patch of code is triggered when CMake >= 3.16.0 and it is on clang.
cmake/OpenCVFindFrameworks.cmake
Outdated
| set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") | ||
| set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") | ||
| # NOTES: | ||
| # 1. For CMake <= 3.5.1, OpenMP_CXX_INCLUDE_DIRS is not defined. |
There was a problem hiding this comment.
3.5.1
This is not correct.
This is a minimal CMake version on which we take a look look for OpenCV 4.x.
https://cmake.org/cmake/help/v3.6/module/FindOpenMP.html
- still no
_INCLUDE_DIRStoo.
There was a problem hiding this comment.
I checked CMake docs and found _INCLUDE_DIRS is defined since CMake>=3.16.0. See https://cmake.org/cmake/help/v3.16/module/FindOpenMP.html#backward-compatibility.
cmake/OpenCVFindFrameworks.cmake
Outdated
| # NOTES: | ||
| # 1. OpenMP_CXX_INCLUDE_DIRS is defined since CMake >= 3.16.0. | ||
| # 2. OpenMP_CXX_LIBRARIES is defined since CMake >= 3.9.0. | ||
| # 2. For gnu openmp (libgomp, e.g. on Linux), OpenMP_CXX_INCLUDE_DIRS is null |
There was a problem hiding this comment.
double 2.
Not sure if we really need such detailed comment here (especially not sure about its accuracy).
OpenMP_CXX_LIBRARIESandOpenMP_CXX_INCLUDE_DIRSare available with modern CMake scripts only. We use checks to detect that case.
There was a problem hiding this comment.
Okay, I will copy-paste all the comments in the first comment of this PR and delete them from the code.
modules/core/CMakeLists.txt
Outdated
| ocv_target_link_libraries(${the_module} LINK_PRIVATE "${HPX_LIBRARIES}") | ||
| endif() | ||
|
|
||
| if(HAVE_OPENMP AND DEFINED OpenMP_CXX_INCLUDE_DIRS AND OpenMP_CXX_LIBRARIES) |
There was a problem hiding this comment.
OpenMP_CXX_INCLUDE_DIRS AND OpenMP_CXX_LIBRARIES
mess of different variables
There was a problem hiding this comment.
According to my test above, there is a case when _LIBRARIES is defined and has value but _INCLUDE_DIRS is not defined at all (CMake >= 3.9, <= 3.15). This condition is basically ensuring the old behavior is not changed, which is OpenMP_CXX_LIBRARIES will not be linked until OpenMP_CXX_INCLUDE_DIRS is defined.
Do you suggest using OpenMP_CXX_LIBRARIES in replace of OpenMP_CXX_INCLUDE_DIRS for definition checking?
Update:
It is now using if(HAVE_OPENMP AND DEFINED OpenMP_CXX_INCLUDE_DIRS AND OpenMP_CXX_INCLUDE_DIRS AND OpenMP_CXX_LIBRARIES) as conditions for linking libomp.
There was a problem hiding this comment.
No need to use other variables here.
Logic should protect usage from empty/non-defined values. No more, no less.
if(HAVE_OPENMP AND DEFINED OpenMP_CXX_LIBRARIES AND OpenMP_CXX_LIBRARIES)
ocv_target_link_libraries(${the_module} LINK_PRIVATE "${OpenMP_CXX_LIBRARIES}")
endif()
There was a problem hiding this comment.
Okay, done with latest commit.
There was a problem hiding this comment.
Okay, done with latest commit.
* fix openmp include and link issue on macos * turn off have_openmp if OpenMP_CXX_INCLUDE_DIRS is empty * test commit * use condition HAVE_OPENMP and OpenMP_CXX_LIBRARIES for linking * remove trailing whitespace * remove notes * update conditions * use OpenMP_CXX_LIBRARIES for linking
* fix openmp include and link issue on macos * turn off have_openmp if OpenMP_CXX_INCLUDE_DIRS is empty * test commit * use condition HAVE_OPENMP and OpenMP_CXX_LIBRARIES for linking * remove trailing whitespace * remove notes * update conditions * use OpenMP_CXX_LIBRARIES for linking
|
On macos, i am getting when building the 4.12.0 tag |
|
specifically it fails linking |
Fixes #23092
Fixes #18478
Helpful notes:
regardless CMake version. Passing flag
-fopenmpis enough.null and need to include header and link libomp in addition to passing flag.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.