#!/bin/bash
#
# build -- hack to make torch library's weird build system work properly
#
# WARNING: ONLY FOR LINUX!
#

# Restrict executable search path
PATH=/usr/bin:/bin
ROOT=`dirname $0`
PATH=$ROOT:$PATH

# Some useful variables
CONFIG=$ROOT/config

# Determine build architecture
ARCH=32
[ `uname -m` = "x86_64" ] && ARCH=64

# Setup target installation directories
TARGET=/tmp/target
[ -d $TARGET ] && rm -rf $TARGET
mkdir -p $TARGET/{lib,include}

# Build shared library
cp $CONFIG/Linux.cfg.$ARCH.shared $ROOT/Linux.cfg
xmake

# Copy the libtorch.so produced by above command to the installation
# target directory.
find $ROOT -type f -iname libtorch.so -exec cp '{}' $TARGET/lib ';'

# Clean shared library build and build static library
xmake clean
cp $CONFIG/Linux.cfg.$ARCH.static $ROOT/Linux.cfg
xmake

# Copy the libtorch.a produced by above command to the installation
# target directory.
find $ROOT -type f -iname libtorch.a -exec cp '{}' $TARGET/lib ';'

# Copy all the header files to the installation target directory
find $ROOT -type f -iname '*.h' -exec cp '{}' $TARGET/include ';'

# Clean-up
xmake clean
rm $ROOT/Linux.cfg
mv /tmp/target $ROOT
