The AddLLVM.cmake file exposes a function find_first_existing_vc_file which in case of git repository looks for .git/logs/HEAD. This file can be missing when repository is checked out using repo tool. So, the function tries to create an empty file in the same directory. This can fail if the source directory is read only. E.g. if the llvm source directory is mounted inside docker as ro (read-only).
E.g. error reported,
CMake Error at /src/external/llvm-project/llvm/cmake/modules/AddLLVM.cmake:1940 (file):
file failed to open for writing (Read-only file system):
/src/external/llvm-project/.git/logs/HEAD
Call Stack (most recent call first):
/src/external/llvm-project/clang/lib/Basic/CMakeLists.txt:7 (find_first_existing_vc_file)
Two solutions are proposed,
- Do not depend on creating .git/logs/HEAD. Instead just skip it. This requires rebuilding VCSRevision header for every build.
- Gracefully handle file(WRITE ...) error, which this patch is providing. No existing behaviour changes.
For more context,
https://reviews.llvm.org/D31985
https://reviews.llvm.org/D57063
Nit: can you add a comment indicating this is never expected to exist, and is just used to force regeneration for the read-only case? Otherwise it may not be obvious to someone reading what the trick here is.