Skip to content

Instantly share code, notes, and snippets.

@mebasoglu
Created April 6, 2022 10:48
Show Gist options
  • Save mebasoglu/84e2bb6f086ac9dc4ccd6413e05fcfff to your computer and use it in GitHub Desktop.
Save mebasoglu/84e2bb6f086ac9dc4ccd6413e05fcfff to your computer and use it in GitHub Desktop.
Open3D installer script
#! /usr/bin/env bash
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
NC='\033[0m' # No Color
# Emojis
ANGRY_FACE='\U1F621'
CHECK='\U2705'
OK_HAND='\U1F44C'
usage() {
echo "Usage: $0 [-p /path/to/py3/executable] [-j num-of-threads] [-c] [-h]"
echo
echo "-p Python 3 path. Default tries to find automatically."
echo "-j Number of threads to use building project. Default uses all."
echo "-c If specified, build with Cuda support."
echo "-h Show this text."
}
install_dependencies() {
if hash sudo 2>/dev/null; then
echo "Found sudo."
SUDO=sudo
else
echo "Couldn't find sudo."
SUDO=""
fi
${SUDO} apt-get install -y \
xorg-dev \
libglu1-mesa-dev \
python3-dev \
libsdl2-dev \
libc++-7-dev \
libc++abi-7-dev \
ninja-build \
libxi-dev \
libtbb-dev \
libosmesa6-dev \
libudev-dev \
autoconf \
libtool \
python3-pip
pip3 install --upgrade pip
pip3 install setuptools
# ${SUDO} apt-get install -y build-essential curl git python3
# curl -s -L https://apt.kitware.com/kitware-archive.sh | bash
# ${SUDO} apt-get install -y cmake
}
check_package() {
# check_package "command"
echo -e "${PURPLE}Checking $1.${NC}"
if ! hash "$1" 2>/dev/null; then
echo -e "${RED}Couldn't find $1.${NC} ${ANGRY_FACE}"
exit 1
fi
}
# https://stackoverflow.com/q/8654051/11495016
numCompare() {
awk "BEGIN {exit !($1 >= $2)}"
}
check_version() {
# check_version "package" "current version" "desired version"
echo -e "${PURPLE}Checking version of $1.${NC}"
numCompare "$2" "$3"
EXIT_CODE=$?
if [ ${EXIT_CODE} -ne 0 ]; then
echo -e "${RED}$1 version must be greater than or equal to $3.${NC} ${ANGRY_FACE}"
exit 1
fi
}
check_requirements() {
# Check packages.
check_package "g++"
check_package "cmake"
check_package "git"
check_package "python3"
# Check version of g++ >= 5
GCC_VERSION="$(g++ --version | head -n 1 | cut -d ' ' -f 4 | cut -d '.' -f 1)"
check_version "g++" "${GCC_VERSION}" "5"
# Check version of cmake >= 3.19
CMAKE_VERSION="$(cmake --version | head -n 1 | cut -d ' ' -f 3)"
check_version "cmake" "${CMAKE_VERSION}" "3.19"
if [ "$1" != "false" ]; then
# OPTIONAL
# Check if cuda exists.
check_package "nvcc"
# Check version of cuda >= 10.1
CUDA_VERSION="$(nvcc --version | tail -n 1 | cut -d ' ' -f 6)"
CUDA_VERSION=${CUDA_VERSION:1}
check_version "nvcc" "${CUDA_VERSION}" "10.1"
fi
echo -e "${GREEN}All requirements are satisfied.${NC} ${CHECK}"
}
main() {
# We need to declare locale variable to use getopts inside a function.
local OPTIND
PY3_PATH="false"
USE_CUDA="false"
THREADS=$(nproc)
# Parse arguments.
while getopts ":h:cp:j:" arg; do
case $arg in
p) # Python 3 executable path.
PY3_PATH=${OPTARG}
echo "Python 3 executable path is ${PY3_PATH}"
;;
c) # Wheter use Cuda or not.
USE_CUDA="true"
echo "Use cuda."
;;
j) # Specify how many threads to use.
THREADS=${OPTARG}
echo "Use ${THREADS} threads."
;;
h | *) # Display help.
usage "$@"
exit 0
;;
esac
done
check_requirements ${USE_CUDA}
# Clone repo.
echo -e "${BLUE}Clonning repo.${NC}"
git clone https://github.com/isl-org/Open3D
EXIT_CODE=$?
if [ "${EXIT_CODE}" -eq 128 ]; then
echo -e "${RED}An error occured during clonning repo.${NC} ${ANGRY_FACE}"
exit 1
fi
cd Open3D || exit
# Install dependencies if the os is Ubuntu (or an Ubuntu flavour).
IS_UBUNTU=$(grep -i ubuntu /etc/os-release)
if [ -n "${IS_UBUNTU}" ]; then
echo -e "${BLUE}Installing dependencies.${NC}"
install_dependencies
fi
echo -e "${BLUE}Creating build directory.${NC}"
mkdir build
cd build || exit
set -e
# Create build files.
echo -e "${BLUE}Creating build files.${NC}"
PY3_CMAKE=$(awk -v PY3_PATH="${PY3_PATH}" 'BEGIN {printf (PY3_PATH=="false"?"":"-DPython3_ROOT=%s"), PY3_PATH }')
CUDA_CMAKE=$(awk -v USE_CUDA="${USE_CUDA}" 'BEGIN {printf (USE_CUDA=="false"?"":"-DBUILD_CUDA_MODULE=ON") }')
CMAKE_OPTIONS="${PY3_CMAKE} ${CUDA_CMAKE}"
cmake "${CMAKE_OPTIONS}" ..
# Build project.
echo -e "${BLUE}Building project.${NC}"
make -j"${THREADS}"
# Install Open3D C++ library.
echo -e "${BLUE}Installing Open3D C++ library.${NC}"
if ! hash sudo 2>/dev/null; then
make install
else
sudo make install
fi
# Install pip package to the current python environment
echo -e "${BLUE}Installing Python 3 package.${NC}"
make install-pip-package
echo -e "${GREEN}Successfully installed Open3D.${NC} ${OK_HAND}"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment