#!/bin/bash
set -eu
make_helper() {
local containing_dir="${1}"
local app_name="${2}"
local feature="${3}"
local flags="${4}"
local helper_name="${app_name} Helper"
local helper_stem="${containing_dir}/${helper_name}"
local original_helper="${helper_stem}.app"
if [[ ! -d "${original_helper}" ]]; then
echo "${0}: error: ${original_helper} is a required directory" >& 2
exit 1
fi
local original_helper_exe="${original_helper}/Contents/MacOS/${helper_name}"
if [[ ! -f "${original_helper_exe}" ]]; then
echo "${0}: error: ${original_helper_exe} is a required file" >& 2
exit 1
fi
local feature_helper="${helper_stem} ${feature}.app"
rsync -acC --delete --include '*.so' "${original_helper}/" "${feature_helper}"
local helper_feature="${helper_name} ${feature}"
local helper_feature_exe="${feature_helper}/Contents/MacOS/${helper_feature}"
mv "${feature_helper}/Contents/MacOS/${helper_name}" "${helper_feature_exe}"
local change_flags="$(dirname "${0}")/change_mach_o_flags.py"
"${change_flags}" ${flags} "${helper_feature_exe}"
local feature_info="${feature_helper}/Contents/Info"
local feature_info_plist="${feature_info}.plist"
defaults write "${feature_info}" "CFBundleDisplayName" "${helper_feature}"
defaults write "${feature_info}" "CFBundleExecutable" "${helper_feature}"
cfbundleid="$(defaults read "${feature_info}" "CFBundleIdentifier")"
feature_cfbundleid="${cfbundleid}.${feature}"
defaults write "${feature_info}" "CFBundleIdentifier" "${feature_cfbundleid}"
cfbundlename="$(defaults read "${feature_info}" "CFBundleName")"
feature_cfbundlename="${cfbundlename} ${feature}"
defaults write "${feature_info}" "CFBundleName" "${feature_cfbundlename}"
plutil -convert xml1 "${feature_info_plist}"
chmod a+r "${feature_info_plist}"
}
if [[ ${#} -ne 2 ]]; then
echo "usage: ${0} <directory_within_contents> <app_name>" >& 2
exit 1
fi
DIRECTORY_WITHIN_CONTENTS="${1}"
APP_NAME="${2}"
CONTENTS_DIR="${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}"
CONTAINING_DIR="${CONTENTS_DIR}/${DIRECTORY_WITHIN_CONTENTS}"
make_helper "${CONTAINING_DIR}" "${APP_NAME}" "EH" "--executable-heap"
make_helper "${CONTAINING_DIR}" "${APP_NAME}" "NP" "--no-pie"