Possible Bug in cmake-file-api related to relative paths

I know the following is going to sound Android Studio / Gradle specific, but please hear me out as I think this is a bug in file api, not in Gradle / Android. Also, apologies in advance for the long message, but there’s a lot of setup here, and I don’t think I can shorten it without losing important information.

I have a large application for one of the supported platforms is Android. We use CMake via the Android Studio Gradle CMake Plugin. As of CMake 3.18 and Android Studio 4.2, this invokes cmake-file-api so that Gradle can get a detailed description of the build.

I believe I’m hitting a bug in cmake-file-api that is preventing Gradle from working, but I am hoping someone could help me narrow this down further. Here are the key points distilled down:

  1. Android Studio generates my cmake ninja build into the folder D:\src\game-engine\Client\AndroidClient\engine\.cxx\cmake\release\armeabi-v7a.
  2. Before doing so, it creates file api requests in the form of:
 |_ src
    |_ game-engine
       |_ Client
          |_ AndroidClient
             |_ engine
                |_ .cxx
                   |_ cmake
                      |_ release
                         |_ armeabi-v7a       # This is the build directory / CMakeCache.txt location
                            |_ .cmake
                               |_ api
                                  |_ v1
                                     |_ query
                                        |_ client-agp
                                           |_ cache-v2
                                           |_ cmakeFiles-v1
                                           |_ codemodel-v2
  1. Gradle runs cmake configure step with the following command line:
    "-DCMAKE_CXX_FLAGS=-fexceptions -frtti" 
  1. One of the outputs of this build is libfmod.so, and if I print the target properties of this target, I see the following:
fmod LIBRARY_OUTPUT_DIRECTORY = D:\src\game-engine\Client\AndroidClient\engine\build\intermediates\cmake\release\obj\armeabi-v7a
fmod RUNTIME_OUTPUT_DIRECTORY = D:\src\game-engine\Client\AndroidClient\engine\build\intermediates\cmake\release\obj\armeabi-v7a

Just to confirm, If I add some debugging to print the value of $<TARGET_FILE:fmod>, I see this:

$ ninja genexdebug
[12.320 235/235]cmd.exe /C "cd /D D:\src\game-engine\Client\AndroidClient\engine\.cxx\cmake\release\armeabi-v7a\ThirdParty\f...ake.exe -E echo D:/src/game-engine/Client/AndroidClient/engine/build/intermediates/cmake/release/obj/armeabi-v7a/libfmod.so"

Clearly at this point we can establish that CMake (correctly) believes this file belongs in D:/src/game-engine/Client/AndroidClient/engine/build/intermediates/cmake/release/obj/armeabi-v7a/libfmod.so.

  1. Moving along, let’s look at the reply from the cmake file api.
# target-fmod-Release-<hash>.json
	"artifacts" : 
			"path" : "D:/src/game-engine/Client/AndroidClient/engine/build/intermediates/cmake/release/obj/armeabi-v7a/libfmod.so"

Looks correct. Now for engine, which depends on libfmod.so.

# target-engine-Release-<hash>.json
	"artifacts" : 
			"path" : "D:/src/game-engine/Client/AndroidClient/engine/build/intermediates/cmake/release/obj/armeabi-v7a/libengine.so"

	"link" : 
		"commandFragments" : 
				"fragment" : "-Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments   -Xlinker --no-map-whole-files",
				"role" : "flags"
				"backtrace" : 2,
				"fragment" : "..\\..\\..\\..\\..\\build\\intermediates\\cmake\\release\\obj\\armeabi-v7a\\libfmod.so",
				"role" : "libraries"

The Problem
Ok… Finally I can demonstrate the problem. There are 5 ..\'s in the fragment above. Starting from the folder containing the CMakeCache.txt / build.ninja and resolving the aforementioned relative path we end up with

# Reply from file api
# What CMake believes the path to be at configure time.

I tried creating a minimal reproducer of this, but in all of my attempts, the fragment gets output with an absolute path, not a relative path. So it isn’t clear to me how to force it to generate an absolute path, or why it might be generating an incorrect relative path.

Any suggestions on whether this is a valid bug and if there’s anything I can do to help create a smaller reproducer?

1 Like

@brad.king Looks like a potential file-api issue.

Yes. I’ve reproduced the problem and opened CMake Issue 22301 to track it.

Thanks for the quick response and action :slight_smile: