def generate_gir(self, state, args, kwargs):
        if len(args) != 1:
            raise MesonException('Gir takes one argument')
        girtarget = args[0]
        while hasattr(girtarget, 'held_object'):
            girtarget = girtarget.held_object
        if not isinstance(girtarget, (build.Executable, build.SharedLibrary)):
            raise MesonException('Gir target must be an executable or shared library')
        try:
            pkgstr = subprocess.check_output(['pkg-config', '--cflags', 'gobject-introspection-1.0'])
        except Exception:
            global girwarning_printed
            if not girwarning_printed:
                mlog.log(mlog.bold('Warning:'), 'gobject-introspection dependency was not found, disabling gir generation.')
                girwarning_printed = True
            return []
        pkgargs = pkgstr.decode().strip().split()
        ns = kwargs.pop('namespace')
        nsversion = kwargs.pop('nsversion')
        libsources = kwargs.pop('sources')
        girfile = '%s-%s.gir' % (ns, nsversion)
        depends = [girtarget]

        scan_command = ['g-ir-scanner', '@INPUT@']
        scan_command += pkgargs
        scan_command += ['--no-libtool', '--namespace='+ns, '--nsversion=' + nsversion, '--warn-all',
                         '--output', '@OUTPUT@']

        extra_args = kwargs.pop('extra_args', [])
        if not isinstance(extra_args, list):
            extra_args = [extra_args]
        scan_command += extra_args

        for incdirs in girtarget.include_dirs:
            for incdir in incdirs.get_incdirs():
                scan_command += ['-I%s' % os.path.join(state.environment.get_source_dir(), incdir)]

        if 'link_with' in kwargs:
            link_with = kwargs.pop('link_with')
            if not isinstance(link_with, list):
                link_with = [link_with]
            for link in link_with:
                lib = link.held_object
                scan_command += ['-l%s' % lib.name]
                if isinstance(lib, build.SharedLibrary):
                    scan_command += ['-L%s' %
                            os.path.join(state.environment.get_build_dir(),
                                lib.subdir)]
                    depends.append(lib)

        if 'includes' in kwargs:
            includes = kwargs.pop('includes')
            if isinstance(includes, str):
                scan_command += ['--include=%s' % includes]
            elif isinstance(includes, list):
                scan_command += ['--include=%s' % inc for inc in includes]
            else:
                raise MesonException('Gir includes must be str or list')
        if state.global_args.get('c'):
            scan_command += ['--cflags-begin']
            scan_command += state.global_args['c']
            scan_command += ['--cflags-end']
        if kwargs.get('symbol_prefix'):
            sym_prefix = kwargs.pop('symbol_prefix')
            if not isinstance(sym_prefix, str):
                raise MesonException('Gir symbol prefix must be str')
            scan_command += ['--symbol-prefix=%s' % sym_prefix]
        if kwargs.get('identifier_prefix'):
            identifier_prefix = kwargs.pop('identifier_prefix')
            if not isinstance(identifier_prefix, str):
                raise MesonException('Gir identifier prefix must be str')
            scan_command += ['--identifier-prefix=%s' % identifier_prefix]
        if kwargs.get('export_packages'):
            pkgs = kwargs.pop('export_packages')
            if isinstance(pkgs, str):
                scan_command += ['--pkg-export=%s' % pkgs]
            elif isinstance(pkgs, list):
                scan_command += ['--pkg-export=%s' % pkg for pkg in pkgs]
            else:
                raise MesonException('Gir export packages must be str or list')

        deps = None
        if 'dependencies' in kwargs:
            deps = kwargs.pop('dependencies')
            if not isinstance (deps, list):
                deps = [deps]
            for dep in deps:
                girdir = dep.held_object.get_variable ("girdir")
                if girdir:
                    scan_command += ["--add-include-path=%s" % girdir]
                for lib in dep.held_object.libs:
                    if os.path.isabs(lib) and dep.held_object.is_libtool:
                        scan_command += ["-L%s" % os.path.dirname(lib)]
                        libname = os.path.basename(lib)
                        if libname.startswith("lib"):
                            libname = libname[3:]
                        libname = libname.split(".so")[0]
                        lib = "-l%s" % libname
                    scan_command += [lib]

        inc_dirs = None
        if kwargs.get('include_directories'):
            inc_dirs = kwargs.pop('include_directories')
            if not isinstance(inc_dirs, list):
                inc_dirs = [inc_dirs]
            for ind in inc_dirs:
                if isinstance(ind.held_object, build.IncludeDirs):
                    scan_command += ['--add-include-path=%s' % inc for inc in ind.held_object.get_incdirs()]
                else:
                    raise MesonException('Gir include dirs should be include_directories()')
        if isinstance(girtarget, build.Executable):
            scan_command += ['--program', girtarget]
        elif isinstance(girtarget, build.SharedLibrary):
            scan_command += ["-L@PRIVATE_OUTDIR_ABS_%s@" % girtarget.get_id()]
            libname = girtarget.get_basename()
            scan_command += ['--library', libname]
        scankwargs = {'output' : girfile,
                      'input' : libsources,
                      'command' : scan_command,
                      'depends' : depends,
                     }
        if kwargs.get('install'):
            scankwargs['install'] = kwargs['install']
            scankwargs['install_dir'] = os.path.join(state.environment.get_datadir(), 'gir-1.0')
        scan_target = GirTarget(girfile, state.subdir, scankwargs)

        typelib_output = '%s-%s.typelib' % (ns, nsversion)
        typelib_cmd = ['g-ir-compiler', scan_target, '--output', '@OUTPUT@']
        if inc_dirs:
            for incd in inc_dirs:
                typelib_cmd += ['--includedir=%s' % inc for inc in
                                incd.held_object.get_incdirs()]
        if deps:
            for dep in deps:
                girdir = dep.held_object.get_variable ("girdir")
                if girdir:
                    typelib_cmd += ["--includedir=%s" % girdir]

        kwargs['output'] = typelib_output
        kwargs['command'] = typelib_cmd
        # Note that this can't be libdir, because e.g. on Debian it points to
        # lib/x86_64-linux-gnu but the girepo dir is always under lib.
        kwargs['install_dir'] = 'lib/girepository-1.0'
        typelib_target = TypelibTarget(typelib_output, state.subdir, kwargs)
        return [scan_target, typelib_target]
dataset/ETHPy150Open mesonbuild/meson/mesonbuild/modules/gnome.py/GnomeModule.generate_gir/original