16 from distutils.sysconfig import get_python_lib
17 from distutils.util import strtobool
19 from scripts.common import get_meson
20 from scripts.common import git
21 from scripts.common import win32_get_short_path_name
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')
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/[^/]+$')
36 if isinstance(o, str):
38 if isinstance(o, list):
40 raise AssertionError('Object {!r} must be a string or a list'.format(o))
43 if isinstance(o, str):
45 if isinstance(o, list):
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))
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
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):
62 env[var] = val + env_val
63 env[var] = env[var].replace(os.pathsep + os.pathsep, os.pathsep).strip(os.pathsep)
65 def is_library_target_and_not_plugin(target, filename):
67 Don't add plugins to PATH/LD_LIBRARY_PATH because:
69 2. It causes us to exceed the PATH length limit on Windows and Wine
71 if not target['type'].startswith('shared'):
73 if not target['installed']:
75 # Check if this output of that target is a shared library
76 if not SHAREDLIB_REG.search(filename):
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)):
83 # None of the installed files in the target correspond to the built
86 if GSTPLUGIN_FILEPATH_REG.search(install_filename.replace('\\', '/')):
91 def get_subprocess_env(options, gst_version):
92 env = os.environ.copy()
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),
104 prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'meson'),
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")
116 lib_path_envvar = 'PATH'
117 elif platform.system() == 'Darwin':
118 lib_path_envvar = 'DYLD_LIBRARY_PATH'
120 lib_path_envvar = 'LD_LIBRARY_PATH'
122 prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(SCRIPTDIR, 'subprojects',
123 'gst-python', 'plugin'),
125 prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(PREFIX_DIR, 'lib',
128 prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(options.builddir, 'subprojects',
131 prepend_env_var(env, "GST_VALIDATE_SCENARIOS_PATH",
132 os.path.join(PREFIX_DIR, 'share', 'gstreamer-1.0',
133 'validate', 'scenarios'),
135 prepend_env_var(env, "GI_TYPELIB_PATH", os.path.join(PREFIX_DIR, 'lib',
136 'lib', 'girepository-1.0'),
138 prepend_env_var(env, "PKG_CONFIG_PATH", os.path.join(PREFIX_DIR, 'lib', 'pkgconfig'),
142 prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'gstreamer', 'tools'),
145 # Library and binary search paths
146 prepend_env_var(env, "PATH", os.path.join(PREFIX_DIR, 'bin'),
148 if lib_path_envvar != 'PATH':
149 prepend_env_var(env, lib_path_envvar, os.path.join(PREFIX_DIR, 'lib'),
151 prepend_env_var(env, lib_path_envvar, os.path.join(PREFIX_DIR, 'lib64'),
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']),
159 targets_s = subprocess.check_output(meson + ['introspect', options.builddir, '--targets'])
160 targets = json.loads(targets_s.decode())
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:
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),
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),
180 elif target['type'] == 'executable' and target['installed']:
181 paths.add(os.path.join(options.builddir, root))
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,
189 prepend_env_var(env, 'PATH', p, options.sysroot)
193 prepend_env_var(env, "MONO_PATH", p, options.sysroot)
196 encoding_targets = 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
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])
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))
232 if path.endswith('gstomx.conf'):
233 prepend_env_var(env, 'GST_OMX_CONFIG_DIR', os.path.dirname(path),
237 prepend_env_var(env, 'GST_PRESET_PATH', p, options.sysroot)
239 for t in encoding_targets:
240 prepend_env_var(env, 'GST_ENCODING_TARGET_PATH', t, options.sysroot)
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,
250 for python_dir in python_dirs:
251 prepend_env_var(env, 'PYTHONPATH', python_dir, options.sysroot)
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)
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/', '')
263 prepend_env_var (env, 'XDG_DATA_DIRS', os.path.join(options.builddir,
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()
276 # https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
278 return (hasattr(sys, 'real_prefix') or
279 (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))
281 if __name__ == "__main__":
282 parser = argparse.ArgumentParser(prog="gstreamer-uninstalled")
284 parser.add_argument("--builddir",
285 default=DEFAULT_BUILDDIR,
286 help="The meson build directory")
287 parser.add_argument("--srcdir",
289 help="The top level source directory")
290 parser.add_argument("--sysroot",
292 help="The sysroot path used during cross-compilation")
293 options, args = parser.parse_known_args()
295 if not os.path.exists(options.builddir):
296 print("GStreamer not built in %s\n\nBuild it and try again" %
299 options.builddir = os.path.abspath(options.builddir)
301 if not os.path.exists(options.srcdir):
302 print("The specified source dir does not exist" %
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')
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]
319 args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
320 args += ['/k', 'prompt [gst-{}] $P$G'.format(gst_version)]
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)
331 # Let the GC remove the tmp file
332 args.append("--rcfile")
333 args.append(tmprc.name)
335 exit(subprocess.call(args, close_fds=False,
336 env=get_subprocess_env(options, gst_version)))
337 except subprocess.CalledProcessError as e: