Skip to content

Commit b614987

Browse files
authored
Merge pull request #175 from sue445/feature/create_go_makefile
Impl `create_go_makefile`
2 parents f6d4624 + 84d382f commit b614987

File tree

8 files changed

+130
-110
lines changed

8 files changed

+130
-110
lines changed

.yardopts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
--hide-void-return
44
_tools/ruby_h_to_go/*.rb
55
_tools/ruby_h_to_go/lib/**/*.rb
6+
gem/**/*.rb
67
-
78
CHANGELOG.md
89
LICENSE

_tools/patch_for_go_gem/patch_for_go_gem.rb

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def perform
3131
create_go_mod
3232
update_gem_name_c
3333
update_extconf_rb
34+
update_gemspec
3435
end
3536

3637
private
@@ -125,65 +126,45 @@ def update_gem_name_c
125126
save_file(file_path: gem_name_c_path, content:)
126127
end
127128

128-
def update_extconf_rb # rubocop:disable Metrics/MethodLength
129+
def update_extconf_rb
129130
extconf_rb_path = File.join(ext_dir, "extconf.rb")
130131

131132
content = File.read(extconf_rb_path)
132133

133-
unless content.include?(<<~RUBY)
134-
require "mkmf"
135-
136-
find_executable("go")
137-
RUBY
138-
134+
unless content.include?(%(require "go_gem/mkmf"))
139135
content.gsub!(<<~RUBY, <<~RUBY)
140136
require "mkmf"
141137
RUBY
142138
require "mkmf"
143-
144-
find_executable("go")
145-
146-
# rubocop:disable Style/GlobalVars
147-
$objs = []
148-
def $objs.empty?; false; end
149-
# rubocop:enable Style/GlobalVars
150-
139+
require "go_gem/mkmf"
151140
RUBY
152141
end
153142

154-
unless content.include?(<<~RUBY)
155-
create_makefile("#{gem_name}/#{gem_name}")
156-
157-
case `\#{CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
158-
RUBY
159-
143+
unless content.include?(%(create_go_makefile("#{gem_name}/#{gem_name}")))
160144
content.gsub!(<<~RUBY, <<~RUBY)
161145
create_makefile("#{gem_name}/#{gem_name}")
162146
RUBY
163-
create_makefile("#{gem_name}/#{gem_name}")
147+
create_go_makefile("#{gem_name}/#{gem_name}")
148+
RUBY
149+
end
164150

165-
case `\#{CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
166-
when /Free Software Foundation/
167-
ldflags = "-Wl,--unresolved-symbols=ignore-all"
168-
when /clang/
169-
ldflags = "-undefined dynamic_lookup"
170-
end
151+
save_file(file_path: extconf_rb_path, content:)
152+
end
171153

172-
current_dir = File.expand_path(".")
154+
def update_gemspec
155+
content = File.read(gemspec_file)
173156

174-
File.open("Makefile", "a") do |f|
175-
f.write <<~MAKEFILE.gsub(/^ {8}/, "\t")
176-
$(DLLIB): Makefile $(srcdir)/*.go
177-
cd $(srcdir); \
178-
CGO_CFLAGS='$(INCFLAGS)' CGO_LDFLAGS='\#{ldflags}' \\
179-
go build -p 4 -buildmode=c-shared -o \#{current_dir}/$(DLLIB)
180-
MAKEFILE
181-
end
157+
return if content.include?(%(.add_dependency "go_gem")) || content.include?(%(.add_runtime_dependency "go_gem"))
182158

183-
RUBY
184-
end
159+
content =~ /Gem::Specification\.new\s+do\s+\|(.+)\|/
160+
spec_var_name = ::Regexp.last_match(1)
185161

186-
save_file(file_path: extconf_rb_path, content:)
162+
content.gsub!(/^end\n/, <<~RUBY)
163+
#{spec_var_name}.add_dependency "go_gem"
164+
end
165+
RUBY
166+
167+
save_file(file_path: gemspec_file, content:)
187168
end
188169

189170
# @param file_path [String]

_tools/patch_for_go_gem/spec/patch_for_go_gem_spec.rb

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -88,47 +88,18 @@ module github.com/username/#{gem_name}
8888
end
8989

9090
describe file(File.join(gem_name, "ext", gem_name, "extconf.rb")) do
91-
let(:content1) do
92-
<<~RUBY
93-
require "mkmf"
94-
95-
find_executable("go")
96-
97-
# rubocop:disable Style/GlobalVars
98-
$objs = []
99-
def $objs.empty?; false; end
100-
# rubocop:enable Style/GlobalVars
101-
RUBY
102-
end
103-
104-
let(:content2) do
105-
<<~RUBY
106-
create_makefile("#{gem_name}/#{gem_name}")
107-
108-
case `\#{CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
109-
when /Free Software Foundation/
110-
ldflags = "-Wl,--unresolved-symbols=ignore-all"
111-
when /clang/
112-
ldflags = "-undefined dynamic_lookup"
113-
end
114-
115-
current_dir = File.expand_path(".")
91+
it { should be_file }
92+
it { should exist }
11693

117-
File.open("Makefile", "a") do |f|
118-
f.write <<~MAKEFILE.gsub(/^ {8}/, "\t")
119-
$(DLLIB): Makefile $(srcdir)/*.go
120-
cd $(srcdir); \
121-
CGO_CFLAGS='$(INCFLAGS)' CGO_LDFLAGS='\#{ldflags}' \\
122-
go build -p 4 -buildmode=c-shared -o \#{current_dir}/$(DLLIB)
123-
MAKEFILE
124-
end
125-
RUBY
126-
end
94+
its(:content) { should include %(require "go_gem/mkmf") }
95+
its(:content) { should include %(create_go_makefile("#{gem_name}/#{gem_name}")) }
96+
its(:content) { should_not include %(create_makefile("#{gem_name}/#{gem_name}")) }
97+
end
12798

99+
describe file(File.join(gem_name, "#{gem_name}.gemspec")) do
128100
it { should be_file }
129101
it { should exist }
130102

131-
its(:content) { should include content1 }
132-
its(:content) { should include content2 }
103+
its(:content) { should include %(spec.add_dependency "go_gem") }
133104
end
134105
end

gem/lib/go_gem/mkmf.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
module GoGem
4+
# Helper module for creating Go Makefiles
5+
module Mkmf
6+
# Create Makefile for go-gem
7+
#
8+
# @param target [String]
9+
# @param srcprefix [String,nil]
10+
#
11+
# @example
12+
# require "mkmf"
13+
# require "go_gem/mkmf" # Append this
14+
#
15+
# # Use create_go_makefile instead of create_makefile
16+
# # create_makefile("example/example")
17+
# create_go_makefile("example/example")
18+
def create_go_makefile(target, srcprefix = nil)
19+
find_executable("go")
20+
21+
# rubocop:disable Style/GlobalVars
22+
$objs = []
23+
def $objs.empty?; false; end
24+
# rubocop:enable Style/GlobalVars
25+
26+
create_makefile(target, srcprefix)
27+
28+
case `#{CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
29+
when /Free Software Foundation/
30+
ldflags = "-Wl,--unresolved-symbols=ignore-all"
31+
when /clang/
32+
ldflags = "-undefined dynamic_lookup"
33+
end
34+
35+
current_dir = File.expand_path(".")
36+
37+
File.open("Makefile", "a") do |f|
38+
f.write <<~MAKEFILE.gsub(/^ {8}/, "\t")
39+
$(DLLIB): Makefile $(srcdir)/*.go
40+
cd $(srcdir); \
41+
CGO_CFLAGS='$(INCFLAGS)' CGO_LDFLAGS='#{ldflags}' \
42+
go build -p 4 -buildmode=c-shared -o #{current_dir}/$(DLLIB)
43+
MAKEFILE
44+
end
45+
end
46+
end
47+
end
48+
49+
include GoGem::Mkmf # rubocop:disable Style/MixinUsage

gem/spec/go_gem/mkmf_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe GoGem::Mkmf do
4+
describe "#create_go_makefile" do
5+
gem_name = "new_gem"
6+
7+
before(:all) do
8+
@temp_dir = Dir.mktmpdir
9+
10+
Dir.chdir(@temp_dir) do
11+
create_go_makefile("#{gem_name}/#{gem_name}")
12+
end
13+
end
14+
15+
after(:all) do
16+
FileUtils.remove_entry_secure(@temp_dir) if @temp_dir && Dir.exist?(@temp_dir)
17+
end
18+
19+
around do |example|
20+
Dir.chdir(@temp_dir) do
21+
example.run
22+
end
23+
end
24+
25+
describe file("Makefile") do
26+
it { should be_file }
27+
it { should exist }
28+
29+
# content of create_makefile
30+
its(:content) { should match(%r{^SHELL = /bin/sh$}) }
31+
32+
# content of create_go_makefile
33+
its(:content) { should match(%r{^\$\(DLLIB\): Makefile \$\(srcdir\)/\*\.go$}) }
34+
end
35+
end
36+
end

gem/spec/go_gem_spec.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

gem/spec/spec_helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# frozen_string_literal: true
22

33
require "go_gem"
4+
require "go_gem/mkmf"
5+
6+
require "tmpdir"
7+
require "serverspec"
8+
require "mkmf"
9+
10+
set :backend, :exec
411

512
RSpec.configure do |config|
613
# Enable flags like --only-failures and --next-failure
@@ -12,4 +19,10 @@
1219
config.expect_with :rspec do |c|
1320
c.syntax = :expect
1421
end
22+
23+
config.filter_run_when_matching :focus
24+
25+
config.define_derived_metadata do |meta|
26+
meta[:aggregate_failures] = true
27+
end
1528
end

ruby/testdata/example/ext/example/extconf.rb

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,14 @@
33
require "mkmf"
44

55
############ Appended for go native extension
6-
find_executable("go")
7-
8-
# rubocop:disable Style/GlobalVars
9-
$objs = []
10-
def $objs.empty?; false; end
11-
# rubocop:enable Style/GlobalVars
12-
6+
require "go_gem/mkmf"
137
############ Appended for go native extension
148

159
# Makes all symbols private by default to avoid unintended conflict
1610
# with other gems. To explicitly export symbols you can use RUBY_FUNC_EXPORTED
1711
# selectively, or entirely remove this flag.
1812
append_cflags("-fvisibility=hidden")
1913

20-
create_makefile("example/example")
21-
2214
############ Appended for go native extension
23-
case `#{CONFIG["CC"]} --version` # rubocop:disable Lint/LiteralAsCondition
24-
when /Free Software Foundation/
25-
ldflags = "-Wl,--unresolved-symbols=ignore-all"
26-
when /clang/
27-
ldflags = "-undefined dynamic_lookup"
28-
end
29-
30-
current_dir = File.expand_path(".")
31-
32-
File.open("Makefile", "a") do |f|
33-
f.write <<~MAKEFILE.gsub(/^ {8}/, "\t")
34-
$(DLLIB): Makefile $(srcdir)/*.go
35-
cd $(srcdir); \
36-
CGO_CFLAGS='$(INCFLAGS)' CGO_LDFLAGS='#{ldflags}' \
37-
go build -p 4 -buildmode=c-shared -o #{current_dir}/$(DLLIB)
38-
MAKEFILE
39-
end
15+
create_go_makefile("example/example")
4016
############ Appended for go native extension

0 commit comments

Comments
 (0)