33require 'fileutils'
44require 'mkmf'
55require 'yaml'
6+ require 'open-uri'
67
78module ChDB
89 module ExtConf
910 class << self
1011 def configure
11- configure_cross_compiler
12-
1312 download_and_extract
1413
1514 configure_extension
@@ -33,7 +32,12 @@ def configure_extension
3332 lib_path = File . expand_path ( 'ext/chdb/lib' , package_root_dir )
3433 append_ldflags ( "-L#{ lib_path } " )
3534
36- append_ldflags ( "-Wl,-rpath,'$$ORIGIN/../lib'" )
35+ target_platform = determine_target_platform
36+ if target_platform . include? ( 'darwin' )
37+ append_ldflags ( '-Wl,-rpath,@loader_path/../lib' )
38+ else
39+ append_ldflags ( "-Wl,-rpath,'$$ORIGIN/../lib'" )
40+ end
3741
3842 abort_could_not_find ( 'chdb.h' ) unless find_header ( 'chdb.h' , include_path )
3943
@@ -53,9 +57,10 @@ def abort_could_not_find(missing)
5357 def download_and_extract
5458 target_platform = determine_target_platform
5559 version = fetch_chdb_version
56- download_dir = setup_download_directory ( target_platform , version )
60+ download_dir = determine_download_directory ( target_platform , version )
5761
5862 unless Dir . exist? ( download_dir )
63+ FileUtils . mkdir_p ( download_dir )
5964 file_name = get_file_name ( target_platform )
6065 url = build_download_url ( version , file_name )
6166 download_tarball ( url , download_dir , file_name )
@@ -68,18 +73,25 @@ def download_and_extract
6873 private
6974
7075 def determine_target_platform
71- ENV [ 'TARGET' ] || host_platform
76+ return ENV [ 'TARGET' ] . strip if ENV [ 'TARGET' ] && !ENV [ 'TARGET' ] . strip . empty?
77+
78+ case RUBY_PLATFORM
79+ when /aarch64-linux/ then 'aarch64-linux-gnu'
80+ when /x86_64-linux/ then 'x86_64-linux-gnu'
81+ when /arm64-darwin/ then 'arm64-darwin'
82+ when /x86_64-darwin/ then 'x86_64-darwin'
83+ else
84+ 'unknown-platform'
85+ end
7286 end
7387
7488 def fetch_chdb_version
7589 dependencies = YAML . load_file ( File . join ( package_root_dir , 'dependencies.yml' ) , symbolize_names : true )
7690 dependencies [ :chdb ] [ :version ]
7791 end
7892
79- def setup_download_directory ( target_platform , version )
80- download_dir = File . join ( package_root_dir , 'deps' , version , target_platform )
81- FileUtils . mkdir_p ( download_dir )
82- download_dir
93+ def determine_download_directory ( target_platform , version )
94+ File . join ( package_root_dir , 'deps' , version , target_platform )
8395 end
8496
8597 def get_file_name ( target_platform )
@@ -99,6 +111,7 @@ def build_download_url(version, file_name)
99111 def download_tarball ( url , download_dir , file_name )
100112 tarball = File . join ( download_dir , file_name )
101113 puts "Downloading chdb library for #{ determine_target_platform } ..."
114+
102115 URI . open ( url ) do |remote | # rubocop:disable Security/Open
103116 IO . copy_stream ( remote , tarball )
104117 end
@@ -111,16 +124,24 @@ def extract_tarball(download_dir, file_name)
111124
112125 def copy_files ( download_dir , _version )
113126 ext_chdb_path = File . join ( package_root_dir , 'ext/chdb' )
114- [ %w[ include *.h ] , %w[ lib *.so ] , %w[ lib *.dylib ] ] . each do |( src_dir , pattern ) |
127+ [ %w[ *.h ] , %w[ *.so ] , %w[ *.dylib ] ] . each do |( glob_pattern ) |
128+ src_dir , pattern = File . split ( glob_pattern )
129+
130+ dest_subdir = case pattern
131+ when '*.h' then 'include'
132+ else 'lib'
133+ end
115134 src = File . join ( download_dir , src_dir , pattern )
116- dest = File . join ( ext_chdb_path , src_dir )
135+ dest = File . join ( ext_chdb_path , dest_subdir )
117136 FileUtils . mkdir_p ( dest )
118137 FileUtils . cp_r ( Dir . glob ( src ) , dest , remove_destination : true )
119- end
120- end
121138
122- def host_platform
123- RbConfig ::CONFIG [ 'host_os' ] . downcase
139+ target_platform = determine_target_platform
140+ if target_platform . include? ( 'darwin' ) && ( pattern == '*.so' )
141+ system ( "install_name_tool -id '@rpath/libchdb.so' #{ File . join ( dest , 'libchdb.so' ) } " )
142+ system ( "codesign -f -s - #{ File . join ( dest , 'libchdb.so' ) } " )
143+ end
144+ end
124145 end
125146
126147 def package_root_dir
0 commit comments