diff --git a/_tools/ruby_h_to_go/config.yml b/_tools/ruby_h_to_go/config.yml new file mode 100644 index 00000000..55f70013 --- /dev/null +++ b/_tools/ruby_h_to_go/config.yml @@ -0,0 +1,6 @@ +# This tag appears in https://pkg.go.dev/github.com/ruby-go-gem/go-gem-wrapper/ruby +default_tag: ruby_3_3 + +available_tags: + - ruby_3_3 + - ruby_3_4 diff --git a/_tools/ruby_h_to_go/lib/ruby_h_to_go.rb b/_tools/ruby_h_to_go/lib/ruby_h_to_go.rb index b9cfe571..b440f0a3 100644 --- a/_tools/ruby_h_to_go/lib/ruby_h_to_go.rb +++ b/_tools/ruby_h_to_go/lib/ruby_h_to_go.rb @@ -3,11 +3,13 @@ require "forwardable" require "ruby_header_parser" require "go_gem/util" +require "yaml" require_relative "ruby_h_to_go/type_helper" require_relative "ruby_h_to_go/argument_definition" require_relative "ruby_h_to_go/cli" +require_relative "ruby_h_to_go/config" require_relative "ruby_h_to_go/go_util" require_relative "ruby_h_to_go/enum_definition" require_relative "ruby_h_to_go/function_definition" @@ -17,4 +19,8 @@ # Generate Go binding from ruby.h module RubyHToGo + # @return [RubyHToGo::Config] + def self.config + @config ||= RubyHToGo::Config.new + end end diff --git a/_tools/ruby_h_to_go/lib/ruby_h_to_go/config.rb b/_tools/ruby_h_to_go/lib/ruby_h_to_go/config.rb new file mode 100644 index 00000000..03c1faf0 --- /dev/null +++ b/_tools/ruby_h_to_go/lib/ruby_h_to_go/config.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module RubyHToGo + # Manage config.yml + class Config + def initialize + @config = YAML.load_file(File.expand_path("../../config.yml", __dir__)) + end + + # @return [String] + def default_tag + @config["default_tag"] + end + + # @return [Array] + def available_tags + @config["available_tags"] + end + end +end diff --git a/_tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb b/_tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb index 88a7a11f..13d63000 100644 --- a/_tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb +++ b/_tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb @@ -2,7 +2,7 @@ module RubyHToGo # helper methods for generating go code - module GoUtil + module GoUtil # rubocop:disable Metrics/ModuleLength # @param str [String] # @return [String] def self.snake_to_camel(str) @@ -13,19 +13,38 @@ def self.snake_to_camel(str) # Generate initial go file whether not exists # @param go_file_path [String] - def self.generate_initial_go_file(go_file_path) + def self.generate_initial_go_file(go_file_path) # rubocop:disable Metrics/MethodLength return if File.exist?(go_file_path) - ruby_build_tag = GoGem::Util.ruby_minor_version_build_tag - - File.binwrite(go_file_path, <<~GO) + header = +<<~GO // THE AUTOGENERATED LICENSE. ALL THE RIGHTS ARE RESERVED BY ROBOTS. // WARNING: This file has automatically been generated // Code generated by ruby_h_to_go. DO NOT EDIT. - //go:build #{ruby_build_tag} + GO + + ruby_build_tag = GoGem::Util.ruby_minor_version_build_tag + + header << + if ruby_build_tag == RubyHToGo.config.default_tag + other_tags = RubyHToGo.config.available_tags - [RubyHToGo.config.default_tag] + condition = other_tags.map { |tag| "!#{tag}" }.join(" && ") + <<~GO + // FIXME: https://pkg.go.dev/ doesn't support custom build tag. + // Therefore, if no build tag is passed, treat it as the default tag + //go:build #{ruby_build_tag} || (#{condition}) + + GO + else + <<~GO + //go:build #{ruby_build_tag} + + GO + end + + header << <<~GO package ruby /* @@ -38,6 +57,8 @@ def self.generate_initial_go_file(go_file_path) ) GO + + File.binwrite(go_file_path, header) end C_TYPE_TO_GO_TYPE = { diff --git a/ruby/enum_ruby_3_3_generated.go b/ruby/enum_ruby_3_3_generated.go index b4f5f03c..18151be8 100644 --- a/ruby/enum_ruby_3_3_generated.go +++ b/ruby/enum_ruby_3_3_generated.go @@ -3,7 +3,9 @@ // WARNING: This file has automatically been generated // Code generated by ruby_h_to_go. DO NOT EDIT. -//go:build ruby_3_3 +// FIXME: https://pkg.go.dev/ doesn't support custom build tag. +// Therefore, if no build tag is passed, treat it as the default tag +//go:build ruby_3_3 || !ruby_3_4 package ruby diff --git a/ruby/function_ruby_3_3_generated.go b/ruby/function_ruby_3_3_generated.go index 2c843b5a..2316270f 100644 --- a/ruby/function_ruby_3_3_generated.go +++ b/ruby/function_ruby_3_3_generated.go @@ -3,7 +3,9 @@ // WARNING: This file has automatically been generated // Code generated by ruby_h_to_go. DO NOT EDIT. -//go:build ruby_3_3 +// FIXME: https://pkg.go.dev/ doesn't support custom build tag. +// Therefore, if no build tag is passed, treat it as the default tag +//go:build ruby_3_3 || !ruby_3_4 package ruby diff --git a/ruby/type_ruby_3_3_generated.go b/ruby/type_ruby_3_3_generated.go index b292d43a..b5301deb 100644 --- a/ruby/type_ruby_3_3_generated.go +++ b/ruby/type_ruby_3_3_generated.go @@ -3,7 +3,9 @@ // WARNING: This file has automatically been generated // Code generated by ruby_h_to_go. DO NOT EDIT. -//go:build ruby_3_3 +// FIXME: https://pkg.go.dev/ doesn't support custom build tag. +// Therefore, if no build tag is passed, treat it as the default tag +//go:build ruby_3_3 || !ruby_3_4 package ruby