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