def add_dependencies(targets, deps, order=None):
    """ Add dependent formats and optionally put into right build order. """
    for target, deps in deps.items():
        if target in targets:
            targets = list(set(targets).union(deps))
    if order:
        return list(filter(targets.__contains__, order))
    return targets
