Skip to content

Commit a2959de

Browse files
authored
Merge pull request #20 from shopify-playground/ec-parse-extconf
Parse the extconf instead of running it:
2 parents a9ea619 + 93ad428 commit a2959de

File tree

5 files changed

+36
-34
lines changed

5 files changed

+36
-34
lines changed

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ PATH
22
remote: .
33
specs:
44
easy_compile (0.1.1)
5+
prism
56
rake-compiler
67
thor
78

89
GEM
910
remote: https://rubygems.org/
1011
specs:
1112
minitest (5.25.5)
13+
prism (1.6.0)
1214
rake (13.3.0)
1315
rake-compiler (1.3.0)
1416
rake

easy_compile.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
2525

2626
spec.add_dependency "rake-compiler"
2727
spec.add_dependency "thor"
28+
spec.add_dependency "prism"
2829
end

lib/easy_compile/cli.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ def run_rake_tasks!(*tasks)
134134
rake_specs = Gem.loaded_specs["rake"]
135135
rake_executable = rake_specs.bin_file("rake")
136136
rake_path = rake_specs.full_require_paths
137-
load_paths = (rake_compiler_path + rake_path).join(File::PATH_SEPARATOR)
137+
prism_path = Gem.loaded_specs["prism"].full_require_paths
138+
load_paths = (rake_compiler_path + rake_path + prism_path).join(File::PATH_SEPARATOR)
138139

139140
system({ "RUBYLIB" => load_paths }, "bundle exec #{RbConfig.ruby} #{rake_executable} #{all_tasks} -R#{rakelibdir}", exception: true)
140141
end

lib/easy_compile/compilation_tasks.rb

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
require "bundler"
44
require "rubygems/package_task"
55
require "rake/extensiontask"
6+
require_relative "create_makefile_finder"
67

78
module EasyCompile
89
class CompilationTasks
910
attr_reader :gemspec, :native, :create_packaging_task, :extension_task
10-
attr_accessor :binary_name
1111

1212
def initialize(create_packaging_task = false, gemspec = nil)
1313
@gemspec = Bundler.load_gemspec(gemspec || find_gemspec)
@@ -17,10 +17,9 @@ def initialize(create_packaging_task = false, gemspec = nil)
1717
end
1818

1919
def setup
20-
with_mkmf_monkey_patch do
21-
gemspec.extensions.each do |path|
22-
define_task(path)
23-
end
20+
gemspec.extensions.each do |path|
21+
binary_name = parse_extconf(path)
22+
define_task(path, binary_name)
2423
end
2524

2625
setup_packaging if create_packaging_task
@@ -52,46 +51,21 @@ def setup_packaging
5251
end
5352
end
5453

55-
def with_mkmf_monkey_patch
56-
require "mkmf"
57-
58-
instance = self
59-
60-
previous_create_makefile = method(:create_makefile)
61-
Object.define_method(:create_makefile) do |name, *args|
62-
instance.binary_name = name
63-
previous_create_makefile.call(name, *args)
64-
end
65-
66-
Object.define_method(:create_rust_makefile) do |name, *args|
67-
instance.binary_name = name
68-
end
69-
70-
yield
71-
ensure
72-
Object.remove_method(:create_makefile)
73-
Object.remove_method(:create_rust_makefile)
74-
end
75-
76-
def define_task(path)
77-
require File.expand_path(path)
78-
54+
def define_task(path, binary_name)
7955
@extension_task = Rake::ExtensionTask.new do |ext|
8056
ext.name = File.basename(binary_name)
8157
ext.config_script = File.basename(path)
8258
ext.ext_dir = File.dirname(path)
83-
ext.lib_dir = binary_lib_dir if binary_lib_dir
59+
ext.lib_dir = binary_lib_dir(binary_name) if binary_lib_dir(binary_name)
8460
ext.gem_spec = gemspec
8561
ext.cross_platform = normalized_platform
8662
ext.cross_compile = true
8763
end
8864

8965
disable_shared unless Gem.win_platform?
90-
ensure
91-
self.binary_name = nil
9266
end
9367

94-
def binary_lib_dir
68+
def binary_lib_dir(binary_name)
9569
dir = File.dirname(binary_name)
9670
return if dir == "."
9771

@@ -137,5 +111,12 @@ def verify_gemspec!
137111
This tool can't be used on pure Ruby gems.
138112
EOM
139113
end
114+
115+
def parse_extconf(path)
116+
visitor = CreateMakefileFinder.new
117+
Prism.parse_file(path).value.accept(visitor)
118+
119+
visitor.binary_name
120+
end
140121
end
141122
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require "prism"
4+
5+
module EasyCompile
6+
class CreateMakefileFinder < Prism::Visitor
7+
attr_reader :binary_name
8+
9+
def visit_call_node(node)
10+
super
11+
looking_for = [:create_makefile, :create_rust_makefile]
12+
return unless looking_for.include?(node.name)
13+
14+
@binary_name = node.arguments.child_nodes.first.content
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)