diff options
author | Kaiyi Li <kaiyili@google.com> | 2022-09-05 10:41:18 -0700 |
---|---|---|
committer | Kaiyi Li <kaiyili@google.com> | 2022-09-07 07:37:09 -0700 |
commit | 74f4d575ffd96545550edca7114de4ceabb4e080 (patch) | |
tree | 51f09bae5265548706c72219a0afb7575b7c3dd7 | |
parent | 6c06ef62a8589bc87bf802a75b669a02addcdae4 (diff) | |
download | gfxstream-protocols-74f4d575ffd96545550edca7114de4ceabb4e080.tar.gz |
vulkan codegen: introduce SingleFileModule
... to share the code for handling files across different types of files.
Test: run the script no modification to the generated code
Change-Id: I3c26491a51e0f2aa667a2c26e16970ff5e9d6ff5
-rw-r--r-- | registry/vulkan/scripts/cereal/common/codegen.py | 215 | ||||
-rwxr-xr-x | scripts/generate-vulkan-sources.sh | 44 |
2 files changed, 138 insertions, 121 deletions
diff --git a/registry/vulkan/scripts/cereal/common/codegen.py b/registry/vulkan/scripts/cereal/common/codegen.py index 0dea8b98..54dd50cf 100644 --- a/registry/vulkan/scripts/cereal/common/codegen.py +++ b/registry/vulkan/scripts/cereal/common/codegen.py @@ -23,33 +23,115 @@ import sys import shutil import subprocess +# Class capturing a single file + + +class SingleFileModule(object): + def __init__(self, suffix, directory, basename, customAbsDir=None, suppress=False): + self.directory = directory + self.basename = basename + self.customAbsDir = customAbsDir + self.suffix = suffix + self.file = None + + self.preamble = "" + self.postamble = "" + + self.suppress = suppress + + def begin(self, globalDir): + if self.suppress: + return + + # Create subdirectory, if needed + if self.customAbsDir: + absDir = self.customAbsDir + else: + absDir = os.path.join(globalDir, self.directory) + + filename = os.path.join(absDir, self.basename) + + self.file = open(filename + self.suffix, "w", encoding="utf-8") + self.file.write(self.preamble) + + def append(self, toAppend): + if self.suppress: + return + + self.file.write(toAppend) + + def end(self): + if self.suppress: + return + + self.file.write(self.postamble) + self.file.close() + # Class capturing a .cpp file and a .h file (a "C++ module") + + class Module(object): - def __init__(self, directory, basename, customAbsDir = None, suppress = False, implOnly = False, headerOnly = False, suppressFeatureGuards = False): + def __init__( + self, directory, basename, customAbsDir=None, suppress=False, implOnly=False, + headerOnly=False, suppressFeatureGuards=False): + self._headerFileModule = SingleFileModule( + ".h", directory, basename, customAbsDir, suppress or implOnly) + self._implFileModule = SingleFileModule( + ".cpp", directory, basename, customAbsDir, suppress or headerOnly) + + self._headerOnly = headerOnly + self._implOnly = implOnly + self.directory = directory self.basename = basename + self._customAbsDir = customAbsDir + + self.suppressFeatureGuards = suppressFeatureGuards - self.headerPreamble = "" - self.implPreamble = "" + @property + def suppress(self): + raise AttributeError("suppress is write only") - self.headerPostamble = "" - self.implPostamble = "" + @suppress.setter + def suppress(self, value: bool): + self._headerFileModule.suppress = self._implOnly or value + self._implFileModule.suppress = self._headerOnly or value - self.headerFileHandle = "" - self.implFileHandle = "" + @property + def headerPreamble(self) -> str: + return self._headerFileModule.preamble - self.customAbsDir = customAbsDir + @headerPreamble.setter + def headerPreamble(self, value: str): + self._headerFileModule.preamble = value - self.suppress = suppress + @property + def headerPostamble(self) -> str: + return self._headerFileModule.postamble - self.implOnly = implOnly - self.headerOnly = headerOnly + @headerPostamble.setter + def headerPostamble(self, value: str): + self._headerFileModule.postamble = value - self.suppressFeatureGuards = suppressFeatureGuards + @property + def implPreamble(self) -> str: + return self._implFileModule.preamble + + @implPreamble.setter + def implPreamble(self, value: str): + self._implFileModule.preamble = value + + @property + def implPostamble(self) -> str: + return self._implFileModule.postamble + + @implPostamble.setter + def implPostamble(self, value: str): + self._implFileModule.postamble = value def getMakefileSrcEntry(self): - if self.customAbsDir: + if self._customAbsDir: return self.basename + ".cpp \\\n" dirName = self.directory baseName = self.basename @@ -57,7 +139,7 @@ class Module(object): return " " + joined + ".cpp \\\n" def getCMakeSrcEntry(self): - if self.customAbsDir: + if self._customAbsDir: return "\n" + self.basename + ".cpp " dirName = Path(self.directory) baseName = Path(self.basename) @@ -65,82 +147,38 @@ class Module(object): return "\n " + str(joined) + ".cpp " def begin(self, globalDir): - if self.suppress: - return - - # Create subdirectory, if needed - if self.customAbsDir: - absDir = self.customAbsDir - else: - absDir = os.path.join(globalDir, self.directory) - - filename = os.path.join(absDir, self.basename) - - fpHeader = None - fpImpl = None - - if not self.implOnly: - fpHeader = open(filename + ".h", "w", encoding="utf-8") - - if not self.headerOnly: - fpImpl = open(filename + ".cpp", "w", encoding="utf-8") - - self.headerFileHandle = fpHeader - self.implFileHandle = fpImpl - - if not self.implOnly: - self.headerFileHandle.write(self.headerPreamble) - - if not self.headerOnly: - self.implFileHandle.write(self.implPreamble) + self._headerFileModule.begin(globalDir) + self._implFileModule.begin(globalDir) def appendHeader(self, toAppend): - if self.suppress: - return - - if not self.implOnly: - self.headerFileHandle.write(toAppend) + self._headerFileModule.append(toAppend) def appendImpl(self, toAppend): - if self.suppress: - return - - if not self.headerOnly: - self.implFileHandle.write(toAppend) + self._implFileModule.append(toAppend) def end(self): - if self.suppress: - return + self._headerFileModule.end() + self._implFileModule.end() clang_format_command = shutil.which('clang-format') assert (clang_format_command is not None) - def formatFile(filename: str): - assert (subprocess.call([clang_format_command, "-i", "--style=file", filename]) == 0) + def formatFile(filename: Path): + assert (subprocess.call([clang_format_command, "-i", + "--style=file", str(filename.resolve())]) == 0) - if not self.implOnly: - self.headerFileHandle.write(self.headerPostamble) - self.headerFileHandle.close() - formatFile(self.headerFileHandle.name) + if not self._headerFileModule.suppress: + formatFile(Path(self._headerFileModule.file.name)) - if not self.headerOnly: - self.implFileHandle.write(self.implPostamble) - self.implFileHandle.close() - formatFile(self.implFileHandle.name) + if not self._implFileModule.suppress: + formatFile(Path(self._implFileModule.file.name)) # Class capturing a .proto protobuf definition file -class Proto(object): +class Proto(SingleFileModule): - def __init__(self, directory, basename, customAbsDir = None, suppress = False): - self.directory = directory - self.basename = basename - self.customAbsDir = customAbsDir - - self.preamble = "" - self.postamble = "" - - self.suppress = suppress + def __init__(self, directory, basename, customAbsDir=None, suppress=False): + super().__init__(".proto", directory, basename, customAbsDir, suppress) def getMakefileSrcEntry(self): if self.customAbsDir: @@ -159,35 +197,6 @@ class Proto(object): joined = os.path.join(dirName, baseName) return "\n " + joined + ".proto " - def begin(self, globalDir): - if self.suppress: - return - - # Create subdirectory, if needed - if self.customAbsDir: - absDir = self.customAbsDir - else: - absDir = os.path.join(globalDir, self.directory) - - filename = os.path.join(absDir, self.basename) - - fpProto = open(filename + ".proto", "w", encoding="utf-8") - self.protoFileHandle = fpProto - self.protoFileHandle.write(self.preamble) - - def append(self, toAppend): - if self.suppress: - return - - self.protoFileHandle.write(toAppend) - - def end(self): - if self.suppress: - return - - self.protoFileHandle.write(self.postamble) - self.protoFileHandle.close() - class CodeGen(object): def __init__(self,): diff --git a/scripts/generate-vulkan-sources.sh b/scripts/generate-vulkan-sources.sh index dd7e4444..fcb78927 100755 --- a/scripts/generate-vulkan-sources.sh +++ b/scripts/generate-vulkan-sources.sh @@ -45,18 +45,25 @@ fi cd $PROJECT_ROOT AOSP_DIR=$(pwd)/../../ -export VK_CEREAL_GUEST_ENCODER_DIR=$AOSP_DIR/device/generic/goldfish-opengl/system/vulkan_enc -export VK_CEREAL_GUEST_HAL_DIR=$AOSP_DIR/device/generic/goldfish-opengl/system/vulkan -export VK_CEREAL_HOST_DECODER_DIR=$AOSP_DIR/device/generic/vulkan-cereal/stream-servers/vulkan -export VK_CEREAL_HOST_INCLUDE_DIR=$AOSP_DIR/device/generic/vulkan-cereal/stream-servers +VK_CEREAL_GUEST_DIR=$AOSP_DIR/device/generic/goldfish-opengl +VK_CEREAL_HOST_DIR=$AOSP_DIR/device/generic/vulkan-cereal +export VK_CEREAL_GUEST_ENCODER_DIR=$VK_CEREAL_GUEST_DIR/system/vulkan_enc +export VK_CEREAL_GUEST_HAL_DIR=$VK_CEREAL_GUEST_DIR/system/vulkan +export VK_CEREAL_HOST_DECODER_DIR=$VK_CEREAL_HOST_DIR/stream-servers/vulkan +export VK_CEREAL_HOST_INCLUDE_DIR=$VK_CEREAL_HOST_DIR/stream-servers export VK_CEREAL_BASELIB_PREFIX=base export VK_CEREAL_BASELIB_LINKNAME=gfxstream-base.headers export VK_CEREAL_VK_HEADER_TARGET=gfxstream_vulkan_headers VK_CEREAL_OUTPUT_DIR=$VK_CEREAL_HOST_DECODER_DIR/cereal -mkdir -p $VK_CEREAL_GUEST_HAL_DIR -mkdir -p $VK_CEREAL_GUEST_HAL_DIR -mkdir -p $VK_CEREAL_OUTPUT_DIR +if [ -d "$VK_CEREAL_GUEST_DIR" ]; then + mkdir -p $VK_CEREAL_GUEST_ENCODER_DIR + mkdir -p $VK_CEREAL_GUEST_HAL_DIR +fi +if [ -d "$VK_CEREAL_HOST_DIR" ]; then + mkdir -p $VK_CEREAL_HOST_DECODER_DIR + mkdir -p $VK_CEREAL_OUTPUT_DIR +fi VULKAN_REGISTRY_DIR=$AOSP_DIR/external/gfxstream-protocols/registry/vulkan VULKAN_REGISTRY_XML_DIR=$VULKAN_REGISTRY_DIR/xml @@ -66,17 +73,18 @@ python3 $VULKAN_REGISTRY_SCRIPTS_DIR/genvk.py -registry $VULKAN_REGISTRY_XML_DIR # Generate gfxstream specific Vulkan definitions. for OUT_DIR in $VK_CEREAL_HOST_DECODER_DIR $VK_CEREAL_GUEST_ENCODER_DIR; do - OUT_FILE_BASENAME=vulkan_gfxstream.h - mkdir -p $OUT_DIR - python3 registry/vulkan/scripts/genvk.py -registry registry/vulkan/xml/vk.xml -o $OUT_DIR \ - $OUT_FILE_BASENAME + if [ -d "$OUT_DIR" ]; then + OUT_FILE_BASENAME=vulkan_gfxstream.h + python3 registry/vulkan/scripts/genvk.py -registry registry/vulkan/xml/vk.xml -o $OUT_DIR \ + $OUT_FILE_BASENAME - if [ $? -ne 0 ]; then - echo "Failed to generate gfxstream specific vulkan headers." 1>&2 - exit 1 - fi - if ! clang-format -i $OUT_DIR/$OUT_FILE_BASENAME; then - echo "Failed to reformat gfxstream specific vulkan headers." 1>&2 - exit 1 + if [ $? -ne 0 ]; then + echo "Failed to generate gfxstream specific vulkan headers." 1>&2 + exit 1 + fi + if ! clang-format -i $OUT_DIR/$OUT_FILE_BASENAME; then + echo "Failed to reformat gfxstream specific vulkan headers." 1>&2 + exit 1 + fi fi done
\ No newline at end of file |