Skip to content

Instantly share code, notes, and snippets.

@kam800
Created May 26, 2018 12:22
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kam800/0d04917b4f051c1dd906d629d685f571 to your computer and use it in GitHub Desktop.
Save kam800/0d04917b4f051c1dd906d629d685f571 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script checks the binary and all nested frameworks for duplicated classes (both ObjectiveC and Swift).
# The script fails if it finds any duplicated classes.
function print_classes() {
otool -ov "$1" |
sed -n '/Contents of (__DATA,__objc_classlist)/,/Contents of/p' | # all lines for (__DATA,__objc_classlist) section
egrep '^[0-9]' | # take just a class header
cut -d ' ' -f 3 | # take just a symbol name
sort | # uniq requires sorted data
uniq
}
function print_allClasses() {
print_classes "${EXECUTABLE_NAME}" # print classes for main executable
find Frameworks -name '*.framework' | # and the rest of the frameworks
while read framework_path
do
local framework_name="$(basename -s .framework "${framework_path}")"
print_classes "${framework_path}/${framework_name}"
done
}
function print_countsOfAllClasses() {
print_allClasses |
sort | # uniq requires sorted data
uniq -c # uniq with count printing
}
function print_countsOfDuplicatedClasses() {
print_countsOfAllClasses |
egrep -v '^ *1 ' # skip unique symbols
}
function print_countsOfDuplicatedClassesWithoutIgnores() {
print_countsOfDuplicatedClasses
# To ignore duplicates, use following code.
# print_countsOfDuplicatedClasses |
# grep -v "2 _OBJC_CLASS_$_sampleIgnore1" |
# grep -v "2 _OBJC_CLASS_$_sampleIgnore2" |
# grep -v "2 _OBJC_CLASS_$_sampleIgnore3"
}
function checkDuplicatedClasses() {
local duplicated_files="$(print_countsOfDuplicatedClassesWithoutIgnores)"
if [ -n "${duplicated_files}" ]
then
echo "Duplicated symbols detected:"
echo "${duplicated_files}"
echo "If you want to ignore those symbols, then modify print_countsOfDuplicatedClassesWithoutIgnores() in $(basename $0)"
exit 1
fi
exit 0
}
app_dir="${BUILT_PRODUCTS_DIR}/${EXECUTABLE_FOLDER_PATH}"
cd "${app_dir}" && checkDuplicatedClasses
@tinder-hemanthprasad
Copy link

Thanks for the awesome script. Just FYI, had to remove empty lines from print_allClasses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment