From 4430de0a4950f7cec07a6e21307d01521b5e5bb0 Mon Sep 17 00:00:00 2001 From: fruffy Date: Tue, 27 Feb 2024 20:24:21 -0500 Subject: [PATCH] Fun with GPTs. --- tools/install_mac_deps.sh | 66 ++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/tools/install_mac_deps.sh b/tools/install_mac_deps.sh index 3a1271ee82c..118ca3cbca5 100755 --- a/tools/install_mac_deps.sh +++ b/tools/install_mac_deps.sh @@ -1,39 +1,63 @@ #! /bin/bash -# Script for building P4C on MacOS. +# Script to install P4C dependencies on MacOS. set -e # Exit on error. set -x # Make command execution verbose - -# Set up Homebrew differently for arm64. -if [[ $(uname -m) == 'arm64' ]]; then +# Check if brew shellenv command is already in zprofile +if ! grep -q 'brew shellenv' ~/.zprofile; then + # Set up Homebrew differently for arm64. + if [[ $(uname -m) == 'arm64' ]]; then (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> ~/.zprofile - eval "$(/opt/homebrew/bin/brew shellenv)" -else + else (echo; echo 'eval "$(/usr/local/bin/brew shellenv)"') >> ~/.zprofile - eval "$(/usr/local/bin/brew shellenv)" + fi +fi + +# Check if Homebrew is already installed +if ! which brew > /dev/null 2>&1; then + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" fi -if [[ ! -x brew ]]; then - /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" +# Source zprofile only if necessary +if ! source ~/.zprofile &> /dev/null; then + echo "Warning: Failed to source ~/.zprofile." fi + HOMEBREW_PREFIX=$(brew --prefix) -# Install some custom requirements on OS X using brew -BOOST_LIB="boost@1.84" -brew install autoconf automake bdw-gc ccache cmake \ - libtool openssl pkg-config coreutils bison grep \ - ${BOOST_LIB} +# Check if any packages need installation. +REQUIRED_PACKAGES=(autoconf automake bdw-gc ccache cmake libtool openssl pkg-config coreutils bison grep) +MISSING_PACKAGES=() +for package in "${REQUIRED_PACKAGES[@]}"; do + if ! brew list -q "$package"; then + MISSING_PACKAGES+=("$package") + fi +done -# We need to link boost and openssl. -brew link ${BOOST_LIB} openssl -# Prefer Homebrew's bison and grep over the macOS-provided version. -# For Bison only `$(brew --prefix bison)/bin` seems to work... -echo 'export PATH="$(brew --prefix bison)/bin:$PATH"' >> ~/.bash_profile -echo 'export PATH="$HOMEBREW_PREFIX/opt/grep/libexec/gnubin:$PATH"' >> ~/.bash_profile -source ~/.bash_profile +# Install missing packages. +if [[ ${#MISSING_PACKAGES[@]} -gt 0 ]]; then + brew install "${MISSING_PACKAGES[@]}" +fi +# Check if linking is needed +if ! brew ls --linked --formula ${BOOST_LIB} > /dev/null 2>&1; then + brew link ${BOOST_LIB} openssl +fi + +# Check if PATH modification is needed +if ! grep -q "$(brew --prefix bison)/bin" ~/.bash_profile; then + echo 'export PATH="$(brew --prefix bison)/bin:$PATH"' >> ~/.bash_profile +fi +if ! grep -q "$HOMEBREW_PREFIX/opt/grep/libexec/gnubin" ~/.bash_profile; then + echo 'export PATH="$HOMEBREW_PREFIX/opt/grep/libexec/gnubin:$PATH"' >> ~/.bash_profile +fi + +# Source bash_profile only if necessary +if ! source ~/.bash_profile &> /dev/null; then + echo "Warning: Failed to source ~/.bash_profile." +fi # Fixes for stuck grpcio installation. export GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=1