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