Skip to content

[libc++] Remap headers in the debug info when building the library #143004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2025

Conversation

ldionne
Copy link
Member

@ldionne ldionne commented Jun 5, 2025

The libc++ build includes a step where headers are generated. This is required in order to preprocess some files such as the assertion handler and the __config_site header. As a result, the library is built against headers located inside the build directory, and the path to those headers is what's included in the debug information of the library.

However, these headers in the build directory are usually not persistent, which means that the debug information might end up referring to headers that don't exist anymore. To solve this problem, this patch uses the -fdebug-prefix-map flag supported by Clang and GCC to remap the generated headers to the original headers in the source directory. This provides the illusion that the library was truly built against the in-source version of the headers.

The libc++ build includes a step where headers are generated. This is
required in order to preprocess some files such as the assertion handler
and the __config_site header. As a result, the library is built against
headers located inside the build directory, and the path to those headers
is what's included in the debug information of the library.

However, these headers in the build directory are usually not persistent,
which means that the debug information might end up referring to headers
that don't exist anymore. To solve this problem, this patch uses the
-fdebug-prefix-map flag supported by Clang and GCC to remap the generated
headers to the original headers in the source directory. This provides
the illusion that the library was truly built against the in-source
version of the headers.
@ldionne ldionne requested a review from a team as a code owner June 5, 2025 16:23
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jun 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2025

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

Changes

The libc++ build includes a step where headers are generated. This is required in order to preprocess some files such as the assertion handler and the __config_site header. As a result, the library is built against headers located inside the build directory, and the path to those headers is what's included in the debug information of the library.

However, these headers in the build directory are usually not persistent, which means that the debug information might end up referring to headers that don't exist anymore. To solve this problem, this patch uses the -fdebug-prefix-map flag supported by Clang and GCC to remap the generated headers to the original headers in the source directory. This provides the illusion that the library was truly built against the in-source version of the headers.


Full diff: https://github.com/llvm/llvm-project/pull/143004.diff

1 Files Affected:

  • (modified) libcxx/include/CMakeLists.txt (+5)
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 43cefd5600646..52611e43968bc 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -1706,6 +1706,11 @@ add_dependencies(cxx-headers generate-cxx-headers)
 target_include_directories(cxx-headers INTERFACE ${LIBCXX_GENERATED_INCLUDE_TARGET_DIR}
                                                  ${LIBCXX_GENERATED_INCLUDE_DIR})
 
+# Make sure to map the generated include directory back to libc++'s actual source directory when generating
+# debug information. Otherwise, the debug information will refer to generated headers which are created during
+# the build and generally not persistent.
+target_add_compile_flags_if_supported(cxx-headers INTERFACE "-fdebug-prefix-map=${LIBCXX_GENERATED_INCLUDE_DIR}=${LIBCXX_SOURCE_DIR}/include")
+
 if (LIBCXX_INSTALL_HEADERS)
   foreach(file ${files})
     get_filename_component(dir ${file} DIRECTORY)

@ldionne ldionne merged commit 4d50b40 into llvm:main Jun 9, 2025
181 of 191 checks passed
@ldionne ldionne deleted the review/debug-symbol-remapping branch June 9, 2025 20:52
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
…lvm#143004)

The libc++ build includes a step where headers are generated. This is
required in order to preprocess some files such as the assertion handler
and the __config_site header. As a result, the library is built against
headers located inside the build directory, and the path to those
headers is what's included in the debug information of the library.

However, these headers in the build directory are usually not
persistent, which means that the debug information might end up
referring to headers that don't exist anymore. To solve this problem,
this patch uses the -fdebug-prefix-map flag supported by Clang and GCC
to remap the generated headers to the original headers in the source
directory. This provides the illusion that the library was truly built
against the in-source version of the headers.
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
…lvm#143004)

The libc++ build includes a step where headers are generated. This is
required in order to preprocess some files such as the assertion handler
and the __config_site header. As a result, the library is built against
headers located inside the build directory, and the path to those
headers is what's included in the debug information of the library.

However, these headers in the build directory are usually not
persistent, which means that the debug information might end up
referring to headers that don't exist anymore. To solve this problem,
this patch uses the -fdebug-prefix-map flag supported by Clang and GCC
to remap the generated headers to the original headers in the source
directory. This provides the illusion that the library was truly built
against the in-source version of the headers.
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
…lvm#143004)

The libc++ build includes a step where headers are generated. This is
required in order to preprocess some files such as the assertion handler
and the __config_site header. As a result, the library is built against
headers located inside the build directory, and the path to those
headers is what's included in the debug information of the library.

However, these headers in the build directory are usually not
persistent, which means that the debug information might end up
referring to headers that don't exist anymore. To solve this problem,
this patch uses the -fdebug-prefix-map flag supported by Clang and GCC
to remap the generated headers to the original headers in the source
directory. This provides the illusion that the library was truly built
against the in-source version of the headers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants