Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions _tools/ruby_h_to_go/config.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions _tools/ruby_h_to_go/lib/ruby_h_to_go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,4 +19,8 @@

# Generate Go binding from ruby.h
module RubyHToGo
# @return [RubyHToGo::Config]
def self.config
@config ||= RubyHToGo::Config.new
end
end
20 changes: 20 additions & 0 deletions _tools/ruby_h_to_go/lib/ruby_h_to_go/config.rb
Original file line number Diff line number Diff line change
@@ -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<String>]
def available_tags
@config["available_tags"]
end
end
end
33 changes: 27 additions & 6 deletions _tools/ruby_h_to_go/lib/ruby_h_to_go/go_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

/*
Expand All @@ -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 = {
Expand Down
4 changes: 3 additions & 1 deletion ruby/enum_ruby_3_3_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion ruby/function_ruby_3_3_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion ruby/type_ruby_3_3_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading