module Sqlite3::ExtConf

Constants

ENV_ALLOWLIST

Public Class Methods

abort_could_not_find(missing) click to toggle source
# File ext/sqlite3/extconf.rb, line 153
def abort_could_not_find(missing)
  abort("\nCould not find #{missing}.\nPlease visit https://github.com/sparklemotion/sqlite3-ruby for installation instructions.\n\n")
end
configure() click to toggle source
# File ext/sqlite3/extconf.rb, line 9
def configure
  configure_cross_compiler

  if system_libraries?
    message "Building sqlite3-ruby using system #{libname}.\n"
    configure_system_libraries
  else
    message "Building sqlite3-ruby using packaged sqlite3.\n"
    configure_packaged_libraries
  end

  configure_extension

  create_makefile('sqlite3/sqlite3_native')
end
configure_cross_compiler() click to toggle source
# File ext/sqlite3/extconf.rb, line 25
def configure_cross_compiler
  RbConfig::CONFIG["CC"] = RbConfig::MAKEFILE_CONFIG["CC"] = ENV["CC"] if ENV["CC"]
  ENV["CC"] = RbConfig::CONFIG["CC"]
end
configure_extension() click to toggle source
# File ext/sqlite3/extconf.rb, line 80
def configure_extension
  if Gem::Requirement.new("< 2.7").satisfied_by?(Gem::Version.new(RUBY_VERSION))
    append_cppflags("-DTAINTING_SUPPORT")
  end

  if find_header("sqlite3.h")
    # noop
  elsif sqlcipher? && find_header("sqlcipher/sqlite3.h")
    append_cppflags("-DUSING_SQLCIPHER_INC_SUBDIR")
  else
    abort_could_not_find("sqlite3.h")
  end

  abort_could_not_find(libname) unless find_library(libname, "sqlite3_libversion_number", "sqlite3.h")

  # Functions defined in 1.9 but not 1.8
  have_func('rb_proc_arity')

  # Functions defined in 2.1 but not 2.0
  have_func('rb_integer_pack')

  # These functions may not be defined
  have_func('sqlite3_initialize')
  have_func('sqlite3_backup_init')
  have_func('sqlite3_column_database_name')
  have_func('sqlite3_enable_load_extension')
  have_func('sqlite3_load_extension')

  unless have_func('sqlite3_open_v2') # https://www.sqlite.org/releaselog/3_5_0.html
    abort("\nPlease use a version of SQLite3 >= 3.5.0\n\n")
  end

  have_func('sqlite3_prepare_v2')
  have_type('sqlite3_int64', 'sqlite3.h')
  have_type('sqlite3_uint64', 'sqlite3.h')
end
configure_packaged_libraries() click to toggle source
# File ext/sqlite3/extconf.rb, line 50
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 = [
        "-fPIC", # needed for linking the static library into a shared library
        "-O2", # see https://github.com/sparklemotion/sqlite3-ruby/issues/335 for some benchmarks
      ]
      env["CFLAGS"] = [env["CFLAGS"], additional_cflags].flatten.join(" ")
      recipe.configure_options += env.select { |k,v| ENV_ALLOWLIST.include?(k) }
                                     .map { |key, value| "#{key}=#{value.strip}" }
    end

    unless File.exist?(File.join(recipe.target, recipe.host, recipe.name, recipe.version))
      recipe.cook
    end
    recipe.activate

    ENV["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] = "t" # on macos, pkg-config will not return --cflags without this
    pcfile = File.join(recipe.path, "lib", "pkgconfig", "sqlite3.pc")
    if pkg_config(pcfile)
      # see https://bugs.ruby-lang.org/issues/18490
      libs = xpopen(["pkg-config", "--libs", "--static", pcfile], err: [:child, :out], &:read)
      libs.split.each { |lib| append_ldflags(lib) } if $?.success?
    else
      abort("\nCould not configure the build properly. Please install either the `pkg-config` utility or the `pkg-config` rubygem.\n\n")
    end
  end
end
configure_system_libraries() click to toggle source
# File ext/sqlite3/extconf.rb, line 45
def configure_system_libraries
  pkg_config(libname)
  append_cppflags("-DUSING_SQLCIPHER") if sqlcipher?
end
cross_build?() click to toggle source
# File ext/sqlite3/extconf.rb, line 157
def cross_build?
  enable_config("cross-build")
end
download() click to toggle source
# File ext/sqlite3/extconf.rb, line 161
def download
  minimal_recipe.download
end
libname() click to toggle source
# File ext/sqlite3/extconf.rb, line 34
def libname
  sqlcipher? ? "sqlcipher" : "sqlite3"
end
mini_portile_config() click to toggle source
# File ext/sqlite3/extconf.rb, line 133
def mini_portile_config
  {
    sqlite3: {
      # checksum verified by first checking the published sha3(256) checksum against https://sqlite.org/download.html:
      #
      # $ sha3sum -a 256 ports/archives/sqlite-autoconf-3390300.tar.gz
      # dfa055c70724cd63f0b7da6e9f53530d8da51fe021e3f864d58c7c847d590e1d  ports/archives/sqlite-autoconf-3390300.tar.gz
      #
      # $ sha256sum ports/archives/sqlite-autoconf-3390300.tar.gz
      # 7868fb3082be3f2cf4491c6fba6de2bddcbc293a35fefb0624ee3c13f01422b9  ports/archives/sqlite-autoconf-3390300.tar.gz
      #
      version: "3.39.3",
      files: [{
                url: "https://www.sqlite.org/2022/sqlite-autoconf-3390300.tar.gz",
                sha256: "7868fb3082be3f2cf4491c6fba6de2bddcbc293a35fefb0624ee3c13f01422b9",
              }],
    }
  }
end
minimal_recipe() click to toggle source
# File ext/sqlite3/extconf.rb, line 117
def minimal_recipe
  MiniPortile.new(libname, sqlite3_config[:version]).tap do |recipe|
    recipe.files = sqlite3_config[:files]
    recipe.target = File.join(package_root_dir, "ports")
    recipe.patch_files = Dir[File.join(package_root_dir, "patches", "*.patch")].sort
  end
end
package_root_dir() click to toggle source
# File ext/sqlite3/extconf.rb, line 125
def package_root_dir
  File.expand_path(File.join(File.dirname(__FILE__), "..", ".."))
end
print_help() click to toggle source
sqlcipher?() click to toggle source
# File ext/sqlite3/extconf.rb, line 38
def sqlcipher?
  with_config("sqlcipher") ||
    with_config("sqlcipher-dir") ||
    with_config("sqlcipher-include") ||
    with_config("sqlcipher-lib")
end
sqlite3_config() click to toggle source
# File ext/sqlite3/extconf.rb, line 129
def sqlite3_config
  mini_portile_config[:sqlite3]
end
system_libraries?() click to toggle source
# File ext/sqlite3/extconf.rb, line 30
def system_libraries?
  sqlcipher? || enable_config("system-libraries")
end