android / ndk

The Android Native Development Kit
1.98k stars 257 forks source link

[BUG] r23b and CMake 3.21 on windows build can't find ninja.exe #1609

Closed skolson closed 2 years ago

skolson commented 2 years ago

Description

On windows 10 with an existing project using NDK 23.0.7599858 CMake 3.21.1, upgrading to r23b causes this error:

[CXX1410] ...\arm64-v8a\android_gradle_build.json debug|arm64-v8a : expected buildTargetsCommandComponents command 'ninja.exe' to exist

Sync succeeds but shows the above error in the output. Build fails with the same error. ninja.exe was and still is on the windows system PATH so it should be able to find it. During research tried each of the following, one at a time, all with no effect: add "-DCMAKE_MAKE_PROGRAM=D:\\Android\\CMake\\ninja.exe" to gradle cmake arguments Upgrade CMake from 3.21.1 to 3.21.4 add set(CMAKE_MAKE_PROGRAM "D:\\Android\\CMake\\ninja.exe") to CMakeLists.txt

Saw NDK issue 1581 which seemed related and based on the comments tried: add set(CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH 1) to CMakeLists.txt

None of the above changed the error. Below is the top of the android_gradle_build.json file. Note that the setting from CMAKE_MAKE_PROGRAM is used in cleanCommandsComponents but not in buildTargetsCommandComponents

  "buildFiles": [
    "D:\\WorkingShared\\SqlCipherKotlinMP\\kmp-sc\\src\\androidMain\\cpp\\CMakeLists.txt"
  ],
  "cleanCommandsComponents": [
    [
      "D:\\Android\\CMake\\ninja.exe",
      "-C",
      "D:\\WorkingShared\\SqlCipherKotlinMP\\kmp-sc\\.cxx\\Debug\\6s4k6g6a\\arm64-v8a",
      "clean"
    ]
  ],
  "buildTargetsCommandComponents": [
    "ninja.exe",
    "-C",
    "D:\\WorkingShared\\SqlCipherKotlinMP\\kmp-sc\\.cxx\\Debug\\6s4k6g6a\\arm64-v8a",
    "{LIST_OF_TARGETS_TO_BUILD}"
  ],

I can't see why the PATH isn't being used to find ninja.exe, or why setting CMAKE_MAKE_PROGRAM isn't helping.

I'm also unsure if this is an NDK issue or a gradle plugin issue specific to these NDK and CMake versions, sorry if I guessed wrong by posting here :-)

The project is a Kotlin library that I'm upgrading to Kotlin 1.6.0, which is what triggered the NDK upgrade. If it is useful I can create a github repo with this project.

Environment Details

Android Studio 2021.1.1 beta 3 NDK Version: 23.1.7779620 CMake 3.21.4 ninja 1.10.2 Gradle 7.3 com.android.tools.build:gradle 7.1.0-beta03 Host OS: Windows 10

DanAlbert commented 2 years ago

If it is useful I can create a github repo with this project.

This is always useful, and usually required for us to able to make any progress. QA found no problems on Windows so we know typical cases work fine (we wouldn't have shipped otherwise), and guessing at what went wrong means you have to wait a lot longer for a fix because it's nearly impossible for us to do.

skolson commented 2 years ago

Thanks. Repo: Github repo

DanAlbert commented 2 years ago

Going to be a while before I can test anything on Windows, so looking at the diff here: https://github.com/skolson/SqlCipherKotlinMP/commit/3dc96e9087dc20f66a9a630f187ca2088f1bfa4e

If you undo the NDK update, the problem goes away? That commit updates the NDK, CMake, and the Gradle plugin among other things, but the CMake or gradle plugin updates seem plausible. The NDK update isn't out of the question either but since we didn't see this in our own testing I'm skeptical.

skolson commented 2 years ago

Thanks for the quick update. Short answer is yes, if I switch back to 23.0.7599858 with no other change, the ninja issue goes away. But this is misleading :-). I tried to upgrade to r23b because during the upgrade to Kotlin 1.6, I hit the error below with 23.0.7599858. I found some stuff that indicated this error was specific to using CMake 3.21 or later with that NDK and might be fixed on r23b, which is why I got to where I am. I've been using CMake 3.21.1 (for maybe a year or so) to work around other issues :-)

I will start backing off one change at a time until I get back to working again, and will post again when I have more info.

CMake Error at D:/Android/CMake/share/cmake-3.21/Modules/CMakeTestCCompiler.cmake:69 (message):
  The C compiler

    "D:/Android/sdk/ndk/23.0.7599858/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: D:/WorkingShared/SqlCipherKotlinMP/kmp-sc/.cxx/Debug/4h402q86/arm64-v8a/CMakeFiles/CMakeTmp

    Run Build Command(s):D:\Android\CMake\ninja.exe cmTC_a4e6a && [1/2] Building C object CMakeFiles/cmTC_a4e6a.dir/testCCompiler.c.o
    [2/2] Linking C executable cmTC_a4e6a
    FAILED: cmTC_a4e6a
    cmd.exe /C "cd . && D:\Android\sdk\ndk\23.0.7599858\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe --target=aarch64-none-linux-android16 --sysroot=D:/Android/sdk/ndk/23.0.7599858/toolchains/llvm/prebuilt/windows-x86_64/sysroot -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fexceptions  -O2 -g -DNDEBUG -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Qunused-arguments -Wl,--no-undefined  -Wl,--gc-sections CMakeFiles/cmTC_a4e6a.dir/testCCompiler.c.o -o cmTC_a4e6a  -latomic -lm && cd ."
    ld: error: cannot open crtbegin_dynamic.o: No such file or directory
    ld: error: cannot open crtend_android.o: No such file or directory
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.
skolson commented 2 years ago

Progress! I found a much easier way to cause the issue. See Repo [Issue-specific-test-case]https://github.com/skolson/NdkIssue1609). I built a new default C++ project in Android studio. Project builds fine using default 3.18.1 of CMake. I then added the cmake.dir=D\:\\Android\\CMake to local.properties, and in the app build.gradle changed the cmake version from 3.18.1 to 3.21.4. Problem recreated. The works/fails behavior stays the same even if the NDK version changes. So my original issue title about this being specific to just r23b is wrong, it can happen with others. Then if I change the CMakeLists.txt file to add this: cmake_minimum_required(VERSION 3.21.4), Sync gets this as if it's ignoring the cmake.dir in local.properties:

CMake Error at CMakeLists.txt:6 (cmake_minimum_required):
CMake 3.21.4 or higher is required.  You are running version
3.18.1-g262b901-dirty

The doc at (https://developer.android.com/reference/tools/gradle-api/7.0/com/android/build/api/dsl/Cmake) says when the version is changed in build.gradle that the system PATH is used to locate CMake, but that's not happening in my case. I have triple-checked the PATH and it is correct and working outside of Android Studio. So the PATH isn't used and cmake.dir in local.properties is being ignored.

Does this info mean this is a plugin issue and not NDK? Are there diagnostics I can run to help locate this problem? I've been using various versions of the NDK with CMake 3.21.1 on my project for more than a year and have never seen this until yesterday. That plus your comment above makes me think this is my environment somehow, but I don't know how to diagnose the root cause. The system PATH has not changed. CMake was upgraded in-place to 3.21.4. I'm hoping with this test case you can recreate the error. In any case the error message is misleading when gradle config specifies a higher CMake than it can find.

Let me know what else I can do to help diagnose this.

Also it just occurred to me that yesterday Android Studio popped an update notice about the kotlin plugin, and I upgraded. It's the only change outside of CMake and NDK versions I can think of that happened. Any chance that's relevant?

skolson commented 2 years ago

One more FYI - I'm beginning to believe this error is hiding some other error. Attached is the CMakeOutput.log for one of the builds of the original project that gradle failed, using CMake 3.21.4 and NDK 23.1.7779620. Despite gradle failing with the ninja message, this log file proves that the CMake used is 3.21.4 and the ninja.exe is used form the correct location and the log looks like CMake worked even though gradle failed.

Hope this is useful. If there are any other tasks I can do or diagnostics I can send, let me know. Also another FYI, I've been using 3.21 for quite a while due to an old issue. I changed the project back to using the default CMake of 3.18.1 hoping that the old issue is fixed and sure enough it is, the project builds fine. So the ninja error is not a stopper for me any more, but I have time to help diagnose whatever is wrong when using a non-default CMake. Thanks again for your help.. CMakeOutput.log

DanAlbert commented 2 years ago

Hope this is useful. If there are any other tasks I can do or diagnostics I can send, let me know.

Will do! afaik we have what we need now and just need to find the time to investigate. There isn't currently a timeline for r23c, but we'll look at this for that release.

rprichard commented 2 years ago

I suspect this is an issue with the Android Gradle Plugin, and it's probably a duplicate of these bugs:

Should we close this issue?

skolson commented 2 years ago

That sure looks like the same issue, so I'm good with closing this one. Interesting that the bug is on all platforms not just windows.