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

[BUG] CMake finding & linking host libraries #890

Closed
mattvchandler opened this issue Jan 21, 2019 · 8 comments
Closed

[BUG] CMake finding & linking host libraries #890

mattvchandler opened this issue Jan 21, 2019 · 8 comments
Assignees
Labels
Milestone

Comments

@mattvchandler
Copy link

mattvchandler commented Jan 21, 2019

Description

I have an app that builds and uses Freetype. This worked fine until I updated the NDK to v19.0.5232133
Now, CMake finds the host system's harfbuzz and tries to link against it, which fails:

...
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29") 
-- Found harfbuzz: /usr/include/harfbuzz (found version "2.3.0") 
...
/home/matt/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: /usr/lib/x86_64-linux-gnu/libharfbuzz.so: incompatible target

CMAKE_FIND_ROOT_PATH_MODE_LIBRARY and ..._INCLUDE are set to ONLY, so I would expect CMake to ignore the host system paths. Freetype uses a FindHarfBuzz.cmake file (included in the test case), but as far as I can tell (I only know basic CMake configuration) it doesn't override this in anyway.

I can't verify which NDK version last worked, but it would have been the last one recommended in the android studio SDK manager before 19.0.5232133

Test case gist

Environment Details

  • NDK Version: 19.0.5232133
  • Build system: cmake
  • Host OS: Linux (Debian Testing amd64), also observed on Windows with mingw2
  • Compiler: clang
  • ABI: any
  • STL: c++_static
  • NDK API level:19
  • Device API level: 28
@alexgarret
Copy link

Hi @mattvchandler I am facing the same issue with the NDKr19.
I will keep you updated if I find a workaround for this.

@DanAlbert
Copy link
Member

Checked both the SDK's older version of CMake (3.6) and r18. CMake version doesn't make a difference, but this works with r18. These sorts of issues are usually just a missing setting in the toolchain file. Looking into it now.

@DanAlbert DanAlbert self-assigned this Jan 22, 2019
@DanAlbert
Copy link
Member

Looks like it's because CMake will search the host system even when CMAKE_FIND_ROOT_PATH_MODE_INCLUDE is ONLY when CMAKE_FIND_ROOT_PATH is not set. Easy fix is to just put back the explicit CMAKE_FIND_ROOT_PATH that we had before and thought was obsolete.

https://android-review.googlesource.com/c/platform/ndk/+/880977

@mattvchandler
Copy link
Author

I just tried adding set(CMAKE_FIND_ROOT_PATH "${ANDROID_NDK}") to android.toolchain.cmake.
It builds just fine now. Thanks!

@huangqinjin
Copy link

The candidate directories for searching are generated by joining roots and prefixes. Roots include CMAKE_SYSROOT and CMAKE_FIND_ROOT_PATH . Prefixes include /usr/include for find_path and CMAKE_SYSTEM_LIBRARY_PATH for find_library . If roots are empty, then the candidates are just prefixes, which is the host system directories. See CMake.cmFindCommon::RerootPaths.

I think it's better to set CMAKE_SYSROOT rather than leave it as empty, the following resolves this issue.

diff --git a/android.toolchain.cmake b/android.toolchain.cmake
index e58ff05..3da882a 100644
--- a/android.toolchain.cmake
+++ b/android.toolchain.cmake
@@ -387,8 +387,9 @@ set(ANDROID_TOOLCHAIN_PREFIX
 # (https://cmake.org/cmake/help/v3.6/variable/CMAKE_SYSTEM_LIBRARY_PATH.html)
 # instead.
 set(ANDROID_SYSROOT "${ANDROID_TOOLCHAIN_ROOT}/sysroot")
+set(CMAKE_SYSROOT ${ANDROID_SYSROOT})
 list(APPEND CMAKE_SYSTEM_LIBRARY_PATH
-  "${ANDROID_SYSROOT}/usr/lib/${ANDROID_TOOLCHAIN_NAME}/${ANDROID_PLATFORM_LEVEL}")
+  "/usr/lib/${ANDROID_TOOLCHAIN_NAME}/${ANDROID_PLATFORM_LEVEL}")
 
 set(ANDROID_HOST_PREBUILTS "${ANDROID_NDK}/prebuilt/${ANDROID_HOST_TAG}")
 

@mattvchandler
Copy link
Author

I just tested that change on my app, and it also builds successfully.

daquexian added a commit to JDAI-CV/DNNLibrary that referenced this issue Jan 26, 2019
daquexian added a commit to JDAI-CV/DNNLibrary that referenced this issue Feb 1, 2019
@DanAlbert
Copy link
Member

I've merged a fix for this into r19 for r19b (will probably ship this in a couple of weeks; I'm still waiting a bit in case more bugs are filed).

@huangqinjin: I think you're probably right. For r19b I'm going to stick with my patch because I'd rather we revert to the previous behavior which didn't seem to cause any problems, but I'll leave this open to look into that for r20.

@DanAlbert
Copy link
Member

Actually I've filed it as a new bug (#902) because I want to keep this one associated with r19b for tracking reasons.

@DanAlbert DanAlbert added this to the r19b milestone Feb 1, 2019
disigma pushed a commit to wimal-build/ndk that referenced this issue Feb 22, 2019
Even though this shouldn't end up being used, it needs to be set so
that `CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY` works as intended.

Test: Tried to build freetype, it didn't find harfbuzz on my system
Bug: android/ndk#890
Change-Id: If4ea8cd1266261a001e20af79f35da52f2a5380e
(cherry picked from commit 5be007d)
grendello added a commit to grendello/xamarin-android that referenced this issue Mar 12, 2019
`cmake` 3.10 is the current version of the tool in the Android SDK,
an update from the much older 3.6. This update brings it closer to
the host `cmake` versions (currently mostly 3.12.*)

Bump NDK to release [19b][0] (a.k.a. 19.1), with the following changes:

    * [Issue 855][1]: ndk-build automatically disables multithreaded
      linking for LLD on Windows, where it may hang. It is not
      possible for the NDK to detect this situation for CMake,
      so CMake users and custom build systems must pass
      `-Wl,--no-threads` when linking with LLD on Windows.
    * [Issue 849][2]: Fixed unused command line argument warning when
      using standalone toolchains to compile C code.
    * [Issue 890][3]: Fixed CMAKE_FIND_ROOT_PATH. CMake projects will
      no longer search the host's sysroot for headers and libraries.
    * [Issue 907][4]: Fixed find_path for NDK headers in CMake.

[0]: https:/android-ndk/ndk/wiki/Changelog-r19#r19b
[1]: android/ndk#855
[2]: android/ndk#849
[3]: android/ndk#890
[4]: android/ndk#907
grendello added a commit to grendello/xamarin-android that referenced this issue Mar 12, 2019
`cmake` 3.10 is the current version of the tool in the Android SDK,
an update from the much older 3.6. This update brings it closer to
the host `cmake` versions (currently mostly 3.12.*)

Bump NDK to release [19b][0] (a.k.a. 19.1), with the following changes:

  * [Issue 855][1]: ndk-build automatically disables multithreaded
    linking for LLD on Windows, where it may hang. It is not
    possible for the NDK to detect this situation for CMake,
    so CMake users and custom build systems must pass
    `-Wl,--no-threads` when linking with LLD on Windows.
  * [Issue 849][2]: Fixed unused command line argument warning when
    using standalone toolchains to compile C code.
  * [Issue 890][3]: Fixed CMAKE_FIND_ROOT_PATH. CMake projects will
    no longer search the host's sysroot for headers and libraries.
  * [Issue 907][4]: Fixed find_path for NDK headers in CMake.

[0]: https:/android-ndk/ndk/wiki/Changelog-r19#r19b
[1]: android/ndk#855
[2]: android/ndk#849
[3]: android/ndk#890
[4]: android/ndk#907
jonpryor pushed a commit to dotnet/android that referenced this issue Mar 13, 2019
`cmake` 3.10 is the current version of the tool in the Android SDK,
an update from the much older 3.6.  This update brings it closer to
the host `cmake` versions (currently mostly 3.12.*)

Bump NDK to release [19b][0] (a.k.a. 19.1), which changes:

  * [Issue 855][1]: ndk-build automatically disables multithreaded
    linking for LLD on Windows, where it may hang.  It is not
    possible for the NDK to detect this situation for CMake,
    so CMake users and custom build systems must pass
    `-Wl,--no-threads` when linking with LLD on Windows.
  * [Issue 849][2]: Fixed unused command line argument warning when
    using standalone toolchains to compile C code.
  * [Issue 890][3]: Fixed CMAKE_FIND_ROOT_PATH.  CMake projects will
    no longer search the host's sysroot for headers and libraries.
  * [Issue 907][4]: Fixed find_path for NDK headers in CMake.

[0]: https:/android-ndk/ndk/wiki/Changelog-r19#r19b
[1]: android/ndk#855
[2]: android/ndk#849
[3]: android/ndk#890
[4]: android/ndk#907
mehulagg pushed a commit to mehulagg/superproject that referenced this issue Dec 21, 2019
* Update ndk from branch 'ndk-release-r19'
  to ad8a8068b079a8a5efdaed3968659cd685b1a885
  - Merge "Set CMAKE_FIND_ROOT_PATH to avoid host libraries." into ndk-release-r19
  - Set CMAKE_FIND_ROOT_PATH to avoid host libraries.
    
    Even though this shouldn't end up being used, it needs to be set so
    that `CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY` works as intended.
    
    Test: Tried to build freetype, it didn't find harfbuzz on my system
    Bug: android/ndk#890
    Change-Id: If4ea8cd1266261a001e20af79f35da52f2a5380e
    (cherry picked from commit 5be007d29204a9f1701ad0383820ec4ed5c12d25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants