[GTK] [CMake] Add support for generating gtkdoc
[WebKit-https.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 library_build_dir = None
27 is_cmake = None
28
29
30 def is_cmake_build():
31     global is_cmake
32
33     if is_cmake is None:
34         if build_path('CMakeCache.txt'):
35             is_cmake = True
36         else:
37             is_cmake = False
38
39     return is_cmake
40
41
42 def library_build_path(*args):
43     global library_build_dir
44     if not library_build_dir:
45         if is_cmake_build():
46             library_build_dir = build_path('lib', *args)
47         else:
48             library_build_dir = build_path('.libs', *args)
49     return library_build_dir
50
51
52 def script_path(*args):
53     global script_dir
54     if not script_dir:
55         script_dir = os.path.join(os.path.dirname(__file__), '..', 'Scripts')
56     return os.path.join(*(script_dir,) + args)
57
58
59 def top_level_path(*args):
60     return os.path.join(*((os.path.join(os.path.dirname(__file__), '..', '..'),) + args))
61
62
63 def get_build_path(build_types=('Release', 'Debug'), fatal=True):
64     global build_dir
65     if build_dir:
66         return build_dir
67
68     def is_valid_build_directory(path):
69         return os.path.exists(os.path.join(path, 'GNUmakefile')) or \
70             os.path.exists(os.path.join(path, 'Programs', 'GtkLauncher')) or \
71             os.path.exists(os.path.join(path, 'Programs', 'MiniBrowser')) or \
72             os.path.exists(os.path.join(path, 'CMakeCache.txt')) or \
73             os.path.exists(os.path.join(path, 'bin/GtkLauncher')) or \
74             os.path.exists(os.path.join(path, 'bin/MiniBrowser'))
75
76     if len(sys.argv[1:]) > 1 and os.path.exists(sys.argv[-1]) and is_valid_build_directory(sys.argv[-1]):
77         return sys.argv[-1]
78
79     # Debian and Ubuntu build both flavours of the library (with gtk2
80     # and with gtk3); they use directories build-2.0 and build-3.0 for
81     # that, which is not handled by the above cases; we check that the
82     # directory where we are called from is a valid build directory,
83     # which should handle pretty much all other non-standard cases.
84     build_dir = os.getcwd()
85     if is_valid_build_directory(build_dir):
86         return build_dir
87
88     for build_type in build_types:
89         build_dir = top_level_path('WebKitBuild', build_type)
90         if is_valid_build_directory(build_dir):
91             return build_dir
92
93     # distcheck builds in a directory named _build in the top-level path.
94     build_dir = top_level_path("_build")
95     if is_valid_build_directory(build_dir):
96         return build_dir
97
98     build_dir = top_level_path()
99     if is_valid_build_directory(build_dir):
100         return build_dir
101
102     build_dir = top_level_path("WebKitBuild")
103     if is_valid_build_directory(build_dir):
104         return build_dir
105
106     print('Could not determine build directory.')
107     if fatal:
108         sys.exit(1)
109
110
111 def build_path_for_build_types(build_types, *args):
112     return os.path.join(*(get_build_path(build_types),) + args)
113
114
115 def build_path(*args):
116     return build_path_for_build_types(('Release', 'Debug'), *args)
117
118
119 def pkg_config_file_variable(package, variable):
120     process = subprocess.Popen(['pkg-config', '--variable=%s' % variable, package],
121                                stdout=subprocess.PIPE)
122     stdout = process.communicate()[0].decode("utf-8")
123     if process.returncode:
124         return None
125     return stdout.strip()
126
127
128 def prefix_of_pkg_config_file(package):
129     return pkg_config_file_variable(package, 'prefix')
130
131
132 def gtk_version_of_pkg_config_file(pkg_config_path):
133     process = subprocess.Popen(['pkg-config', pkg_config_path, '--print-requires'],
134                                stdout=subprocess.PIPE)
135     stdout = process.communicate()[0].decode("utf-8")
136
137     if 'gtk+-3.0' in stdout:
138         return 3
139     return 2
140
141
142 def parse_output_lines(fd, parse_line_callback):
143     output = ''
144     read_set = [fd]
145     while read_set:
146         try:
147             rlist, wlist, xlist = select.select(read_set, [], [])
148         except select.error as e:
149             parse_line_callback("WARNING: error while waiting for fd %d to become readable\n" % fd)
150             parse_line_callback("    error code: %d, error message: %s\n" % (e[0], e[1]))
151             continue
152
153         if fd in rlist:
154             try:
155                 chunk = os.read(fd, 1024)
156             except OSError as e:
157                 if e.errno == errno.EIO:
158                     # Child process finished.
159                     chunk = ''
160                 else:
161                     raise e
162             if not chunk:
163                 read_set.remove(fd)
164
165             output += chunk
166             while '\n' in output:
167                 pos = output.find('\n')
168                 parse_line_callback(output[:pos + 1])
169                 output = output[pos + 1:]
170
171             if not chunk and output:
172                 parse_line_callback(output)
173                 output = ''