Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][No QA] Create run-android script to setup ccache #48758

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"startAndroidEmulator": "./scripts/start-android.sh",
"postinstall": "./scripts/postInstall.sh",
"clean": "npx react-native clean-project-auto",
"android": "./scripts/set-pusher-suffix.sh && npx react-native run-android --mode=developmentDebug --appId=com.expensify.chat.dev --active-arch-only",
"android": "./scripts/run-android.sh",
"ios": "./scripts/set-pusher-suffix.sh && npx react-native run-ios --list-devices --mode=\"DebugDevelopment\" --scheme=\"New Expensify Dev\"",
"pod-install": "./scripts/pod-install.sh",
"ipad": "concurrently \"npx react-native run-ios --simulator=\\\"iPad Pro (12.9-inch) (6th generation)\\\" --mode=\\\"DebugDevelopment\\\" --scheme=\\\"New Expensify Dev\\\"\"",
Expand Down
60 changes: 60 additions & 0 deletions scripts/ccache-utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

SCRIPTS_DIR=$(dirname "${BASH_SOURCE[0]}")

source "$SCRIPTS_DIR/shellUtils.sh"

# Ask to install ccache if it isn't installed already
install_ccache() {
if ! which ccache; then
if ask_yes_no "ccache not found. Would you like to install it to speed up your local builds?"; then
brew install ccache
fi
fi
}

# Coming from https://reactnative.dev/docs/build-speed#local-caches, symlink ccache to C compilers used by React Native.
# Typically these packages exist in /usr/bin. Also typical is that `/usr/local/bin` appears before `/usr/bin` in your $PATH.
# So the way this works is to symlink ccache to the `/usr/local/bin` version of the package so it gets picked up by RN build commands.
packagesToSymlink=(
gcc
g++
cc
c++
clang
clang++
)

# Directory to store stashed original files in `/usr/local/bin` if they exist
tempDir=$(mktemp -d)

create_ccache_symlinks() {
info "Stashing local C++ compilers and symlinking them with ccache"
for pkg in "${packagesToSymlink[@]}"; do
# stash the existing binaries if present
if [[ -f "/usr/local/bin/$pkg" ]]; then
sudo mv -v "/usr/local/bin/$pkg" "$tempDir/$pkg"
fi

# create the symlinks
sudo ln -sv "$(which ccache)" "/usr/local/bin/$pkg"
done
}

cleanup_ccache_symlinks() {
info "Cleaning up ccache symlinks and restoring stashed packages"
for pkg in "${packagesToSymlink[@]}"; do
# remove symlinks
if [[ -L "/usr/local/bin/$pkg" ]]; then
sudo unlink -v "/usr/local/bin/$pkg"
fi

# restore stashed binaries
if [[ -f "$tempDir/$pkg" ]]; then
sudo mv -v "$tempDir/$pkg" "/usr/local/bin/$pkg"
fi
done

# Remove the temporary directory
rmdir "$tempDir"
}
24 changes: 24 additions & 0 deletions scripts/run-android.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

SCRIPTS_DIR=$(dirname "${BASH_SOURCE[0]}")

source "$SCRIPTS_DIR/ccache-utils.sh"
source "$SCRIPTS_DIR/shellUtils.sh"

"$SCRIPTS_DIR"/set-pusher-suffix.sh

# Install ccache to perform cached build
install_ccache

if which ccache; then
# remove the symlinks before exiting so we don't leave symlinks developers may not be aware of
trap cleanup_ccache_symlinks EXIT

create_ccache_symlinks

# warn developers to clear cache if build fails
trap 'warning "ccache was used for this failing build. Consider clearing the cache with `ccache --clear`"' ERR
fi

# Run the android build
npx react-native run-android --mode=developmentDebug --appId=com.expensify.chat.dev --active-arch-only
42 changes: 42 additions & 0 deletions scripts/shellUtils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ if [ -z "${RED+x}" ]; then
declare -r RED=$'\e[1;31m'
fi

# Check if YELLOW has already been defined
if [ -z "${YELLOW+x}" ]; then
declare -r YELLOW=$'\e[1;33m'
fi

# Check if BLUE has already been defined
if [ -z "${BLUE+x}" ]; then
declare -r BLUE=$'\e[1;34m'
Expand All @@ -33,6 +38,10 @@ function error {
echo "💥 $RED$1$RESET"
}

function warning {
echo "⚠️ $YELLOW$1$RESET"
}

function info {
echo "$BLUE$1$RESET"
}
Expand Down Expand Up @@ -122,3 +131,36 @@ read_lines_into_array() {
eval "$array_name+=(\"$line\")"
done
}

ask_yes_no() {
local prompt_text="$1"
local default="$2"
local response

# Determine the prompt with the default option shown
if [[ "$default" == "Y" ]]; then
prompt_text="$prompt_text [Y/n] "
elif [[ "$default" == "N" ]]; then
prompt_text="$prompt_text [y/N] "
else
prompt_text="$prompt_text [y/n] "
fi

# Loop until a valid response is given
while true; do
# Prompt the user for input
read -r -p "$prompt_text" response

# If response is empty, use the default
if [[ -z "$response" ]]; then
response="$default"
fi

# Check if the response is Yes or No
case "$response" in
[yY][eE][sS]|[yY]) return 0 ;; # Yes: Return true (success)
[nN][oO]|[nN]) return 1 ;; # No: Return false (failure)
*) echo "Please answer yes or no." ;;
esac
done
}
Loading