1#!/usr/bin/env ruby 2# Copyright (c) 2021-2022 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 48end 49 50module Compiler 51 module_function 52 53 def entrypoints 54 @entrypoints ||= @data['entrypoints'].map {|x| Entrypoint.new x } 55 end 56 57 def entrypoints_crc32 58 require "zlib" 59 Zlib.crc32(entrypoints.map(&:signature).join) 60 end 61 62 def environment_checksum(cross_values_h) 63 require "zlib" 64 cross_values = File.read(cross_values_h) 65 cross_values_crc32 = Zlib.crc32(cross_values) 66 combined_crc32 = Zlib.crc32_combine(Compiler::entrypoints_crc32, cross_values_crc32, cross_values.length) 67 end 68 69 def wrap_data(data) 70 @data = data 71 end 72end 73 74def Gen.on_require(data) 75 Compiler.wrap_data(data) 76end 77