Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use zlib target instead of ZLIB_INCLUDE_DIRS and ZLIB_LIBRARIES #25569

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from

Conversation

FantasqueX
Copy link
Contributor

@FantasqueX FantasqueX commented May 10, 2024

Currently, when one target needs zlib as a dependency, it needs to use both ocv_include_directories and target_link_libraries. However in modern CMake, only target_link_libraries is needed if zlib has a target_include_directories. In this patch, if zlib is found by find_package, an ALIAS target zlib will be created. CMake >= 3.1 ensures a target ZLIB::ZLIB.
https://cmake.org/cmake/help/latest/module/FindZLIB.html
If zlib is built, an library target zlib will be created.

Tested zlib (built or found) and zlib-ng each with libpng, libtiff and openexr on Windows and ArchLinux.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@FantasqueX FantasqueX marked this pull request as draft May 10, 2024 17:52
@FantasqueX FantasqueX force-pushed the use-zlib-target-1 branch from 0b785fc to bb70678 Compare May 11, 2024 02:23
@FantasqueX FantasqueX marked this pull request as ready for review May 11, 2024 02:24
@FantasqueX FantasqueX force-pushed the use-zlib-target-1 branch from bb70678 to a5d85bd Compare May 11, 2024 03:55
@asmorkalov asmorkalov self-requested a review May 13, 2024 08:12
@asmorkalov
Copy link
Contributor

2024-05-11T12:51:24.1604533Z -- Check if the system is big endian - little endian
2024-05-11T12:51:24.1736149Z -- Found ZLIB: /usr/lib/aarch64-linux-gnu/libz.so (found suitable version "1.2.11", minimum required is "1.2.3") 
2024-05-11T12:51:24.1739357Z CMake Error at cmake/OpenCVFindLibsGrfmt.cmake:33 (add_library):
2024-05-11T12:51:24.1789268Z   add_library cannot create ALIAS target "zlib" because target "ZLIB::ZLIB"
2024-05-11T12:51:24.1790475Z   is imported but not globally visible.
2024-05-11T12:51:24.1791224Z Call Stack (most recent call first):
2024-05-11T12:51:24.1791936Z   CMakeLists.txt:820 (include)

@FantasqueX
Copy link
Contributor Author

@asmorkalov Hi, thanks for your review. Could you provide your CMake command and some information about your environment? So, I could try to reproduce the error :)

@asmorkalov
Copy link
Contributor

It happens with all Linux environments in CI. Pipelines: https://github.com/opencv/ci-gha-workflow/tree/main/.github/workflows, Dockerfiles: https://github.com/opencv-infrastructure/opencv-gha-dockerfile. Also you can extract all commands and env settings from CI raw log.

@FantasqueX
Copy link
Contributor Author

I found the problem which only occurs when CMake < 3.18.

https://cmake.org/cmake/help/latest/command/add_library.html

New in version 3.18: An ALIAS can target a non-GLOBAL Imported Target. Such alias is scoped to the directory in which it is created and below. The ALIAS_GLOBAL target property can be used to check if the alias is global or not.

@FantasqueX
Copy link
Contributor Author

The configure issue on Android seems to be resolved on my machine. However, I don't fully understand. Any suggestion is welcome :)

@FantasqueX
Copy link
Contributor Author

@asmorkalov Hi, could you please approve the workflow and review this PR :)

@FantasqueX
Copy link
Contributor Author

I think I should give up. I'm unable to fix those failing checks.

@sturkmen72
Copy link
Contributor

@FantasqueX let me try to help.

@sturkmen72
Copy link
Contributor

sturkmen72 commented May 22, 2024

@FantasqueX let me try to help.

see sturkmen72@853f838 i tested it on Windows.

4.x...sturkmen72:opencv:zlib-help

@FantasqueX
Copy link
Contributor Author

@asmorkalov Hi, thanks to @sturkmen72 we fix a bug in this PR. Could you please approve the workflow again?😊

@asmorkalov
Copy link
Contributor

iOS:

-- Configuring done
CMake Error in 3rdparty/zlib/CMakeLists.txt:
  Target "zlib" INTERFACE_INCLUDE_DIRECTORIES property contains path:

    "/Users/opencv-cn/GHA-OCV-1/_work/opencv/opencv/ios/build/build-armv7-iphoneos/3rdparty/zlib"

  which is prefixed in the build directory.


CMake Error in 3rdparty/zlib/CMakeLists.txt:
  Target "zlib" INTERFACE_INCLUDE_DIRECTORIES property contains path:

    "/Users/opencv-cn/GHA-OCV-1/_work/opencv/opencv/opencv/3rdparty/zlib"

  which is prefixed in the source directory.


CMake Generate step failed.  Build files cannot be regenerated correctly.
-- Generating done

@FantasqueX
Copy link
Contributor Author

FantasqueX commented May 30, 2024

I pushed a commit to fix iOS build according to https://stackoverflow.com/questions/25676277/cmake-target-include-directories-prints-an-error-when-i-try-to-add-the-source
Since I don't have a Mac device, I cannot test it locally :(

@FantasqueX
Copy link
Contributor Author

@asmorkalov Hi, could you please approve the workflow?

@FantasqueX
Copy link
Contributor Author

@asmorkalov Hi, could you please approve the workflow again? I fixed the test locally.

1 similar comment
@FantasqueX
Copy link
Contributor Author

@asmorkalov Hi, could you please approve the workflow again? I fixed the test locally.

@asmorkalov asmorkalov added this to the 4.11.0 milestone Jun 18, 2024
Comment on lines 792 to 1408
if(NOT BUILD_SHARED_LIBS)
ocv_install_target(${ZLIB_LIBRARY} EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev)
endif()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should break static linkage, if OpenCV is build against own zlib-ng, but not system-wide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for late reply. This ocv_install_target statement isn't removed. It is moved to outer space because both static linkage and dynamic linkage need this statement in Android build. So, this will only affect dynamic linkage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not redistribute system or sysroot libraries even for static OpenCV builds. Only own 3rdparty built binaries are redistributed.

Which builder configuration does check of your modification?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3rdparty/zlib/CMakeLists.txt Show resolved Hide resolved
@asmorkalov asmorkalov self-assigned this Jun 18, 2024
@asmorkalov asmorkalov requested a review from opencv-alalek July 1, 2024 09:46
@@ -75,7 +75,7 @@ if(MSVC)
endif(MSVC)

add_library(${PNG_LIBRARY} STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} ${lib_srcs} ${lib_hdrs})
target_link_libraries(${PNG_LIBRARY} ${ZLIB_LIBRARIES})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_LIBRARY is not used in documentation. Only _LIBRARIES is there:

https://cmake.org/cmake/help/book/mastering-cmake/chapter/Finding%20Packages.html


The same is related to FindZLIB: https://cmake.org/cmake/help/latest/module/FindZLIB.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Previously OpenCV used this variable. So, I used it. Essentially, we just need a name for interface library if we link system zlib and target library if we build zlib manually.

Comment on lines 792 to 1408
if(NOT BUILD_SHARED_LIBS)
ocv_install_target(${ZLIB_LIBRARY} EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev)
endif()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not redistribute system or sysroot libraries even for static OpenCV builds. Only own 3rdparty built binaries are redistributed.

Which builder configuration does check of your modification?

@asmorkalov
Copy link
Contributor

@FantasqueX please rebase the patch and fix conflicts.

@FantasqueX
Copy link
Contributor Author

I think I can rebase the patch this weekend :)

Currently, when one target needs zlib as a dependency, it needs to use both ocv_include_directories and target_link_libraries. However in modern CMake, only target_link_libraries is needed if zlib has a target_include_directories. In this patch, if zlib is found by find_package, an ALIAS target zlib will be created. CMake >= 3.1 ensures a target ZLIB::ZLIB. https://cmake.org/cmake/help/latest/module/FindZLIB.html If zlib is built, an library target zlib will be created.
3rdparty/zlib-ng/CMakeLists.txt Show resolved Hide resolved
3rdparty/zlib/CMakeLists.txt Show resolved Hide resolved
3rdparty/zlib/CMakeLists.txt Show resolved Hide resolved
@FantasqueX
Copy link
Contributor Author

@asmorkalov Could you please elaborate why those changes cause breakage? I still hope this patch will be merged.

@asmorkalov asmorkalov modified the milestones: 4.11.0, 4.12.0 Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants