Skip to content

fix find zlib.so instead of zlib.a when NDK >= 19#19522

Merged
alalek merged 2 commits intoopencv:3.4from
zchrissirhcz:3.4-fix-android-find-zlib-shared-since-ndk19
Feb 14, 2021
Merged

fix find zlib.so instead of zlib.a when NDK >= 19#19522
alalek merged 2 commits intoopencv:3.4from
zchrissirhcz:3.4-fix-android-find-zlib-shared-since-ndk19

Conversation

@zchrissirhcz
Copy link
Copy Markdown
Contributor

On Android platform, libopencv_imgcodecs.a is built, expected to
depend on libz.so. However, since Android NDK r19, NDK's libz.a
is found instead of libz.so, leading to link error
(not found libz.a) on machines without same NDK version & direcotry.

Since Android NDK-r19, toolchain pieces are installed to
$NDK/toolchains/llvm/prebuilt/<host-tag>/..., including libz.so.
Also installed to old paths (<NDK>/platforms and <NDK>/sysroot)
in NDK r19, r20, r21, but since NDK 22, old paths are removed.

With this commit, libz.so can be correctly found in NDK<19 and NDK>=19.

ZLIB_LIBRARIES is also simplified as z, by appending match (regex)
patterns for new toolchain installation directory's libz.so's paths.

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 other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to 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

On Android platform, `libopencv_imgcodecs.a` is built, expected to
depend on `libz.so`. However, since Android NDK r19, NDK's `libz.a`
is found instead of `libz.so`, leading to link error
(not found libz.a) on machines without same NDK version & direcotry.

Since Android NDK-r19, toolchain pieces are installed to
`$NDK/toolchains/llvm/prebuilt/<host-tag>/...`, including `libz.so`.
Also installed to old paths (`<NDK>/platforms` and `<NDK>/sysroot`)
in NDK r19, r20, r21, but since NDK 22, old paths are removed.

- https://github.com/android/ndk/wiki/Changelog-r19
- https://github.com/android/ndk/wiki/Changelog-r22

With this commit, `libz.so` can be correctly found in NDK<19 and NDK>=19.

`ZLIB_LIBRARIES` is also simplified as `z`, by appending match (regex)
patterns for new toolchain installation directory's libz.so's paths.
@zchrissirhcz zchrissirhcz changed the base branch from master to 3.4 February 13, 2021 04:45
@zchrissirhcz
Copy link
Copy Markdown
Contributor Author

Note: this commit tested locally, with cmake 3.19.4, without modification of cmake-3.19.4\share\cmake-3.19\Modules\FindZLIB.cmake.

@zchrissirhcz
Copy link
Copy Markdown
Contributor Author

Note: this PR should only affect android platform. Android arm64 platform may fail, #19521 fix that failure and should be merged first.

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

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

Thank you for contribution!

if(ZLIB_LIBRARIES MATCHES "/usr/(lib|lib32|lib64)/libz.so$")
if(ZLIB_LIBRARIES MATCHES "/usr/(lib|lib32|lib64)/libz.so$"
OR ZLIB_LIBRARIES MATCHES "/usr/lib/(aarch64-linux-android|x86_64-linux-android)/(2[1-9]|3[0-9])/libz.so"
OR ZLIB_LIBRARIES MATCHES "/usr/lib/(arm-linux-androideabi|i686-linux-android)/(1[6-9]|(2|3)[0-9])/libz.so"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe we can use here generic pattern here:

"/usr/lib.*/libz.so$"

and add two control variables:

    if((OPENCV_FORCE_ANDROID_ZLIB_Z
        OR ZLIB_LIBRARIES MATCHES "/usr/lib.*/libz.so")
        AND NOT OPENCV_SKIP_ANDROID_ZLIB_Z
    )
      set(ZLIB_LIBRARIES z)
    endif()

(checked locally with NDK 18, 20, 21)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The mentioned generic pattern is simple. It's OK to use that.

For the mentioned two control variables, on android platform:

  • I think there are only two origins for zlib: build from source(libz.a), or use NDK diretories(libz.so), thus :
  • OPENCV_FORCE_ANDROID_ZLIB_Z: is same as NOT BUILD_ZLIB
  • OPENCV_SKIP_ANDROID_ZLIB_Z: is same as BUILD_ZLIB

So I would like to use:

    if(ZLIB_LIBRARIES MATCHES "/usr/lib.*/libz.so$")
      set(ZLIB_LIBRARIES z)
    endif()

@alalek alalek merged commit a5a421a into opencv:3.4 Feb 14, 2021
@zchrissirhcz zchrissirhcz deleted the 3.4-fix-android-find-zlib-shared-since-ndk19 branch February 14, 2021 19:43
@alalek alalek mentioned this pull request Feb 21, 2021
@alalek alalek mentioned this pull request Apr 9, 2021
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.

2 participants