Skip to content

Commit e0359df

Browse files
committed
ext: support system libraries via --enable-system-libraries
1 parent 21e9569 commit e0359df

2 files changed

Lines changed: 70 additions & 87 deletions

File tree

.github/workflows/sqlite3-ruby.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,28 @@ on:
1616
- '*'
1717

1818
jobs:
19-
test:
19+
sqlite3:
2020
strategy:
2121
fail-fast: false
2222
matrix:
2323
os: ["ubuntu-latest", "macos-latest", "windows-2022"]
2424
ruby: ["3.1", "3.0", "2.7", "2.6", "2.5"]
25+
lib: ["system", "packaged"]
2526
include:
2627
- os: "ubuntu-latest"
2728
ruby: "truffleruby-head"
2829
- os: "ubuntu-latest"
2930
ruby: "head"
3031
- os: "windows-2022"
3132
ruby: "mingw"
33+
sys: "enable"
34+
- os: "windows-2022"
35+
ruby: "mingw"
36+
sys: "disable"
37+
- lib: "system"
38+
sys: "enable"
39+
- lib: "packaged"
40+
sys: "disable"
3241
runs-on: ${{matrix.os}}
3342
steps:
3443
- if: matrix.os == 'windows-2022'
@@ -43,19 +52,16 @@ jobs:
4352
bundler-cache: true
4453
apt-get: "libsqlite3-dev"
4554
brew: "sqlite3"
46-
mingw: sqlite3
47-
48-
- name: compile msys2
49-
run: bundle exec rake compile:msys2
50-
- name: test
51-
run: bundle exec rake test
55+
mingw: "sqlite3"
56+
- run: bundle exec rake compile -- --${{matrix.sys}}-system-libraries
57+
- run: bundle exec rake test
5258

53-
ubuntu:
59+
old_sqlite3:
5460
runs-on: ubuntu-latest
5561
container:
5662
image: ruby:2.7.5-buster # old enough to not support SQLITE_DBCONFIG_DQS_DDL
5763
steps:
5864
- uses: actions/checkout@v3
5965
- run: bundle install
60-
- run: bundle exec rake compile
66+
- run: bundle exec rake compile -- --enable-system-libraries
6167
- run: bundle exec rake test

ext/sqlite3/extconf.rb

Lines changed: 55 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,90 +7,66 @@
77
ENV["CC"] = RbConfig::CONFIG["CC"]
88

99
cross_build_p = enable_config("cross-build")
10-
11-
MiniPortile.new("sqlite3", "3.38.5").tap do |recipe|
12-
# checksum verified by first checking the published sha3(256) checksum:
13-
#
14-
# $ sha3sum -a 256 sqlite-autoconf-3380500.tar.gz
15-
# ab649fea76f49a6ec7f907f001d87b8bd76dec0679c783e3992284c5a882a98c sqlite-autoconf-3380500.tar.gz
16-
# $ sha256sum sqlite-autoconf-3380500.tar.gz
17-
# 5af07de982ba658fd91a03170c945f99c971f6955bc79df3266544373e39869c sqlite-autoconf-3380500.tar.gz
18-
#
19-
recipe.files = [{
20-
url: "https://www.sqlite.org/2022/sqlite-autoconf-3380500.tar.gz",
21-
sha256: "5af07de982ba658fd91a03170c945f99c971f6955bc79df3266544373e39869c",
22-
}]
23-
recipe.target = File.join(package_root_dir, "ports")
24-
recipe.patch_files = Dir[File.join(package_root_dir, "patches", "*.patch")].sort
25-
26-
recipe.configure_options += ["--enable-shared=no", "--enable-static=yes"]
27-
ENV.to_h.tap do |env|
28-
env["CFLAGS"] = [env["CFLAGS"], "-fPIC"].join(" ") # needed for linking the static library into a shared library
29-
recipe.configure_options += env
30-
.select { |k,v| ["CC", "CFLAGS", "LDFLAGS", "LIBS", "CPPFLAGS", "LT_SYS_LIBRARY_PATH", "CPP"].include?(k) }
31-
.map { |key, value| "#{key}=#{value.strip}" }
32-
end
33-
34-
unless File.exist?(File.join(recipe.target, recipe.host, recipe.name, recipe.version))
35-
recipe.cook
36-
end
37-
recipe.activate
38-
39-
ENV["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] = "t" # on macos, pkg-config will not return --cflags without this
40-
pcfile = File.join(recipe.path, "lib", "pkgconfig", "sqlite3.pc")
41-
if pkg_config(pcfile)
42-
# see https://bugs.ruby-lang.org/issues/18490
43-
libs = xpopen(["pkg-config", "--libs", "--static", pcfile], err: [:child, :out], &:read)
44-
libs.split.each { |lib| append_ldflags(lib) } if $?.success?
45-
else
46-
message("Please install either the `pkg-config` utility or the `pkg-config` rubygem.\n")
10+
system_libraries_p = enable_config("system-libraries")
11+
sqlcipher_p = with_config("sqlcipher")
12+
13+
if system_libraries_p
14+
# if sqlcipher_p # TODO test and document this
15+
# message "Building sqlite3-ruby using system sqlcipher.\n"
16+
# pkg_config("sqlcipher") # TODO test and document this
17+
# else
18+
message "Building sqlite3-ruby using system sqlite3.\n"
19+
pkg_config("sqlite3") # TODO document
20+
# end
21+
else
22+
message "Building sqlite3-ruby using packaged sqlite3.\n"
23+
MiniPortile.new("sqlite3", "3.38.5").tap do |recipe|
24+
# checksum verified by first checking the published sha3(256) checksum:
25+
#
26+
# $ sha3sum -a 256 sqlite-autoconf-3380500.tar.gz
27+
# ab649fea76f49a6ec7f907f001d87b8bd76dec0679c783e3992284c5a882a98c sqlite-autoconf-3380500.tar.gz
28+
# $ sha256sum sqlite-autoconf-3380500.tar.gz
29+
# 5af07de982ba658fd91a03170c945f99c971f6955bc79df3266544373e39869c sqlite-autoconf-3380500.tar.gz
30+
#
31+
recipe.files = [{
32+
url: "https://www.sqlite.org/2022/sqlite-autoconf-3380500.tar.gz",
33+
sha256: "5af07de982ba658fd91a03170c945f99c971f6955bc79df3266544373e39869c",
34+
}]
35+
recipe.target = File.join(package_root_dir, "ports")
36+
recipe.patch_files = Dir[File.join(package_root_dir, "patches", "*.patch")].sort
37+
38+
recipe.configure_options += ["--enable-shared=no", "--enable-static=yes"]
39+
ENV.to_h.tap do |env|
40+
env["CFLAGS"] = [env["CFLAGS"], "-fPIC"].join(" ") # needed for linking the static library into a shared library
41+
recipe.configure_options += env
42+
.select { |k,v| ["CC", "CFLAGS", "LDFLAGS", "LIBS", "CPPFLAGS", "LT_SYS_LIBRARY_PATH", "CPP"].include?(k) }
43+
.map { |key, value| "#{key}=#{value.strip}" }
44+
end
45+
46+
unless File.exist?(File.join(recipe.target, recipe.host, recipe.name, recipe.version))
47+
recipe.cook
48+
end
49+
recipe.activate
50+
51+
ENV["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] = "t" # on macos, pkg-config will not return --cflags without this
52+
pcfile = File.join(recipe.path, "lib", "pkgconfig", "sqlite3.pc")
53+
if pkg_config(pcfile)
54+
# see https://bugs.ruby-lang.org/issues/18490
55+
libs = xpopen(["pkg-config", "--libs", "--static", pcfile], err: [:child, :out], &:read)
56+
libs.split.each { |lib| append_ldflags(lib) } if $?.success?
57+
else
58+
message("Please install either the `pkg-config` utility or the `pkg-config` rubygem.\n")
59+
end
4760
end
4861
end
4962

50-
# ldflags = cppflags = nil
51-
# if RbConfig::CONFIG["host_os"] =~ /darwin/
52-
# begin
53-
# if with_config('sqlcipher')
54-
# brew_prefix = `brew --prefix sqlcipher`.chomp
55-
# ldflags = "#{brew_prefix}/lib"
56-
# cppflags = "#{brew_prefix}/include/sqlcipher"
57-
# pkg_conf = "#{brew_prefix}/lib/pkgconfig"
58-
# else
59-
# brew_prefix = `brew --prefix sqlite3`.chomp
60-
# ldflags = "#{brew_prefix}/lib"
61-
# cppflags = "#{brew_prefix}/include"
62-
# pkg_conf = "#{brew_prefix}/lib/pkgconfig"
63-
# end
64-
65-
# # pkg_config should be less error prone than parsing compiler
66-
# # commandline options, but we need to set default ldflags and cpp flags
67-
# # in case the user doesn't have pkg-config installed
68-
# ENV['PKG_CONFIG_PATH'] ||= pkg_conf
69-
# rescue
70-
# end
71-
# end
72-
73-
# if with_config('sqlcipher')
74-
# pkg_config("sqlcipher")
75-
# else
76-
# pkg_config("sqlite3")
77-
# end
78-
79-
# # --with-sqlite3-{dir,include,lib}
80-
# if with_config('sqlcipher')
81-
# $CFLAGS << ' -DUSING_SQLCIPHER'
82-
# dir_config("sqlcipher", cppflags, ldflags)
83-
# else
84-
# dir_config("sqlite3", cppflags, ldflags)
85-
# end
86-
8763
# if RbConfig::CONFIG["host_os"] =~ /mswin/
8864
# $CFLAGS << ' -W3'
8965
# end
9066

9167
append_cflags("-DTAINTING_SUPPORT") if Gem::Requirement.new("< 2.7").satisfied_by?(Gem::Version.new(RUBY_VERSION))
9268

93-
def asplode missing
69+
def abort_could_not_find_library(missing)
9470
if RUBY_PLATFORM =~ /mingw|mswin/
9571
abort "#{missing} is missing. Install SQLite3 from " +
9672
"http://www.sqlite.org/ first."
@@ -104,13 +80,14 @@ def asplode missing
10480
end
10581
end
10682

107-
asplode('sqlite3.h') unless find_header 'sqlite3.h'
83+
abort_could_not_find_library('sqlite3.h') unless find_header 'sqlite3.h'
10884

10985
# TODO sqlcipher support
11086
# if with_config('sqlcipher')
111-
# asplode('sqlcipher') unless find_library 'sqlcipher', 'sqlite3_libversion_number'
87+
# append_cflags("-DUSING_SQLCIPHER")
88+
# abort_could_not_find_library('sqlcipher') unless find_library 'sqlcipher', 'sqlite3_libversion_number'
11289
# else
113-
asplode('sqlite3') unless find_library("sqlite3", "sqlite3_libversion_number", "sqlite3.h")
90+
abort_could_not_find_library('sqlite3') unless find_library("sqlite3", "sqlite3_libversion_number", "sqlite3.h")
11491
# end
11592

11693
# Functions defined in 1.9 but not 1.8

0 commit comments

Comments
 (0)