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