def filter_migration_targets(targets, app_labels=None, exclude=None):
    """Filter migration execution targets based on the given criteria.

    Args:
        targets (list of tuple):
            The migration targets to be executed.

        app_labels (set of unicode, optional):
            The app labels to limit the targets to.

        exclude (set, optional):
            Explicit targets to exclude.

    Returns:
        list of tuple:
        The resulting list of migration targets.
    """
    if app_labels is not None:
        if not isinstance(app_labels, set):
            app_labels = set(app_labels)

        targets = (
            target
            for target in targets
            if target[0] in app_labels
        )

    if exclude:
        if not isinstance(exclude, set):
            exclude = set(exclude)

        targets = (
            target
            for target in targets
            if target not in exclude
        )

    return list(targets)