@@ -18,34 +18,39 @@ def configure
1818 create_makefile ( 'chdb/chdb_native' )
1919 end
2020
21+ def compiled?
22+ return false if cross_build?
23+
24+ major_version = RUBY_VERSION . match ( /(\d +\. \d +)/ ) [ 1 ]
25+ version_dir = File . join ( package_root_dir , 'lib' , 'chdb' , major_version )
26+
27+ extension = if determine_target_platform . include? ( 'darwin' )
28+ 'bundle'
29+ else
30+ 'so'
31+ end
32+ lib_file = "#{ libname } .#{ extension } "
33+
34+ File . exist? ( File . join ( version_dir , lib_file ) )
35+ end
36+
2137 def configure_cross_compiler
2238 RbConfig ::CONFIG [ 'CC' ] = RbConfig ::MAKEFILE_CONFIG [ 'CC' ] = ENV [ 'CC' ] if ENV [ 'CC' ]
2339 ENV [ 'CC' ] = RbConfig ::CONFIG [ 'CC' ]
2440 end
2541
42+ def cross_build?
43+ enable_config ( 'cross-build' )
44+ end
45+
2646 def libname
27- 'chdb '
47+ 'chdb_native '
2848 end
2949
3050 def configure_extension
3151 include_path = File . expand_path ( 'ext/chdb/include' , package_root_dir )
3252 append_cppflags ( "-I#{ include_path } " )
33-
34- lib_path = File . expand_path ( 'ext/chdb/lib' , package_root_dir )
35- append_ldflags ( "-L#{ lib_path } " )
36-
37- target_platform = determine_target_platform
38- if target_platform . include? ( 'darwin' )
39- append_ldflags ( '-Wl,-rpath,@loader_path/../lib' )
40- else
41- append_ldflags ( "-Wl,-rpath,'$$ORIGIN/../lib'" )
42- end
43-
4453 abort_could_not_find ( 'chdb.h' ) unless find_header ( 'chdb.h' , include_path )
45-
46- return if find_library ( libname , nil , lib_path )
47-
48- abort_could_not_find ( libname )
4954 end
5055
5156 def abort_could_not_find ( missing )
@@ -60,9 +65,25 @@ def download_and_extract
6065 target_platform = determine_target_platform
6166 version = fetch_chdb_version
6267 download_dir = determine_download_directory ( target_platform , version )
63-
64- unless Dir . exist? ( download_dir )
68+ need_download = false
69+
70+ if Dir . exist? ( download_dir )
71+ required_files = [
72+ File . join ( download_dir , 'chdb.h' ) ,
73+ File . join ( download_dir , 'libchdb.so' )
74+ ]
75+
76+ need_download = !required_files . all? { |f | File . exist? ( f ) }
77+ if need_download
78+ puts 'Missing required files, cleaning download directory...'
79+ FileUtils . rm_rf ( Dir . glob ( "#{ download_dir } /*" ) )
80+ end
81+ else
6582 FileUtils . mkdir_p ( download_dir )
83+ need_download = true
84+ end
85+
86+ if need_download
6687 file_name = get_file_name ( target_platform )
6788 url = build_download_url ( version , file_name )
6889 download_tarball ( url , download_dir , file_name )
@@ -83,7 +104,7 @@ def determine_target_platform
83104 when /arm64-darwin/ then 'arm64-darwin'
84105 when /x86_64-darwin/ then 'x86_64-darwin'
85106 else
86- 'unknown- platform'
107+ raise ArgumentError , "Unsupported platform: #{ RUBY_PLATFORM } ."
87108 end
88109 end
89110
@@ -114,8 +135,19 @@ def download_tarball(url, download_dir, file_name)
114135 tarball = File . join ( download_dir , file_name )
115136 puts "Downloading chdb library for #{ determine_target_platform } ..."
116137
117- URI . open ( url ) do |remote | # rubocop:disable Security/Open
118- IO . copy_stream ( remote , tarball )
138+ max_retries = 3
139+ retries = 0
140+
141+ begin
142+ URI . open ( url ) do |remote | # rubocop:disable Security/Open
143+ IO . copy_stream ( remote , tarball )
144+ end
145+ rescue StandardError => e
146+ raise "Failed to download after #{ max_retries } attempts: #{ e . message } " unless retries < max_retries
147+
148+ retries += 1
149+ puts "Download failed: #{ e . message } . Retrying (attempt #{ retries } /#{ max_retries } )..."
150+ retry
119151 end
120152 end
121153
@@ -125,24 +157,27 @@ def extract_tarball(download_dir, file_name)
125157 end
126158
127159 def copy_files ( download_dir , _version )
128- ext_chdb_path = File . join ( package_root_dir , 'ext/chdb' )
129- [ %w[ *.h ] , %w[ *.so ] , %w[ *.dylib ] ] . each do |( glob_pattern ) |
130- src_dir , pattern = File . split ( glob_pattern )
131-
160+ [ %w[ *.h ] , %w[ *.so ] ] . each do |( glob_pattern ) |
161+ # Removed the unused variable src_dir
162+ pattern = File . basename ( glob_pattern )
132163 dest_subdir = case pattern
133164 when '*.h' then 'include'
134165 else 'lib'
135166 end
136- src = File . join ( download_dir , src_dir , pattern )
137- dest = File . join ( ext_chdb_path , dest_subdir )
138- FileUtils . mkdir_p ( dest )
139- FileUtils . cp_r ( Dir . glob ( src ) , dest , remove_destination : true )
140-
141- # target_platform = determine_target_platform
142- # if target_platform.include?('darwin') && (pattern == '*.so')
143- # system("install_name_tool -id '@rpath/libchdb.so' #{File.join(dest, 'libchdb.so')}")
144- # system("codesign -f -s - #{File.join(dest, 'libchdb.so')}")
145- # end
167+ dest_dir = File . join ( package_root_dir , 'ext/chdb' , dest_subdir )
168+ src_files = Dir . glob ( File . join ( download_dir , pattern ) )
169+
170+ extra_dirs = [ ]
171+ extra_dirs << File . join ( package_root_dir , 'lib/chdb/lib' ) if pattern == '*.so'
172+
173+ ( [ dest_dir ] + extra_dirs ) . each do |dest |
174+ FileUtils . mkdir_p ( dest )
175+
176+ src_files . each do |src_file |
177+ dest_file = File . join ( dest , File . basename ( src_file ) )
178+ FileUtils . ln_s ( File . expand_path ( src_file ) , dest_file , force : true )
179+ end
180+ end
146181 end
147182 end
148183
0 commit comments