uninstalled: Add libnice plugin to GST_PLUGIN_PATH
[platform/upstream/gstreamer.git] / gst-uninstalled.py
1 #!/usr/bin/env python3
2
3 import argparse
4 import contextlib
5 import json
6 import os
7 import platform
8 import re
9 import site
10 import shutil
11 import subprocess
12 import sys
13 import tempfile
14 import pathlib
15
16 from distutils.sysconfig import get_python_lib
17 from distutils.util import strtobool
18
19 from scripts.common import get_meson
20 from scripts.common import git
21 from scripts.common import win32_get_short_path_name
22
23 SCRIPTDIR = os.path.dirname(os.path.realpath(__file__))
24 PREFIX_DIR = os.path.join(SCRIPTDIR, 'prefix')
25 # Use '_build' as the builddir instead of 'build'
26 DEFAULT_BUILDDIR = os.path.join(SCRIPTDIR, 'build')
27 if not os.path.exists(DEFAULT_BUILDDIR):
28     DEFAULT_BUILDDIR = os.path.join(SCRIPTDIR, '_build')
29
30
31 def listify(o):
32     if isinstance(o, str):
33         return [o]
34     if isinstance(o, list):
35         return o
36     raise AssertionError('Object {!r} must be a string or a list'.format(o))
37
38 def stringify(o):
39     if isinstance(o, str):
40         return o
41     if isinstance(o, list):
42         if len(o) == 1:
43             return o[0]
44         raise AssertionError('Did not expect object {!r} to have more than one element'.format(o))
45     raise AssertionError('Object {!r} must be a string or a list'.format(o))
46
47 def prepend_env_var(env, var, value):
48     # Try not to exceed maximum length limits for env vars on Windows
49     if os.name is 'nt':
50         value = win32_get_short_path_name(value)
51     env_val = env.get(var, '')
52     val = os.pathsep + value + os.pathsep
53     # Don't add the same value twice
54     if val in env_val or env_val.startswith(value + os.pathsep):
55         return
56     env[var] = val + env_val
57     env[var] = env[var].replace(os.pathsep + os.pathsep, os.pathsep).strip(os.pathsep)
58
59
60 def get_subprocess_env(options, gst_version):
61     env = os.environ.copy()
62
63     env["CURRENT_GST"] = os.path.normpath(SCRIPTDIR)
64     env["GST_VALIDATE_SCENARIOS_PATH"] = os.path.normpath(
65         "%s/subprojects/gst-devtools/validate/data/scenarios" % SCRIPTDIR)
66     env["GST_VALIDATE_PLUGIN_PATH"] = os.path.normpath(
67         "%s/subprojects/gst-devtools/validate/plugins" % options.builddir)
68     env["GST_VALIDATE_APPS_DIR"] = os.path.normpath(
69         "%s/subprojects/gst-editing-services/tests/validate" % SCRIPTDIR)
70     prepend_env_var(env, "PATH", os.path.normpath(
71         "%s/subprojects/gst-devtools/validate/tools" % options.builddir))
72     prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'meson'))
73     env["GST_VERSION"] = gst_version
74     env["GST_ENV"] = 'gst-' + gst_version
75     env["GST_PLUGIN_SYSTEM_PATH"] = ""
76     env["GST_PLUGIN_SCANNER"] = os.path.normpath(
77         "%s/subprojects/gstreamer/libs/gst/helpers/gst-plugin-scanner" % options.builddir)
78     env["GST_PTP_HELPER"] = os.path.normpath(
79         "%s/subprojects/gstreamer/libs/gst/helpers/gst-ptp-helper" % options.builddir)
80     env["GST_REGISTRY"] = os.path.normpath(options.builddir + "/registry.dat")
81
82     sharedlib_reg = re.compile(r'\.so|\.dylib|\.dll')
83     typelib_reg = re.compile(r'.*\.typelib$')
84
85     if os.name is 'nt':
86         lib_path_envvar = 'PATH'
87     elif platform.system() == 'Darwin':
88         lib_path_envvar = 'DYLD_LIBRARY_PATH'
89     else:
90         lib_path_envvar = 'LD_LIBRARY_PATH'
91
92     prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(SCRIPTDIR, 'subprojects',
93                                                         'gst-python', 'plugin'))
94     prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(PREFIX_DIR, 'lib',
95                                                         'gstreamer-1.0'))
96     prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(options.builddir, 'subprojects',
97         'libnice', 'gst'))
98     prepend_env_var(env, "GST_VALIDATE_SCENARIOS_PATH", os.path.join(
99         PREFIX_DIR, 'share', 'gstreamer-1.0', 'validate', 'scenarios'))
100     prepend_env_var(env, "GI_TYPELIB_PATH", os.path.join(PREFIX_DIR, 'lib',
101                                                          'lib', 'girepository-1.0'))
102     prepend_env_var(env, "PKG_CONFIG_PATH", os.path.join(PREFIX_DIR, 'lib', 'pkgconfig'))
103
104     # gst-indent
105     prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'gstreamer', 'tools'))
106
107     # Library and binary search paths
108     prepend_env_var(env, "PATH", os.path.join(PREFIX_DIR, 'bin'))
109     if lib_path_envvar != 'PATH':
110         prepend_env_var(env, lib_path_envvar, os.path.join(PREFIX_DIR, 'lib'))
111     elif 'QMAKE' in os.environ:
112         # There's no RPATH on Windows, so we need to set PATH for the qt5 DLLs
113         prepend_env_var(env, 'PATH', os.path.dirname(os.environ['QMAKE']))
114
115     meson = get_meson()
116     targets_s = subprocess.check_output(meson + ['introspect', options.builddir, '--targets'])
117     targets = json.loads(targets_s.decode())
118     paths = set()
119     mono_paths = set()
120     srcdir_path = pathlib.Path(options.srcdir)
121     for target in targets:
122         filenames = listify(target['filename'])
123         for filename in filenames:
124             if filename.startswith(options.sysroot):
125                 filename = filename[len(options.sysroot):]
126             root = os.path.dirname(filename)
127             if srcdir_path / "subprojects/gst-devtools/validate/plugins" in (srcdir_path / root).parents:
128                 continue
129             if filename.endswith('.dll'):
130                 mono_paths.add(os.path.join(options.builddir, root))
131             if typelib_reg.search(filename):
132                 prepend_env_var(env, "GI_TYPELIB_PATH",
133                                 os.path.join(options.builddir, root))
134             elif sharedlib_reg.search(filename):
135                 if not target['type'].startswith('shared'):
136                     continue
137
138                 prepend_env_var(env, lib_path_envvar,
139                                 os.path.join(options.builddir, root))
140             elif target['type'] == 'executable' and target['installed']:
141                 paths.add(os.path.join(options.builddir, root))
142
143     with open(os.path.join(options.builddir, 'GstPluginsPath.json')) as f:
144         for plugin_path in json.load(f):
145             prepend_env_var(env, 'GST_PLUGIN_PATH', plugin_path)
146
147     for p in paths:
148         prepend_env_var(env, 'PATH', p)
149
150     if os.name != 'nt':
151         for p in mono_paths:
152             prepend_env_var(env, "MONO_PATH", p)
153
154     presets = set()
155     encoding_targets = set()
156     pkg_dirs = set()
157     python_dirs = set(["%s/subprojects/gstreamer/libs/gst/helpers/" % options.srcdir])
158     if '--installed' in subprocess.check_output(meson + ['introspect', '-h']).decode():
159         installed_s = subprocess.check_output(meson + ['introspect', options.builddir, '--installed'])
160         for path, installpath in json.loads(installed_s.decode()).items():
161             installpath_parts = pathlib.Path(installpath).parts
162             path_parts = pathlib.Path(path).parts
163
164             # We want to add all python modules to the PYTHONPATH
165             # in a manner consistent with the way they would be imported:
166             # For example if the source path /home/meh/foo/bar.py
167             # is to be installed in /usr/lib/python/site-packages/foo/bar.py,
168             # we want to add /home/meh to the PYTHONPATH.
169             # This will only work for projects where the paths to be installed
170             # mirror the installed directory layout, for example if the path
171             # is /home/meh/baz/bar.py and the install path is
172             # /usr/lib/site-packages/foo/bar.py , we will not add anything
173             # to PYTHONPATH, but the current approach works with pygobject
174             # and gst-python at least.
175             if 'site-packages' in installpath_parts:
176                 install_subpath = os.path.join(*installpath_parts[installpath_parts.index('site-packages') + 1:])
177                 if path.endswith(install_subpath):
178                     python_dirs.add(path[:len (install_subpath) * -1])
179
180             if path.endswith('.prs'):
181                 presets.add(os.path.dirname(path))
182             elif path.endswith('.gep'):
183                 encoding_targets.add(
184                     os.path.abspath(os.path.join(os.path.dirname(path), '..')))
185             elif path.endswith('.pc'):
186                 # Is there a -uninstalled pc file for this file?
187                 uninstalled = "{0}-uninstalled.pc".format(path[:-3])
188                 if os.path.exists(uninstalled):
189                     pkg_dirs.add(os.path.dirname(path))
190
191             if path.endswith('gstomx.conf'):
192                 prepend_env_var(env, 'GST_OMX_CONFIG_DIR', os.path.dirname(path))
193
194         for p in presets:
195             prepend_env_var(env, 'GST_PRESET_PATH', p)
196
197         for t in encoding_targets:
198             prepend_env_var(env, 'GST_ENCODING_TARGET_PATH', t)
199
200         for pkg_dir in pkg_dirs:
201             prepend_env_var(env, "PKG_CONFIG_PATH", pkg_dir)
202     prepend_env_var(env, "PKG_CONFIG_PATH", os.path.join(options.builddir,
203                                                          'subprojects',
204                                                          'gst-plugins-good',
205                                                          'pkgconfig'))
206
207     for python_dir in python_dirs:
208         prepend_env_var(env, 'PYTHONPATH', python_dir)
209
210     mesonpath = os.path.join(SCRIPTDIR, "meson")
211     if os.path.join(mesonpath):
212         # Add meson/ into PYTHONPATH if we are using a local meson
213         prepend_env_var(env, 'PYTHONPATH', mesonpath)
214
215     return env
216
217 def get_windows_shell():
218     command = ['powershell.exe' ,'-noprofile', '-executionpolicy', 'bypass', '-file', 'cmd_or_ps.ps1']
219     result = subprocess.check_output(command)
220     return result.decode().strip()
221
222 # https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
223 def in_venv():
224     return (hasattr(sys, 'real_prefix') or
225             (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))
226
227 if __name__ == "__main__":
228     parser = argparse.ArgumentParser(prog="gstreamer-uninstalled")
229
230     parser.add_argument("--builddir",
231                         default=DEFAULT_BUILDDIR,
232                         help="The meson build directory")
233     parser.add_argument("--srcdir",
234                         default=SCRIPTDIR,
235                         help="The top level source directory")
236     parser.add_argument("--sysroot",
237                         default='',
238                         help="The sysroot path used during cross-compilation")
239     options, args = parser.parse_known_args()
240
241     if not os.path.exists(options.builddir):
242         print("GStreamer not built in %s\n\nBuild it and try again" %
243               options.builddir)
244         exit(1)
245     options.builddir = os.path.abspath(options.builddir)
246
247     if not os.path.exists(options.srcdir):
248         print("The specified source dir does not exist" %
249               options.srcdir)
250         exit(1)
251
252     # The following incantation will retrieve the current branch name.
253     gst_version = git("rev-parse", "--symbolic-full-name", "--abbrev-ref", "HEAD",
254                       repository_path=options.srcdir).strip('\n')
255
256     if not args:
257         if os.name is 'nt':
258             shell = get_windows_shell()
259             if shell == 'powershell.exe':
260                 args = ['powershell.exe']
261                 args += ['-NoLogo', '-NoExit']
262                 prompt = 'function global:prompt {  "[gst-' + gst_version + '"+"] PS " + $PWD + "> "}'
263                 args += ['-Command', prompt]
264             else:
265                 args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
266                 args += ['/k', 'prompt [gst-{}] $P$G'.format(gst_version)]
267         else:
268             args = [os.environ.get("SHELL", os.path.realpath("/bin/sh"))]
269         if "bash" in args[0] and not strtobool(os.environ.get("GST_BUILD_DISABLE_PS1_OVERRIDE", r"FALSE")):
270             bashrc = os.path.expanduser('~/.bashrc')
271             if os.path.exists(bashrc):
272                 tmprc = tempfile.NamedTemporaryFile(mode='w')
273                 with open(bashrc, 'r') as src:
274                     shutil.copyfileobj(src, tmprc)
275                 tmprc.write('\nexport PS1="[gst-%s] $PS1"' % gst_version)
276                 tmprc.flush()
277                 # Let the GC remove the tmp file
278                 args.append("--rcfile")
279                 args.append(tmprc.name)
280     try:
281         exit(subprocess.call(args, close_fds=False,
282                              env=get_subprocess_env(options, gst_version)))
283     except subprocess.CalledProcessError as e:
284         exit(e.returncode)