1# coding=utf-8 2# 3# Copyright (c) 2025 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16from dataclasses import dataclass 17from typing import TYPE_CHECKING, ClassVar 18 19from taihe.driver.backend import Backend, BackendConfig 20 21if TYPE_CHECKING: 22 from taihe.driver.contexts import CompilerInstance 23 24 25@dataclass 26class AbiHeaderBackendConfig(BackendConfig): 27 NAME = "abi-header" 28 29 def construct(self, instance: "CompilerInstance") -> Backend: 30 from taihe.codegen.abi.gen_abi import ABIHeadersGenerator 31 32 class ABIHeaderBackendImpl(Backend): 33 def __init__(self, ci: "CompilerInstance"): 34 super().__init__(ci) 35 self._ci = ci 36 37 def generate(self): 38 om = self._ci.output_manager 39 am = self._ci.analysis_manager 40 pg = self._ci.package_group 41 ABIHeadersGenerator(om, am).generate(pg) 42 43 return ABIHeaderBackendImpl(instance) 44 45 46@dataclass 47class AbiSourcesBackendConfig(BackendConfig): 48 NAME = "abi-source" 49 DEPS: ClassVar = ["abi-header"] 50 51 def construct(self, instance: "CompilerInstance") -> Backend: 52 from taihe.codegen.abi.gen_abi import ABISourcesGenerator 53 54 class ABISourcesBackendImpl(Backend): 55 def __init__(self, ci: "CompilerInstance"): 56 super().__init__(ci) 57 self._ci = ci 58 59 def generate(self): 60 om = self._ci.output_manager 61 am = self._ci.analysis_manager 62 pg = self._ci.package_group 63 ABISourcesGenerator(om, am).generate(pg) 64 65 return ABISourcesBackendImpl(instance) 66 67 68@dataclass 69class CAuthorBackendConfig(BackendConfig): 70 NAME = "c-author" 71 DEPS: ClassVar = ["abi-source"] 72 73 def construct(self, instance: "CompilerInstance") -> Backend: 74 from taihe.codegen.abi.gen_impl import ( 75 CImplHeadersGenerator, 76 CImplSourcesGenerator, 77 ) 78 79 # TODO: unify CImpl{Headers,Sources}Generator 80 class CImplBackendImpl(Backend): 81 def __init__(self, ci: "CompilerInstance"): 82 super().__init__(ci) 83 self._ci = ci 84 85 def generate(self): 86 om = self._ci.output_manager 87 am = self._ci.analysis_manager 88 pg = self._ci.package_group 89 CImplSourcesGenerator(om, am).generate(pg) 90 CImplHeadersGenerator(om, am).generate(pg) 91 92 return CImplBackendImpl(instance)