1#!/usr/bin/env ruby 2# Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15require 'yaml' 16require 'ostruct' 17 18class Entrypoint < OpenStruct 19 def initialize(dscr) 20 super(dscr) 21 if self.entrypoint.nil? && !self.external? 22 raise "Entrypoint description must contains 'entrypoint' field: #{dscr.marshal_dump}" 23 end 24 abort "ERROR: 'bridge' field must be specified for #{self.entrypoint}" unless dscr['bridge'] # !self.respond_to? 'bridge' 25 end 26 27 def enum_name 28 self.name.snakecase.upcase 29 end 30 31 def bridge_name 32 return nil unless self.has_bridge? 33 "#{self.name}Bridge" 34 end 35 36 def has_bridge? 37 return !self.bridge.nil? && self.bridge != 'none' 38 end 39 40 def external? 41 has_property? 'external' 42 end 43 44 def has_property? prop 45 self.properties&.include? prop 46 end 47 48 def return_type 49 self.signature[0] 50 end 51 52 def general_return_type 53 ret = self.return_type 54 return 'VOID' if ret == 'void' 55 return 'FLOAT' if ['float', 'double'].include? ret 56 return 'INTEGER' 57 end 58 59end 60 61module Compiler 62 module_function 63 64 def entrypoints 65 @entrypoints ||= @data['entrypoints'].map {|x| Entrypoint.new x } 66 end 67 68 def entrypoints_crc32 69 require "zlib" 70 Zlib.crc32(entrypoints.map(&:signature).join) 71 end 72 73 def environment_checksum(cross_values_h) 74 require "zlib" 75 cross_values = File.read(cross_values_h) 76 cross_values_crc32 = Zlib.crc32(cross_values) 77 combined_crc32 = Zlib.crc32_combine(Compiler::entrypoints_crc32, cross_values_crc32, cross_values.length) 78 end 79 80 def wrap_data(data) 81 @data = data 82 end 83end 84 85def Gen.on_require(data) 86 Compiler.wrap_data(data) 87end 88