summaryrefslogtreecommitdiff
path: root/build-fcts.sh
blob: 9445b640f915c79735e2cf117f44af034b2ad929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash

# based on devs/seoz/build.sh

# /etc/ld.so.conf.d/efl.conf:
#   /opt/efl/lib
# /etc/profile.d/efl.sh:
#   export EFLDIR=/opt/efl
#   export PATH=$PATH:$EFLDIR/bin
#   export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$EFLDIR/lib/pkgconfig
#   #export XDG_DATA_DIRS=$XDG_DATA_DIRS:$EFLDIR/share

set -e
shopt -s expand_aliases

MAKE_J="-j5"
export TERM=xterm-color

if [ "x${PREFIX}" = "x" ]
then
   echo "you must at least set PREFIX var"
   exit 1
fi

RESET="\033[0m"
RED="\033[0;31m"
GREEN="\033[0;32m"
BROWN="\033[0;33m"
PURPLE="\033[0;35m"

export CC=${CC:-"ccache gcc"}
export CFLAGS=${CFLAGS:-"-ggdb3 -O2 -march=native -ffast-math -W -Wall -Wextra -Wshadow -Wstrict-prototypes"}

export PATH=$PREFIX/bin:$PATH
export LD_LIBRARY_PATH=$PREFIX"/lib"
export PKG_CONFIG_PATH=$PREFIX"/lib/pkgconfig"

GIT_PULL=${GIT_PULL:-1}
BUILD_ANYWAY=${BUILD_ANYWAY:-0}
BUILD_DIR=${BUILD_DIR:-"build"}
USE_BUILD_DIR=${USE_BUILD_DIR:-1}
TRY_CMAKE=${TRY_CMAKE:-1}

TAGS_PATH=$HOME"/share/ctags/efl.tags"

CLEAN_LEVEL=0
for arg in $@; do
    if [ "x$arg" = "x+c" ]; then CLEAN_LEVEL=$(($CLEAN_LEVEL+1)); fi
    if [ "x$arg" = "x-c" ]; then CLEAN_LEVEL=$(($CLEAN_LEVEL-1)); fi
    if [ "x$arg" = "x+p" ]; then GIT_PULL=1; fi
    if [ "x$arg" = "x-p" ]; then GIT_PULL=0; fi
    if [ "x$arg" = "x+b" ]; then BUILD_ANYWAY=1; fi
    if [ "x$arg" = "x-b" ]; then BUILD_ANYWAY=0; fi
done

function say()
{
    cmd=$1
    echo -e " $BROWN* $GREEN$cmd$RESET ..."
}

function exec_cmd()
{
    cmd=$1
    say "$cmd" && $cmd
}

function fatal()
{
    cmd=$1
    echo -e "$PURPLE$cmd$RESET miserably$RED FAILED$RESET"
    exit 1
}

function get_sudo_pwd()
{
    echo -e "\n==== $RED SUDO$RESET PASSWD"
    sudo_test=/tmp/_sudo.test
    echo -n "enter sudo-password: " && stty -echo && read SUDO_PASSWD && stty echo || exit 1
    echo "$SUDO_PASSWD" | sudo -S touch $sudo_test
    if [ ! -e $sudo_test ]
    then
        fatal "cmdline provided sudo password failed!"
    else
        sudo rm -f $sudo_test
    fi
    echo
}

function efl_ctags()
{
    # efl ctags
    echo -e "\n==== TAGS"
    ex=""
    for dir in admin arcanist base bindings devs; do
        ex="$ex --exclude=$dir"
    done
    [ -e $TAGS_PATH ] && rm $TAGS_PATH
    ctags --tag-relative=yes -h ".h.x.cpp.c" --exclude="*.js" --exclude="*.pxi" --exclude="*.la" $ex -R -o $TAGS_PATH
}

function build()
{
   local src_dir=$1
   local autogen_flags=" --prefix=${PREFIX} $2"
   local extra=$3

   local use_make=0
   local use_meson=0
   local try_cmake=$TRY_CMAKE
   local use_build_dir=$USE_BUILD_DIR
   for ef in $extra; do
      [ "$ef" = "USE_MAKE" ] && use_make=1
      [ "$ef" = "USE_MESON" ] && use_meson=1
      [ "$ef" = "NO_CMAKE" ] && try_cmake=0
      [ "$ef" = "NO_BUILD_DIR" ] && use_build_dir=0
   done

   local build_dir="$src_dir"
   [ $use_build_dir -eq 1 ] && build_dir="$src_dir/$BUILD_DIR/"

   local do_build=$BUILD_ANYWAY

   echo -e "\n==== work on $PURPLE${src_dir##*/}$RESET in $GREEN$build_dir$RESET"
   pushd $src_dir > /dev/null || fatal "pushd"

   if [ $GIT_PULL -eq 1 ]
   then
      echo -e "  $RED:: git pull$RESET"
      local STASH=$(git stash | grep Saved | wc -l)
      git checkout master
      local HEAD_PREV=$(git log HEAD~1.. --pretty=format:'%H')
      git pull --rebase origin
      local HEAD_AFTER=$(git log HEAD~1.. --pretty=format:'%H')
      [ $STASH -gt 0 ] && git stash pop
      [ "$HEAD_PREV" != "$HEAD_AFTER" ] && do_build=1
   fi

   if [ $do_build -gt 0 -a $use_make -eq 1 ]
   then
      do_make "$2"
   elif [ $do_build -gt 0 -a $use_meson -eq 1 -a -f "meson.build" ]
   then
      do_meson "$2"
   elif [ $do_build -gt 0 ]
   then
      local configure="./configure"
      local autogen="./autogen.sh"
      local cmakedir="./"
      local cmakefile="./CMakeLists.txt"

      echo -e "  $RED:: clean $RESET"
      [ $CLEAN_LEVEL -ge 4 -a -d $BUILD_DIR ] && exec_cmd "rm -fr $BUILD_DIR" 2>/dev/null
      if [ $use_build_dir -eq 1 ]
      then
         [ ! -d "$BUILD_DIR" ] && ( mkdir "$BUILD_DIR" || fatal "mkdir" )
         pushd "$BUILD_DIR" >/dev/null || fatal "pushd"
         configure=".$configure"
         cmakedir=".$cmakedir"
         cmakefile=".$cmakefile"
      fi

      [ $CLEAN_LEVEL -ge 2 -a -f Makefile ] && exec_cmd "make distclean" >/dev/null || true
      [ $CLEAN_LEVEL -ge 1 -a -f Makefile ] && exec_cmd "make clean" >/dev/null || true
      if [ $CLEAN_LEVEL -ge 5 -a -f "$autogen" ]
      then
         say "$autogen" && NOCONFIGURE=1 $autogen >/dev/null 2>&1
         [ -f "$configure" ] || fatal "no $configure -> no can do !!!"
      fi
      if [ $CLEAN_LEVEL -ge 3 -o ! -f Makefile ]
      then
         if [ $try_cmake -eq 1 -a -f "$cmakefile" ]
         then
            exec_cmd "cmake $cmakedir -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX}" || fatal "cmake"
         else
            exec_cmd "$configure $autogen_flags" || fatal "configure"
         fi
      fi

      echo -e "  $RED:: build $RESET"
      exec_cmd "make $MAKE_J" || fatal "make"
      echo -e "  $RED:: install $RESET"
      say "make install" && echo "$SUDO_PASSWD" | sudo -S make install >/dev/null 2>&1 || fatal "make install"
      echo "$SUDO_PASSWD" | sudo -S ldconfig
      [ $use_build_dir -eq 1 ] && popd
   fi

   popd
}

function do_make()
{
    echo -e "  $RED:: build $RESET"
    exec_cmd "make $MAKE_J" || fatal "make"
    echo -e "  $RED:: install $RESET"
    say "make install" && echo "$SUDO_PASSWD" | sudo -S make install >/dev/null 2>&1 || fatal "make install"
}

function do_meson()
{
   local mesontoken="build/meson-private/coredata.dat" # mesonmain.py#validate_dirs
   echo -e "  $RED:: clean $RESET"
   [ $CLEAN_LEVEL -ge 4 -a -d $BUILD_DIR ] && exec_cmd "rm -fr $BUILD_DIR" 2>/dev/null
   if [ ! -f "$mesontoken" ]
   then
      exec_cmd "meson --prefix=${PREFIX} $1 $BUILD_DIR" || fatal "meson"
   else
      [ $CLEAN_LEVEL -ge 3 ] && ( exec_cmd "meson configure --clearcache $BUILD_DIR" || fatal "meson configure" )
   fi
   [ $CLEAN_LEVEL -ge 1 -a $CLEAN_LEVEL -lt 4 -a -f $BUILD_DIR/build.ninja ] && exec_cmd "ninja -C $BUILD_DIR clean"
   echo -e "  $RED:: build $RESET"
   exec_cmd "ninja -C $BUILD_DIR" || fatal "ninja"
   echo -e "  $RED:: install $RESET"
   say "ninja -C $BUILD_DIR install" && echo "$SUDO_PASSWD" | sudo -S ninja -C $BUILD_DIR install >/dev/null 2>&1 || fatal "make install"
   echo "$SUDO_PASSWD" | sudo -S ldconfig
}