Initialize Tizen 2.3
[framework/web/webkit-efl.git] / Tools / gtk / common.py
1 #!/usr/bin/env python
2 # Copyright (C) 2011 Igalia S.L.
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17
18 import errno
19 import os
20 import select
21 import subprocess
22 import sys
23
24 script_dir = None
25 build_dir = None
26
27
28 def script_path(*args):
29     global script_dir
30     if not script_dir:
31         script_dir = os.path.join(os.path.dirname(__file__), '..', 'Scripts')
32     return os.path.join(*(script_dir,) + args)
33
34
35 def top_level_path(*args):
36     return os.path.join(*((script_path('..', '..'),) + args))
37
38
39 def get_build_path(build_types=('Release', 'Debug')):
40     global build_dir
41     if build_dir:
42         return build_dir
43
44     def is_valid_build_directory(path):
45         return os.path.exists(os.path.join(path, 'GNUmakefile')) or \
46             os.path.exists(os.path.join(path, 'Programs', 'DumpRenderTree'))
47
48     if len(sys.argv[1:]) > 1 and os.path.exists(sys.argv[-1]) and is_valid_build_directory(sys.argv[-1]):
49         return sys.argv[-1]
50
51     # Debian and Ubuntu build both flavours of the library (with gtk2
52     # and with gtk3); they use directories build-2.0 and build-3.0 for
53     # that, which is not handled by the above cases; we check that the
54     # directory where we are called from is a valid build directory,
55     # which should handle pretty much all other non-standard cases.
56     build_dir = os.getcwd()
57     if is_valid_build_directory(build_dir):
58         return build_dir
59
60     for build_type in build_types:
61         build_dir = top_level_path('WebKitBuild', build_type)
62         if is_valid_build_directory(build_dir):
63             return build_dir
64
65     # distcheck builds in a directory named _build in the top-level path.
66     build_dir = top_level_path("_build")
67     if is_valid_build_directory(build_dir):
68         return build_dir
69
70     build_dir = top_level_path()
71     if is_valid_build_directory(build_dir):
72         return build_dir
73
74     build_dir = top_level_path("WebKitBuild")
75     if is_valid_build_directory(build_dir):
76         return build_dir
77
78     print('Could not determine build directory.')
79     sys.exit(1)
80
81
82 def build_path_for_build_types(build_types, *args):
83     return os.path.join(*(get_build_path(build_types),) + args)
84
85
86 def build_path(*args):
87     return build_path_for_build_types(('Release', 'Debug'), *args)
88
89
90 def pkg_config_file_variable(package, variable):
91     process = subprocess.Popen(['pkg-config', '--variable=%s' % variable, package],
92                                stdout=subprocess.PIPE)
93     stdout = process.communicate()[0].decode("utf-8")
94     if process.returncode:
95         return None
96     return stdout.strip()
97
98
99 def prefix_of_pkg_config_file(package):
100     return pkg_config_file_variable(package, 'prefix')
101
102
103 def gtk_version_of_pkg_config_file(pkg_config_path):
104     process = subprocess.Popen(['pkg-config', pkg_config_path, '--print-requires'],
105                                stdout=subprocess.PIPE)
106     stdout = process.communicate()[0].decode("utf-8")
107
108     if 'gtk+-3.0' in stdout:
109         return 3
110     return 2
111
112
113 def parse_output_lines(fd, parse_line_callback):
114     output = ''
115     read_set = [fd]
116     while read_set:
117         try:
118             rlist, wlist, xlist = select.select(read_set, [], [])
119         except select.error as e:
120             parse_line_callback("WARNING: error while waiting for fd %d to become readable\n" % fd)
121             parse_line_callback("    error code: %d, error message: %s\n" % (e[0], e[1]))
122             continue
123
124         if fd in rlist:
125             try:
126                 chunk = os.read(fd, 1024)
127             except OSError as e:
128                 if e.errno == errno.EIO:
129                     # Child process finished.
130                     chunk = ''
131                 else:
132                     raise e
133             if not chunk:
134                 read_set.remove(fd)
135
136             output += chunk
137             while '\n' in output:
138                 pos = output.find('\n')
139                 parse_line_callback(output[:pos + 1])
140                 output = output[pos + 1:]
141
142             if not chunk and output:
143                 parse_line_callback(output)
144                 output = ''