From 636a0e3edee3ef352f73c09e90f79b8dc756bdbd Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 8 Sep 2023 11:47:43 -0400 Subject: [PATCH 1/2] doc: update INSTALLATION.md with instructions on how to link against a specific shared library. --- INSTALLATION.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/INSTALLATION.md b/INSTALLATION.md index 36253654..cb64f725 100644 --- a/INSTALLATION.md +++ b/INSTALLATION.md @@ -167,14 +167,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 @@ -199,7 +199,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: From 1614fa84fb2a206e568fd0fb5135b0382054f8d3 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 8 Sep 2023 13:43:29 -0400 Subject: [PATCH 2/2] feat: allow setting sqlite compiler flags in extconf Closes #401 --- INSTALLATION.md | 39 +++++++++++++++++++++++++++++++++++++++ ext/sqlite3/extconf.rb | 15 +++++++++++---- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/INSTALLATION.md b/INSTALLATION.md index cb64f725..138153ac 100644 --- a/INSTALLATION.md +++ b/INSTALLATION.md @@ -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 diff --git a/ext/sqlite3/extconf.rb b/ext/sqlite3/extconf.rb index 4ec7d0d3..8d266c7c 100644 --- a/ext/sqlite3/extconf.rb +++ b/ext/sqlite3/extconf.rb @@ -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 @@ -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