msvc: copy_pdb_idb script doesn't depend on the pch file, which can cause linker crashes
Initially reported as https://developercommunity.visualstudio.com/t/ICE-when-using-LTCGPCH-after-adding-a-f/10267604
CMake's REUSE_FROM
precompiled headers functionality has a bug for MSVC compilers that affects incremental compiles with LTCG, causing a compiler crash. While Microsoft should likely issue a error instead of having the compiler crash, it seems like the root of the error is from CMake itself. The bug can easily be reproduced with instructions from https://github.com/russelltg/VS2022-Corrupted-PDB-bug/ (if it's a good fit for a test case, I'm happy to add it as one. It's a tad complex though)
It seems the bad order of operations is:
- generate shared PCH+PDB
- copy shared PDB into library dir (this is the
copy_idb_pdb
script) - build library, which appends to this PDB file
- link library
- notably, this also links to the
cmake_pch.cxx.obj
file, which loads up the original PDB file
- notably, this also links to the
- add a new function to the struct
- generate shared PCH+PDB
- shared PDB is not copied
- build library, which continues appending to that old PDB file, which has the old definition of the struct
- link library, which loads both the library and non-library PDB file, which have conflicting definitions of this struct, and linker crashes
!8787 (merged) fixes the situation by copying the shared PDB when the PCH file changes, which results in PDB files with non-contradictory debug information.
NOTE: an early solution had it depend on the PDB file directly. However, this does not work as no rule is actually setup to generate that PDB file, so the PCH file was chosen instead. I think the pch obj file would also work fine.