Skip to content

Commit

Permalink
Merge create_patched_sdk*.sh scripts with minor adjustments (#69)
Browse files Browse the repository at this point in the history
* Move scripts to tools subdir

* --help

* Merge create_patched_sdk*.sh scripts

* Flesh out usage

* Replace printf + \n with echo

* Simplify

* Revert 'tools' creation and use

* Fix minor oversights

* Not sure how I managed this ....

* Pretty sure this needs a path

* -_-

* Ensure that both names and paths work for the else cond

* Favor iOS tbd_tool flag setup

* Validate sdk_platform

* Exit if tbd_tool is not executable

* Rename version variable for clarity

* Unify tbd_options and write_options

* Remove extra space

---------

Co-authored-by: Leptos <leptos.0.null@gmail.com>
  • Loading branch information
L1ghtmann and leptos-null authored Jan 1, 2024
1 parent ff2f93a commit ca52092
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 298 deletions.
281 changes: 165 additions & 116 deletions create_patched_sdk.sh
Original file line number Diff line number Diff line change
@@ -1,182 +1,231 @@
#!/usr/bin/env bash

print_usage() {
printf "Usage: $(basename $0) {use_simulator} {sdks_output_path} {overwrite_existing} {no_warnings} {tbd_tool_path} {xcode_installation_path}\n"
printf "Note: {} options are optional to provide, and can be ignored with a '-'\n"
cat << EOF
Usage: $(basename $0) {use_simulator} {sdks_output_path} {no_overwrite} {no_warnings} {tbd_tool} {xcode_installation_path} {sdk_platform}
Options:
- use_simulator (bool)
Dump binaries from simulator runtime as opposed to DeviceSupport files
- sdks_output_path (str)
Directory to install SDK to (default: '\$THEOS/sdks')
- no_overwrite (bool)
Disable overwrite of existing dumped binaries (default: 0)
- no_warnings (bool)
Disable 'tbd' warnings (default: 0)
- tbd_tool (str)
Path to 'tbd' tool from inoahdev (default: 'tbd')
- xcode_installation_path (str)
Path to target Xcode version (default: currently selected Xcode.app)
- sdk_platform (str)
Target platform for patched SDK (default: 'iOS'; alternatively 'tvOS')
Note: {} options are optional to provide, and can be ignored with a '-'
EOF
}

ignored() {
if [[ $1 == "-" ]]; then
return 0
else
return 1
fi
if [[ $1 == "-" ]]; then
return 0
else
return 1
fi
}

if [[ $1 == "-h" ]]; then
print_usage
exit 0
if [[ $# -lt 1 ]] || [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then
print_usage
exit 0
fi

sdk_platform="$7"
if [[ $# -lt 7 ]] || ignored $sdk_platform; then
echo "No sdk_platform provided. Defaulting to iOS."
sdk_platform="iOS"
elif [[ $(echo $sdk_platform | tr '[A-Z]' '[a-z]') == ios ]]; then
sdk_platform="iOS"
elif [[ $(echo $sdk_platform | tr '[A-Z]' '[a-z]') == tvos ]]; then
sdk_platform="tvOS"
else
echo "Unsupported sdk_platform provided. Please choose either 'iOS' or 'tvOS'."
exit 1
fi

sdks_output_path="$2"
if [[ $# -lt 2 ]] || ignored $sdks_output_path; then
if [[ -z $THEOS ]]; then
printf 'No Theos installation found. Please either install Theos or provide a path to an sdks directory\n\n'
print_usage

exit 1
fi
sdks_output_path="$THEOS/sdks"
if [[ -z $THEOS ]]; then
printf 'No Theos installation found. Please either install Theos or provide a path to an sdks directory\n\n'
print_usage
exit 1
fi
sdks_output_path="$THEOS/sdks"
fi

if ! [[ -d $sdks_output_path ]]; then
mkdir -p "$sdks_output_path"
mkdir -p "$sdks_output_path"
fi

# tbd info
version="v3"
tbd_version="v3"
if [[ $sdk_platform == iOS ]]; then
archs_option=("--replace-archs" armv7 armv7s arm64 arm64e)
else
archs_option=("--replace-archs" arm64 arm64e)
fi

archs_option=("--replace-archs" armv7 armv7s arm64 arm64e)
tbd_options=("--ignore-clients" "--ignore-undefineds" "--allow-private-objc-symbols" "--ignore-missing-exports")
write_options=("--preserve-subdirs" "--replace-path-extension")

no_overwrite="--no-overwrite"
if [[ $# -gt 2 ]] && ! ignored $3; then
no_overwrite=""
no_overwrite=""
fi

no_warnings="--ignore-warnings"
no_warnings=""
tbd_tool="$5"
if [[ $# -gt 3 ]] && ! ignored $4; then
no_warnings="--ignore-warnings"
fi

if [[ $# -lt 5 ]] || ignored $tbd_tool; then
tbd_tool="tbd"
tbd_exists=$(command -v $tbd_tool)

if [[ -z $tbd_exists ]]; then
printf 'No installation of tbd found. Please install the latest release of tbd from here; https://github.com/inoahdev/tbd/releases or provide a path to a tbd installation\n\n'
print_usage

exit 1
fi
tbd_tool="tbd"
if ! [[ -x $(which $tbd_tool) ]]; then
printf 'No installation of tbd found. Please install the latest release of tbd from here; https://github.com/inoahdev/tbd/releases or provide a path to a tbd installation\n\n'
print_usage
exit 1
fi
else
tbd_exists=$(command -v "$tbd_tool")
if [[ -z $tbd_exists ]]; then
printf "Provided tbd-tool (%s) doesn't exist or isn't executable\n" "$tbd_tool"
exit 1
fi
if ! [[ -x $(which $tbd_tool) ]]; then
echo "Provided tbd-tool ($tbd_tool) doesn't exist or isn't executable"
exit 1
fi
fi

use_simulator="$1"
if [[ $# -lt 1 ]]; then
use_simulator="-"
use_simulator="-"
fi

xcode_developer_path="$6/Contents/Developer"
if [[ $# -lt 6 ]] || ignored $6; then
xcode_developer_path=$(xcode-select -p)
if [[ $# -lt 6 ]] || [[ -z $6 ]] || ignored $6; then
xcode_developer_path=$(xcode-select -p)
fi

if [[ -z $xcode_developer_path ]]; then
printf 'No Xcode installation found. Please either install Xcode or provide a path to an Xcode installation\n\n'
print_usage

exit 1
printf 'No Xcode installation found. Please either install Xcode or provide a path to an Xcode installation\n\n'
print_usage
exit 1
fi

xcode_sim_runtime_path="$xcode_developer_path/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot"
if ! [[ -d $xcode_sim_runtime_path ]]; then
xcode_sim_runtime_path="$xcode_developer_path/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot"
if [[ $sdk_platform == iOS ]]; then
xcode_sim_runtime_path="$xcode_developer_path/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot"
if ! [[ -d $xcode_sim_runtime_path ]]; then
xcode_sim_runtime_path="$xcode_developer_path/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot"
fi
xcode_plat_sdks_path="$xcode_developer_path/Platforms/iPhoneOS.platform/Developer/SDKs"
xcode_default_sdk_path="$xcode_plat_sdks_path/iPhoneOS.sdk"
else
xcode_sim_runtime_path="$xcode_developer_path/Platforms/AppleTVOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/tvOS.simruntime/Contents/Resources/RuntimeRoot"
if ! [[ -d $xcode_sim_runtime_path ]]; then
xcode_sim_runtime_path="$xcode_developer_path/Platforms/AppleTVOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/tvOS.simruntime/Contents/Resources/RuntimeRoot"
fi
xcode_plat_sdks_path="$xcode_developer_path/Platforms/AppleTVOS.platform/Developer/SDKs"
xcode_default_sdk_path="$xcode_plat_sdks_path/AppleTVOS.sdk"
fi
xcode_default_sdk_path="$xcode_developer_path/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk"

preferred_xcode_sdk_path=""
for xcode_sdk_path in "$xcode_plat_sdks_path/"*; do
xcode_sdk_real=$(readlink -f "$xcode_sdk_path")

for xcode_sdk_path in "$xcode_developer_path/Platforms/iPhoneOS.platform/Developer/SDKs/"*; do
xcode_sdk_real=$(realpath "$xcode_sdk_path")

if [[ $xcode_sdk_real == $xcode_default_sdk_path ]]; then
preferred_xcode_sdk_path=$xcode_sdk_path
fi
if [[ $xcode_sdk_real == $xcode_default_sdk_path ]]; then
preferred_xcode_sdk_path=$xcode_sdk_path
fi
done

if [[ -z $preferred_xcode_sdk_path ]]; then
printf 'Failed to find sdk for simulator runtime\n'
exit 1
echo 'Failed to find sdk for simulator runtime'
exit 1
fi

preferred_xcode_sdk_name=$(basename $preferred_xcode_sdk_path)

xcode_sdk_ios_version=${preferred_xcode_sdk_name:8} # Remove 'iPhoneOS' in front of sdk name
xcode_sdk_ios_version=${xcode_sdk_ios_version%????} # Remove '.sdk' at back of sdk name
if [[ $sdk_platform == iOS ]]; then
xcode_sdk_version=${preferred_xcode_sdk_name:8} # Remove 'iPhoneOS' in front of sdk name
else
xcode_sdk_version=${preferred_xcode_sdk_name:9} # Remove 'AppleTVOS' in front of sdk name
fi
xcode_sdk_version=${xcode_sdk_version%????} # Remove '.sdk' at back of sdk name

sdks_output_path_single_sdk_path=""

device_support_dir="$HOME/Library/Developer/Xcode/iOS DeviceSupport/"
device_support_dir="$HOME/Library/Developer/Xcode/$sdk_platform DeviceSupport/"
if [[ -d $device_support_dir ]] && ignored $use_simulator; then
for symbols_path in "$device_support_dir"*; do
if ! [[ -d $symbols_path ]]; then
continue
fi

ios_version="$(basename "$symbols_path" | grep -o "\d\+\(\.\d\+\)\{1,2\}")"
sdk_name=$(printf "iPhoneOS%s.sdk" $ios_version)

symbols_actual_path="$symbols_path/Symbols/System"
if ! [[ -d $symbols_actual_path ]]; then
printf "Symbols for iOS %s don't exist\n" "$ios_version"
continue
fi

sdks_output_path_single_sdk_path="$sdks_output_path/$sdk_name"
if [[ -d $sdks_output_path_single_sdk_path ]]; then
printf 'SDK for iOS %s already exists\n' "$ios_version"
continue
fi

printf 'Creating SDK for iOS %s ...\n' "$ios_version"

if [[ $xcode_sdk_ios_version != $ios_version ]]; then
printf "Warning: Xcode SDK for iOS %s will be used as a base for sdk for iOS %s\n" $xcode_sdk_ios_version "$ios_version"
fi

mkdir -p "$sdks_output_path_single_sdk_path"
cp -R "$xcode_default_sdk_path/"* "$sdks_output_path_single_sdk_path"

"$tbd_tool" \
-p $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v $version -r all "$symbols_actual_path" \
-o "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/System"

if [[ $? -ne 0 ]]; then
printf 'Failed to create tbds from Symbols directory for iOS %s\n' $ios_version
fi
done
for symbols_path in "$device_support_dir"*; do
if ! [[ -d $symbols_path ]]; then
continue
fi

os_version="$(basename "$symbols_path" | grep -o "\d\+\(\.\d\+\)\{1,2\}")"
if [[ $sdk_platform == ios ]]; then
sdk_name=$(printf "iPhoneOS%s.sdk" "$os_version")
else
sdk_name=$(printf "AppleTVOS%s.sdk" "$os_version")
fi

symbols_actual_path="$symbols_path/Symbols/System"
if ! [[ -d $symbols_actual_path ]]; then
echo "Symbols for $sdk_platform $os_version don't exist"
continue
fi

sdks_output_path_single_sdk_path="$sdks_output_path/$sdk_name"
if [[ -d $sdks_output_path_single_sdk_path ]]; then
echo "SDK for $sdk_platform $os_version already exists"
continue
fi

echo "Creating SDK for $sdk_platform $os_version ..."

if [[ $xcode_sdk_version != $os_version ]]; then
echo "Warning: $sdk_platform $xcode_sdk_version Xcode SDK will be used as a base for sdk for $sdk_platform $os_version"
fi

mkdir -p "$sdks_output_path_single_sdk_path"
cp -R "$xcode_default_sdk_path/"* "$sdks_output_path_single_sdk_path"

"$tbd_tool" \
-p $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v "$tbd_version" -r all "$symbols_actual_path" \
-o "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/System"

if [[ $? -ne 0 ]]; then
echo "Failed to create tbds from Symbols directory for $sdk_platform $os_version"
fi
done
else
if ignored $use_simulator; then
printf 'No DeviceSupport binaries found, falling back to dumping from simulator runtime binaries\n'
fi
if ignored $use_simulator; then
echo 'No DeviceSupport binaries found, falling back to dumping from simulator runtime binaries'
fi

sdks_output_path_single_sdk_path="$sdks_output_path/$preferred_xcode_sdk_name"
if [[ -d $sdks_output_path_single_sdk_path ]]; then
echo "SDK for $sdk_platform $xcode_sdk_version already exists"
exit 1
fi

sdks_output_path_single_sdk_path="$sdks_output_path/$preferred_xcode_sdk_name"
if [[ -d $sdks_output_path_single_sdk_path ]]; then
printf 'SDK for iOS %s already exists\n' $xcode_sdk_ios_version
exit 1
fi
echo "Creating sdk for $sdk_platform $xcode_sdk_version ..."

printf 'Creating sdk for iOS %s ...\n' "$xcode_sdk_ios_version"
mkdir -p "$sdks_output_path_single_sdk_path"
cp -R "$xcode_default_sdk_path/"* "$sdks_output_path_single_sdk_path"

mkdir -p "$sdks_output_path_single_sdk_path"
cp -R "$xcode_default_sdk_path/"* "$sdks_output_path_single_sdk_path"
parse_paths=("-p" $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v "$tbd_version" "-r" "all" "$xcode_sim_runtime_path/Developer"
"-p" $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v "$tbd_version" "-r" "all" "$xcode_sim_runtime_path/System"
"-p" $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v "$tbd_version" "-r" "all" "$xcode_sim_runtime_path/Library")

parse_paths=("-p" $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v $version "-r" "all" "$xcode_sim_runtime_path/Developer"
"-p" $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v $version "-r" "all" "$xcode_sim_runtime_path/System"
"-p" $no_warnings "${tbd_options[@]}" "${archs_option[@]}" -v $version "-r" "all" "$xcode_sim_runtime_path/Library")
write_paths=("-o" "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/Developer"
"-o" "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/System"
"-o" "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/Library")

write_paths=("-o" "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/Developer"
"-o" "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/System"
"-o" "${write_options[@]}" $no_overwrite "$sdks_output_path_single_sdk_path/Library")
"$tbd_tool" "${parse_paths[@]}" "${write_paths[@]}"

"$tbd_tool" "${parse_paths[@]}" "${write_paths[@]}"

if [[ $? -ne 0 ]]; then
printf 'Failed to create tbds from iPhoneSimulator runtime for iOS %s\n' $xcode_sdk_ios_version
fi
if [[ $? -ne 0 ]]; then
echo "Failed to create tbds from simulator runtime for $sdk_platform $xcode_sdk_version"
fi
fi
Loading

0 comments on commit ca52092

Please sign in to comment.