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