github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/librdkafka_vendor/bundle-import.sh (about)

     1  #!/bin/bash
     2  #
     3  # Updates the bundled prebuilt librdkafka libraries to specified version.
     4  #
     5  
     6  set -e
     7  
     8  
     9  usage() {
    10      echo "Usage: $0 librdkafka-static-bundle-<VERSION>.tgz"
    11      echo ""
    12      echo "This tool must be run from the TOPDIR/kafka/librdkafka_vendor directory"
    13      exit 1
    14  }
    15  
    16  
    17  # Parse dynamic libraries from linker command line.
    18  # Will print a list matching -lfoo and -framework X..
    19  parse_dynlibs() {
    20      local libs=
    21      while [[ $# -gt 0 ]]; do
    22          if [[ $1 == -l* ]]; then
    23              libs="${libs} $1"
    24          elif [[ $1 == -framework ]]; then
    25              libs="${libs} $1 $2"
    26              shift # remove one (extra) arg
    27          fi
    28          shift # remove one arg
    29      done
    30  
    31      echo "$libs"
    32  }
    33  
    34  # Parse dynamic library dependecies from pkg-config file and print
    35  # them to stdout.
    36  parse_pc_dynlibs() {
    37      local pc=$1
    38      parse_dynlibs $(sed -n 's/^Libs: \(..*\)/\1/p' "$pc")
    39  }
    40  
    41  setup_build() {
    42      # Copies static library from the temp directory into final location,
    43      # extracts dynamic lib list from the pkg-config file,
    44      # and generates the build_..go file
    45      local btype=$1
    46      local apath=$2
    47      local pc=$3
    48      local srcinfo=$4
    49      local build_tag=
    50      local gpath="../build_${btype}.go"
    51      local dpath="librdkafka_${btype}.a"
    52  
    53      if [[ $btype == glibc_linux ]]; then
    54          build_tag="// +build !musl"
    55      elif [[ $btype == musl_linux ]]; then
    56          build_tag="// +build musl"
    57      fi
    58  
    59      local dynlibs=$(parse_pc_dynlibs $pc)
    60  
    61      echo "Copying $apath to $dpath"
    62      cp "$apath" "$dpath"
    63  
    64      echo "Generating $gpath (extra build tag: $build_tag)"
    65  
    66      cat >$gpath <<EOF
    67  // +build !dynamic
    68  $build_tag
    69  
    70  // This file was auto-generated by librdkafka_vendor/bundle-import.sh, DO NOT EDIT.
    71  
    72  package kafka
    73  
    74  // #cgo CFLAGS: -DUSE_VENDORED_LIBRDKAFKA -DLIBRDKAFKA_STATICLIB
    75  // #cgo LDFLAGS: \${SRCDIR}/librdkafka_vendor/${dpath} $dynlibs
    76  import "C"
    77  
    78  // LibrdkafkaLinkInfo explains how librdkafka was linked to the Go client
    79  const LibrdkafkaLinkInfo = "static ${btype} from ${srcinfo}"
    80  EOF
    81  
    82      git add "$dpath" "$gpath"
    83  
    84  }
    85  
    86  
    87  bundle="$1"
    88  [[ -f $bundle ]] || usage
    89  
    90  bundlename=$(basename "$bundle")
    91  
    92  bdir=$(mktemp -d tmpXXXXXX)
    93  
    94  echo "Extracting bundle $bundle:"
    95  tar -xzvf "$bundle" -C "$bdir/"
    96  
    97  echo "Copying librdkafka files"
    98  for f in rdkafka.h LICENSES.txt ; do
    99      cp $bdir/$f . || true
   100      git add "$f"
   101  done
   102  
   103  
   104  for btype in glibc_linux musl_linux darwin_amd64 darwin_arm64 windows ; do
   105      lib=$bdir/librdkafka_${btype}.a
   106      pc=${lib/%.a/.pc}
   107      [[ -f $lib ]] || (echo "Expected file $lib missing" ; exit 1)
   108      [[ -f $pc ]] || (echo "Expected file $pc missing" ; exit 1)
   109  
   110      setup_build $btype $lib $pc $bundlename
   111  done
   112  
   113  rm -rf "$bdir"
   114  
   115  echo "All done"