mac-codesign.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/bash
  2. # This script signs a specific object with the specified identity, entitlements,
  3. # and optional flags. If the target is a bundle, it will also sign all frameworks
  4. # and dylibs within the bundle.
  5. set -eu
  6. function usage() {
  7. echo "Usage: $0 [-t] [-e <entitlements file or "preserve">] <identity> <target to codesign>"
  8. exit 1
  9. }
  10. USE_SECURE_TIMESTAMP=0
  11. ENTITLEMENTS_FILE=""
  12. while getopts ":te:" opt; do
  13. case $opt in
  14. t)
  15. USE_SECURE_TIMESTAMP=1
  16. ;;
  17. e)
  18. ENTITLEMENTS_FILE=$OPTARG
  19. ;;
  20. \?)
  21. usage
  22. ;;
  23. esac
  24. done
  25. if [ $USE_SECURE_TIMESTAMP -eq 1 ]; then
  26. TIMESTAMP_FLAG="--timestamp"
  27. else
  28. TIMESTAMP_FLAG="--timestamp=none"
  29. fi
  30. shift $((OPTIND - 1))
  31. if [ $# -ne 2 ]; then
  32. usage
  33. fi
  34. IDENTITY=$1
  35. TARGET_PATH=$2
  36. # Signs the given target with the specified identity and optional flags.
  37. function sign() {
  38. /usr/bin/codesign -f -s "$IDENTITY" $TIMESTAMP_FLAG ${2:-} "$1"
  39. }
  40. if [ -d "$TARGET_PATH" ]; then
  41. # Newlines are the only valid separator character in find's output.
  42. IFS=$'\n'
  43. for framework in $(find "$TARGET_PATH" -depth -not -path "*/Helpers/*" -name '*.dylib' -or -name '*.framework'); do
  44. sign "$framework"
  45. done
  46. unset IFS
  47. fi
  48. TARGET_EXTRA_CODESIGN_FLAGS="-o runtime"
  49. if [ -n "$ENTITLEMENTS_FILE" ]; then
  50. # "preserve" is a special keyword which tells us we should preserve the
  51. # existing entitlements in the target.
  52. if [ "$ENTITLEMENTS_FILE" == "preserve" ]; then
  53. TARGET_EXTRA_CODESIGN_FLAGS+=" --preserve-metadata=entitlements"
  54. else
  55. TARGET_EXTRA_CODESIGN_FLAGS+=" --entitlements $ENTITLEMENTS_FILE"
  56. fi
  57. fi
  58. sign "$TARGET_PATH" "$TARGET_EXTRA_CODESIGN_FLAGS"