Skip to content

Commit

Permalink
feat: allow setting sqlite compiler flags in extconf
Browse files Browse the repository at this point in the history
Closes #401
  • Loading branch information
flavorjones committed Sep 8, 2023
1 parent 636a0e3 commit 1614fa8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
39 changes: 39 additions & 0 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
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(" ")

This comment has been minimized.

Copy link
@paddor

paddor Nov 27, 2024

I'd like to compile with -Os, but this line makes that impossible, as it appends -O2 after whatever is in $CFLAGS. Maybe more_cflags should be at first position in this array?

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 1614fa8

Please sign in to comment.