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