meson: use new python module

This patch makes the tests pass running uninstalled and installed, with
python2 and python3 on linux, windows and osx.

The main gist is to use the new python-module to do the lifting done
by pythondetector, and with that add support for python2 and windows.
This commit is contained in:
Havard Graff 2018-05-14 10:05:15 +02:00 committed by Mathieu Duponchelle
parent a15ed715f0
commit af4ade3743
7 changed files with 41 additions and 124 deletions

View file

@ -566,7 +566,7 @@ def TIME_ARGS(time):
time % Gst.SECOND) time % Gst.SECOND)
__all__.append('TIME_ARGS') __all__.append('TIME_ARGS')
from . import _gi_gst import _gi_gst
_gi_gst _gi_gst
# maybe more python and less C some day if core turns a bit more introspection # maybe more python and less C some day if core turns a bit more introspection

View file

@ -2,17 +2,18 @@ pysources = ['Gst.py', 'GstPbutils.py']
install_data(pysources, install_data(pysources,
install_dir: pygi_override_dir) install_dir: pygi_override_dir)
gstpython = shared_library('_gi_gst', gstpython = python.extension_module('_gi_gst',
sources: ['gstmodule.c'], sources: ['gstmodule.c'],
name_prefix: '',
name_suffix: py_so_suffix,
install: true, install: true,
install_dir : pygi_override_dir, install_dir : pygi_override_dir,
include_directories : [configinc],
dependencies : [gst_dep, python_dep, pygobject_dep]) dependencies : [gst_dep, python_dep, pygobject_dep])
gi_overrides_build_dir = meson.current_build_dir()
# Workaround to get uninstalled working. # Workaround to get uninstalled working.
foreach source: pysources foreach source: pysources
run_command(python3, '-c', 'import os; os.symlink("@0@/@1@", "@2@/@3@")'.format( run_command(python, '-c', 'import os; os.symlink("@0@/@1@", "@2@/@3@")'.format(
meson.current_source_dir(), source, meson.current_source_dir(), source,
meson.current_build_dir(), source)) gi_overrides_build_dir, source))
endforeach endforeach

View file

@ -11,6 +11,8 @@ gst_version_major = version_arr[0]
gst_version_minor = version_arr[1] gst_version_minor = version_arr[1]
api_version = '@0@.0'.format(gst_version_major) api_version = '@0@.0'.format(gst_version_major)
add_project_arguments('-DHAVE_CONFIG_H', language: 'c')
gst_req = '>= @0@.@1@.0'.format(gst_version_major, gst_version_minor) gst_req = '>= @0@.@1@.0'.format(gst_version_major, gst_version_minor)
gst_dep = dependency('gstreamer-1.0', version : gst_req, gst_dep = dependency('gstreamer-1.0', version : gst_req,
@ -21,32 +23,32 @@ gmodule_dep = dependency('gmodule-2.0')
pygobject_dep = dependency('pygobject-3.0', fallback: ['pygobject', 'pygobject_dep'], version : '>= 3.8') pygobject_dep = dependency('pygobject-3.0', fallback: ['pygobject', 'pygobject_dep'], version : '>= 3.8')
python_dep = dependency('python3') python_dep = dependency('python3')
python3 = import('python3').find_python() pymod = import('python')
pythondetector = find_program('scripts/pythondetector') python = pymod.find_installation(get_option('python'))
python_dep = python.dependency(required : true)
cres = run_command(pythondetector, '--sosuffix') python_abi_flags = python.get_variable('ABIFLAGS', '')
if cres.returncode() != 0 pylib_loc = python.get_variable('LIBPL', '')
error('Could not detect python sosuffix' + cres.stdout() + cres.stderr()) if host_machine.system() != 'windows'
assert(pylib_loc != '', 'Python dynamic library path could not be determined')
endif endif
py_so_suffix = cres.stdout().strip() message('python_abi_flags = @0@'.format(python_abi_flags))
message('pylib_loc = @0@'.format(pylib_loc))
cres = run_command(pythondetector, '--abiflags')
if cres.returncode() != 0
error('Could not detect python abiflags' + cres.stdout() + cres.stderr())
endif
python_abi_flags = cres.stdout().strip()
cres = run_command(pythondetector, '--libloc')
if cres.returncode() != 0
error('Could not detect python library location' + cres.stdout() + cres.stderr())
endif
pylib_loc = cres.stdout().strip()
assert(pylib_loc != 'None', 'Python dynamic library path could not be determined')
pygi_override_dir = get_option('pygi-overrides-dir') pygi_override_dir = get_option('pygi-overrides-dir')
if pygi_override_dir == '' if pygi_override_dir == ''
cres = run_command(pythondetector, '--pygi-overridedir', cres = run_command(python, '-c','''
get_option('prefix')) import os, sys, gi.overrides
paths = gi.overrides.__path__
prefix = os.path.abspath(sys.argv[1])
for path in paths:
path = os.path.abspath(path)
if os.path.commonprefix([path, prefix]) == prefix:
print(path)
exit(0)
exit(1)
''', get_option('prefix'))
if cres.returncode() != 0 if cres.returncode() != 0
error('Could not detect PyGObject overrides location' + cres.stdout() + cres.stderr()) error('Could not detect PyGObject overrides location' + cres.stdout() + cres.stderr())
endif endif
@ -55,7 +57,7 @@ if pygi_override_dir == ''
message(cres.stderr()) message(cres.stderr())
endif endif
endif endif
message('pygobject overrides directory ' + pygi_override_dir) message('pygobject overrides directory = @0@'.format(pygi_override_dir))
pylib_suffix = 'so' pylib_suffix = 'so'
if host_machine.system() == 'windows' if host_machine.system() == 'windows'
@ -81,4 +83,4 @@ subdir('gi')
subdir('plugin') subdir('plugin')
subdir('testsuite') subdir('testsuite')
run_command(python3, '-c', 'import shutil; shutil.copy("hooks/pre-commit.hook", ".git/hooks/pre-commit")') run_command(python, '-c', 'import shutil; shutil.copy("hooks/pre-commit.hook", ".git/hooks/pre-commit")')

View file

@ -1,2 +1,4 @@
option('pygi-overrides-dir', type : 'string', value : '', option('pygi-overrides-dir', type : 'string', value : '',
description: 'Path to pygobject overrides directory') description: 'Path to pygobject overrides directory')
option('python', type : 'string', value : 'python3')

View file

@ -1,6 +1,5 @@
gst_elements_shared = shared_library('gstpython', gst_elements_shared = shared_library('gstpython',
['gstpythonplugin.c'], ['gstpythonplugin.c'],
c_args : '-DHAVE_CONFIG_H -DPY_LIB_LOC=@0@'.format(pylib_loc),
include_directories : [configinc], include_directories : [configinc],
dependencies : [gst_dep, pygobject_dep, gstbase_dep, python_dep, gmodule_dep], dependencies : [gst_dep, pygobject_dep, gstbase_dep, python_dep, gmodule_dep],
install : true, install : true,

View file

@ -1,84 +0,0 @@
#!/usr/bin/env python3
import os
import platform
import subprocess
import sys
from distutils import sysconfig
try:
sys.path.remove(os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)),
'..')))
except ValueError:
pass
def get_python_abiflags():
return sysconfig.get_config_var("ABIFLAGS")
def get_python_libloc():
# OSX is a pain. Python as shipped by apple installs libpython in /usr/lib
# so we hardcode that. Other systems can use --with-libpython-dir to
# override this.
if platform.system().lower() == 'darwin':
return '/usr/lib'
pylib_loc = sysconfig.get_config_var("LIBPL")
pylib_ldlibrary = sysconfig.get_config_var("LDLIBRARY")
py_sharedlib = os.path.join(pylib_loc, pylib_ldlibrary)
if os.path.exists(py_sharedlib):
return pylib_loc
# Workaround for Fedora
pylib_loc = sysconfig.get_config_var("LIBDIR")
pylib_ldlibrary = sysconfig.get_config_var("LDLIBRARY")
py_sharedlib = os.path.join(pylib_loc, pylib_ldlibrary)
if os.path.exists(py_sharedlib):
return pylib_loc
return "None"
if __name__ == "__main__":
if len(sys.argv) > 3:
print("At most 2 arguments accepted")
exit(1)
if sys.argv[1] == '--abiflags':
print(get_python_abiflags())
elif sys.argv[1] == '--sosuffix':
get = sysconfig.get_config_var
suffix = get("EXT_SUFFIX") or get("SO") or ".so"
print(suffix[1:])
elif sys.argv[1] == '--pygi-overridedir':
prefix = sys.argv[2]
version = sys.version_info
# If we are installing in the same prefix as PyGobject
# make sure to install in the right place.
import gi.overrides
try:
gi.overrides.__path__.remove(os.path.abspath(os.path.join(
os.path.dirname(os.path.realpath(__file__)), '..', 'gi')))
except ValueError:
pass
overrides_path = gi.overrides.__path__[0]
if os.path.commonprefix([overrides_path, prefix]) == prefix:
print(overrides_path)
exit(0)
# Otherwise follow python's way of install site packages inside
# the provided prefix
if os.name == 'posix':
print(os.path.join(
prefix, 'lib', 'python%d.%d' % (version.major, version.minor),
'site-packages', 'gi', 'overrides'))
else:
print(os.path.join(
prefix, 'Lib', 'Python%d%d' % (version.major, version.minor),
'site-packages', 'gi', 'overrides'))
elif sys.argv[1] == '--libloc':
print(get_python_libloc())

View file

@ -1,4 +1,4 @@
runtests = find_program('runtests.py') runtests = files('runtests.py')
tests = [ tests = [
['Test gst', 'test_gst.py'], ['Test gst', 'test_gst.py'],
@ -17,22 +17,19 @@ if not meson.is_subproject()
endif endif
endif endif
runcmd = run_command(python3, '-c', '''with open("@0@/mesonconfig.py", "w") as f: runcmd = run_command(python, '-c', '''with open("@0@/mesonconfig.py", "w") as f:
f.write("path='@1@'")'''.format( f.write("path='@1@'")'''.format(
meson.current_source_dir(), join_paths(meson.current_build_dir(), '../'))) join_paths(meson.current_build_dir()), join_paths(meson.current_build_dir(), '..')))
if runcmd.returncode() != 0 if runcmd.returncode() != 0
error('Could not configure testsuite config file.' + runcmd.stderr()) error('Could not configure testsuite config file.' + runcmd.stderr())
endif endif
gi_dir = join_paths(pygi_override_dir, '..', '..')
foreach i: tests foreach i: tests
test_name = i.get(0) test_name = i.get(0)
env = environment() env = environment()
env.set('GST_PLUGIN_PATH_1_0', meson.build_root()) env.prepend('PYTHONPATH', [gi_dir, gi_overrides_build_dir])
env.set('GST_PLUGIN_SYSTEM_PATH_1_0', '') test(test_name, python, args: [runtests, i.get(1)], env: env)
foreach plugindir: pluginsdirs
env.append('GST_PLUGIN_PATH_1_0', plugindir)
endforeach
env.set('GST_REGISTRY', '@0@/@1@.registry'.format(meson.current_build_dir(), i.get(1)))
test(test_name, runtests, args: [i.get(1)], env: env)
endforeach endforeach