33require "rake"
44require "rake/tasklib"
55
6+ require_relative "util"
7+
68module 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
187217end
0 commit comments