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