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