python: Avoid using 'is' to compare strings
[platform/upstream/gstreamer.git] / gst-env.py
index 8668973..ad00cd8 100755 (executable)
@@ -19,6 +19,7 @@ from distutils.util import strtobool
 from scripts.common import get_meson
 from scripts.common import git
 from scripts.common import win32_get_short_path_name
+from scripts.common import get_wine_shortpath
 
 SCRIPTDIR = os.path.dirname(os.path.realpath(__file__))
 PREFIX_DIR = os.path.join(SCRIPTDIR, 'prefix')
@@ -29,8 +30,11 @@ if not os.path.exists(DEFAULT_BUILDDIR):
 
 TYPELIB_REG = re.compile(r'.*\.typelib$')
 SHAREDLIB_REG = re.compile(r'\.so|\.dylib|\.dll')
-GSTPLUGIN_FILEPATH_REG = re.compile(r'.*/lib[^/]*/gstreamer-1.0/[^/]+$')
 
+# libdir is expanded from option of the same name listed in the `meson
+# introspect --buildoptions` output.
+GSTPLUGIN_FILEPATH_REG_TEMPLATE = r'.*/{libdir}/gstreamer-1.0/[^/]+$'
+GSTPLUGIN_FILEPATH_REG = None
 
 def listify(o):
     if isinstance(o, str):
@@ -52,7 +56,7 @@ def prepend_env_var(env, var, value, sysroot):
     if value.startswith(sysroot):
         value = value[len(sysroot):]
     # Try not to exceed maximum length limits for env vars on Windows
-    if os.name is 'nt':
+    if os.name == 'nt':
         value = win32_get_short_path_name(value)
     env_val = env.get(var, '')
     val = os.pathsep + value + os.pathsep
@@ -76,43 +80,71 @@ def is_library_target_and_not_plugin(target, filename):
     if not SHAREDLIB_REG.search(filename):
         return False
     # Check if it's installed to the gstreamer plugin location
-    for install_filename in target['install_filename']:
+    for install_filename in listify(target['install_filename']):
         if install_filename.endswith(os.path.basename(filename)):
             break
     else:
         # None of the installed files in the target correspond to the built
         # filename, so skip
         return False
+
+    global GSTPLUGIN_FILEPATH_REG
+    if GSTPLUGIN_FILEPATH_REG is None:
+        GSTPLUGIN_FILEPATH_REG = re.compile(GSTPLUGIN_FILEPATH_REG_TEMPLATE)
     if GSTPLUGIN_FILEPATH_REG.search(install_filename.replace('\\', '/')):
         return False
     return True
 
 
+def get_wine_subprocess_env(options, env):
+    with open(os.path.join(options.builddir, 'meson-info', 'intro-buildoptions.json')) as f:
+        buildoptions = json.load(f)
+
+    prefix, = [o for o in buildoptions if o['name'] == 'prefix']
+    path = os.path.normpath(os.path.join(prefix['value'], 'bin'))
+    prepend_env_var(env, "PATH", path, options.sysroot)
+    wine_path = get_wine_shortpath(
+        options.wine.split(' '),
+        [path] + env.get('WINEPATH', '').split(';')
+    )
+    if options.winepath:
+        wine_path += ';' + options.winepath
+    env['WINEPATH'] = wine_path
+    env['WINEDEBUG'] = 'fixme-all'
+
+    return env
+
+
 def get_subprocess_env(options, gst_version):
     env = os.environ.copy()
 
     env["CURRENT_GST"] = os.path.normpath(SCRIPTDIR)
+    env["GST_VERSION"] = gst_version
     env["GST_VALIDATE_SCENARIOS_PATH"] = os.path.normpath(
         "%s/subprojects/gst-devtools/validate/data/scenarios" % SCRIPTDIR)
     env["GST_VALIDATE_PLUGIN_PATH"] = os.path.normpath(
         "%s/subprojects/gst-devtools/validate/plugins" % options.builddir)
     env["GST_VALIDATE_APPS_DIR"] = os.path.normpath(
         "%s/subprojects/gst-editing-services/tests/validate" % SCRIPTDIR)
+    env["GST_ENV"] = 'gst-' + gst_version
+    env["GST_REGISTRY"] = os.path.normpath(options.builddir + "/registry.dat")
     prepend_env_var(env, "PATH", os.path.normpath(
         "%s/subprojects/gst-devtools/validate/tools" % options.builddir),
         options.sysroot)
+
+    if options.wine:
+        return get_wine_subprocess_env(options, env)
+
     prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'meson'),
         options.sysroot)
-    env["GST_VERSION"] = gst_version
-    env["GST_ENV"] = 'gst-' + gst_version
+
     env["GST_PLUGIN_SYSTEM_PATH"] = ""
     env["GST_PLUGIN_SCANNER"] = os.path.normpath(
         "%s/subprojects/gstreamer/libs/gst/helpers/gst-plugin-scanner" % options.builddir)
     env["GST_PTP_HELPER"] = os.path.normpath(
         "%s/subprojects/gstreamer/libs/gst/helpers/gst-ptp-helper" % options.builddir)
-    env["GST_REGISTRY"] = os.path.normpath(options.builddir + "/registry.dat")
 
-    if os.name is 'nt':
+    if os.name == 'nt':
         lib_path_envvar = 'PATH'
     elif platform.system() == 'Darwin':
         lib_path_envvar = 'DYLD_LIBRARY_PATH'
@@ -142,6 +174,14 @@ def get_subprocess_env(options, gst_version):
     prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'gstreamer', 'tools'),
                     options.sysroot)
 
+    # tools: gst-launch-1.0, gst-inspect-1.0
+    prepend_env_var(env, "PATH", os.path.join(options.builddir, 'subprojects',
+                                              'gstreamer', 'tools'),
+                    options.sysroot)
+    prepend_env_var(env, "PATH", os.path.join(options.builddir, 'subprojects',
+                                              'gst-plugins-base', 'tools'),
+                    options.sysroot)
+
     # Library and binary search paths
     prepend_env_var(env, "PATH", os.path.join(PREFIX_DIR, 'bin'),
                     options.sysroot)
@@ -161,6 +201,15 @@ def get_subprocess_env(options, gst_version):
     paths = set()
     mono_paths = set()
     srcdir_path = pathlib.Path(options.srcdir)
+
+    build_options_s = subprocess.check_output(meson + ['introspect', options.builddir, '--buildoptions'])
+    build_options = json.loads(build_options_s.decode())
+    libdir, = [o['value'] for o in build_options if o['name'] == 'libdir']
+    libdir = libdir.replace('\\', '/')
+
+    global GSTPLUGIN_FILEPATH_REG_TEMPLATE
+    GSTPLUGIN_FILEPATH_REG_TEMPLATE = GSTPLUGIN_FILEPATH_REG_TEMPLATE.format(libdir=libdir)
+
     for target in targets:
         filenames = listify(target['filename'])
         for filename in filenames:
@@ -285,6 +334,12 @@ if __name__ == "__main__":
     parser.add_argument("--sysroot",
                         default='',
                         help="The sysroot path used during cross-compilation")
+    parser.add_argument("--wine",
+                        default='',
+                        help="Build a wine env based on specified wine command")
+    parser.add_argument("--winepath",
+                        default='',
+                        help="Exra path to set to WINEPATH.")
     options, args = parser.parse_known_args()
 
     if not os.path.exists(options.builddir):
@@ -302,8 +357,11 @@ if __name__ == "__main__":
     gst_version = git("rev-parse", "--symbolic-full-name", "--abbrev-ref", "HEAD",
                       repository_path=options.srcdir).strip('\n')
 
+    if options.wine:
+        gst_version += '-' + os.path.basename(options.wine)
+
     if not args:
-        if os.name is 'nt':
+        if os.name == 'nt':
             shell = get_windows_shell()
             if shell == 'powershell.exe':
                 args = ['powershell.exe']