github.com/weaveworks/common@v0.0.0-20230728070032-dd9e68f319d5/tools/build/haskell/copy-libraries (about)

     1  #!/bin/bash
     2  #
     3  # Copy dynamically linked libraries for a binary, so we can assemble a Docker
     4  # image.
     5  #
     6  # Run with:
     7  #   copy-libraries /path/to/binary /output/dir
     8  #
     9  # Dependencies:
    10  # - awk
    11  # - cp
    12  # - grep
    13  # - ldd
    14  # - mkdir
    15  
    16  set -o errexit
    17  set -o nounset
    18  set -o pipefail
    19  
    20  # Path to a Linux binary that we're going to run in the container.
    21  binary_path="${1}"
    22  # Path to directory to write the output to.
    23  output_dir="${2}"
    24  
    25  exe_name=$(basename "${binary_path}")
    26  
    27  # Identify linked libraries.
    28  libraries=($(ldd "${binary_path}" | awk '{print $(NF-1)}' | grep -v '=>'))
    29  # Add /bin/sh, which we need for Docker imports.
    30  libraries+=('/bin/sh')
    31  
    32  mkdir -p "${output_dir}"
    33  
    34  # Copy executable and all needed libraries into temporary directory.
    35  cp "${binary_path}" "${output_dir}/${exe_name}"
    36  for lib in "${libraries[@]}"; do
    37      mkdir -p "${output_dir}/$(dirname "$lib")"
    38      # Need -L to make sure we get actual libraries & binaries, not symlinks to
    39      # them.
    40      cp -L "${lib}" "${output_dir}/${lib}"
    41  done