[llvm] [cmake] Fix relative paths in prefix map (PR #74132)

Paul Kirth via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 1 11:32:33 PST 2023


https://github.com/ilovepi created https://github.com/llvm/llvm-project/pull/74132

When building debug version of LLVM with `LLVM_USE_RELATIVE_PATHS_IN_FILES=On` would cause source paths to be incorrect, and be prefixed by the build directory.  This lead to source locations like the following: `../build/llvm/...`. Such paths do not exist, and existing debuggers can't adjust their search location because of the incorrect prefix. Ultimately, this happened because the relative path creation goes in the wrong direction, from source-dir to build-dir instead of from build-dir to source-dir.

This patch swaps the directionality of the relative paths so that they get a proper prefix from the build directory. Given a build dir at `/build` and a project directory at `/llvm-project`, we get source locations like: `../llvm-project/llvm/lib/Transforms/...`, which a debugger can resolve once pointed to the correct project directory.

>From 56708954fba18375c5a5eb70fa9f02163a6789e0 Mon Sep 17 00:00:00 2001
From: Paul Kirth <paulkirth at google.com>
Date: Tue, 29 Aug 2023 09:04:54 -0700
Subject: [PATCH] Fix relative paths in prefix map

Previously, building a debug version of LLVM when -ffile-prefix-map
would cause llvm project paths to be prefixed by the build directory,
so source locations would be "../build/llvm/...". This happened because
the relative path went in the wrong direction, from source-dir to
build-dir instead of from build-dir to source-dir.

This patch swaps the directionality of the relative paths so that they
get a proper prefix from the build directory. Given a build dir at
/build and a project directory at /llvm-project, we get source locations
like: "../llvm-project/llvm/lib/Transforms/...", which a debugger can
resolve once pointed to the correct directory.
---
 llvm/cmake/modules/HandleLLVMOptions.cmake | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake
index 6a3c49edc912ded..97fd908d08f176b 100644
--- a/llvm/cmake/modules/HandleLLVMOptions.cmake
+++ b/llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -1341,7 +1341,7 @@ if(LLVM_USE_RELATIVE_PATHS_IN_DEBUG_INFO)
   else()
     set(source_root "${LLVM_MAIN_SRC_DIR}")
   endif()
-  file(RELATIVE_PATH relative_root "${source_root}" "${CMAKE_BINARY_DIR}")
+  file(RELATIVE_PATH relative_root "${CMAKE_BINARY_DIR}" "${source_root}")
   append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${CMAKE_BINARY_DIR}=${relative_root}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
   append_if(SUPPORTS_FDEBUG_PREFIX_MAP "-fdebug-prefix-map=${source_root}/=${LLVM_SOURCE_PREFIX}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
   add_flag_if_supported("-no-canonical-prefixes" NO_CANONICAL_PREFIXES)
@@ -1356,7 +1356,7 @@ if(LLVM_USE_RELATIVE_PATHS_IN_FILES)
   else()
     set(source_root "${LLVM_MAIN_SRC_DIR}")
   endif()
-  file(RELATIVE_PATH relative_root "${source_root}" "${CMAKE_BINARY_DIR}")
+  file(RELATIVE_PATH relative_root "${CMAKE_BINARY_DIR}" "${source_root}")
   append_if(SUPPORTS_FFILE_PREFIX_MAP "-ffile-prefix-map=${CMAKE_BINARY_DIR}=${relative_root}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
   append_if(SUPPORTS_FFILE_PREFIX_MAP "-ffile-prefix-map=${source_root}/=${LLVM_SOURCE_PREFIX}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
   add_flag_if_supported("-no-canonical-prefixes" NO_CANONICAL_PREFIXES)



More information about the llvm-commits mailing list