Skip to content

install tools using bash script on mac #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
210 changes: 210 additions & 0 deletions setup/mac_installer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
#!/bin/bash

# TDT4102 Tools Installation Script for macOS
# Installs: Xcode CLI Tools, Homebrew, Meson, Ninja, SDL2 libraries

set -e # Exit on error

#
MIN_MACOS_VERSION="14.0.0"
MIN_MESON_VERSION="1.11.0"

INSTALL_XCODE=0
INSTALL_HOMEBREW=0
INSTALL_MESON=0
INSTALL_LIBS=0

version_gte() {
local v1 v2
IFS='.' read -ra v1 <<< "$1"
IFS='.' read -ra v2 <<< "$2"
for i in 0 1 2; do
local n1=${v1[$i]:-0} n2=${v2[$i]:-0}
(( n1 > n2 )) && return 0
(( n1 < n2 )) && return 1
done
return 0
}

check_macos_version() {
local version
version=$(sw_vers -productVersion)
if ! version_gte "$version" "$MIN_MACOS_VERSION"; then
echo "macOS $version is not supported (minimum: $MIN_MACOS_VERSION)"
echo "Please update macOS or contact course staff."
return 1
fi
}

install_xcode_tools() {
echo "Checking if Xcode Command Line Tools are installed... "
if xcode-select -p &>/dev/null; then
echo "Xcode Command Line Tools are already installed, skipping..."
return 0
fi

echo "Xcode Command Line Tools are not installed!"
echo "Installing Xcode Command Line Tools..."
echo "A dialog may appear - please follow the instructions."
echo "This may take a while..."

xcode-select --install 2>/dev/null || true

#xcode-select --install returns immediately (new process starts), so we timeout to wait for installation
local timeout=1800 elapsed=0
while ! xcode-select -p &>/dev/null; do
sleep 5
elapsed=$((elapsed + 5))
if [ $elapsed -ge $timeout ]; then
echo "Xcode installation timed out. Run manually: xcode-select --install"
return 1
fi
done
echo "Xcode Command Line Tools installed."
return 0
}

install_homebrew() {
echo "Checking if Homebrew is installed..."
if command -v brew &>/dev/null; then
echo "Homebrew is already installed, skipping..."
return 0
fi

echo "Homebrew is not installed!"
echo "Installing Homebrew..."
echo "You might be asked for your password during installation."

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

local arch
arch=$(uname -m)

# Apple Silicon
if [ "$arch" = "arm64" ]; then
if [ ! -f ~/.zshenv ] || ! grep -q '/opt/homebrew/bin/brew shellenv' ~/.zshenv; then
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshenv
fi
eval "$(/opt/homebrew/bin/brew shellenv)"
else
# Intel
if [ ! -f ~/.zshenv ] || ! grep -q '/usr/local/opt' ~/.zshenv; then
echo 'PATH="/usr/local/opt:$PATH"' >> ~/.zshenv
fi
export PATH="/usr/local/opt:$PATH"
fi

[ -f ~/.zshenv ] && source ~/.zshenv

if command -v brew &>/dev/null; then
echo "Homebrew installed."
return 0
else
echo "Homebrew installation failed."
return 1
fi
}

install_meson() {
echo "Installing Meson and Ninja"

brew install meson

# Check Meson
if ! command -v meson &>/dev/null; then
echo "Meson installation failed."
return 1
fi
local meson_version
meson_version=$(meson --version)
if ! version_gte "$meson_version" "$MIN_MESON_VERSION"; then
echo "Meson version outdated. Try: brew upgrade meson"
return 1
fi
# Check Ninja
if ! command --version ninja &>/dev/null; then
echo "Ninja installation failed."
return 1
fi
}


install_libraries() {
echo "Installing required libraries..."
local libs=(
"sdl2"
"sdl2_image"
"sdl2_mixer"
"jpeg-turbo"
"libpng"
"libtiff"
"cmake"
)
brew install "${libs[@]}"

# Check libraries
local installed all_ok=true
installed=$(brew list)
for pkg in "sdl2" "sdl2_image" "sdl2_mixer" "jpeg-turbo" "libpng" "libtiff" "cmake"; do
if ! echo "$installed" | grep -q "$pkg"; then
echo "Package '$pkg' failed to install."
all_ok=false
fi
done
if [ "$all_ok" = true ]; then
echoe "All libraries installed."
return 0
else
echo "One or more libraries failed to install."
return 1
fi
}

main() {
echo "TDT4102 Tools Installation Script for macOS"
echo "PLEASE KEEP THIS WINDOW OPEN UNTIL PROMPTED"

# default install everything
if [[ $# -eq 0 ]]; then
INSTALL_XCODE=1
INSTALL_HOMEBREW=1
INSTALL_MESON=1
INSTALL_LIBS=1
else
while [[ $# -gt 0 ]]; do
case $1 in
--xcode) INSTALL_XCODE=1 ;;
--homebrew) INSTALL_HOMEBREW=1 ;;
--meson) INSTALL_MESON=1 ;;
--libs) INSTALL_LIBS=1 ;;
*) err "Unknown option: $1";;
esac
shift
done
fi


# Check macOS version
if ! check_macos_version; then
exit 1
fi


[[ $INSTALL_XCODE -eq 1 ]] && install_xcode
[[ $INSTALL_HOMEBREW -eq 1 ]] && install_brew

if [[ $INSTALL_HOMEBREW -eq 1 || $INSTALL_MESON -eq 1 || $INSTALL_LIBS -eq 1 ]]; then
echo "Updating Homebrew..."
brew update || echo "update failed"
brew upgrade || echo "upgrade failed"
fi

[[ $INSTALL_MESON -eq 1 ]] && install_meson
[[ $INSTALL_LIBS -eq 1 ]] && install_libraries

echo "\nInstallation completed! Please restart VS Code before continuing\n"
read -r -p "Press enter to exit"
}

#Run main
main "$@"