Skip to content

Commit 4f31e91

Browse files
committed
Fix compilation and linking issues
- Resolved linking errors encountered during the build process. - Adjusted relevant code to ensure successful compilation.
1 parent dcf9c86 commit 4f31e91

File tree

16 files changed

+111
-188
lines changed

16 files changed

+111
-188
lines changed

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ AllCops:
22
Exclude:
33
- 'vendor/**/*'
44
- 'spec/**/*'
5+
- 'tmp/**/*'
6+
- 'ext/**/extconf.rb'
57

68
NewCops: enable
79

chdb.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,7 @@ Gem::Specification.new do |s|
5454

5555
s.rdoc_options = ['--main', 'README.md']
5656

57+
s.add_dependency 'csv', '~> 3.1'
58+
5759
s.extensions << 'ext/chdb/extconf.rb'
5860
end

ext/chdb/chdb.c

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
#include "chdb.h"
44
#include "constants.h"
55
#include "connection.h"
6+
#include "exception.h"
67
#include "local_result.h"
78

8-
VALUE cChDBError;
9-
VALUE cLocalResult;
10-
119
void init_chdb_constants()
1210
{
1311
VALUE mChDB = rb_define_module("ChDB");
@@ -19,47 +17,12 @@ void init_chdb_constants()
1917
rb_define_const(mmChDBOpen, "CREATE", INT2FIX(CHDB_OPEN_CREATE));
2018
}
2119

22-
void init_exception()
23-
{
24-
VALUE mChDB = rb_define_module("ChDB");
25-
if (rb_const_defined(mChDB, rb_intern("Exception")))
26-
{
27-
cChDBError = rb_const_get(mChDB, rb_intern("Exception"));
28-
}
29-
else
30-
{
31-
cChDBError = rb_define_class_under(mChDB, "Exception", rb_eStandardError);
32-
}
33-
}
34-
35-
void init_local_result()
36-
{
37-
VALUE mChDB = rb_define_module("ChDB");
38-
cLocalResult = rb_define_class_under(mChDB, "LocalResult", rb_cObject);
39-
rb_define_alloc_func(cLocalResult, local_result_alloc);
40-
rb_define_method(cLocalResult, "buf", local_result_buf, 0);
41-
rb_define_method(cLocalResult, "elapsed", local_result_elapsed, 0);
42-
rb_define_method(cLocalResult, "rows_read", local_result_rows_read, 0);
43-
rb_define_method(cLocalResult, "bytes_read", local_result_bytes_read, 0);
44-
}
45-
46-
void init_connection()
47-
{
48-
VALUE mChDB = rb_define_module("ChDB");
49-
VALUE cConnection = rb_define_class_under(mChDB, "Connection", rb_cObject);
50-
rb_define_alloc_func(cConnection, connection_alloc);
51-
rb_define_method(cConnection, "initialize", connection_initialize, 2);
52-
rb_define_method(cConnection, "query", connection_query, 2);
53-
rb_define_method(cConnection, "close", connection_close, 0);
54-
}
55-
56-
57-
void Init_chdb(void)
20+
void Init_chdb_native()
5821
{
5922
DEBUG_PRINT("Initializing chdb extension");
6023

61-
init_chdb_constants();
6224
init_exception();
25+
init_chdb_constants();
6326
init_local_result();
6427
init_connection();
6528

ext/chdb/chdb.h

Lines changed: 0 additions & 123 deletions
This file was deleted.

ext/chdb/connection.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
#include "connection.h"
22

3+
#include "constants.h"
4+
#include "exception.h"
5+
#include "include/chdb.h"
6+
#include "local_result.h"
7+
8+
void init_connection()
9+
{
10+
VALUE mChDB = rb_define_module("ChDB");
11+
VALUE cConnection = rb_define_class_under(mChDB, "Connection", rb_cObject);
12+
rb_define_alloc_func(cConnection, connection_alloc);
13+
rb_define_method(cConnection, "initialize", connection_initialize, 2);
14+
rb_define_method(cConnection, "query", connection_query, 2);
15+
rb_define_method(cConnection, "close", connection_close, 0);
16+
}
17+
318
VALUE connection_alloc(VALUE klass)
419
{
520
Connection *conn = ALLOC(Connection);

ext/chdb/connection.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ typedef struct
1010

1111
extern const rb_data_type_t ConnectionType;
1212

13+
void init_connection();
14+
1315
VALUE connection_alloc(VALUE klass);
1416

1517
VALUE connection_initialize(VALUE self, VALUE argc, VALUE argv);
@@ -18,4 +20,4 @@ VALUE connection_query(VALUE self, VALUE query, VALUE format);
1820

1921
VALUE connection_close(VALUE self);
2022

21-
#endif
23+
#endif

ext/chdb/exception.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "exception.h"
2+
3+
VALUE cChDBError;
4+
5+
void init_exception()
6+
{
7+
VALUE mChDB = rb_define_module("ChDB");
8+
if (rb_const_defined(mChDB, rb_intern("Exception")))
9+
{
10+
cChDBError = rb_const_get(mChDB, rb_intern("Exception"));
11+
}
12+
else
13+
{
14+
cChDBError = rb_define_class_under(mChDB, "Exception", rb_eStandardError);
15+
}
16+
}

ext/chdb/exception.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef CHDB_EXCEPTION_H
2+
#define CHDB_EXCEPTION_H
3+
4+
#include <ruby.h>
5+
6+
extern VALUE cChDBError;
7+
8+
void init_exception();
9+
10+
#endif

ext/chdb/extconf.rb

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
require 'fileutils'
44
require 'mkmf'
55
require 'yaml'
6+
require 'open-uri'
67

78
module 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

Comments
 (0)