def __init__(self, source, build_dir): """Create a new sub-generator generating to `build_dir` from `source`. Both the `source` and `build_dir` must be provided as an absolute path (see the note in the manual about relative path). The `source` must exists and might be either a file or a directory depending of the sub-generator. """ self._source = Path(source) if not self._source.exists(): raise ValueError("source does not exists: '{}'".format(source)) if not self._source.is_absolute(): raise ValueError("relative path to {name} sub-generator " "source: '{source}'" .format(name=self.name, source=self._source)) self._build_dir = Path(build_dir) if not self._build_dir.is_absolute(): raise ValueError("relative path to {name} sub-generator " "build directory: '{build_dir}'" .format(name=self.name, build_dir=self._build_dir)) if not self._build_dir.is_subdir_of(samurai_sys.build_dir): raise ValueError("sub-generator build directory '{}' " "is not a sub-directory of the main " "build directory '{}'" .format(self._build_dir, samurai_sys.build_dir))
def __init__(self, manifest, build_dir=None): manifest = Path(manifest) if manifest.is_dir(): manifest /= samurai_sys.default_manifest if build_dir is None: build_dir = get_default_build_dir_for(manifest) super().__init__(manifest, build_dir)
def __init__(self, source_dir, build_dir=None, **kwds): source_dir = Path(source_dir) if not source_dir.is_dir(): raise ValueError("source_dir '%s' is not a directory" .format(source_dir)) if build_dir is None: build_dir = get_default_build_dir_for(source_dir) super().__init__(build_dir=build_dir, **kwds) self._source_dir = source_dir
class SubGenerator(metaclass=abc.ABCMeta): """Abstract class sub-generator. A sub-generator is a foreign Ninja's file generator program (such as cmake(1) or samurai itself). This class is the basic building block to extend samurai API with foreign generator. Users only have to instantiate a SubGenerator sub-class in their manifest and add it to samurai's evaluator context. Sub-generator uses Ninja's `subninja` statement behind the scene. """ def __init__(self, source, build_dir): """Create a new sub-generator generating to `build_dir` from `source`. Both the `source` and `build_dir` must be provided as an absolute path (see the note in the manual about relative path). The `source` must exists and might be either a file or a directory depending of the sub-generator. """ self._source = Path(source) if not self._source.exists(): raise ValueError("source does not exists: '{}'".format(source)) if not self._source.is_absolute(): raise ValueError("relative path to {name} sub-generator " "source: '{source}'" .format(name=self.name, source=self._source)) self._build_dir = Path(build_dir) if not self._build_dir.is_absolute(): raise ValueError("relative path to {name} sub-generator " "build directory: '{build_dir}'" .format(name=self.name, build_dir=self._build_dir)) if not self._build_dir.is_subdir_of(samurai_sys.build_dir): raise ValueError("sub-generator build directory '{}' " "is not a sub-directory of the main " "build directory '{}'" .format(self._build_dir, samurai_sys.build_dir)) @property def name(self): return type(self).__name__ @property def source(self): """Return the source of this sub-generator.""" return self._source.to_original() @property def build_dir(self): return self._build_dir.to_original() @property def output(self): return (self._build_dir/config.BUILD_FILENAME).to_original() def generate(self): """Generate a Ninja build file using a foreign generator. This method is called by the dumper to generate a fresh new build system. Sub-classes are supposed to ensure that by first deleting any cache file. """ # Disable the sub generator so that it does not regenerate itself. # Ninja is in charge of running the sub-generator when it needs to. if samurai_sys.autoregen: return LOGGER.info(">>> start sub-generator %s on '%s' in '%s'", self.name, self.source, self.build_dir) try: rc = self._do_generate() if rc != 0: raise SubGenerationError(self, rc) finally: LOGGER.info(">>> stop sub-generator %s", self.name) @abc.abstractmethod def _do_generate(self): """Perform the generation.