33require "bundler"
44require "rubygems/package_task"
55require "rake/extensiontask"
6+ require_relative "create_makefile_finder"
67
78module 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
141122end
0 commit comments