Skip to content

Commit 0241320

Browse files
authored
Improve Ruby extension Compilation (#13)
* Adopt template in spec.files (otherwise, gem consumers can't compile from source) * Update project compilation (use DLEXT instead of SOEXT) * Adjust rake tasks
1 parent ceffcda commit 0241320

File tree

6 files changed

+33
-36
lines changed

6 files changed

+33
-36
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
*.so
4242
*.so.*
4343
*.dylib
44+
*.bundle
4445

4546
# Executables
4647
*.exe
@@ -64,3 +65,6 @@ modules.order
6465
Module.symvers
6566
Mkfile.old
6667
dkms.conf
68+
69+
# Temporary files
70+
tmp/*

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test_sources = $(wildcard test/*.c)
88
test_objects = $(test_sources:.c=.o)
99
non_main_objects = $(filter-out src/main.o, $(objects))
1010

11-
soext ?= $(shell ruby -e 'puts RbConfig::CONFIG["SOEXT"]')
11+
soext ?= $(shell ruby -e 'puts RbConfig::CONFIG["DLEXT"]')
1212
lib_name = lib$(exec).$(soext)
1313
ruby_extension = ext/erbx/$(lib_name)
1414

@@ -47,4 +47,4 @@ test: $(test_objects) $(non_main_objects)
4747

4848
clean:
4949
rm -f $(exec) $(test_exec) $(lib_name) $(ruby_extension)
50-
rm -f src/*.o test/*.o
50+
rm -rf src/*.o test/*.o lib/erbx/*.bundle tmp

Rakefile

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@ Rake::TestTask.new(:test) do |t|
1717
end
1818

1919
Rake::Task[:compile].enhance do
20-
IO.popen("make") do |output|
21-
output.each_line do |line|
22-
puts "#{line}"
23-
end
24-
end
20+
IO.popen("make") do |output|
21+
output.each_line do |line|
22+
puts "#{line}"
23+
end
24+
end
25+
26+
if $?.exitstatus != 0
27+
raise "src/* could not be compiled #{$?.exitstatus}"
28+
end
29+
end
2530

26-
if $?.exitstatus != 0
27-
raise "src/* could not be compiled #{$?.exitstatus}"
28-
end
29-
end
3031

31-
Rake::Task[:clean].enhance do
32-
IO.popen("make clean") do |output|
33-
output.each_line do |line|
34-
puts "#{line}"
35-
end
36-
end
37-
end
32+
Rake::Task[:clean].enhance do
33+
IO.popen("make clean") do |output|
34+
output.each_line do |line|
35+
puts "#{line}"
36+
end
37+
end
38+
end
3839

39-
task default: [:compile, :test]
40+
task default: [:compile, :test]

erbx.gemspec

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ Gem::Specification.new do |spec|
1313
spec.required_ruby_version = ">= 3.0.0"
1414

1515
spec.require_paths = ["lib"]
16-
spec.files = [
17-
"ext/erbx/extension.c",
18-
"lib/erbx.rb",
19-
# "lib/erbx/version.rb"
20-
]
16+
17+
gemspec = File.basename(__FILE__)
18+
spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls|
19+
ls.readlines("\x0", chomp: true).reject do |f|
20+
(f == gemspec) ||
21+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
22+
end
23+
end
2124

2225
spec.extensions = ["ext/erbx/extconf.rb"]
2326
spec.metadata["allowed_push_host"] = "https://rubygems.org"

ext/erbx/extconf.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,5 @@
99
abort "#{extension_name}.h can't be found"
1010
end
1111

12-
# expected_functions = [
13-
# "erbx_lex",
14-
# "erbx_lex_file",
15-
# ]
16-
#
17-
# expected_functions.each do |expected_function|
18-
# unless find_library(extension_name, expected_function)
19-
# abort "lib#{extension_name}.so can't be found or #{expected_function}() not defined in it"
20-
# end
21-
# end
22-
2312
create_header
2413
create_makefile("#{extension_name}/#{extension_name}")

lib/erbx/liberbx.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module LibERBX
88
extend FFI::Library
99

1010
def self.library_extension
11-
RbConfig::CONFIG["SOEXT"]
11+
RbConfig::CONFIG["DLEXT"]
1212
end
1313

1414
def self.library_name

0 commit comments

Comments
 (0)