invalid cmake_pch.hxx generated when targeting Android on Windows and source dir is passed without drive letter
Consider the attached example project precompile_header.7z. It's CMakeLists.txt is
cmake_minimum_required(VERSION 3.15.0)
project(precompile_header LANGUAGES CXX)
add_executable(precompile_header main.cpp)
target_precompile_headers(precompile_header PRIVATE header.h)
Open a MinGW shell on Windows and pass an Android toolchain file. Pass the project directory as absolute path but without drive letter.
cd some\build\dir
cmake -DCMAKE_TOOLCHAIN_FILE=C:\android\sdk\ndk\22.1.7171670\build\cmake\android.toolchain.cmake -GNinja \playground\precompile_header
Building results in an error:
C:\android\sdk\ndk\22.1.7171670\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi16 --gcc-toolchain=C:/android/sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/android/sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/windows-x86_64/sysroot -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -fPIE -Winvalid-pch -fpch-instantiate-templates -Xclang -emit-pch -Xclang -include -Xclang D:/playground/precompile_header/build/CMakeFiles/precompile_header.dir/cmake_pch.hxx -x c++-header -MD -MT CMakeFiles/precompile_header.dir/cmake_pch.hxx.pch -MF CMakeFiles\precompile_header.dir\cmake_pch.hxx.pch.d -o CMakeFiles/precompile_header.dir/cmake_pch.hxx.pch -c D:/playground/precompile_header/build/CMakeFiles/precompile_header.dir/cmake_pch.hxx.cxx
In file included from <built-in>:1:
D:/playground/precompile_header/build/CMakeFiles/precompile_header.dir/cmake_pch.hxx:5:10: fatal error: '/playground/precompile_header/header.h' file not found
#include "/playground/precompile_header/header.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
ninja: build stopped: subcommand failed.
The reason being that the CMake-generated cmake_pch.hxx
contains this include directive:
#include "/playground/precompile_header/header.h"
which cannot be handled by the NDK's compiler.
Adding the drive letter to the include directive fixes the build
#include "d:/playground/precompile_header/header.h"
Passing the project directory with an absolute path with drive letter works.
Passing the project directory with relative path works.
I cannot reproduce this with Desktop MinGW nor MSVC.