Nix upstreaming - Adding build files and supporting scripts
[WebKit-https.git] / Tools / jhbuild / jhbuild-wrapper
1 #!/usr/bin/env python
2 # Copyright (C) 2011 Igalia S.L.
3 # Copyright (C) 2012 Gustavo Noronha Silva <gns@gnome.org>
4 # Copyright (C) 2012 Intel Corporation
5 # Copyright (C) 2012, 2013 Nokia Corporation and/or its subsidiary(-ies).
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21 import jhbuildutils
22 import os
23 import shlex
24 import subprocess
25 import sys
26
27 jhbuild_revision = '24a7dd8e2be84026bf3b303321adad371a694b23'
28
29 dependencies_path = jhbuildutils.get_dependencies_path()
30 installation_prefix = os.path.abspath(os.path.join(dependencies_path, 'Root'))
31 source_path = os.path.abspath(os.path.join(dependencies_path, 'Source'))
32 jhbuild_source_path = os.path.join(source_path, 'jhbuild')
33 jhbuild_path = os.path.join(installation_prefix, 'bin', 'jhbuild')
34
35 def jhbuild_installed():
36     return os.path.exists(jhbuild_path)
37
38
39 def jhbuild_cloned():
40     return os.path.exists(jhbuild_source_path)
41
42
43 def jhbuild_at_expected_revision():
44     process = subprocess.Popen(['git', 'rev-list', 'HEAD^..'], cwd=jhbuild_source_path,
45                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
46     output, err = process.communicate()
47     if process.returncode != 0:
48         raise Exception('failed to find jhbuild revision: %s' % err)
49
50     return output.strip() == jhbuild_revision
51
52
53 def update_jhbuild():
54     process = subprocess.Popen(['git', 'remote', 'update', 'origin'], cwd=jhbuild_source_path)
55     process.wait()
56     if process.returncode != 0:
57         raise Exception('jhbuild remote update origin failed with return code: %i' % process.returncode)
58
59     process = subprocess.Popen(['git', 'checkout', '%s' % jhbuild_revision],
60                                cwd=jhbuild_source_path)
61     process.wait()
62     if process.returncode != 0:
63         raise Exception('failed to checkout treeish %s: %i' % (jhbuild_revision, process.returncode))
64
65
66 def clone_jhbuild():
67     if not os.path.exists(source_path):
68         os.makedirs(source_path)
69     if not os.path.exists(installation_prefix):
70         os.makedirs(installation_prefix)
71
72     process = subprocess.Popen(['git', 'clone', 'git://git.gnome.org/jhbuild'], cwd=source_path)
73     process.wait()
74     if process.returncode != 0:
75         raise Exception('jhbuild git clone failed with return code: %i' % process.returncode)
76
77
78 def install_jhbuild():
79     if "GREP_OPTIONS" in os.environ:
80         del os.environ['GREP_OPTIONS']
81     process = subprocess.Popen(['bash', './autogen.sh', '--prefix=%s' % installation_prefix], cwd=jhbuild_source_path)
82     process.wait()
83     if process.returncode != 0:
84         raise Exception('jhbuild configure failed with return code: %i' % process.returncode)
85
86     # This is a hackish approach to make the subprocess.Popen call even when people set
87     # MAKE to 'make -j3' instead of using the MAKEFLAGS environment variable.
88     make = shlex.split(os.environ.get('MAKE', 'make'))
89
90     process = subprocess.Popen(make + ['install'], cwd=jhbuild_source_path)
91     process.wait()
92     if process.returncode != 0:
93         raise Exception('jhbuild configure failed with return code: %i' % process.returncode)
94
95
96 def determine_platform():
97     if '--efl' in sys.argv:
98         return "efl";
99     if '--gtk' in sys.argv:
100         return "gtk";
101     if '--nix' in sys.argv:
102         return "nix";
103     raise ValueError('No platform specified for jhbuild-wrapper.')
104
105
106 def ensure_jhbuild(platform):
107     if not jhbuild_cloned():
108         clone_jhbuild()
109         update_jhbuild()
110         install_jhbuild()
111     elif not jhbuild_installed() \
112             or not jhbuild_at_expected_revision():
113         update_jhbuild()
114         install_jhbuild()
115
116 # Work-around the fact that we may get called from inside the jhbuild environment
117 # which will cause problems if we just cleaned the jhbuild install root
118 if os.environ.has_key('UNDER_JHBUILD'):
119     del os.environ['ACLOCAL_FLAGS']
120
121 try:
122     platform = determine_platform()
123 except ValueError as e:
124     sys.exit(e)
125 ensure_jhbuild(platform)
126
127 os.execve(jhbuild_path, [jhbuild_path, '--no-interact', '-f', jhbuildutils.get_config_file_for_platform(platform)] + sys.argv[2:], os.environ)