[dali_1.9.31] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / build / scripts / dali_env
1 #!/usr/bin/perl
2
3 # Copyright (c) 2020 Samsung Electronics Co., Ltd.
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8
9 # http://www.apache.org/licenses/LICENSE-2.0
10
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 use Config;
18 use Cwd;
19 use Cwd 'abs_path';
20 use File::Basename;
21 use File::Path;
22 use File::Copy;
23 use File::Copy::Recursive qw(dircopy);
24 use strict;
25 use Getopt::Long;
26 use Pod::Usage;
27
28 ################################################################################
29 #                                SYSTEM PACKAGES                               #
30 ################################################################################
31 # Add any required system packages to this list - if they are not present, then
32 # this script will attempt to install them for you.
33 my @system_packages = (
34     "automake",
35     "cmake",
36     "g++",
37     "pkg-config",
38     "libtool",
39     "ccache",
40     "libelementary-dev",
41     "libexif-dev",
42     "libgles2-mesa-dev",
43     "libdrm-dev",
44     "libgif-dev",
45     "libturbojpeg",
46     "libturbojpeg0-dev",
47     "libfribidi-dev",
48     "libharfbuzz-dev",
49     "doxygen",
50     "lcov",
51     "libcurl4-gnutls-dev",
52     "curl",
53     "libssl-dev",
54     "cifs-utils",
55     "libgtest-dev",
56     "libcairo2-dev",
57     "libopencv-dev",
58     "gettext",
59     "libwebp-dev",
60 );
61
62 # Some packages like require building from source
63 # v8 is currently disabled until we can get it working without a http proxy being setup
64 my @source_pkgs = (
65
66     {"name" => "disabled-v8",
67      "force-rebuild" => 0,
68      "use_depot_tools" => 1,
69      "repo" => "https://chromium.googlesource.com/v8/v8.git",
70      "depot_tools_repo" => "https://chromium.googlesource.com/chromium/tools/depot_tools.git",
71
72      # original version used with DALi is 3.25.19. 3.32.7 is the latest we can use before
73      # upgrading DALi to use  c++0x or c++11
74      "version" => " 3.32.7", "make" => "make -j8 library=shared", "build-mode" =>"debug" },
75     {"name" => "gtest" },
76 );
77
78 ### Detect any http proxy, part of v8 installation requires this information
79 my $http_proxy_port;
80 my $http_proxy_ip;
81
82 if( exists $ENV{http_proxy} )
83 {
84   # string  split into 3 items  http, //ip, port
85   my @http_proxy_info =  split( /:/,$ENV{http_proxy}, );
86
87   $http_proxy_ip =  @http_proxy_info[1];
88   $http_proxy_ip =~ s/[\/]//g;;  # remove forward slashes
89   $http_proxy_port =  @http_proxy_info[2];
90 }
91
92 # Make best guess as to where this program was run from (note, it is
93 # always possible to override the location of $0 by the calling
94 # program, so we can't really tell for sure that this is where we
95 # expect it to be. :/
96
97 my $new_env   = 0;
98 my $exec_path = $0;
99 if($0 !~ m!^/!)
100 {
101     $exec_path = abs_path($0);
102 }
103 $exec_path = dirname($exec_path);
104
105 my $root_path = getcwd();
106
107 # Search for the root dali-env directory
108 if($exec_path =~ m!dali-env/opt/bin!)
109 {
110     # We are using the installed dali_env script
111
112     $root_path = $exec_path;
113     while(basename($root_path) ne "dali-env" && $root_path ne "")
114     {
115         $root_path = dirname($root_path);
116     }
117 }
118 elsif($root_path =~ m!dali-env$! or $root_path =~ m!dali-env/!)
119 {
120     # We are NOT using the installed dali_env script
121     # Find dali-env root from current directory
122
123     while(basename($root_path) ne "dali-env" && $root_path ne "")
124     {
125         $root_path = dirname($root_path);
126     }
127 }
128 else
129 {
130     # dali-env root dir should be in the current directory
131
132     $root_path .= "/dali-env";
133
134     if(! -e $root_path)
135     {
136       # Creating a new dali-env
137
138       $new_env = 1;
139     }
140 }
141
142 my $src_path         = "$root_path/src-packages";
143 my $sbs_path         = "$root_path/target";
144 my $install_path     = "$root_path/opt";
145
146 my $opt_create=0;
147 my $opt_setenv=0;
148 my $opt_help=0;
149 my $opt_man=0;
150
151 GetOptions("create"     => \$opt_create,
152            "setenv"     => \$opt_setenv,
153            "help"       => \$opt_help,
154            "man"        => \$opt_man) or pod2usage(2);
155
156 pod2usage(1) if $opt_help;
157 pod2usage(-exitstatus => 0, -verbose => 2) if $opt_man;
158
159
160 ################################################################################
161
162 sub create_env
163 {
164     mkpath("$install_path/bin");
165     mkpath("$install_path/lib/pkgconfig");
166     mkpath("$install_path/include");
167     mkpath("$install_path/share/aclocal");
168     mkpath("$src_path");
169     mkpath("$sbs_path");
170
171     copy($0, "$install_path/bin/dali_env");
172     chmod(0755, "$install_path/bin/dali_env");
173 }
174
175 ################################################################################
176
177 sub in_dali_env
178 {
179     my $cwd = substr(getcwd(), 0, length($root_path));
180     #print "cwd = $cwd\nroot = $root_path\n";
181     return $cwd eq $root_path;
182 }
183
184 ################################################################################
185
186 sub create_setenv
187 {
188     print <<"EOF";
189 #
190 # To use the desktop libraries, please add the following lines to your .bashrc or
191 # create a setenv script from them, e.g. by running this command as follows
192 # \$ $install_path/bin/dali_env -s > setenv
193 #
194 # You can then source this script by using
195 # \$ . setenv
196 #
197 # Use DESKTOP_PREFIX when running configure or cmake in dali/build/tizen:
198 # \$ CXXFLAGS="-g -O0" ./configure --prefix=\$DESKTOP_PREFIX
199 # _OR_
200 # \$ CXXFLAGS="-g -O0" cmake -DCMAKE_INSTALL_PREFIX=\$DESKTOP_PREFIX
201
202 export DESKTOP_PREFIX=$install_path
203 export PATH=$install_path/bin:\$PATH
204 export LD_LIBRARY_PATH=$install_path/lib:\$LD_LIBRARY_PATH
205 export INCLUDEDIR=$install_path/include
206 export PKG_CONFIG_PATH=$install_path/lib/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig
207 export DOTNET_CLI_TELEMETRY_OPTOUT=1
208 export DALI_WINDOW_WIDTH=480
209 export DALI_WINDOW_HEIGHT=800
210
211 EOF
212 }
213
214 ################################################################################
215
216 sub check_system_package
217 {
218     my $package;
219     foreach $package (@_)
220     {
221         my @x=split(/\s+/, `dpkg -l $package 2> /dev/null|grep $package`);
222         if($x[0] ne "ii")
223         {
224             # Check if the package is available to install, exit-code is 0 if the package is found.
225             if(system("apt-cache show $package > /dev/null 2>&1") == 0)
226             {
227                 print "Attempting to install $package\n";
228                 system("sudo apt-get -y --force-yes install $package");
229             }
230         }
231     }
232 }
233
234 sub check_system_packages
235 {
236     print "Checking for required system packages (may require sudo password)\n";
237
238     check_system_package(@system_packages);
239
240     my $gnome_v =`dpkg -l gnome-common| tail -1| sed "s/ \\+/ /g" | cut -d' ' -f 3`;
241     my @am = split(/\./, `automake --version | head -1 | cut -f4 -d' '`);
242     if($gnome_v =~ /$2.24/ && $am[1]>10)
243     {
244         die "Gnome common and automake are not compatible - automake is too new\n";
245     }
246     my @gpp_v = (`g++ --version  | head -1` =~ /(\d+)\.(\d+)\.(\d+)/);
247
248     if(! (($gpp_v[0] > 4)
249           ||
250           ($gpp_v[0] == 4 && $gpp_v[1] > 4)
251           ||
252           ($gpp_v[0] == 4 && $gpp_v[1] == 4 && $gpp_v[2] >= 5)))
253     {
254         die "You need g++ 4.5.1 or greater to build dali\n";
255     }
256 }
257
258 sub check_source_packages
259 {
260     my $pkgref;
261
262     foreach $pkgref (@source_pkgs)
263     {
264         my $pkg = $pkgref->{"name"};
265         if($pkg eq "v8")
266         {
267             install_v8( $pkgref );
268         }
269         elsif ($pkg eq "gtest")
270         {
271             if(! -e "/usr/lib/libgtest.a")
272             {
273                 print "Attempting to build $pkg\n";
274                 # from https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/
275                 run_command("cd /usr/src/gtest; sudo cmake CMakeLists.txt; sudo make; sudo cp *.a /usr/lib; cd -;");
276             }
277         }
278     }
279 }
280
281 ################################################################################
282
283 sub create_link
284 {
285     my $arch=`uname -i`;
286     $arch =~ s/\r|\n//g;
287
288     my $link = "/usr/lib/$arch-linux-gnu/libturbojpeg.so";
289
290     unless (-e $link)
291     {
292        print "Creating libjpegturbo symbolic link\n";
293        system("sudo ln -s $link.0 $link");
294     }
295 }
296
297 ################################################################################
298 # Helper to run and print out the command being run and quit if it fails
299 #
300 sub run_command
301 {
302   my $command = $_[0];
303   my $ret;
304   print("Running: $command\n");
305   $ret = system("$command");
306   if($ret >> 8) { die "$command failed \n"; }
307 }
308
309 ################################################################################
310 # later versions of v8 (post mid 2014) require googles depot_tools to build.
311 #
312 sub install_google_depot_tools
313 {
314
315 ####
316 # clone the depo_tools into the source directory and set the path up
317 ####
318     my $v8 = $_[0];
319
320     my $depot_tools_directory = $src_path . "/depot_tools";
321     my $depot_tools_repo = $v8->{"depot_tools_repo"};
322
323     # clear the directory if exists
324     rmtree( $depot_tools_directory );
325
326     # clone the depot tools
327     run_command( "git clone " . $depot_tools_repo. " " . $depot_tools_directory );
328
329     # add it the the path
330     $ENV{PATH} = "$ENV{PATH}:$depot_tools_directory";
331
332     # need to setup a config file for the proxy
333     create_boto_config_file( $v8 , $depot_tools_directory );
334
335     # set the config location as an environment variable ( used by scripts depot_tools)
336     $ENV{NO_AUTH_BOTO_CONFIG}="$src_path/depot_tools/.boto";
337
338     # change to depot tools directory
339     chdir( $depot_tools_directory );
340
341     # fetch v8
342     run_command("fetch --nohooks v8");
343
344 }
345
346
347 ################################################################################
348 # later versions of v8 use boto, which currently requires having proxy manually set
349 #
350 sub create_boto_config_file
351 {
352     my $v8 = $_[0];
353     my $depot_tools_directory = $_[1];
354     print(" depot_tools directory = $depot_tools_directory\n");
355
356     print("Configuring boto with http proxy IP = ". $http_proxy_ip . ", Port = " . $http_proxy_port . "\n");
357
358 # Create the proxy info for the boto file
359 my $fileContents = <<"END";
360 [Boto]
361 debug = 0
362 num_retries = 2
363
364 proxy = $http_proxy_ip
365 proxy_port = $http_proxy_port
366 END
367       # Place the config file in the depot tools folder
368     my $filename = $depot_tools_directory . "/" . ".boto";
369     print("Creating Boto config file with proxy settings to file ". $filename . "\n");
370     my $fh;
371     open( $fh, '>',  $filename );
372     print { $fh } $fileContents;
373     close( $fh );
374
375     # export the environment variable
376     run_command("gclient config https://gclient.googlecode.com/svn/trunk/gclient");
377
378     run_command("gclient runhooks");
379
380
381
382 }
383 ################################################################################
384 # We need a specific version of V8 to work with DALi
385 # - Check a txt file in dali-env to see if v8 needs upgrading (checks gcc version too)
386 # - Clones the source
387 # - builds dependencies (v8 automatically clones it's GYP build system)
388 # - Builds it
389 # - Create a package file
390 # It is cloned, then built from source, we create a package file for it, then
391 # it's copied into dali-env
392 sub install_v8
393 {
394     my $v8 = $_[0];
395     my $ret;
396     my $v8Version = $v8->{"version"};
397     print( "Checking if V8 ". $v8Version. " is installed \n");
398
399 ####
400 # Check currently installed version
401 # We create a text file with v8 and gcc version in the filename to compare with
402 # Version file is stored as "v8_2.3.4_installed_built_with_gcc_4_8_3.txt"
403 ####
404     # get the gcc version, so if the compiler is updated v8 is re-built
405     # note: v8 requires gcc version GCC >= 4.6
406     my $gccVersion = `gcc --version | grep ^gcc | sed 's/^.* //g'`;
407     chomp( $gccVersion );
408     my $versionTextFile = $src_path . "/v8_" . $v8Version. "_" . $v8->{"build-mode"} . "_installed_built_with_gcc_". $gccVersion .".txt";
409
410     # use stat to see if file exists
411     my @install_stats = stat $versionTextFile;
412     if( (scalar(@install_stats)) && $v8->{"force-rebuild"} != 1 )
413     {
414       print("Correct V8 version installed\n");
415       return;
416     }
417     else
418     {
419       # delete older versions of the version file first ( otherwise when downgrading it thinks version is still installed)
420       system( "rm " . $src_path . "/v8_*.txt  >/dev/null 2>&1");
421     }
422
423
424 ####
425 # Clone the v8 source repository and checkout the version we want
426 ####
427     # Need to clone it from repo
428     my $v8_source_directory;
429
430
431
432     # newer version of v8 use depot_tools with gclient, git cloned builds do not work
433     if( $v8->{"use_depot_tools"} == 1)
434     {
435       install_google_depot_tools( $v8 );
436
437       # v8 is checkout out under depot_tools path
438       $v8_source_directory = $src_path . "/depot_tools/v8";
439     }
440     else
441     {
442       $v8_source_directory = $src_path . "/v8";
443
444       # delete the old v8 source directpry if exists
445       rmtree( $v8_source_directory );
446
447       # clone the repository
448       run_command( "git clone " . $v8->{"repo"} . " " . $v8_source_directory );
449     }
450
451     # change to the source directoy for the checkout
452     chdir( $v8_source_directory );
453
454     # checkout the version DALi is compatible with
455     run_command( "git checkout ". $v8Version );
456
457 ####
458 # Run make dependencies then make for the specific target
459 ####
460     if( $v8->{"use_depot_tools"} == 1)
461     {
462       run_command("gclient sync");
463     }
464     else
465     {
466       run_command("make dependencies");
467     }
468
469     # assemble the make command
470     my $makeCommand = $v8->{"make"};
471
472     # need to append architecture and build mode, e.g. x64.debug
473     my $buildTarget;
474     if( $Config{use64bitint} ) {
475        print("Building 64 bit version of V8\n");
476        $buildTarget= "x64." . $v8->{"build-mode"}
477     }
478     else{
479       print("Building 32 bit version of V8\n");
480        $buildTarget= "ia32." . $v8->{"build-mode"}
481     }
482     $makeCommand .= " " . $buildTarget;
483     print("Running: $makeCommand\n");
484     run_command( $makeCommand );
485
486 ####
487 # Manually install the library / header files
488 ####
489
490     # Need to manually install (make install not available on v8 )
491     my $libSourceDir = "$v8_source_directory/out/$buildTarget/lib.target/";
492     my $headerSourceDir = "$v8_source_directory/include/";
493
494     my $libDestinationDir = $install_path . "/lib/";
495     my $headerDestinationDir = $install_path . "/include/v8/";
496
497     # delete any current v8 libs
498     system( "rm " . $libDestinationDir . "libv8*");
499     system( "rm " . $libDestinationDir . "libicu*");
500
501
502     # copy the library and header files
503     dircopy( $libSourceDir, $libDestinationDir);
504     dircopy( $headerSourceDir, $headerDestinationDir);
505
506
507     # Copy libv8.so to libv8.so.version (  e.g. libv8.so.1.2.4)
508     my $v8SoFile = $libDestinationDir . "libv8.so";
509     my $v8SoVersionFile = $libDestinationDir . "libv8.so." . $v8Version;
510     move( $v8SoFile, $v8SoVersionFile );
511
512     # symlink the libv8.so.1.2.3 to libv8.so
513     symlink( $v8SoVersionFile, $v8SoFile );
514     print( "source dir = " . $libSourceDir . " dest dir ". $libDestinationDir . " \n" );
515
516
517 ####
518 # Create the package file in,
519 # we keep the library files and header files in v8 sub-directories
520 ####
521 my $fileContents = <<"END";
522 prefix=$install_path
523 exec_prefix=\${prefix}
524 apiversion=$v8Version
525 libdir=\${exec_prefix}/lib
526 includedir=\${prefix}/include/v8
527
528 Name: v8 JavaScript engine - runtime library
529 Description: V8 is Google's open source JavaScript engine.
530 Version: \${apiversion}
531 Libs: -L\${libdir} -lv8 -licuuc -licui18n
532 Cflags: -I\${includedir}
533 END
534
535   my $filename = $install_path . "/lib/pkgconfig/" . "v8.pc";
536   print("writing to file ". $filename . "\n");
537   my $fh;
538   if( open( $fh, '>',  $filename ) )
539   {
540     print { $fh } $fileContents;
541     close( $fh );
542   }
543   else
544   {
545     die "failed to create " . $filename ."\n";
546   }
547
548   print("Installed V8 " .$v8Version . " OK\n");
549
550 #####
551 #
552 ####
553       my $versionFile;
554       open( $versionFile, '>',  $versionTextFile );
555       close( $versionFile );
556       print("Installing V8 version $v8Version\n");
557
558 }
559
560 ################################################################################
561 #                                       MAIN
562 ################################################################################
563
564
565 if($opt_create)
566 {
567     my $new_root = getcwd() . "/dali-env";
568
569     if($exec_path =~ m!dali-env/opt/bin!)
570     {
571         die "Already in a dali-env directory\n";
572         # Could query if user wants to re-create?
573     }
574     elsif(-e $root_path)
575     {
576         die "$root_path already exists\n";
577     }
578     elsif(-e $new_root)
579     {
580         die "A dali-env directory already exists here\n";
581     }
582
583     check_system_packages();
584
585     create_link();
586
587     create_env();
588
589     # do this after source directory structure created in create_env
590     check_source_packages();
591
592     create_setenv();
593 }
594 elsif($opt_setenv)
595 {
596     if(! -d $root_path)
597     {
598         die "$root_path does not exist\n";
599     }
600     elsif($new_env)
601     {
602         die "$root_path is not an existing environment\n";
603     }
604     create_setenv();
605 }
606 else
607 {
608     pod2usage(1);
609 }
610
611 __END__
612
613 =head1 NAME
614
615 dali_env - Create the DALi environment for Ubuntu
616
617 =head1 SYNOPSIS
618
619 dali_env [-c] [-s] [-h|-m]
620
621 =head1 OPTIONS
622
623 =over 28
624
625 =item B<-c|--create>
626
627 Create a DALi environment directory in the current directory.
628
629 =item B<-s|--setenv>
630
631 Display environment variables to setup.
632
633 =item B<-h|--help>
634
635 Display this help
636
637 =item B<-m|--man>
638
639 Display the manual page
640
641 =back
642
643 =head1 DESCRIPTION
644
645 B<dali_env>
646
647 Gets the required dependencies for DALi and them to a local directory. Can also create a setenv script to point to the installation.
648
649 =cut