Search code examples
c++android-ndkcmakensight

Tegra Nsight and locating the Android NDK


I am trying to build an Android native activity application, using CMake to generate a Tegra Nsight Visual Studio project. For the native activity, I must locate android_native_app_glue.c/.h, which is in a predictable location within the Android NDK (sources/android/native_app_glue). My CMake Android toolchain file defines only the CMAKE_SYSTEM_NAME, and API versions, as described in the CMake documentation.

However, I don't see any CMake variables that define where the Android NDK being used is actually located. According to this, when building in Android Studio, the ANDROID_NDK CMake variable is defined, but that isn't the case when invoking CMake from the command line.

I have dumped all CMake variables, and the only thing that contain the location of my NDK are CMAKE_CXX_COMPILER and CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES. I could parse one of these to determine the ANDROID_NDK location - but I'm thinking there must be a more straightforward way?


Solution

  • Apparently the NDK used to actually compile is determined by Visual Studio. You can change this in the Visual Studio Nsight properties. So, at CMake generation time, the actual NDK cannot be reliably determined.

    However, looking through the CMake sources, it uses the registry to determine the version of Nsight Tegra to compile with. There exists a registry key HKLM\SOFTWARE\NVIDIA Corporation\Nsight Tegra\ndkRoot, which gives the location of the NDK root installed with Nsight, and this can be used in CMake to locate the android_native_app_glue.c/.h files:

    get_filename_component(ANDROID_NDK "[HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Nsight Tegra\\;ndkRoot]" ABSOLUTE)
    set(app_sources 
        "${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c"
        "${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.h"
    ...)
    

    It is possible, that these files may not be from the NDK actually used to compile, if the NDK root is overridden in the Visual Studio Nsight properties, but this seems like a reasonable solution.