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