#!/bin/bash
#===========================
# UMVIRT LINUX FROM SCRATCH 
#===========================
# Compilation script.
# Network mode. 
#===========================
# Release: 0.2.2
# Package: llvm
#===========================
# DB commit: a32a16b5f59557e98086285561660418604e0237
# APP commit: 1ca4178aea919e4c9e869e3d650ff8af9678bd6f
#===========================

echo "ULFS Package installation start"
echo "==============================="
echo "Package: llvm" 
echo "Release: 0.2.2" 

echo "checking config file"
if [ -f $ULFS_CONFIG_FILE ]
then
echo "loading config file $ULFS_CONFIG_FILE..."
. $ULFS_CONFIG_FILE
fi
#Creating log directory
mkdir -p /var/log/ulfs-packages/llvm/
#Saving start timestamp
date +%s > /var/log/ulfs-packages/llvm/start.time
#Going to source directory...
cd /sources
#Checking dependances...
      #Checking cmake...
      if [ ! -f /var/cache/ulfs-packages/cmake ]; then
           echo "Dependance \"cmake\" not found. Trying to install...";
           wget --no-check-certificate https://umvirt.com/linux/packages//0.2.2/cmake/install -O - | bash
           if [ ! -f /var/cache/ulfs-packages/cmake ]; then
	             echo "Dependance \"cmake\" is not installed. Exiting..."
               exit
           fi
      fi
#Saving downloading timestamp
date +%s > /var/log/ulfs-packages/llvm/download.time
#Downloading source package archive...
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/l/llvm-18.1.7.src.tar.xz.md5sum
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/l/llvm-18.1.7.src.tar.xz
#Checking source package file existance
if [ ! -f llvm-18.1.7.src.tar.xz ]; then
echo "Error: Can't find llvm-18.1.7.src.tar.xz. Exiting!"
exit
fi
#Checking source package file checksum
if [ -f llvm-18.1.7.src.tar.xz.md5sum ]; then
    MD5=`LANG=C md5sum -c llvm-18.1.7.src.tar.xz.md5sum | grep OK`
    if [ "$MD5" == "" ] ; then
    echo "Error: Checksum of llvm-18.1.7.src.tar.xz is wrong. Exiting!"
    exit
    fi
fi
#Downloadning add-ons...
#Downloadning add-on  "llvm-cmake-18.src.tar.xz"...
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/l/llvm-cmake-18.src.tar.xz.md5sum
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/l/llvm-cmake-18.src.tar.xz
#Checking addon file existance
if [ ! -f llvm-cmake-18.src.tar.xz ]; then
echo "Error: Can't find llvm-cmake-18.src.tar.xz. Exiting!"
exit
fi
#Checking add-on file checksum
if [ -f llvm-cmake-18.src.tar.xz.md5sum ]; then
    MD5=`LANG=C md5sum -c llvm-cmake-18.src.tar.xz.md5sum | grep OK`
    if [ "$MD5" == "" ] ; then
    echo "Error: Checksum of llvm-cmake-18.src.tar.xz is wrong. Exiting!"
    exit
    fi
fi
#Downloadning add-on  "llvm-third-party-18.src.tar.xz"...
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/l/llvm-third-party-18.src.tar.xz.md5sum
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/l/llvm-third-party-18.src.tar.xz
#Checking addon file existance
if [ ! -f llvm-third-party-18.src.tar.xz ]; then
echo "Error: Can't find llvm-third-party-18.src.tar.xz. Exiting!"
exit
fi
#Checking add-on file checksum
if [ -f llvm-third-party-18.src.tar.xz.md5sum ]; then
    MD5=`LANG=C md5sum -c llvm-third-party-18.src.tar.xz.md5sum | grep OK`
    if [ "$MD5" == "" ] ; then
    echo "Error: Checksum of llvm-third-party-18.src.tar.xz is wrong. Exiting!"
    exit
    fi
fi
#Downloadning add-on  "clang-18.1.7.src.tar.xz"...
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/c/clang-18.1.7.src.tar.xz.md5sum
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/c/clang-18.1.7.src.tar.xz
#Checking addon file existance
if [ ! -f clang-18.1.7.src.tar.xz ]; then
echo "Error: Can't find clang-18.1.7.src.tar.xz. Exiting!"
exit
fi
#Checking add-on file checksum
if [ -f clang-18.1.7.src.tar.xz.md5sum ]; then
    MD5=`LANG=C md5sum -c clang-18.1.7.src.tar.xz.md5sum | grep OK`
    if [ "$MD5" == "" ] ; then
    echo "Error: Checksum of clang-18.1.7.src.tar.xz is wrong. Exiting!"
    exit
    fi
fi
#Downloadning add-on  "compiler-rt-18.1.7.src.tar.xz"...
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/c/compiler-rt-18.1.7.src.tar.xz.md5sum
wget --no-check-certificate -nc https://umvirt.com/linux/downloads/0.2.2/packages/c/compiler-rt-18.1.7.src.tar.xz
#Checking addon file existance
if [ ! -f compiler-rt-18.1.7.src.tar.xz ]; then
echo "Error: Can't find compiler-rt-18.1.7.src.tar.xz. Exiting!"
exit
fi
#Checking add-on file checksum
if [ -f compiler-rt-18.1.7.src.tar.xz.md5sum ]; then
    MD5=`LANG=C md5sum -c compiler-rt-18.1.7.src.tar.xz.md5sum | grep OK`
    if [ "$MD5" == "" ] ; then
    echo "Error: Checksum of compiler-rt-18.1.7.src.tar.xz is wrong. Exiting!"
    exit
    fi
fi
    #Saving cleanup timestamp
    date +%s > /var/log/ulfs-packages/llvm/cleanup.time
rm -rfv /sources/llvm-18.1.7.src/
#Saving extracting timestamp
date +%s > /var/log/ulfs-packages/llvm/unpack.time
#Extracting tar source package archive with default parameters...
tar -xf llvm-18.1.7.src.tar.xz
#Checking package directory size after unpack...
du -s llvm-18.1.7.src | awk 'NR==1 {print $1}' > /var/log/ulfs-packages/llvm/unpack.size 
#Going to source package directory...
cd llvm-18.1.7.src
#Saving configuration timestamp
date +%s > /var/log/ulfs-packages/llvm/configure.time
#Sleep 1 second
sleep 1
if [[ "$ULFS_PKG_DATERESET" == "YES" ]]
then
#Changing all files creation time (except build configuration files) in source directory to find them after installation
find /sources/llvm-18.1.7.src \! -path "*/configure*" \! -path "*/Makefile*" \! -path "*.make" \! -path "*.m4" \! -path "*.am" \! -path "*.mk" \! -path "*.stamp" \! -path "*gentpl.py"  -exec touch -m {} +
fi
#Running configuration script...
echo "checking config file"
if [ -f $ULFS_CONFIG_FILE ]
then
echo "loading config file $ULFS_CONFIG_FILE..."
. $ULFS_CONFIG_FILE
fi
cat > ulfs_configure.sh << EOIS
echo "Initializing distributed build environment... "
if [[ "\$ULFS_ICECC" == "YES" ]]
then
    export PATH="\$ULFS_ICECC_PATH:\$PATH"
    echo "ICECC"
fi

echo "Environment debug..."
echo "PATH: \$PATH"
echo "MAKEFLAGS: \$MAKEFLAGS"
echo "NINJAJOBS: \$NINJAJOBS"
env | grep ULFS


tar -xf ../llvm-cmake-18.src.tar.xz                                   &&
tar -xf ../llvm-third-party-18.src.tar.xz                             &&
sed '/LLVM_COMMON_CMAKE_UTILS/s@../cmake@llvm-cmake-18.src@'          \\
    -i CMakeLists.txt                                                 &&
sed '/LLVM_THIRD_PARTY_DIR/s@../third-party@llvm-third-party-18.src@' \\
    -i cmake/modules/HandleLLVMOptions.cmake

tar -xf ../clang-18.1.7.src.tar.xz -C tools &&
mv tools/clang-18.1.7.src tools/clang

tar -xf ../compiler-rt-18.1.7.src.tar.xz -C projects    &&
mv projects/compiler-rt-18.1.7.src projects/compiler-rt

grep -rl '#!.*python' | xargs sed -i '1s/python\$/python3/'


sed 's/utility/tool/' -i utils/FileCheck/CMakeLists.txt

sed 's/clang_dfsan/& -fno-stack-protector/' \\
    -i projects/compiler-rt/test/dfsan/origin_unaligned_memtrans.c


mkdir -v build &&
cd       build &&

CC=gcc CXX=g++                               \\
cmake -D CMAKE_INSTALL_PREFIX=/usr           \\
      -D CMAKE_SKIP_INSTALL_RPATH=ON         \\
      -D LLVM_ENABLE_FFI=ON                  \\
      -D CMAKE_BUILD_TYPE=Release            \\
      -D LLVM_BUILD_LLVM_DYLIB=ON            \\
      -D LLVM_LINK_LLVM_DYLIB=ON             \\
      -D LLVM_ENABLE_RTTI=ON                 \\
      -D LLVM_TARGETS_TO_BUILD="host;AMDGPU" \\
      -D LLVM_BINUTILS_INCDIR=/usr/include   \\
      -D LLVM_INCLUDE_BENCHMARKS=OFF         \\
      -D CLANG_DEFAULT_PIE_ON_LINUX=ON       \\
      -D CLANG_CONFIG_FILE_SYSTEM_DIR=/etc/clang \\
      -W no-dev -G Ninja ..       



EOIS
cat ulfs_configure.sh | bash 2>&1 | tee /var/log/ulfs-packages/llvm/configure.log 
#Saving build timestamp
date +%s > /var/log/ulfs-packages/llvm/build.time
#Running build script...
echo "checking config file"
if [ -f $ULFS_CONFIG_FILE ]
then
echo "loading config file $ULFS_CONFIG_FILE..."
. $ULFS_CONFIG_FILE
fi
cat > ulfs_build.sh << EOIS
echo "Initializing distributed build environment... "
if [[ "\$ULFS_ICECC" == "YES" ]]
then
    export PATH="\$ULFS_ICECC_PATH:\$PATH"
    echo "ICECC"
fi

echo "Environment debug..."
echo "PATH: \$PATH"
echo "MAKEFLAGS: \$MAKEFLAGS"
echo "NINJAJOBS: \$NINJAJOBS"
env | grep ULFS


cd build
ninja

if [[ "\$ULFS_PKG_TEST" == "YES" ]]
then

rm -f ../projects/compiler-rt/test/tsan/getline_nohang.cpp

systemctl   --user start dbus &&
systemd-run --user --pty -d -G -p LimitCORE=0 ninja check-all

fi


EOIS
cat ulfs_build.sh | bash 2>&1 | tee /var/log/ulfs-packages/llvm/build.log 
#Saving install timestamp
date +%s > /var/log/ulfs-packages/llvm/install.time
#Running install script...
cat > ulfs_install.sh << EOIS
cd build
ninja install


mkdir -pv /etc/clang &&
for i in clang clang++; do
  echo -fstack-protector-strong > /etc/clang/\$i.cfg
done
EOIS
USER=`whoami`
if [ "$USER" == "root" ] ; then 
cat ulfs_install.sh | bash 2>&1 | tee /var/log/ulfs-packages/llvm/install.log 
else
cat ulfs_install.sh | sudo bash 2>&1 | tee /var/log/ulfs-packages/llvm/install.log 
fi
#Saving finish timestamp
date +%s > /var/log/ulfs-packages/llvm/finish.time
#Checking package directory size after unpack...
cd /sources 
du -s llvm-18.1.7.src | awk 'NR==1 {print $1}' > /var/log/ulfs-packages/llvm/install.size 
echo "ULFS package installation completed."
#Producing files list
echo "Looking for installed files..."
if [  -f /var/log/ulfs-packages/llvm/files.txt ]; then
rm /var/log/ulfs-packages/llvm/files.txt
fi
USER=`whoami`
if [ "$USER" == "root" ] ; then 
find /bin -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
find /sbin -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
find /usr -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
find /etc -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
find /opt -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
find /lib -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
find /lib64 -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
find /var -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time \! -path "/var/log/ulfs-packages/llvm/*" >> /var/log/ulfs-packages/llvm/files.txt
else
sudo find /bin -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
sudo find /sbin -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
sudo find /usr -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
sudo find /etc -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
sudo find /opt -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
sudo find /lib -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
sudo find /lib64 -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time >> /var/log/ulfs-packages/llvm/files.txt
sudo find /var -type f -newer /var/log/ulfs-packages/llvm/configure.time \! -newer /var/log/ulfs-packages/llvm/finish.time \!  -path "/var/log/ulfs-packages/llvm/*" >> /var/log/ulfs-packages/llvm/files.txt
fi
#Marking package as installed...
mkdir -p /var/cache/ulfs-packages
USER=`whoami`
if [ "$USER" == "root" ] ; then 
touch /var/cache/ulfs-packages/llvm
else
sudo touch /var/cache/ulfs-packages/llvm
fi
#Calculate delta size
a=`cat /var/log/ulfs-packages/llvm/unpack.size`
b=`cat /var/log/ulfs-packages/llvm/install.size`
c=$(($b-$a))
echo $c > /var/log/ulfs-packages/llvm/delta.size 
#Calculate prepare time
a=`cat /var/log/ulfs-packages/llvm/start.time`
b=`cat /var/log/ulfs-packages/llvm/configure.time`
dp=$(($b-$a))
#Calculate download time
a=`cat /var/log/ulfs-packages/llvm/download.time`
b=`cat /var/log/ulfs-packages/llvm/unpack.time`
dd=$(($b-$a))
#Calculate delta time
a=`cat /var/log/ulfs-packages/llvm/configure.time`
b=`cat /var/log/ulfs-packages/llvm/finish.time`
db=$(($b-$a))
echo $db > /var/log/ulfs-packages/llvm/delta.time 
#Report
echo ""
echo "ULFS Package installation report"
echo "================================"
echo "Package: llvm" 
echo "Release: 0.2.2" 
echo "Build size: $c" 
echo "Prepare time: $dp sec." 
echo "Download time: $dd sec." 
echo "Build time: $db sec." 

#End of script