#!/bin/sh

### Export language settings
export LANG=C
export LC_ALL=C

### Set prefix for XORG installation
PREFIX="/opt/xorg"

### Export some useful XORG settings
# NOTE: Prevent package foo depends on package bar (example: mesa depends on libdrm)
export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"
export LD_LIBRARY_PATH="$PREFIX/lib"
export PATH="$PREFIX/bin:$PATH"

### Export some useful LLVM/CLANG settings
# NOTE: clang binary is stored in $LLVM_BIN_DIR
LLVM_BIN_DIR="/opt/llvm/bin"
export PATH="$LLVM_BIN_DIR:$PATH"

### Export compiler settings
# Here: clang is the default compiler (clang++ and clang-ccp are symlinks)
# NOTE-1: Set CPPFLAGS when you want to use include-dir from $PREFIX installation
# NOTE-2: Generate a symlink "clang-cpp -> clang" in $LLVM_BIN_DIR if necessary.
# XXX: DEBUG: Use 'clang -v'
export CC="clang -v"
export CFLAGS="-Qunused-arguments -fPIC"
export CXX="clang++"
export CXXFLAGS="$CFLAGS"
export CPP="clang-cpp"
export CPPFLAGS="-I$PREFIX/include"

### Force linking against my XORG runtime-libs (here: libdrm)
# Mesa >=8.0.x requires here a higher libdrm-version and needs to be pre-build.
# I have prepared and installed my libdrm runtime-libs into /opt/xorg/lib dir.
# On updates ldconfig command considers all its configuration files and rebuilds
# its cache according to the filename-list (see /etc/ld.so.conf.d/).
# So, the position of a libname in the ldconfig-cache matters.
# In case of identical libnames the 1st in the list is considered at rank #1.
# The systemwide libdrm runtime-libs (see x86_64-linux-gnu.conf) conflicted with
# mine (see xorg.conf). This caused the issue that MESA-DRI drivers were linked
# against the wrong libdrm runtime-libs.
# (Thanks Edwin Török for the vital help.)
# HINT-1: Check with 'ldconfig -p | grep mylibname' which one is on top!
# HINT-2: Use the LDFLAGS workaround only as a last ugly hack!
##export LDFLAGS="-Wl,-rpath,/opt/xorg/lib"

### MESA-DRI ("classic") and GALLIUM drivers
# NOTE-1: Intel Sandybridge GPU has no GALLIUM driver candidate!
# NOTE-2: With GALLIUM_DRIVERS="" you can suppress building of any GALLIUM driver.
# NOTE-3: Explicitly set $DRI_DRIVER_DIR (current setting is default).
DRI_DRIVER_DIR="$PREFIX/lib/dri"
DRI_DRIVERS="i965"
GALLIUM_DRIVERS="swrast"

### Configure options for MESA-DRI, GALLIUM, GLES and OPENVG
DRI_CFG_OPTS="--with-dri-driverdir=$DRI_DRIVER_DIR --with-dri-drivers=$DRI_DRIVERS"
GALLIUM_DRIVERS_CFG_OPTS="--with-gallium-drivers=$GALLIUM_DRIVERS"
GALLIUM_LLVM_CFG_OPTS="--enable-gallium-llvm"
GALLIUM_EGL_CFG_OPTS="--disable-gallium-egl"
GALLIUM_CFG_OPTS="$GALLIUM_DRIVERS_CFG_OPTS $GALLIUM_LLVM_CFG_OPTS $GALLIUM_EGL_CFG_OPTS"
OPENGL_CFG_OPTS="--disable-gles1 --enable-gles2"
OPENVG_CFG_OPTS="--disable-openvg"
DRI3_OPTS="--disable-dri3"
LLVM_SHARED_LIBS_OPTS="--disable-llvm-shared-libs"
## XXX: WORKAROUND: Broken TLS support in GLX with llvm-toolchain v3.6.0rc2 (here: v10.3.7 and v10.4.4)
COMMON_CFG_OPTS="--enable-glx-tls"
DEBUG_CFG_OPTS="--enable-debug"
CFG_OPTS="$DRI_CFG_OPTS $GALLIUM_CFG_OPTS $OPENGL_CFG_OPTS $OPENVG_CFG_OPTS $DRI3_OPTS $LLVM_SHARED_LIBS_OPTS $COMMON_CFG_OPTS $DEBUG_CFG_OPTS"

### Make options
# Set the number of parallel make jobs (here: Intel Sandybridge CPU has 4 cores +1 as suggested by Emil Velikov)
# XXX: DEBUG: Use MAKE_JOBS="1"
##MAKE_JOBS=$(($(getconf _NPROCESSORS_ONLN)+1))
MAKE_JOBS="1"
MAKE_OPTS="-j$MAKE_JOBS"

### Build-dir
cd mesa-git/

### Run autogen/configure
./autogen.sh --prefix=$PREFIX $CFG_OPTS

### Start the build
time -p make $MAKE_OPTS

### Installation
# XXX: Workaround: Explicitly add $LLVM_BIN_DIR to root's PATH
# NOTE: sudo resets the PATH settings: The installation fails when clang is not found!
sudo PATH="$LLVM_BIN_DIR:$PATH" make install

### Update the dynamic linker run-time bindings
sudo ldconfig

### Check for libGL and libdricore shared-libs in the ldconfig-cache
sudo ldconfig --print-cache | egrep 'libGL.so|libdricore'

### Check if the Intel i965 MESA-DRI driver was compiled against desired libdrm runtime-libs
sudo ldd $PREFIX/lib/dri/i965_dri.so | egrep 'libdrm.so|libdrm_intel.so|libkms.so'
