Module: Sqlite3::ExtConf

Defined in:
ext/sqlite3/extconf.rb

Constant Summary collapse

ENV_ALLOWLIST =
["CC", "CFLAGS", "LDFLAGS", "LIBS", "CPPFLAGS", "LT_SYS_LIBRARY_PATH", "CPP"]

Class Method Summary collapse

Class Method Details

.abort_could_not_find(missing) ⇒ Object



174
175
176
# File 'ext/sqlite3/extconf.rb', line 174

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

.abort_pkg_config(id) ⇒ Object



178
179
180
# File 'ext/sqlite3/extconf.rb', line 178

def abort_pkg_config(id)
  abort("\nCould not configure the build properly (#{id}). Please install the `pkg-config` utility.\n\n")
end

.configureObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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_compilerObject



25
26
27
28
# 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_extensionObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/sqlite3/extconf.rb', line 106

def configure_extension
  append_cflags("-fvisibility=hidden") # see https://github.com/rake-compiler/rake-compiler-dock/issues/87

  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")

  # Truffle Ruby doesn't support this yet:
  # https://github.com/oracle/truffleruby/issues/3408
  have_func("rb_enc_interned_str_cstr")

  # 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_func("sqlite3_db_name", "sqlite3.h") # v3.39.0
  have_func("sqlite3_error_offset", "sqlite3.h") # v3.38.0

  have_type("sqlite3_int64", "sqlite3.h")
  have_type("sqlite3_uint64", "sqlite3.h")
end

.configure_packaged_librariesObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'ext/sqlite3/extconf.rb', line 50

def configure_packaged_libraries
  minimal_recipe.tap do |recipe|
    recipe.configure_options += [
      "--disable-shared",
      "--enable-static",
      "--disable-tcl",
      "--enable-fts5"
    ]
    ENV.to_h.tap do |env|
      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
        "-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1",
        "-DSQLITE_USE_URI=1",
        "-DSQLITE_ENABLE_DBPAGE_VTAB=1",
        "-DSQLITE_ENABLE_DBSTAT_VTAB=1"
      ]
      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

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

    # on macos, pkg-config will not return --cflags without this
    ENV["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] = "t"

    # only needed for Ruby 3.1.3, see https://bugs.ruby-lang.org/issues/19233
    RbConfig::CONFIG["PKG_CONFIG"] = config_string("PKG_CONFIG") || "pkg-config"

    lib_path = File.join(recipe.path, "lib")
    pcfile = File.join(lib_path, "pkgconfig", "sqlite3.pc")
    abort_pkg_config("pkg_config") unless pkg_config(pcfile)

    # see https://bugs.ruby-lang.org/issues/18490
    ldflags = xpopen(["pkg-config", "--libs", "--static", pcfile], err: [:child, :out], &:read)
    abort_pkg_config("xpopen") unless $?.success?
    ldflags = ldflags.split

    # see https://github.com/flavorjones/mini_portile/issues/118
    "-L#{lib_path}".tap do |lib_path_flag|
      ldflags.prepend(lib_path_flag) unless ldflags.include?(lib_path_flag)
    end

    ldflags.each { |ldflag| append_ldflags(ldflag) }

    append_cppflags("-DUSING_PACKAGED_LIBRARIES")
    append_cppflags("-DUSING_PRECOMPILED_LIBRARIES") if cross_build?
  end
end

.configure_system_librariesObject



45
46
47
48
# File 'ext/sqlite3/extconf.rb', line 45

def configure_system_libraries
  pkg_config(libname)
  append_cppflags("-DUSING_SQLCIPHER") if sqlcipher?
end

.cross_build?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'ext/sqlite3/extconf.rb', line 182

def cross_build?
  enable_config("cross-build")
end

.darwin?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'ext/sqlite3/extconf.rb', line 194

def darwin?
  RbConfig::CONFIG["target_os"].include?("darwin")
end

.downloadObject



190
191
192
# File 'ext/sqlite3/extconf.rb', line 190

def download
  minimal_recipe.download
end

.libnameObject



34
35
36
# File 'ext/sqlite3/extconf.rb', line 34

def libname
  sqlcipher? ? "sqlcipher" : "sqlite3"
end

.mini_portile_configObject



170
171
172
# File 'ext/sqlite3/extconf.rb', line 170

def mini_portile_config
  YAML.load_file(File.join(package_root_dir, "dependencies.yml"), symbolize_names: true)
end

.minimal_recipeObject



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/sqlite3/extconf.rb', line 148

def minimal_recipe
  require "mini_portile2"

  MiniPortile.new(libname, sqlite3_config[:version]).tap do |recipe|
    if sqlite_source_dir
      recipe.source_directory = sqlite_source_dir
    else
      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
end

.package_root_dirObject



162
163
164
# File 'ext/sqlite3/extconf.rb', line 162

def package_root_dir
  File.expand_path(File.join(File.dirname(__FILE__), "..", ".."))
end


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'ext/sqlite3/extconf.rb', line 198

def print_help
  print(<<~TEXT)
    USAGE: ruby #{$PROGRAM_NAME} [options]

      Flags that are always valid:

          --disable-system-libraries
              Use the packaged libraries, and ignore the system libraries.
              (This is the default behavior.)

          --enable-system-libraries
              Use system libraries instead of building and using the packaged libraries.

          --with-sqlcipher
              Use libsqlcipher instead of libsqlite3.
              (Implies `--enable-system-libraries`.)

          --with-sqlite-source-dir=DIRECTORY
              (dev only) Build sqlite from the source code in DIRECTORY

          --help
              Display this message.


      Flags only used when using system libraries:

          General (applying to all system libraries):

              --with-opt-dir=DIRECTORY
                  Look for headers and libraries in DIRECTORY.

              --with-opt-lib=DIRECTORY
                  Look for libraries in DIRECTORY.

              --with-opt-include=DIRECTORY
                  Look for headers in DIRECTORY.

          Related to sqlcipher:

              --with-sqlcipher-dir=DIRECTORY
                  Look for sqlcipher headers and library in DIRECTORY.
                  (Implies `--with-sqlcipher` and `--enable-system-libraries`.)

              --with-sqlcipher-lib=DIRECTORY
                  Look for sqlcipher library in DIRECTORY.
                  (Implies `--with-sqlcipher` and `--enable-system-libraries`.)

              --with-sqlcipher-include=DIRECTORY
                  Look for sqlcipher headers in DIRECTORY.
                  (Implies `--with-sqlcipher` and `--enable-system-libraries`.)


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

          CC
          CPPFLAGS
          CFLAGS
          LDFLAGS
          LIBS
          LT_SYS_LIBRARY_PATH
          CPP

  TEXT
end

.sqlcipher?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# 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_configObject



166
167
168
# File 'ext/sqlite3/extconf.rb', line 166

def sqlite3_config
  mini_portile_config[:sqlite3]
end

.sqlite_source_dirObject



186
187
188
# File 'ext/sqlite3/extconf.rb', line 186

def sqlite_source_dir
  arg_config("--with-sqlite-source-dir")
end

.system_libraries?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'ext/sqlite3/extconf.rb', line 30

def system_libraries?
  sqlcipher? || enable_config("system-libraries")
end