def generate_build(self, image, targetname, rebuilds=None, cache_repo='', cache_tag='',
                       buildargs=None, **kwargs):
        from_image = self.get_external_base_image(image)
        if cache_repo or cache_tag:
            cache_from = utils.generate_name(image, cache_repo, cache_tag)
        else:
            cache_from = None
        if from_image is None:
            raise errors.NoBaseError("No base image found in %s's dependencies" % image)
        if isinstance(from_image, ExternalDockerfile):
            build_first = from_image
            base_image = from_image.tag
        else:
            base_image = from_image
            build_first = None
        build_steps = []
        istep = 0
        sourceimages = set()
        if rebuilds is None:
            rebuilds = []
        else:
            rebuilds = set(rebuilds)
        for base_name in self.sort_dependencies(image):
            istep += 1
            buildname = 'dmkbuild_%s_%d' % (image, istep)
            secret_files = self.ymldefs[base_name].get('secret_files', None)
            squash = self.ymldefs[base_name].get('squash', bool(secret_files))
            build_steps.append(
                    dockermake.step.BuildStep(
                            base_name,
                            base_image,
                            self.ymldefs[base_name],
                            buildname,
                            bust_cache=base_name in rebuilds,
                            build_first=build_first, cache_from=cache_from,
                            buildargs=buildargs,
                            squash=squash,
                            secret_files=secret_files))
            base_image = buildname
            build_first = None
            for sourceimage, files in iteritems(self.ymldefs[base_name].get('copy_from', {})):
                sourceimages.add(sourceimage)
                for sourcepath, destpath in iteritems(files):
                    istep += 1
                    buildname = 'dmkbuild_%s_%d' % (image, istep)
                    build_steps.append(
                            dockermake.step.FileCopyStep(
                                    sourceimage, sourcepath, destpath,
                                    base_name, base_image, self.ymldefs[base_name],
                                    buildname, bust_cache=base_name in rebuilds,
                                    build_first=build_first, cache_from=cache_from))
                    base_image = buildname
        sourcebuilds = [self.generate_build(img,
                                            img,
                                            cache_repo=cache_repo,
                                            cache_tag=cache_tag,
                                            **kwargs)
                        for img in sourceimages]
        return builds.BuildTarget(imagename=image,
                                  targetname=targetname,
                                  steps=build_steps,
                                  sourcebuilds=sourcebuilds,
                                  from_image=from_image,
                                  **kwargs)