Last active
March 29, 2019 17:55
-
-
Save pkcpkc/3c08c8a4f62aa2f9807c4a5690da234d to your computer and use it in GitHub Desktop.
iOS/sh: Extract sub-libraries from a given library without access to source code to avoid namespace clashes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# We at WeltN24 were facing the problem that two libraries that we integrated | |
# in our iOS project, used the same sub-library which resulted in naming | |
# conflicts due to the non-existing namespaces in Swift. | |
# | |
# If you know what you are doing you can try to solve this conflict by | |
# extracting the shared sub-library from one of the libraries you want to | |
# integrate. You can even do this if you don't have access to the source code | |
# using the lipo (create or operate on universal files) and ar (create and | |
# maintain library archives) commands. | |
# | |
# Why you should know what you are doing? | |
# Manipulating unknown archives can result in unforseen effects because of the | |
# fact that you don't know anything about how the provider of the archive uses | |
# the sub-library, which version of the library was used or if the source | |
# code of the library was manipulated. So... | |
# You should know what you are doing manipulting archives :) | |
# | |
# The script is processing the files in following order: | |
# 1. Extracting architecture slices of multi-architecture file $src_lib | |
# 2. Excluding library archives which match regular expression $reg_exp_remove | |
# 3. Writing new multi-architecture file $dst_lib | |
# | |
# Usage example: | |
# ./strip-lib.sh sourceLib.a targetLib.a "MyRegularExpression" | |
# | |
# Initial script and bash brain: https://github.com/bonkey | |
# Generalization and eager padawan: https://github.com/pkcpkc (Paul.Hackenberger@weltn24.de) | |
test $# -ne 3 && echo "strip-lib.sh <sourceLibrary.a> <targetLibrary.a> <regExpRemove>" && exit 1 | |
tmp_prefix="strip-tmp-" | |
src_lib=$1 | |
dst_lib=$2 | |
reg_exp_remove=$3 | |
# identify supported architectures | |
archs=$(lipo -info $src_lib | sed 's/.*: \(.*\)/\1/') | |
archs_array=("${(@s/ /)archs}") | |
replace_args=() | |
for arch in $archs_array ; do | |
thin_lib=${tmp_prefix}${arch}.a | |
lipo $src_lib -thin $arch -output $thin_lib | |
echo "Removing from $arch:" | |
ar -t $thin_lib | grep -E "$reg_exp_remove" | |
ar -t $thin_lib | grep -E "$reg_exp_remove" | xargs ar -d $thin_lib | |
replace_args=($replace_args -replace $arch $thin_lib) | |
done | |
echo "\nWriting $dst_lib." | |
lipo $src_lib $replace_args -output $dst_lib | |
echo "\nRemoving tmp files." | |
for arch in $archs_array ; do | |
rm ${tmp_prefix}${arch}.a | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment