import re


def IsValidTargetForWrapper(target_extras, executable_target_pattern, spec):
  """Limit targets for Xcode wrapper.

  Xcode sometimes performs poorly with too many targets, so only include
  proper executable targets, with filters to customize.
  Arguments:
    target_extras: Regular expression to always add, matching any target.
    executable_target_pattern: Regular expression limiting executable targets.
    spec: Specifications for target.
  """
  target_name = spec.get('target_name')
  # Always include targets matching target_extras.
  if target_extras is not None and re.search(target_extras, target_name):
    return True

  # Otherwise just show executable targets and xc_tests.
  if (int(spec.get('mac_xctest_bundle', 0)) != 0 or
      (spec.get('type', '') == 'executable' and
       spec.get('product_extension', '') != 'bundle')):

    # If there is a filter and the target does not match, exclude the target.
    if executable_target_pattern is not None:
      if not re.search(executable_target_pattern, target_name):
        return False
    return True
  return False