@@ -18,34 +18,39 @@ def configure
18
18
create_makefile ( 'chdb/chdb_native' )
19
19
end
20
20
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
+
21
37
def configure_cross_compiler
22
38
RbConfig ::CONFIG [ 'CC' ] = RbConfig ::MAKEFILE_CONFIG [ 'CC' ] = ENV [ 'CC' ] if ENV [ 'CC' ]
23
39
ENV [ 'CC' ] = RbConfig ::CONFIG [ 'CC' ]
24
40
end
25
41
42
+ def cross_build?
43
+ enable_config ( 'cross-build' )
44
+ end
45
+
26
46
def libname
27
- 'chdb '
47
+ 'chdb_native '
28
48
end
29
49
30
50
def configure_extension
31
51
include_path = File . expand_path ( 'ext/chdb/include' , package_root_dir )
32
52
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
-
44
53
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 )
49
54
end
50
55
51
56
def abort_could_not_find ( missing )
@@ -60,9 +65,25 @@ def download_and_extract
60
65
target_platform = determine_target_platform
61
66
version = fetch_chdb_version
62
67
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
65
82
FileUtils . mkdir_p ( download_dir )
83
+ need_download = true
84
+ end
85
+
86
+ if need_download
66
87
file_name = get_file_name ( target_platform )
67
88
url = build_download_url ( version , file_name )
68
89
download_tarball ( url , download_dir , file_name )
@@ -83,7 +104,7 @@ def determine_target_platform
83
104
when /arm64-darwin/ then 'arm64-darwin'
84
105
when /x86_64-darwin/ then 'x86_64-darwin'
85
106
else
86
- 'unknown- platform'
107
+ raise ArgumentError , "Unsupported platform: #{ RUBY_PLATFORM } ."
87
108
end
88
109
end
89
110
@@ -114,8 +135,19 @@ def download_tarball(url, download_dir, file_name)
114
135
tarball = File . join ( download_dir , file_name )
115
136
puts "Downloading chdb library for #{ determine_target_platform } ..."
116
137
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
119
151
end
120
152
end
121
153
@@ -125,24 +157,27 @@ def extract_tarball(download_dir, file_name)
125
157
end
126
158
127
159
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 )
132
163
dest_subdir = case pattern
133
164
when '*.h' then 'include'
134
165
else 'lib'
135
166
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
146
181
end
147
182
end
148
183
0 commit comments