[M67 Dev][EFL] Set up build environment
[platform/framework/web/chromium-efl.git] / tizen_src / build / common.sh
1 #!/bin/bash
2
3 export SCRIPTDIR=$(readlink -e $(dirname $0))
4 export TOPDIR=$(readlink -f "${SCRIPTDIR}/../..")
5 export CHROME_SRC="${TOPDIR}"
6
7 function getHostOs() {
8   echo $(uname -s | sed -e 's/Linux/linux/;s/Darwin/mac/')
9 }
10
11 function getHostArch() {
12   echo $(uname -m | sed -e \
13         's/i.86/ia32/;s/x86_64/x64/;s/amd64/x64/;s/arm.*/arm/;s/i86pc/ia32/;s/aarch64/arm64/')
14 }
15
16 function getPythonVersion() {
17   echo $(python --version  2>&1 | sed -e 's/Python \([0-9]\+\.[0-9]\+\)\.[0-9]\+/\1/')
18 }
19
20 function setIfUndef() {
21     eval original=\$$1
22     new=$2
23     if [ -n "$original" ]; then
24         echo $original
25     else
26         echo $new
27     fi
28 }
29
30
31 function hostBuldScriptUsage() {
32 cat << EOF
33 usage: $1 [OPTIONS]
34
35 Build non gbs version of chromium-efl
36
37 OPTIONS:
38    -h, --help            Show this message
39    --build-ewk-unittests Build ewk unittests
40    --ccache              Configure ccache installed in your system
41    --clang               Use chromium's clang compiler to build the sources
42    --no-content-shell    Don't build content_shell application
43    --debug               Build debug version of chromium-efl (out.${host_arch}/Debug instead of out.${host_arch}/Release)
44    -jN                   Set number of jobs, just like with make or ninja
45    --skip-gyp            Skip restore_gyp, jhbuild and gyp_chromium steps
46    --skip-ninja          Skip ninja step
47    --xwalk               Build crosswalk and friends
48
49 examples:
50 $0 --skip-gyp
51 $0 --skip-gyp --ccache
52 $0 --skip-ninja
53 EOF
54   exit
55 }
56
57 function parseHostBuildScriptParams() {
58
59   export SKIP_GYP=0
60   export USE_CCACHE=0
61   export USE_CLANG=0
62   export FORCE_JHBUILD=0
63   export SKIP_NINJA=0
64   export BUILD_EWK_UNITTESTS=0
65   export BUILD_CONTENT_SHELL=1
66   export BUILD_XWALK=0
67   export BUILD_SUBDIRECTORY=Release
68   export COMPONENT_BUILD=0
69
70   local platform="$1"
71   shift
72
73   while [[ $# > 0 ]]; do
74     case "$1" in
75       -h|--help)
76         hostBuldScriptUsage ${0}
77         ;;
78       --skip-gyp)
79         export SKIP_GYP=1
80         ;;
81       --skip-gn)
82         export SKIP_GN=1
83         ;;
84       --ccache)
85         echo using ccache
86         export USE_CCACHE=1
87         source $TOPDIR/tizen_src/build/ccache_env.sh ${platform}
88         ;;
89       --clang)
90         export USE_CLANG=1
91         ;;
92       --no-content-shell)
93         export BUILD_CONTENT_SHELL=0
94         ;;
95       --force-jhbuild)
96         export FORCE_JHBUILD=1
97         ;;
98       --skip-ninja)
99         export SKIP_NINJA=1
100         ;;
101       --build-ewk-unittests)
102         export BUILD_EWK_UNITTESTS=1
103         ;;
104       --debug)
105         export BUILD_SUBDIRECTORY="Debug"
106         ;;
107       --xwalk)
108         export BUILD_XWALK=1
109         ;;
110       --component-build)
111         export COMPONENT_BUILD=1
112         ;;
113       -j*)
114         export JOBS="$1"
115         ;;
116       *)
117         echo "Unknown argument: $1"
118         exit 1
119         ;;
120     esac
121     shift;
122   done
123 }
124
125 function hostGypChromiumEfl() {
126   if [[ $SKIP_GYP == 0 ]]; then
127     local XWALK_ARG=""
128     local COMPONENT_ARG=""
129     if [[ $BUILD_XWALK == 1 ]]; then
130       XWALK_ARG="--xwalk"
131     fi
132     if [[ $COMPONENT_BUILD == 1 ]]; then
133       COMPONENT_ARG="-Dcomponent=shared_library"
134     fi
135     ${TOPDIR}/tizen_src/build/gyp_chromiumefl.sh \
136       $XWALK_ARG \
137       $COMPONENT_ARG \
138       -Doutdir="$TOPDIR/out.$(getHostArch)" \
139       $@
140   fi
141 }
142
143 function hostGnChromiumEfl() {
144   if [[ $SKIP_GN != 1 ]]; then
145     local XWALK_ARG=""
146     local COMPONENT_ARG=""
147     if [[ $BUILD_XWALK == 1 ]]; then
148       XWALK_ARG="xwalk=true"
149     fi
150     if [[ $COMPONENT_BUILD == 1 ]]; then
151       COMPONENT_ARG="component=shared_library"
152     fi
153     ${TOPDIR}/tizen_src/build/gn_chromiumefl.sh \
154       $XWALK_ARG \
155       $COMPONENT_ARG \
156       "outdir=\"$TOPDIR/out.$(getHostArch)\"" \
157       "chromium_efl_tizen_version=30" \
158       $@
159   fi
160 }
161
162 function hostNinja() {
163   if [[ $SKIP_NINJA == 0 ]]; then
164     TARGETS="chromium-ewk efl_webprocess efl_webview_app ubrowser"
165     if [[ $BUILD_EWK_UNITTESTS == 1 ]]; then
166       TARGETS="$TARGETS ewk_unittests"
167     fi
168     if [[ $BUILD_CONTENT_SHELL == 1 ]]; then
169       TARGETS="$TARGETS content_shell dump_syms minidump_stackwalk"
170     fi
171     if [[ $BUILD_XWALK == 1 ]]; then
172       TARGETS="$TARGETS xwalk"
173     fi
174     if [[ $BUILD_CHROMEDRIVER == 1 ]]; then
175       TARGETS="$TARGETS chromedriver"
176     fi
177     export BUILD_SUBDIRECTORY=""
178     BUILDDIR=${GN_GENERATOR_OUTPUT}/${BUILD_SUBDIRECTORY}
179     ninja -C $BUILDDIR ${JOBS} ${TARGETS}
180   fi
181 }
182
183 function error_report() {
184   echo "Error: File:$1 Line:$2"
185   exit 1
186 }
187
188 function findElementInArray() {
189   local elm
190   for elm in "${@:2}"; do
191     [[ "$elm" = "$1" ]] && return 0;
192   done
193   return 1;
194 }
195
196 function setupAndExecuteTargetBuild() {
197
198   local platform="$1"
199   shift
200
201   local PROFILE
202   local ARCHITECTURE
203   local CONF_FLAG
204   local SPEC_FILE="chromium-efl.spec"
205   local -a ARGS
206
207   # "|| :" means "or always succeeding built-in command"
208   PROFILE=$(echo "$@" | grep -Po "(?<=\-P\s)[^\s]*" || :)
209   ARCHITECTURE=$(echo "$@" | grep -Po "(?<=\-A\s)[^\s]*" || :)
210
211   if [ "$PROFILE" == "" ]; then
212     if [[ $platform == "mobile" ]]; then
213       PROFILE=tzmb_v3.0_arm64-wayland
214     elif [[ $platform == "mobile_emulator" ]]; then
215       PROFILE=tzmb_v3.0_emulator-wayland
216     elif [[ $platform == "tv" ]]; then
217       PROFILE=tztv_v3.0_arm-wayland
218     elif [[ $platform == "tv_emulator" ]]; then
219       PROFILE=tztv_v3.0_emulator-wayland
220     else
221       echo "Cannot set default PROFILE for platform=${platform}"
222       exit 1
223     fi
224   fi
225   echo "Set the profile : $PROFILE"
226
227   if [ "$ARCHITECTURE" == "" ]; then
228     if [[ $platform == "mobile" ]]; then
229       ARCHITECTURE=aarch64
230     elif [[ $platform == "mobile_emulator" ]]; then
231       ARCHITECTURE=i586
232     elif [[ $platform == "tv" ]]; then
233       ARCHITECTURE=armv7l
234     elif [[ $platform == "tv_emulator" ]]; then
235       ARCHITECTURE=i586
236     else
237       echo "Cannot set default ARCHITECTURE for platform=${platform}"
238       exit 1
239     fi
240   fi
241   echo "Set the architecture : $ARCHITECTURE"
242
243   if [ "$USE_GLOBAL_GBS_CONF" == "" ]; then
244     CONF_FLAG="--conf ${SCRIPTDIR}/gbs.conf"
245   fi
246
247   EXTRA_PACK_OPTS=""
248   if [ "$PROFILE" == "tztv_v2.4_product" ]; then
249     EXTRA_PACK_OPTS="--extra-packs python-base-x86-arm,python-x86-arm,python-xml-x86-arm"
250   fi
251
252   local count=0
253   local exclusive_options=0
254   local RPMLINT=0
255   local NOINIT=0
256   while [[ $# > 0 ]]; do
257     count=$(( $count + 1 ))
258     case "$1" in
259     --debug)
260         ARGS[$count]=--define
261         count=$(( $count + 1 ))
262         ARGS[$count]="_debug_mode 1"
263     ;;
264     --skip-ninja)
265         ARGS[$count]=--define
266         count=$(( $count + 1 ))
267         ARGS[$count]="_skip_ninja 1"
268     ;;
269     --skip-gyp)
270         ARGS[$count]=--define
271         count=$(( $count + 1 ))
272         ARGS[$count]="_skip_gyp 1"
273     ;;
274     --skip-gn)
275         ARGS[$count]=--define
276         count=$(( $count + 1 ))
277         ARGS[$count]="_skip_gn 1"
278     ;;
279     --xwalk)
280         ARGS[$count]=--define
281         count=$(( $count + 1 ))
282         ARGS[$count]="build_xwalk 1"
283     ;;
284     --component-build)
285         ARGS[$count]=--define
286         count=$(( $count + 1 ))
287         ARGS[$count]="component_build 1"
288     ;;
289     --libs)
290         target="libs"
291         exclusive_options=$(( $exclusive_options + 1 ))
292     ;;
293     --crosswalk-bin)
294         target="crosswalk-bin"
295         exclusive_options=$(( $exclusive_options + 1 ))
296     ;;
297     --gbs-debug)
298         ARGS[$count]=--debug
299     ;;
300     --rpmlint)
301         RPMLINT=1
302         count=$(( $count + 1 ))
303     ;;
304     --noinit)
305         NOINIT=1
306         ARGS[$count]="$1"
307         count=$(( $count + 1 ))
308     ;;
309     *)
310       ARGS[$count]="$1"
311     ;;
312     esac
313     shift;
314   done
315
316   if [ "$exclusive_options" -gt 1 ]; then
317     echo "ERROR: --libs and --crosswalk-bin are mutually exclusive parameters."
318     exit 1
319   fi
320
321   cd $TOPDIR
322   rm -f packaging
323   ln -sf tizen_src/packaging packaging
324
325   if [ "$target" == "libs" ]; then
326     SPEC_FILE="chromium-efl-libs.spec"
327   elif [ "$target" == "crosswalk-bin" ]; then
328     SPEC_FILE="crosswalk-bin.spec"
329   fi
330
331   if [ "$(echo "${PROFILE}" | grep -Po "v3.0")" != "" -a "$NOINIT" == 0 ]; then
332     prepareTizen3Build $PROFILE $RPMLINT
333   fi
334
335   gbs $CONF_FLAG build -P $PROFILE --spec $SPEC_FILE --include-all -A $ARCHITECTURE "${ARGS[@]}" $BUILD_CONF_OPTS $EXTRA_PACK_OPTS --incremental
336 }
337
338 function prepareTizen3Build() {
339   local PROFILE=$1
340   local RPMLINT=$2
341   BUILD_CONF_OPTS=""
342
343   # In case of '_rpmlint 1', build.conf of gbs repo will be used, enabling rpmlint.
344   # Otherwise, locally prepared build.conf will be used, disabling rpmlint.
345   local PREVIOUS_BUILD_CONF_PATH=""
346
347   if [ "$PROFILE" == "tztv_v3.0_arm-wayland" ]; then
348     PREVIOUS_BUILD_CONF_PATH="/var/tmp/$USER-gbs/tztv_v3.0_arm_wayland.conf"
349   elif [ "$PROFILE" == "tzmb_v3.0_arm-wayland" ]; then
350     PREVIOUS_BUILD_CONF_PATH="/var/tmp/$USER-gbs/tzmb_v3.0_arm_wayland.conf"
351   elif [ "$PROFILE" == "tzmb_v3.0_arm64-wayland" ]; then
352     PREVIOUS_BUILD_CONF_PATH="/var/tmp/$USER-gbs/tzmb_v3.0_arm64_wayland.conf"
353   fi
354
355   local PREVIOUS_RPMLINT="!rpmlint"
356   if [ ! -f "$PREVIOUS_BUILD_CONF_PATH" ]; then
357     # Never built before
358     PREVIOUS_RPMLINT=""
359   elif [ "$(grep -Po "\!rpmlint" "$PREVIOUS_BUILD_CONF_PATH" | uniq -d)" != "!rpmlint" ]; then
360     # Once built with rpmlint
361     PREVIOUS_RPMLINT="rpmlint"
362   fi
363
364   # Disable rpmlint
365   if [ "$RPMLINT" == 0 ]; then
366     echo "** Disable rpmlint"
367     BUILD_CONF_DIR=$SCRIPTDIR/build_conf
368     rm -rf $BUILD_CONF_DIR
369     mkdir $BUILD_CONF_DIR
370     REPO=http://download.tizen.org/snapshots/tizen/$platform/latest/repos/$(echo $ARCHITECTURE | sed -e "s/armv7l/arm/;s/aarch64/arm64/")-wayland/packages/repodata/
371     wget $REPO -O $BUILD_CONF_DIR/index.html > /dev/null 2>&1
372     BUILD_CONF_GZ=$(awk -F\" '{ print $6 }' $BUILD_CONF_DIR/index.html | grep "build.conf.gz")
373     BUILD_CONF=$(basename $BUILD_CONF_GZ .gz)
374     wget $REPO$BUILD_CONF_GZ -P $BUILD_CONF_DIR > /dev/null 2>&1
375     if [ ! -f "$BUILD_CONF_DIR/$BUILD_CONF_GZ" ]; then
376       echo "It's failed to donwload build.conf. Please contact system administrator."
377       exit 1
378     fi
379     gunzip $BUILD_CONF_DIR/$BUILD_CONF_GZ
380     sed -i 's/rpmlint-mini\ rpmlint-tizen/!rpmlint-mini\ !rpmlint-tizen/g' $BUILD_CONF_DIR/$BUILD_CONF
381     BUILD_CONF_OPTS="-D $BUILD_CONF_DIR/$BUILD_CONF"
382     if [ "$PREVIOUS_RPMLINT" == "rpmlint" ]; then
383       echo "** Once built with rpmlint"
384       BUILD_CONF_OPTS+=" --clean"
385     fi
386   else # Enable rpmlint
387     echo "** Enable rpmlint"
388     BUILD_CONF_OPTS=""
389     if [ "$PREVIOUS_RPMLINT" == "!rpmlint" ]; then
390       BUILD_CONF_OPTS="--clean"
391     fi
392   fi
393 }