|
| 1 | +require 'dry/cli' |
| 2 | +require 'xcodeproj' |
| 3 | + |
| 4 | +module EmergeCLI |
| 5 | + module Commands |
| 6 | + module Autofixes |
| 7 | + class StripBinarySymbols < EmergeCLI::Commands::GlobalOptions |
| 8 | + desc 'Strip binary symbols from the app' |
| 9 | + |
| 10 | + option :path, type: :string, required: true, desc: 'Path to the xcarchive' |
| 11 | + |
| 12 | + # Constants |
| 13 | + SCRIPT_NAME = 'EmergeTools Strip Binary Symbols'.freeze |
| 14 | + ENABLE_USER_SCRIPT_SANDBOXING = 'ENABLE_USER_SCRIPT_SANDBOXING'.freeze |
| 15 | + INPUT_FILE = '${DWARF_DSYM_FOLDER_PATH}/${EXECUTABLE_NAME}.app.dSYM/' \ |
| 16 | + 'Contents/Resources/DWARF/${EXECUTABLE_NAME}'.freeze |
| 17 | + SCRIPT_CONTENT = %{#!/bin/bash |
| 18 | +set -e |
| 19 | +
|
| 20 | +echo "Starting the symbol stripping process..." |
| 21 | +
|
| 22 | +if [ "Release" = "$\{CONFIGURATION\}" ]; then |
| 23 | + echo "Configuration is Release." |
| 24 | +
|
| 25 | + # Path to the app directory |
| 26 | + APP_DIR_PATH="$\{BUILT_PRODUCTS_DIR\}/$\{EXECUTABLE_FOLDER_PATH\}" |
| 27 | + echo "App directory path: $\{APP_DIR_PATH\}" |
| 28 | +
|
| 29 | + # Strip main binary |
| 30 | + echo "Stripping main binary: $\{APP_DIR_PATH\}/$\{EXECUTABLE_NAME\}" |
| 31 | + strip -rSTx "$\{APP_DIR_PATH\}/$\{EXECUTABLE_NAME\}" |
| 32 | + if [ $? -eq 0 ]; then |
| 33 | + echo "Successfully stripped main binary." |
| 34 | + else |
| 35 | + echo "Failed to strip main binary." >&2 |
| 36 | + fi |
| 37 | +
|
| 38 | + # Path to the Frameworks directory |
| 39 | + APP_FRAMEWORKS_DIR="$\{APP_DIR_PATH\}/Frameworks" |
| 40 | + echo "Frameworks directory path: $\{APP_FRAMEWORKS_DIR\}" |
| 41 | +
|
| 42 | + # Strip symbols from frameworks, if Frameworks/ exists at all |
| 43 | + # ... as long as the framework is NOT signed by Apple |
| 44 | + if [ -d "$\{APP_FRAMEWORKS_DIR\}" ]; then |
| 45 | + echo "Frameworks directory exists. Proceeding to strip symbols from frameworks." |
| 46 | + find "$\{APP_FRAMEWORKS_DIR\}" -type f -perm +111 -maxdepth 2 -mindepth 2 -exec bash -c ' |
| 47 | + codesign -v -R="anchor apple" "\{\}" &> /dev/null || |
| 48 | + ( |
| 49 | + echo "Stripping \{\}" && |
| 50 | + if [ -w "\{\}" ]; then |
| 51 | + strip -rSTx "\{\}" |
| 52 | + if [ $? -eq 0 ]; then |
| 53 | + echo "Successfully stripped \{\}" |
| 54 | + else |
| 55 | + echo "Failed to strip \{\}" >&2 |
| 56 | + fi |
| 57 | + else |
| 58 | + echo "Warning: No write permission for \{\}" |
| 59 | + fi |
| 60 | + ) |
| 61 | + ' \\; |
| 62 | + if [ $? -eq 0 ]; then |
| 63 | + echo "Successfully stripped symbols from frameworks." |
| 64 | + else |
| 65 | + echo "Failed to strip symbols from some frameworks." >&2 |
| 66 | + fi |
| 67 | + else |
| 68 | + echo "Frameworks directory does not exist. Skipping framework stripping." |
| 69 | + fi |
| 70 | +else |
| 71 | + echo "Configuration is not Release. Skipping symbol stripping." |
| 72 | +fi |
| 73 | +
|
| 74 | +echo "Symbol stripping process completed."}.freeze |
| 75 | + |
| 76 | + def call(**options) |
| 77 | + @options = options |
| 78 | + before(options) |
| 79 | + |
| 80 | + raise 'Path must be an xcodeproj' unless @options[:path].end_with?('.xcodeproj') |
| 81 | + raise 'Path does not exist' unless File.exist?(@options[:path]) |
| 82 | + |
| 83 | + Sync do |
| 84 | + project = Xcodeproj::Project.open(@options[:path]) |
| 85 | + |
| 86 | + project.targets.each do |target| |
| 87 | + target.build_configurations.each do |config| |
| 88 | + enable_user_script_sandboxing(config) |
| 89 | + end |
| 90 | + |
| 91 | + add_run_script(target) |
| 92 | + end |
| 93 | + |
| 94 | + project.save |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + private |
| 99 | + |
| 100 | + def enable_user_script_sandboxing(config) |
| 101 | + Logger.info "Enabling user script sandboxing for #{config.name}" |
| 102 | + config.build_settings[ENABLE_USER_SCRIPT_SANDBOXING] = 'NO' |
| 103 | + end |
| 104 | + |
| 105 | + def add_run_script(target) |
| 106 | + phase = target.shell_script_build_phases.find { |item| item.name == SCRIPT_NAME } |
| 107 | + return unless phase.nil? |
| 108 | + Logger.info "Creating script '#{SCRIPT_NAME}'" |
| 109 | + phase = target.new_shell_script_build_phase(SCRIPT_NAME) |
| 110 | + phase.shell_script = SCRIPT_CONTENT |
| 111 | + phase.input_paths = [INPUT_FILE] |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | +end |
0 commit comments