Xcode indexer struggles with cmake generated projects with pch
When enabling xcode indexing logging
defaults write com.apple.dt.Xcode IDEIndexShowLog -bool YES
and trying with minimal project with a precompiled header
$ cat > main.cpp <<EOS
int main() { return RET; }
EOS
$ cat > prefix.pch <<EOS
#define RET 0
EOS
$ cat > CMakeLists.txt <<EOS
cmake_minimum_required(VERSION 3.26)
project(cmake-pch-test)
add_executable(cmake-pch-test main.cpp)
target_precompile_headers(cmake-pch-test PUBLIC prefix.pch)
EOS
$ cmake -B build -G Xcode
$ open build/*.xcodeproj
and opening the xcode project, in the "report navigator" tab you can see that xcode's indexing bit keeps failing (you might need to trigger a save on main.cpp
to make it try).
The error looks something like:
<built-in>:1:10: fatal error: '/Users/mikko.lehtonen/Library/Developer/Xcode/DerivedData/cmake-pch-test-dxybofmxsvntwldnisinbvaymqeu/Index.noindex/PrecompiledHeaders/cmake_pch.hxx-6033049555023640402' file not found
#include "/Users/mikko.lehtonen/Library/Developer/Xcode/DerivedData/cmake-pch-test-dxybofmxsvntwldnisinbvaymqeu/Index.noindex/PrecompiledHeaders/cmake_pch.hxx-6033049555023640402"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
error: Command failed with exit code 1
And indeed, the PrecompiledHeaders
folder is empty.
Honestly, I believe this is probably xcode indexer bug as the project builds otherwise fine, but then I tried to recreate the project with xcode only as “control”, and it does not fail in this manner. So I'm concluding that something in the cmake generated projects trips up xcode's indexer.
In the control project I notice that it is slightly different path it tries to search the pch in the indexing. I tried to harmonize all kinds of build settings/paths with the test project and control project, and I couldn't manage to fix it in the test project nor brake the control project in this manner.
This is annoying when much of the editor features work inconsistently.
Versions
$ xcodebuild -version
Xcode 14.3
Build version 14E222b
$ sw_vers
ProductName: macOS
ProductVersion: 13.2.1
BuildVersion: 22D68
$ cmake --version
cmake version 3.26.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).