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

**URL:** <https://discourse.cmake.org/t/possible-bug-in-cmake-file-api-related-to-relative-paths/3552>\
**Category:** Usage\
**Created:** [June 10, 2021, 7:07pm UTC](https://discourse.cmake.org/t/possible-bug-in-cmake-file-api-related-to-relative-paths/3552 "2021-06-10T19:07:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zachary\_Turner](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zachary_turner/32/1551_2.png) [@Zachary\_Turner](https://discourse.cmake.org/u/Zachary_Turner)\
**Post date:** [June 10, 2021, 7:07pm UTC](https://discourse.cmake.org/t/possible-bug-in-cmake-file-api-related-to-relative-paths/3552/1 "2021-06-10T19:07:15Z")

</div>

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](https://cmake.org/cmake/help/git-stage/manual/cmake-file-api.7.html) 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:

```auto
D:
 |_ 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:

```auto
cmake.exe 
    -HD:\src\game-engine\Client 
    "-DCMAKE_CXX_FLAGS=-fexceptions -frtti" 
    -DCMAKE_FIND_ROOT_PATH=D:\src\game-engine\Client\AndroidClient\engine\.cxx\cmake\release\prefab\armeabi-v7a\prefab 
    -DCMAKE_BUILD_TYPE=Release 
    -DCMAKE_TOOLCHAIN_FILE=C:\Users\divis\AppData\Local\Android\Sdk\ndk\21.3.6528147\build\cmake\android.toolchain.cmake 
    -DANDROID_ABI=armeabi-v7a 
    -DANDROID_NDK=C:\Users\divis\AppData\Local\Android\Sdk\ndk\21.3.6528147 
    -DANDROID_PLATFORM=android-19 
    -DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a 
    -DCMAKE_ANDROID_NDK=C:\Users\divis\AppData\Local\Android\Sdk\ndk\21.3.6528147 
    -DCMAKE_EXPORT_COMPILE_COMMANDS=ON 
    -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=D:\src\game-engine\Client\AndroidClient\engine\build\intermediates\cmake\release\obj\armeabi-v7a 
    -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=D:\src\game-engine\Client\AndroidClient\engine\build\intermediates\cmake\release\obj\armeabi-v7a 
    -DCMAKE_SYSTEM_NAME=Android 
    -DCMAKE_SYSTEM_VERSION=19 
    -DANDROID_TOOLCHAIN=clang 
    -DANDROID_STL=c++_static 
    -DANDROID_ARM_NEON=TRUE 
    -DANDROID_PIE=TRUE 
    -DCMAKE_VERBOSE_MAKEFILE=FALSE 
    -DBUILD_TESTS=OFF 
    -BD:\src\game-engine\Client\AndroidClient\engine\.cxx\cmake\release\armeabi-v7a 
    -GNinja

```

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:

```auto
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:

```auto
$ 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"
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.

```auto
# 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`.

```auto
# 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

```auto
# Reply from file api
D:\src\game-engine\Client\AndroidClient\build\intermediates\cmake\release\obj\armeabi-v7a\libfmod.so
# What CMake believes the path to be at configure time.
D:\src\game-engine\Client\AndroidClient\engine\build\intermediates\cmake\release\obj\armeabi-v7a\libfmod.so

```

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?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [June 11, 2021, 11:30am UTC](https://discourse.cmake.org/t/possible-bug-in-cmake-file-api-related-to-relative-paths/3552/2 "2021-06-11T11:30:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [June 11, 2021, 2:27pm UTC](https://discourse.cmake.org/t/possible-bug-in-cmake-file-api-related-to-relative-paths/3552/3 "2021-06-11T14:27:46Z")

</div>

Yes. I’ve reproduced the problem and opened [CMake Issue 22301](https://gitlab.kitware.com/cmake/cmake/-/issues/22301) to track it.

---

<div class="post-metadata">

**Author:** ![Zachary\_Turner](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zachary_turner/32/1551_2.png) [@Zachary\_Turner](https://discourse.cmake.org/u/Zachary_Turner)\
**Post date:** [June 11, 2021, 4:56pm UTC](https://discourse.cmake.org/t/possible-bug-in-cmake-file-api-related-to-relative-paths/3552/4 "2021-06-11T16:56:20Z")

</div>

Thanks for the quick response and action 🙂
