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