Skip to content

Instantly share code, notes, and snippets.

@jutoart
Forked from kam800/check_duplicated_classes.sh
Created February 11, 2019 03:47
Show Gist options
  • Save jutoart/6ef3444a1e0a2dcd0dc3f1fce7ef8bbb to your computer and use it in GitHub Desktop.
Save jutoart/6ef3444a1e0a2dcd0dc3f1fce7ef8bbb 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment