Skip to content

Commit

Permalink
Merge pull request #402 from sparklemotion/flavorjones-extconf-sqlite…
Browse files Browse the repository at this point in the history
…-cflags

feat: allow setting sqlite compiler flags via extconf args
  • Loading branch information
flavorjones authored Sep 8, 2023
2 parents d28600e + 1614fa8 commit 457022e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
47 changes: 43 additions & 4 deletions INSTALLATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,45 @@ user 0m23.361s
sys 0m5.839s
```

##### Controlling compilation flags for sqlite

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 ...

**If you're installing the gem using `gem install`** then you can pass in these compile-time flags like this:

``` sh
gem install sqlite3 --platform=ruby -- \
--with-sqlite-cflags="-DSQLITE_DEFAULT_CACHE_SIZE=9999 -DSQLITE_DEFAULT_PAGE_SIZE=4444"
```

or the equivalent:

``` sh
CFLAGS="-DSQLITE_DEFAULT_CACHE_SIZE=9999 -DSQLITE_DEFAULT_PAGE_SIZE=4444" \
gem install sqlite3 --platform=ruby
```

**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:

``` ruby
# Gemfile
gem "sqlite3", force_ruby_platform: true # requires bundler >= 2.3.18
```

and then set up a bundler config parameter for `build.sqlite3`:

``` sh
bundle config set build.sqlite3 \
"--with-sqlite-cflags='-DSQLITE_DEFAULT_CACHE_SIZE=9999 -DSQLITE_DEFAULT_PAGE_SIZE=4444'"
```

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:

``` yaml
---
BUNDLE_BUILD__SQLITE3: "--with-sqlite-cflags='-DSQLITE_DEFAULT_CACHE_SIZE=9999 -DSQLITE_DEFAULT_PAGE_SIZE=4444'"
```
#### System libsqlite3
Expand Down Expand Up @@ -167,14 +206,14 @@ db.load_extension("/path/to/sqlite/spellfix.o")
db.execute("CREATE VIRTUAL TABLE demo USING spellfix1;")
```

### How do I use an alternative sqlite3 implementation?
### How do I use my own sqlite3 shared library?

Some packages, like pSQLite Encryption Extension ("SEE"), are intended to be ABI-compatible drop-in replacements for the sqlite3 shared object.
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.

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

```
/opt/see
/opt/sqlite3
├── bin
│   └── sqlite3
├── include
Expand All @@ -199,7 +238,7 @@ You can build this gem against that library like this:
```
gem install sqlite3 --platform=ruby -- \
--enable-system-libraries \
--with-opt-dir=/opt/see
--with-opt-dir=/opt/sqlite
```

Explanation:
Expand Down
15 changes: 11 additions & 4 deletions ext/sqlite3/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ def configure_packaged_libraries
minimal_recipe.tap do |recipe|
recipe.configure_options += ["--enable-shared=no", "--enable-static=yes"]
ENV.to_h.tap do |env|
additional_cflags = [
user_cflags = with_config("sqlite-cflags")
more_cflags = [
"-fPIC", # needed for linking the static library into a shared library
"-O2", # see https://github.com/sparklemotion/sqlite3-ruby/issues/335 for some benchmarks
"-fvisibility=hidden", # see https://github.com/rake-compiler/rake-compiler-dock/issues/87
]
env["CFLAGS"] = [env["CFLAGS"], additional_cflags].flatten.join(" ")
env["CFLAGS"] = [user_cflags, env["CFLAGS"], more_cflags].flatten.join(" ")
recipe.configure_options += env.select { |k,v| ENV_ALLOWLIST.include?(k) }
.map { |key, value| "#{key}=#{value.strip}" }
end
Expand Down Expand Up @@ -234,17 +235,23 @@ def print_help
Flags only used when building and using the packaged libraries:
--with-sqlite-cflags=CFLAGS
Explicitly pass compiler flags to the sqlite library build. These flags will
appear on the commandline before any flags set in the CFLAGS environment
variable. This is useful for setting compilation options in your project's
bundler config. See INSTALLATION.md for more information.
--enable-cross-build
Enable cross-build mode. (You probably do not want to set this manually.)
Environment variables used for compiling the C extension:
Environment variables used for compiling the gem's C extension:
CC
Use this path to invoke the compiler instead of `RbConfig::CONFIG['CC']`
Environment variables passed through to the compilation of packaged libraries:
Environment variables passed through to the compilation of sqlite:
CC
CPPFLAGS
Expand Down

0 comments on commit 457022e

Please sign in to comment.