Skip to content

Commit a186e9e

Browse files
authored
Merge pull request #259 from ruby-go-gem/refactor
Refactor go_gem
2 parents ae7f410 + d613aad commit a186e9e

File tree

5 files changed

+58
-59
lines changed

5 files changed

+58
-59
lines changed

_gem/lib/go_gem/mkmf.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ def $objs.empty?; false; end
2828

2929
create_makefile(target, srcprefix)
3030

31-
case `#{RbConfig::CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
32-
when /Free Software Foundation/
33-
ldflags = "-Wl,--unresolved-symbols=ignore-all"
34-
when /clang/
35-
ldflags = "-undefined dynamic_lookup"
36-
end
37-
31+
ldflags = GoGem::Util.generate_ldflags
3832
current_dir = File.expand_path(".")
3933

4034
goflags = "-tags=#{GoGem::Util.ruby_minor_version_build_tag}"

_gem/lib/go_gem/rake_task.rb

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module GoGem
4444
# end
4545
# end
4646
# end
47-
class RakeTask < ::Rake::TaskLib # rubocop:disable Metrics/ClassLength
47+
class RakeTask < ::Rake::TaskLib
4848
DEFAULT_TASK_NAMESPACE = :go
4949

5050
DEFAULT_GO_BIN_PATH = "go"
@@ -99,61 +99,19 @@ def initialize(gem_name)
9999
#
100100
# @return [Hash<String, String>]
101101
def self.build_env_vars
102-
ldflags = generate_ldflags
103-
cflags = generate_cflags
104-
105-
# FIXME: Workaround for Ubuntu (GitHub Actions)
106-
if RUBY_PLATFORM =~ /linux/i
107-
cflags.gsub!("-Wno-self-assign", "")
108-
cflags.gsub!("-Wno-parentheses-equality", "")
109-
cflags.gsub!("-Wno-constant-logical-operand", "")
110-
cflags.gsub!("-Wsuggest-attribute=format", "")
111-
cflags.gsub!("-Wold-style-definition", "")
112-
cflags.gsub!("-Wsuggest-attribute=noreturn", "")
113-
ldflags.gsub!("-Wl,--unresolved-symbols=ignore-all", "")
114-
end
102+
ldflags = GoGem::Util.generate_ldflags
103+
cflags = GoGem::Util.generate_cflags
115104

116105
ld_library_path = RbConfig::CONFIG["libdir"].to_s
117106

118107
{
119-
"GOFLAGS" => generate_goflags,
108+
"GOFLAGS" => GoGem::Util.generate_goflags,
120109
"CGO_CFLAGS" => cflags,
121110
"CGO_LDFLAGS" => ldflags,
122111
"LD_LIBRARY_PATH" => ld_library_path,
123112
}
124113
end
125114

126-
# @return [String]
127-
def self.generate_goflags
128-
"-tags=#{GoGem::Util.ruby_minor_version_build_tag}"
129-
end
130-
private_class_method :generate_goflags
131-
132-
# @return [String]
133-
def self.generate_ldflags
134-
ldflags = "-L#{RbConfig::CONFIG["libdir"]} -l#{RbConfig::CONFIG["RUBY_SO_NAME"]}"
135-
136-
case `#{RbConfig::CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
137-
when /Free Software Foundation/
138-
ldflags << " -Wl,--unresolved-symbols=ignore-all"
139-
when /clang/
140-
ldflags << " -undefined dynamic_lookup"
141-
end
142-
143-
ldflags
144-
end
145-
private_class_method :generate_ldflags
146-
147-
# @return [String]
148-
def self.generate_cflags
149-
[
150-
RbConfig::CONFIG["CFLAGS"],
151-
"-I#{RbConfig::CONFIG["rubyarchhdrdir"]}",
152-
"-I#{RbConfig::CONFIG["rubyhdrdir"]}",
153-
].join(" ")
154-
end
155-
private_class_method :generate_cflags
156-
157115
# @yield
158116
def within_target_dir
159117
Dir.chdir(target_dir) do # rubocop:disable Style/ExplicitBlockArgument

_gem/lib/go_gem/util.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,52 @@ module Util
1414
def self.ruby_minor_version_build_tag(ruby_version = RUBY_VERSION)
1515
"ruby_#{ruby_version.to_f.to_s.gsub(".", "_")}"
1616
end
17+
18+
# @return [String]
19+
def self.generate_ldflags
20+
ldflags = "-L#{RbConfig::CONFIG["libdir"]} -l#{RbConfig::CONFIG["RUBY_SO_NAME"]}"
21+
22+
case `#{RbConfig::CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
23+
when /Free Software Foundation/
24+
ldflags << " -Wl,--unresolved-symbols=ignore-all"
25+
when /clang/
26+
ldflags << " -undefined dynamic_lookup"
27+
end
28+
29+
# FIXME: Workaround for Ubuntu (GitHub Actions)
30+
ldflags.gsub!("-Wl,--unresolved-symbols=ignore-all", "") if RUBY_PLATFORM =~ /linux/i
31+
32+
ldflags.strip
33+
end
34+
35+
# @return [String]
36+
def self.generate_cflags
37+
cflags =
38+
[
39+
RbConfig::CONFIG["CFLAGS"],
40+
"-I#{RbConfig::CONFIG["rubyarchhdrdir"]}",
41+
"-I#{RbConfig::CONFIG["rubyhdrdir"]}",
42+
].join(" ")
43+
44+
# FIXME: Workaround for Ubuntu (GitHub Actions)
45+
if RUBY_PLATFORM =~ /linux/i
46+
cflags.gsub!("-Wno-self-assign", "")
47+
cflags.gsub!("-Wno-parentheses-equality", "")
48+
cflags.gsub!("-Wno-constant-logical-operand", "")
49+
cflags.gsub!("-Wsuggest-attribute=format", "")
50+
cflags.gsub!("-Wold-style-definition", "")
51+
cflags.gsub!("-Wsuggest-attribute=noreturn", "")
52+
end
53+
54+
# FIXME: Workaround for Alpine
55+
cflags.gsub!("-Wpointer-arith", "") if RUBY_PLATFORM =~ /linux-musl/i
56+
57+
cflags.strip
58+
end
59+
60+
# @return [String]
61+
def self.generate_goflags
62+
"-tags=#{ruby_minor_version_build_tag}"
63+
end
1764
end
1865
end

_gem/sig/go_gem/rake_task.rbs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ module GoGem
3030

3131
def self.build_env_vars: () -> { "GOFLAGS" => String, "CGO_CFLAGS" => String, "CGO_LDFLAGS" => String, "LD_LIBRARY_PATH" => String }
3232

33-
def self.generate_goflags: () -> String
34-
35-
def self.generate_ldflags: () -> String
36-
37-
def self.generate_cflags: () -> String
38-
3933
private
4034

4135
def define_go_test_task: () -> void

_gem/sig/go_gem/util.rbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
module GoGem
22
module Util
33
def self.ruby_minor_version_build_tag: (?String ruby_version) -> String
4+
5+
def self.generate_ldflags: () -> String
6+
7+
def self.generate_cflags: () -> String
8+
9+
def self.generate_goflags: () -> String
410
end
511
end

0 commit comments

Comments
 (0)