From c897784b721ab84e976a761d1062baceb3292cdf Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 16 Sep 2020 12:09:45 +0200
Subject: [PATCH] MISC: Update dependencies dl

- Update depends.bash
- Compile dependencies with the depends.sh script
---
 depends/depends.txt  |   2 +
 depends/rules.txt    |   2 +
 scripts/depends.bash | 143 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 147 insertions(+)
 create mode 100644 depends/depends.txt
 create mode 100644 depends/rules.txt
 create mode 100755 scripts/depends.bash

diff --git a/depends/depends.txt b/depends/depends.txt
new file mode 100644
index 00000000..2364b89b
--- /dev/null
+++ b/depends/depends.txt
@@ -0,0 +1,2 @@
+curl    sqlite3     https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release  sqlite3.tar.gz
+wget    sdl2        https://www.libsdl.org/release/SDL2-2.0.12.tar.gz           sdl2.tar.gz
diff --git a/depends/rules.txt b/depends/rules.txt
new file mode 100644
index 00000000..21129e4c
--- /dev/null
+++ b/depends/rules.txt
@@ -0,0 +1,2 @@
+sqlite3     configure   --disable-shared --disable-tcl
+sdl2        configure   --disable-shared --enable-fcitx
diff --git a/scripts/depends.bash b/scripts/depends.bash
new file mode 100755
index 00000000..2737868d
--- /dev/null
+++ b/scripts/depends.bash
@@ -0,0 +1,143 @@
+#!/bin/bash
+
+DEPENDS_FILE=depends/depends.txt
+RULES_FILE=depends/rules.txt
+BUILD_DIR="$1"
+ROOT_DIR="$PWD"
+NPROC=`getconf _NPROCESSORS_ONLN`
+ABS_BUILD_DIR=`realpath $BUILD_DIR`
+
+function die ()
+{
+    echo "ERROR: $*"
+    exit 1
+}
+
+function curl ()
+{
+    $CURL $1 --output $2 2>/dev/null >/dev/null
+    [ $? -ne 0 ] && die "Failed to download $1 and save it to $2"
+}
+
+function wget ()
+{
+    $WGET $1 -O $2 2>/dev/null >/dev/null
+    [ $? -ne 0 ] && die "Failed to download $1"
+}
+
+function get ()
+{
+    local NUM=$1
+    shift
+    echo $* | cut -d' ' -f$NUM -
+}
+
+function get_from ()
+{
+    local NUM=$1
+    shift
+    echo $* | cut -d' ' -f$NUM- -
+}
+
+function untar ()
+{
+    local OLD=`tar tf depends/$1 | cut -d'/' -f1 - | uniq`
+    (cd depends && $TAR xzf $1 --transform="s!^$OLD!$2!")
+}
+
+[ "x${BUILD_DIR}" = "x" ] && die "You must specify the lektor's build folder as the first argument"
+echo "Lektor's build file is $BUILD_DIR"
+
+CURL=`which curl 2>/dev/null`
+[ $? -ne 0 ] && die "Failed to find the curl binary"
+WGET=`which wget 2>/dev/null`
+[ $? -ne 0 ] && die "Failed to find the wget binary"
+TAR=`which tar 2>/dev/null`
+[ $? -ne 0 ] && die "Failed to find the tar binary"
+
+! [ -f $DEPENDS_FILE ] && die "You must run this script from the root of the project"
+! [ -f $PWD/scripts/depends.bash ] && die "You must run thus script from the root of the project"
+! [ -d $BUILD_DIR/depends_install ] && mkdir $BUILD_DIR/depends_install
+! [ -d $BUILD_DIR/depends_install ] && die "Failed to create depends install folder"
+
+#########################
+# Download dependencies #
+#########################
+
+! [ -d depends ] && mkdir depends
+
+while read LINE
+do
+    FUNC=`get 1 $LINE`
+    FOLD=`get 2 $LINE`
+    URL=`get 3 $LINE`
+    TAR_FILE=`get 4 $LINE`
+
+    if [ -f depends/$TAR_FILE ]
+    then
+        echo "Dependency $FOLD is already downloaded," \
+             "remove depends/$TAR_FILE and depends/$FOLD to re-download it"
+        continue
+    fi
+
+    echo "Downloading $FOLD from $URL to depends/$TAR_FILE"
+    case $FUNC in
+        wget)
+            wget $URL depends/$TAR_FILE
+            ;;
+        curl)
+            curl $URL depends/$TAR_FILE
+            ;;
+    esac
+done < $DEPENDS_FILE
+
+##################################
+# Extract dependencies and build #
+##################################
+
+while read LINE
+do
+    FOLD=`get 2 $LINE`
+    FILE=`get 4 $LINE`
+    if [ -d depends/$FOLD ]
+    then
+        echo "Skip already extracted folder depends/$FOLD"
+        continue
+    fi
+    echo "Extract file depends/$FILE to depends/$FOLD"
+    untar $FILE $FOLD
+done < $DEPENDS_FILE
+
+while read LINE
+do
+    FOLD=`get 1 $LINE`
+    TYPE=`get 2 $LINE`
+    OPT=`get_from 3 $LINE`
+
+    ! [ -d $BUILD_DIR/depends/$FOLD ] && mkdir -p $BUILD_DIR/depends/$FOLD
+    ! [ -d $BUILD_DIR/depends/$FOLD ] && die "Failed to create the build folder" \
+                                             "for $FOLD"
+
+    if [ -f $BUILD_DIR/depends/$FOLD/.lektor ]
+    then
+        echo "Skip already built folder $FOLD, if you want to rebuild it," \
+             "remove file $BUILD_DIR/depends/$FOLD/.lektor"
+        continue
+    fi
+
+    cd $BUILD_DIR/depends/$FOLD
+
+    $ROOT_DIR/depends/$FOLD/configure --prefix=$ABS_BUILD_DIR/depends_install CC=$CC CXX=$CXX $OPT
+    [ $? -ne 0 ] && die "Failed to configure $FOLD"
+    make -j$NPROC
+    [ $? -ne 0 ] && die "Failed to make $FOLD, build folder is $BUILD_DIR/depends/$FOLD"
+    make install
+    [ $? -ne 0 ] && die "Failed to install $FOLD, build folder is $BUILD_DIR/depends/$FOLD" \
+                        ", install folder is $ROOT_DIR/depends_install"
+
+    touch .lektor
+    cd $ROOT_DIR
+    echo "Finished to build $FOLD, result is in $BUILD_DIR/depends/$FOLD"
+done < $RULES_FILE
+
+# Use CC and CXX
-- 
GitLab