Skip to content

Commit ebe3a14

Browse files
authored
Merge e57bbc0 into 6633ccc
2 parents 6633ccc + e57bbc0 commit ebe3a14

24 files changed

+9535
-27
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,16 @@ jobs:
136136

137137
- run: echo $CGO_CFLAGS
138138

139+
- name: export BUILD_TAG for golangci-lint
140+
run: BUILD_TAG=$(bundle exec rake go:build_tag) >> $GITHUB_ENV
141+
142+
- run: echo $BUILD_TAG
143+
139144
- name: golangci-lint
140145
uses: golangci/golangci-lint-action@v6
141146
with:
142147
version: v1.60
148+
args: --build-tags ${{ env.BUILD_TAG }}
143149

144150
- name: Slack Notification (not success)
145151
uses: act10ns/slack@v2

.github/workflows/matrix.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"1.23"
44
],
55
"ruby": [
6-
"3.3"
6+
"3.3",
7+
"3.4"
78
]
89
}

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ GEM
3131
debug_inspector (1.2.0)
3232
diff-lcs (1.5.1)
3333
drb (2.2.1)
34-
ffi (1.17.0)
35-
ffi (1.17.0-arm64-darwin)
34+
ffi (1.17.1)
35+
ffi (1.17.1-arm64-darwin)
3636
fileutils (1.7.3)
3737
i18n (1.14.6)
3838
concurrent-ruby (~> 1.0)

_gem/go_gem.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
1212
spec.description = "Helpers for compiling Go extensions for ruby"
1313
spec.homepage = "https://github.com/ruby-go-gem/go-gem-wrapper"
1414
spec.license = "MIT"
15-
spec.required_ruby_version = ">= 3.3.0"
15+
spec.required_ruby_version = [">= 3.3.0", "< 3.5.0"]
1616

1717
spec.metadata["homepage_uri"] = spec.homepage
1818
spec.metadata["source_code_uri"] = "#{spec.homepage}/tree/main/_gem"

_gem/lib/go_gem/mkmf.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require_relative "util"
4+
35
module GoGem
46
# Helper module for creating Go Makefiles
57
module Mkmf
@@ -35,11 +37,13 @@ def $objs.empty?; false; end
3537

3638
current_dir = File.expand_path(".")
3739

40+
goflags = "-tags=#{GoGem::Util.ruby_minor_version_build_tag}"
41+
3842
File.open("Makefile", "a") do |f|
3943
f.write <<~MAKEFILE.gsub(/^ {8}/, "\t")
4044
$(DLLIB): Makefile $(srcdir)/*.go
4145
cd $(srcdir); \
42-
CGO_CFLAGS='$(INCFLAGS)' CGO_LDFLAGS='#{ldflags}' \
46+
CGO_CFLAGS='$(INCFLAGS)' CGO_LDFLAGS='#{ldflags}' GOFLAGS='#{goflags}' \
4347
go build -p 4 -buildmode=c-shared -o #{current_dir}/$(DLLIB)
4448
MAKEFILE
4549
end

_gem/lib/go_gem/rake_task.rb

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require "rake"
44
require "rake/tasklib"
55

6+
require_relative "util"
7+
68
module GoGem
79
# Provides rake tasks for `go test` with CRuby
810
#
@@ -40,7 +42,7 @@ module GoGem
4042
# end
4143
# end
4244
# end
43-
class RakeTask < ::Rake::TaskLib
45+
class RakeTask < ::Rake::TaskLib # rubocop:disable Metrics/ClassLength
4446
DEFAULT_TASK_NAMESPACE = :go
4547

4648
DEFAULT_GO_BIN_PATH = "go"
@@ -87,27 +89,16 @@ def initialize(gem_name)
8789
define_go_testrace_task
8890
define_go_fmt_task
8991
define_go_build_envs_task
92+
define_go_build_tag_task
9093
end
9194
end
9295

9396
# Generate environment variables to build go programs in the Go gem
9497
#
9598
# @return [Hash<String, String>]
9699
def self.build_env_vars
97-
ldflags = "-L#{RbConfig::CONFIG["libdir"]} -l#{RbConfig::CONFIG["RUBY_SO_NAME"]}"
98-
99-
case `#{RbConfig::CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
100-
when /Free Software Foundation/
101-
ldflags << " -Wl,--unresolved-symbols=ignore-all"
102-
when /clang/
103-
ldflags << " -undefined dynamic_lookup"
104-
end
105-
106-
cflags = [
107-
RbConfig::CONFIG["CFLAGS"],
108-
"-I#{RbConfig::CONFIG["rubyarchhdrdir"]}",
109-
"-I#{RbConfig::CONFIG["rubyhdrdir"]}",
110-
].join(" ")
100+
ldflags = generate_ldflags
101+
cflags = generate_cflags
111102

112103
# FIXME: Workaround for Ubuntu (GitHub Actions)
113104
if RUBY_PLATFORM =~ /linux/i
@@ -123,12 +114,44 @@ def self.build_env_vars
123114
ld_library_path = RbConfig::CONFIG["libdir"].to_s
124115

125116
{
117+
"GOFLAGS" => generate_goflags,
126118
"CGO_CFLAGS" => cflags,
127119
"CGO_LDFLAGS" => ldflags,
128120
"LD_LIBRARY_PATH" => ld_library_path,
129121
}
130122
end
131123

124+
# @return [String]
125+
def self.generate_goflags
126+
"-tags=#{GoGem::Util.ruby_minor_version_build_tag}"
127+
end
128+
private_class_method :generate_goflags
129+
130+
# @return [String]
131+
def self.generate_ldflags
132+
ldflags = "-L#{RbConfig::CONFIG["libdir"]} -l#{RbConfig::CONFIG["RUBY_SO_NAME"]}"
133+
134+
case `#{RbConfig::CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
135+
when /Free Software Foundation/
136+
ldflags << " -Wl,--unresolved-symbols=ignore-all"
137+
when /clang/
138+
ldflags << " -undefined dynamic_lookup"
139+
end
140+
141+
ldflags
142+
end
143+
private_class_method :generate_ldflags
144+
145+
# @return [String]
146+
def self.generate_cflags
147+
[
148+
RbConfig::CONFIG["CFLAGS"],
149+
"-I#{RbConfig::CONFIG["rubyarchhdrdir"]}",
150+
"-I#{RbConfig::CONFIG["rubyhdrdir"]}",
151+
].join(" ")
152+
end
153+
private_class_method :generate_cflags
154+
132155
# @yield
133156
def within_target_dir
134157
Dir.chdir(target_dir) do # rubocop:disable Style/ExplicitBlockArgument
@@ -183,5 +206,12 @@ def define_go_build_envs_task
183206
end
184207
end
185208
end
209+
210+
def define_go_build_tag_task
211+
desc "Print build tag"
212+
task(:build_tag) do
213+
puts GoGem::Util.ruby_minor_version_build_tag
214+
end
215+
end
186216
end
187217
end

_gem/lib/go_gem/util.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
module GoGem
4+
# Common utility methods for {GoGem::Mkmf} and {GoGem::RakeTask}
5+
module Util
6+
# Return ruby version build tag for `go build` and `go test`
7+
#
8+
# @param ruby_version [String]
9+
# @return [String]
10+
#
11+
# @example
12+
# GoGem::Util.ruby_minor_version_build_tag("3.4.1")
13+
# #=> "ruby_3_4"
14+
def self.ruby_minor_version_build_tag(ruby_version = RUBY_VERSION)
15+
"ruby_#{ruby_version.to_f.to_s.gsub(".", "_")}"
16+
end
17+
end
18+
end

_gem/sig/go_gem/rake_task.rbs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ module GoGem
2828

2929
def initialize: (String gem_name) ?{ (RakeTask) -> void } -> void
3030

31-
def self.build_env_vars: () -> { "CGO_CFLAGS" => String, "CGO_LDFLAGS" => String, "LD_LIBRARY_PATH" => String }
31+
def self.build_env_vars: () -> { "GOFLAGS" => String, "CGO_CFLAGS" => String, "CGO_LDFLAGS" => String, "LD_LIBRARY_PATH" => String }
32+
33+
def self.generate_goflags: () -> String
34+
35+
def self.generate_ldflags: () -> String
36+
37+
def self.generate_cflags: () -> String
3238

3339
private
3440

@@ -40,6 +46,8 @@ module GoGem
4046

4147
def define_go_build_envs_task: () -> void
4248

49+
def define_go_build_tag_task: () -> void
50+
4351
def within_target_dir: () { () -> void } -> void
4452

4553
def ext_dir: () -> String

_gem/sig/go_gem/util.rbs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module GoGem
2+
module Util
3+
def self.ruby_minor_version_build_tag: (?String ruby_version) -> String
4+
end
5+
end

_gem/spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "go_gem"
44
require "go_gem/mkmf"
55
require "go_gem/rake_task"
6+
require "go_gem/util"
67

78
require "tmpdir"
89
require "serverspec"

0 commit comments

Comments
 (0)