[PATCH] D59127: [CMake] Support stripping and linking output to .build-id directory

Petr Hosek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 7 23:17:57 PST 2019


phosek created this revision.
phosek added reviewers: smeenai, beanz, jakehehrlich.
Herald added subscribers: llvm-commits, jdoerfert, mgorny.
Herald added a reviewer: alexshap.
Herald added a project: LLVM.

When installing runtimes with install-runtimes-stripped, we don't want
to just strip them, we also want to preserve the debugging information
for potential debugging. To make it possible to later find the stripped
debugging information, we want to use the .build-id layout:

https://fedoraproject.org/wiki/RolandMcGrath/BuildID#Find_files_by_build_ID

That is, for libfoo.so with build ID abcdef1234, the debugging information
will be installed into lib/debug/.build-id/ab/cdef1234. llvm-objcopy
already has support for stripping files and linking the debugging
stripped output into the right location. However, CMake doesn't support
customizing strip invocation for the *-stripped targets. So instead, we
replace CMAKE_STRIP with a custom script that invokes llvm-objcopy with
the right command line flags.


Repository:
  rL LLVM

https://reviews.llvm.org/D59127

Files:
  llvm/cmake/modules/LLVMExternalProjectUtils.cmake
  llvm/runtimes/CMakeLists.txt
  llvm/runtimes/llvm-strip-link.in


Index: llvm/runtimes/llvm-strip-link.in
===================================================================
--- /dev/null
+++ llvm/runtimes/llvm-strip-link.in
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import os
+import sys
+import subprocess
+
+
+ELF_MAGIC = '\x7fELF'
+
+with open(sys.argv[1], "rb") as f:
+    buf = f.read(len(ELF_MAGIC))
+    if buf != ELF_MAGIC:
+        sys.exit(0)
+
+llvm_objcopy = os.path.join('@LLVM_RUNTIME_OUTPUT_INTDIR@', 'llvm-objcopy')
+install_dir = os.path.join(os.getenv('DESTDIR', ''), '@CMAKE_INSTALL_PREFIX@')
+link_dir = os.path.join(install_dir, 'lib', 'debug', '.build-id')
+
+sys.exit(subprocess.call([
+    llvm_objcopy,
+    '--strip-sections',
+    '--build-id-link-dir=' + link_dir,
+    '--build-id-link-input=.debug',
+    '--build-id-link-output=',
+    sys.argv[1],
+]))
Index: llvm/runtimes/CMakeLists.txt
===================================================================
--- llvm/runtimes/CMakeLists.txt
+++ llvm/runtimes/CMakeLists.txt
@@ -318,6 +318,12 @@
     list(APPEND runtime_names ${projName})
   endforeach()
 
+  configure_file(
+    ${CMAKE_CURRENT_SOURCE_DIR}/llvm-strip-link.in
+    ${CMAKE_CURRENT_BINARY_DIR}/llvm-strip-link
+    @ONLY
+  )
+
   function(runtime_default_target)
     cmake_parse_arguments(ARG "" "" "DEPENDS;PREFIXES" ${ARGN})
 
@@ -446,6 +452,7 @@
                                         ${${name}_extra_args}
                              PASSTHROUGH_PREFIXES LLVM_ENABLE_RUNTIMES
                              TOOLCHAIN_TOOLS clang lld llvm-ar llvm-ranlib llvm-nm llvm-objcopy llvm-objdump llvm-strip
+                             USE_STRIP ${CMAKE_CURRENT_BINARY_DIR}/llvm-strip-link
                              EXTRA_TARGETS ${${name}_extra_targets}
                                            ${${name}_test_targets}
                              USE_TOOLCHAIN
Index: llvm/cmake/modules/LLVMExternalProjectUtils.cmake
===================================================================
--- llvm/cmake/modules/LLVMExternalProjectUtils.cmake
+++ llvm/cmake/modules/LLVMExternalProjectUtils.cmake
@@ -35,12 +35,14 @@
 #     Extra targets in the subproject to generate targets for
 #   PASSTHROUGH_PREFIXES prefix...
 #     Extra variable prefixes (name is always included) to pass down
+#   USE_STRIP path
+#     Use provided strip tool/script instead of the default one.
 #   )
 function(llvm_ExternalProject_Add name source_dir)
   cmake_parse_arguments(ARG
     "USE_TOOLCHAIN;EXCLUDE_FROM_ALL;NO_INSTALL;ALWAYS_CLEAN"
     "SOURCE_DIR"
-    "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS;PASSTHROUGH_PREFIXES"
+    "CMAKE_ARGS;TOOLCHAIN_TOOLS;RUNTIME_LIBRARIES;DEPENDS;EXTRA_TARGETS;PASSTHROUGH_PREFIXES;USE_STRIP"
     ${ARGN})
   canonicalize_tool_name(${name} nameCanon)
   if(NOT ARG_TOOLCHAIN_TOOLS)
@@ -125,12 +127,16 @@
     if(llvm-objcopy IN_LIST TOOLCHAIN_TOOLS)
       list(APPEND compiler_args -DCMAKE_OBJCOPY=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-objcopy)
     endif()
-    if(llvm-strip IN_LIST TOOLCHAIN_TOOLS)
+    if(llvm-strip IN_LIST TOOLCHAIN_TOOLS AND NOT ARG_USE_STRIP)
       list(APPEND compiler_args -DCMAKE_STRIP=${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-strip)
     endif()
     list(APPEND ARG_DEPENDS ${TOOLCHAIN_TOOLS})
   endif()
 
+  if(ARG_USE_STRIP)
+    list(APPEND compiler_args -DCMAKE_STRIP=${ARG_USE_STRIP})
+  endif()
+
   add_custom_command(
     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${name}-clobber-stamp
     DEPENDS ${ARG_DEPENDS}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59127.189815.patch
Type: text/x-patch
Size: 3505 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190308/b68b48a1/attachment.bin>


More information about the llvm-commits mailing list