gen_id raw

   1  #!/usr/bin/env bash
   2  
   3  # Usage: env [ CC=... ] [ C_STANDARD=...] [ CXX=... ] [CXX_STANDARD=...] \
   4  #            [ CPPFLAGS=... ] [CFLAGS=...] [CXXFLAGS=...] [LDFLAGS=...] \
   5  #            [ AR=... ] [ NM=... ] [ RANLIB=... ] [ STRIP=... ] [ DEBUG=... ] \
   6  #            [ LTO=... ] ./gen_id [ID_SALT]...
   7  #
   8  # Prints to stdout a SHA256 hash representing the current toolset, used by
   9  # depends/Makefile as a build id for caching purposes (detecting when the
  10  # toolset has changed and the cache needs to be invalidated).
  11  #
  12  # If the DEBUG environment variable is non-empty and the system has `tee`
  13  # available in its $PATH, the pre-image to the SHA256 hash will be printed to
  14  # stderr. This is to help developers debug caching issues in depends.
  15  
  16  # This script explicitly does not `set -e` because id determination is mostly
  17  # opportunistic: it is fine that things fail, as long as they fail consistently.
  18  
  19  # Command variables (CC/CXX/AR) which can be blank are invoked with `bash -c`,
  20  # because the "command not found" error message printed by shells often include
  21  # the line number, like so:
  22  #
  23  #     ./depends/gen_id: line 43: --version: command not found
  24  #
  25  # By invoking with `bash -c`, we ensure that the line number is always 1
  26  
  27  (
  28      # Redirect stderr to stdout
  29      exec 2>&1
  30  
  31      # Unset SOURCE_DATE_EPOCH to prevent it from leaking into tool
  32      # outputs and to maximize reuse of the built package cache.
  33      unset SOURCE_DATE_EPOCH
  34  
  35      echo "BEGIN ALL"
  36  
  37      # Include any ID salts supplied via command line
  38      echo "BEGIN ID SALT"
  39      echo "$@"
  40      echo "END ID SALT"
  41  
  42      echo "BEGIN FLAGS"
  43      echo "CPPFLAGS=${CPPFLAGS}"
  44      echo "CFLAGS=${CFLAGS}"
  45      echo "CXXFLAGS=${CXXFLAGS}"
  46      echo "LDFLAGS=${LDFLAGS}"
  47      echo "END FLAGS"
  48  
  49      # GCC only prints COLLECT_LTO_WRAPPER when invoked with just "-v", but we want
  50      # the information from "-v -E -" as well, so just include both.
  51      echo "BEGIN CC"
  52      bash -c "${CC} -v"
  53      bash -c "${CC} -v -E -xc -o /dev/null - < /dev/null"
  54      bash -c "${CC} -v -E -xobjective-c -o /dev/null - < /dev/null"
  55      echo "C_STANDARD=${C_STANDARD}"
  56      echo "END CC"
  57  
  58      echo "BEGIN CXX"
  59      bash -c "${CXX} -v"
  60      bash -c "${CXX} -v -E -xc++ -o /dev/null - < /dev/null"
  61      bash -c "${CXX} -v -E -xobjective-c++ -o /dev/null - < /dev/null"
  62      echo "CXX_STANDARD=${CXX_STANDARD}"
  63      echo "END CXX"
  64  
  65      # We use lld when cross-compiling for macOS, and it's version should
  66      # be tied to LLVM. However someone compiling with GCC and -fuse-ld=lld
  67      # would not see a cache bust if the LLVM toolchain was updated.
  68      echo "BEGIN lld"
  69      bash -c "ld.lld --version"
  70      echo "END lld"
  71  
  72      echo "BEGIN mold"
  73      bash -c "mold --version"
  74      echo "END mold"
  75  
  76      echo "BEGIN AR"
  77      bash -c "${AR} --version"
  78      env | grep '^AR_'
  79      echo "END AR"
  80  
  81      echo "BEGIN NM"
  82      bash -c "${NM} --version"
  83      env | grep '^NM_'
  84      echo "END NM"
  85  
  86      echo "BEGIN RANLIB"
  87      bash -c "${RANLIB} --version"
  88      env | grep '^RANLIB_'
  89      echo "END RANLIB"
  90  
  91      echo "BEGIN STRIP"
  92      bash -c "${STRIP} --version"
  93      env | grep '^STRIP_'
  94      echo "END STRIP"
  95  
  96      echo "BEGIN LTO"
  97      echo "LTO=${LTO}"
  98      echo "END LTO"
  99  
 100      echo "END ALL"
 101  ) | if [ -n "$DEBUG" ] && command -v tee > /dev/null 2>&1; then
 102          # When debugging and `tee` is available, output the preimage to stderr
 103          # in addition to passing through stdin to stdout
 104          tee >(cat 1>&2)
 105      else
 106          # Otherwise, passthrough stdin to stdout
 107          cat
 108      fi | ${SHA256SUM} - | cut -d' ' -f1
 109