# Build script for qi.
#
# Copyright (c) 2015-2019 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

echo "Making Qi from ${CWD}/qi ..."

cd -- "${CWD}/qi"

make -j1 clean

./configure \
 --prefix=/tools \
 --packagedir=/usr/pkg \
 --targetdir=/

make -j1 install

# Clean up the source repository

rm -f config.mak
make -j1 clean

# Create usr/pkg directory at 'rootdir'
mkdir -p -- "${rootdir}/usr/pkg"

# Copy our custom qirc to /tools
cp -p "${worktree}/archive/qi/qirc" /tools/etc/

# Set compiler options provided by default in Dragora
case $arch in
i586)
    sed -i \
     -e 's|#QICFLAGS=.*|QICFLAGS=-g0 -Os -march=i586 -mtune=i686|' \
     -e 's|#QICXXFLAGS=.*|QICXXFLAGS=-g0 -Os -march=i586 -mtune=i686|' \
     /tools/etc/qirc
    ;;
x86_64)
    sed -i \
     -e 's|#QICFLAGS=.*|QICFLAGS=-g0 -Os -mtune=generic -pipe|' \
     -e 's|#QICXXFLAGS=.*|QICXXFLAGS=-g0 -Os -mtune=generic -pipe|' \
     /tools/etc/qirc
    ;;
*)
    sed -i \
     -e 's|#QICFLAGS=.*|QICFLAGS=-g0 -Os -pipe|' \
     -e 's|#QICXXFLAGS=.*|QICXXFLAGS=-g0 -Os -pipe|' \
     /tools/etc/qirc
    ;;
esac

# Include additional settings from targets/

# We prefix '--enable-multilib' for targets supporting it,
# the native GCC wants this option explicitly
if test -n "$multilib_options"
then
    case $multilib_options in
    *--disable-multilib* | *--enable-multilib* | *--with-multilib-list=)
        true
        ;;
    *)
        multilib_options="--enable-multilib $multilib_options"
        ;;
    esac
fi

cat << EOF >> /tools/etc/qirc

# Multilib options
multilib_options=$multilib_options

# lib<equal> suffix (multilib, if any)
libSuffix=$libSuffix

# GCC (arch-specific) options
gcc_options=$gcc_options
EOF

chmod 644 /tools/etc/qirc

