Skip to content

Commit 457022e

Browse files
authored
Merge pull request #402 from sparklemotion/flavorjones-extconf-sqlite-cflags
feat: allow setting sqlite compiler flags via extconf args
2 parents d28600e + 1614fa8 commit 457022e

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

INSTALLATION.md

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,45 @@ user 0m23.361s
7373
sys 0m5.839s
7474
```
7575

76+
##### Controlling compilation flags for sqlite
77+
78+
Upstream sqlite allows for the setting of some parameters at compile time. If you're an expert and would like to set these, you may do so at gem install time in two different ways ...
79+
80+
**If you're installing the gem using `gem install`** then you can pass in these compile-time flags like this:
81+
82+
``` sh
83+
gem install sqlite3 --platform=ruby -- \
84+
--with-sqlite-cflags="-DSQLITE_DEFAULT_CACHE_SIZE=9999 -DSQLITE_DEFAULT_PAGE_SIZE=4444"
85+
```
86+
87+
or the equivalent:
88+
89+
``` sh
90+
CFLAGS="-DSQLITE_DEFAULT_CACHE_SIZE=9999 -DSQLITE_DEFAULT_PAGE_SIZE=4444" \
91+
gem install sqlite3 --platform=ruby
92+
```
93+
94+
**If you're installing the gem using `bundler`** then you should first pin the gem to the "ruby" platform gem, so that you are compiling from source:
95+
96+
``` ruby
97+
# Gemfile
98+
gem "sqlite3", force_ruby_platform: true # requires bundler >= 2.3.18
99+
```
100+
101+
and then set up a bundler config parameter for `build.sqlite3`:
102+
103+
``` sh
104+
bundle config set build.sqlite3 \
105+
"--with-sqlite-cflags='-DSQLITE_DEFAULT_CACHE_SIZE=9999 -DSQLITE_DEFAULT_PAGE_SIZE=4444'"
106+
```
107+
108+
NOTE the use of single quotes within the double-quoted string to ensure the space between compiler flags is interpreted correctly. The contents of your `.bundle/config` file should look like:
109+
110+
``` yaml
111+
---
112+
BUNDLE_BUILD__SQLITE3: "--with-sqlite-cflags='-DSQLITE_DEFAULT_CACHE_SIZE=9999 -DSQLITE_DEFAULT_PAGE_SIZE=4444'"
113+
```
114+
76115
77116
#### System libsqlite3
78117
@@ -167,14 +206,14 @@ db.load_extension("/path/to/sqlite/spellfix.o")
167206
db.execute("CREATE VIRTUAL TABLE demo USING spellfix1;")
168207
```
169208

170-
### How do I use an alternative sqlite3 implementation?
209+
### How do I use my own sqlite3 shared library?
171210

172-
Some packages, like pSQLite Encryption Extension ("SEE"), are intended to be ABI-compatible drop-in replacements for the sqlite3 shared object.
211+
Some folks have strong opinions about what features they want compiled into sqlite3; or may be using a package like SQLite Encryption Extension ("SEE"). This section will explain how to get your Ruby application to load that specific shared library.
173212

174213
If you've installed your alternative as an autotools-style installation, the directory structure will look like this:
175214

176215
```
177-
/opt/see
216+
/opt/sqlite3
178217
├── bin
179218
│   └── sqlite3
180219
├── include
@@ -199,7 +238,7 @@ You can build this gem against that library like this:
199238
```
200239
gem install sqlite3 --platform=ruby -- \
201240
--enable-system-libraries \
202-
--with-opt-dir=/opt/see
241+
--with-opt-dir=/opt/sqlite
203242
```
204243

205244
Explanation:

ext/sqlite3/extconf.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ def configure_packaged_libraries
5151
minimal_recipe.tap do |recipe|
5252
recipe.configure_options += ["--enable-shared=no", "--enable-static=yes"]
5353
ENV.to_h.tap do |env|
54-
additional_cflags = [
54+
user_cflags = with_config("sqlite-cflags")
55+
more_cflags = [
5556
"-fPIC", # needed for linking the static library into a shared library
5657
"-O2", # see https://github.com/sparklemotion/sqlite3-ruby/issues/335 for some benchmarks
5758
"-fvisibility=hidden", # see https://github.com/rake-compiler/rake-compiler-dock/issues/87
5859
]
59-
env["CFLAGS"] = [env["CFLAGS"], additional_cflags].flatten.join(" ")
60+
env["CFLAGS"] = [user_cflags, env["CFLAGS"], more_cflags].flatten.join(" ")
6061
recipe.configure_options += env.select { |k,v| ENV_ALLOWLIST.include?(k) }
6162
.map { |key, value| "#{key}=#{value.strip}" }
6263
end
@@ -234,17 +235,23 @@ def print_help
234235
235236
Flags only used when building and using the packaged libraries:
236237
238+
--with-sqlite-cflags=CFLAGS
239+
Explicitly pass compiler flags to the sqlite library build. These flags will
240+
appear on the commandline before any flags set in the CFLAGS environment
241+
variable. This is useful for setting compilation options in your project's
242+
bundler config. See INSTALLATION.md for more information.
243+
237244
--enable-cross-build
238245
Enable cross-build mode. (You probably do not want to set this manually.)
239246
240247
241-
Environment variables used for compiling the C extension:
248+
Environment variables used for compiling the gem's C extension:
242249
243250
CC
244251
Use this path to invoke the compiler instead of `RbConfig::CONFIG['CC']`
245252
246253
247-
Environment variables passed through to the compilation of packaged libraries:
254+
Environment variables passed through to the compilation of sqlite:
248255
249256
CC
250257
CPPFLAGS

0 commit comments

Comments
 (0)