aboutsummaryrefslogtreecommitdiff
path: root/autoconf/tea/teaish.tcl
blob: 87d059c32826d1419b3b446fee17a83c8f2b46fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
105
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# Teaish configure script for the SQLite Tcl extension

#
# State for disparate config-time pieces.
#
array set sqlite__Config [proj-strip-hash-comments {
  #
  # The list of feature --flags which the --all flag implies. This
  # requires special handling in a few places.
  #
  all-flag-enables {fts3 fts4 fts5 rtree geopoly}

  # >0 if building in the canonical tree. -1=undetermined
  is-canonical -1
}]

#
# Set up the package info for teaish...
#
apply {{dir} {
  # Figure out the version number...
  set version ""
  if {[file exists $dir/../VERSION]} {
    # The canonical SQLite TEA(ish) build
    set version [proj-file-content -trim $dir/../VERSION]
    set ::sqlite__Config(is-canonical) 1
    set distname sqlite-tcl
  } elseif {[file exists $dir/generic/tclsqlite3.c]} {
    # The copy from the teaish tree, used as a dev/test bed before
    # updating SQLite's tree.
    set ::sqlite__Config(is-canonical) 0
    set fd [open $dir/generic/tclsqlite3.c rb]
    while {[gets $fd line] >=0} {
      if {[regexp {^#define[ ]+SQLITE_VERSION[ ]+"(3.+)"} \
             $line - version]} {
        set distname sqlite-teaish
        break
      }
    }
    close $fd
  }

  if {"" eq $version} {
    proj-fatal "Cannot determine the SQLite version number"
  }

  proj-assert {$::sqlite__Config(is-canonical) > -1}
  proj-assert {[string match 3.*.* $version]} \
    "Unexpected SQLite version: $version"

  set pragmas {}
  if {$::sqlite__Config(is-canonical)} {
    # Disable "make dist" in the canonical tree.  That tree is
    # generated from several pieces and creating/testing working
    # "dist" rules for that sub-build currently feels unnecessary. The
    # copy in the teaish tree, though, should be able to "make dist".
    lappend pragmas no-dist
  } else {
    lappend pragmas full-dist
  }

  teaish-pkginfo-set -vars {
    -name sqlite
    -name.pkg sqlite3
    -version $version
    -name.dist $distname
    -vsatisfies 8.6-
    -libDir sqlite$version
    -pragmas $pragmas
  }
}} [teaish-get -dir]

#
# Must return either an empty string or a list in the form accepted by
# autosetup's [options] function.
#
proc teaish-options {} {
  # These flags and defaults mostly derive from the historical TEA
  # build.  Some, like ICU, are taken from the canonical SQLite tree.
  return [subst -nocommands -nobackslashes {
    with-system-sqlite=0
      => {Use the system-level SQLite instead of the copy in this tree.
          Also requires use of --override-sqlite-version so that the build
          knows what version number to associate with the system-level SQLite.}
    override-sqlite-version:VERSION
      => {For use with --with-system-sqlite to set the version number.}
    threadsafe=1         => {Disable mutexing}
    with-tempstore:=no   => {Use an in-RAM database for temporary tables: never,no,yes,always}
    load-extension=0     => {Enable loading of external extensions}
    math=1               => {Disable math functions}
    json=1               => {Disable JSON functions}
    fts3                 => {Enable the FTS3 extension}
    fts4                 => {Enable the FTS4 extension}
    fts5                 => {Enable the FTS5 extension}
    update-limit         => {Enable the UPDATE/DELETE LIMIT clause}
    geopoly              => {Enable the GEOPOLY extension}
    rtree                => {Enable the RTREE extension}
    session              => {Enable the SESSION extension}
    all=1                => {Disable $::sqlite__Config(all-flag-enables)}
    with-icu-ldflags:LDFLAGS
      => {Enable SQLITE_ENABLE_ICU and add the given linker flags for the
          ICU libraries. e.g. on Ubuntu systems, try '-licui18n -licuuc -licudata'.}
    with-icu-cflags:CFLAGS
      => {Apply extra CFLAGS/CPPFLAGS necessary for building with ICU.
          e.g. -I/usr/local/include}
    with-icu-config:=auto
      => {Enable SQLITE_ENABLE_ICU. Value must be one of: auto, pkg-config,
          /path/to/icu-config}
    icu-collations=0
      => {Enable SQLITE_ENABLE_ICU_COLLATIONS. Requires --with-icu-ldflags=...
          or --with-icu-config}
  }]
}

#
# Gets called by tea-configure-core. Must perform any configuration
# work needed for this extension.
#
proc teaish-configure {} {
  use teaish/feature-tests

  teaish-src-add -dist -dir generic/tclsqlite3.c

  if {[proj-opt-was-provided override-sqlite-version]} {
    teaish-pkginfo-set -version [opt-val override-sqlite-version]
    proj-warn "overriding sqlite version number:" [teaish-pkginfo-get -version]
  } elseif {[proj-opt-was-provided with-system-sqlite]
            && [opt-val with-system-sqlite] ne "0"} {
    proj-fatal "when using --with-system-sqlite also use" \
      "--override-sqlite-version to specify a library version number."
  }

  define CFLAGS [proj-get-env CFLAGS {-O2}]
  sqlite-munge-cflags

  #
  # Add feature flags from legacy configure.ac which are not covered by
  # --flags.
  #
  sqlite-add-feature-flag {
    -DSQLITE_3_SUFFIX_ONLY=1
    -DSQLITE_ENABLE_DESERIALIZE=1
    -DSQLITE_ENABLE_DBPAGE_VTAB=1
    -DSQLITE_ENABLE_BYTECODE_VTAB=1
    -DSQLITE_ENABLE_DBSTAT_VTAB=1
  }

  if {[opt-bool with-system-sqlite]} {
    msg-result "Using system-level sqlite3."
    teaish-cflags-add -DUSE_SYSTEM_SQLITE
    teaish-ldflags-add -lsqlite3
  } elseif {$::sqlite__Config(is-canonical)} {
    teaish-cflags-add -I[teaish-get -dir]/..
  }

  teaish-check-librt
  teaish-check-libz
  sqlite-handle-threadsafe
  sqlite-handle-tempstore
  sqlite-handle-load-extension
  sqlite-handle-math
  sqlite-handle-icu

  sqlite-handle-common-feature-flags; # must be late in the process
}; # teaish-configure

define OPT_FEATURE_FLAGS {} ; # -DSQLITE_OMIT/ENABLE flags.
#
# Adds $args, if not empty, to OPT_FEATURE_FLAGS. This is intended only for holding
# -DSQLITE_ENABLE/OMIT/... flags, but that is not enforced here.
proc sqlite-add-feature-flag {args} {
  if {"" ne $args} {
    define-append OPT_FEATURE_FLAGS {*}$args
  }
}

#
# Check for log(3) in libm and die with an error if it is not
# found. $featureName should be the feature name which requires that
# function (it's used only in error messages). defines LDFLAGS_MATH to
# the required linker flags (which may be empty even if the math APIs
# are found, depending on the OS).
proc sqlite-affirm-have-math {featureName} {
  if {"" eq [get-define LDFLAGS_MATH ""]} {
    if {![msg-quiet proj-check-function-in-lib log m]} {
      user-error "Missing math APIs for $featureName"
    }
    set lfl [get-define lib_log ""]
    undefine lib_log
    if {"" ne $lfl} {
      user-notice "Forcing requirement of $lfl for $featureName"
    }
    define LDFLAGS_MATH $lfl
    teaish-ldflags-prepend $lfl
  }
}

#
# Handle various SQLITE_ENABLE/OMIT_... feature flags.
proc sqlite-handle-common-feature-flags {} {
  msg-result "Feature flags..."
  if {![opt-bool all]} {
    # Special handling for --disable-all
    foreach flag $::sqlite__Config(all-flag-enables) {
      if {![proj-opt-was-provided $flag]} {
        proj-opt-set $flag 0
      }
    }
  }
  foreach {boolFlag featureFlag ifSetEvalThis} [proj-strip-hash-comments {
    all         {} {
      # The 'all' option must be first in this list.  This impl makes
      # an effort to only apply flags which the user did not already
      # apply, so that combinations like (--all --disable-geopoly)
      # will indeed disable geopoly. There are corner cases where
      # flags which depend on each other will behave in non-intuitive
      # ways:
      #
      # --all --disable-rtree
      #
      # Will NOT disable geopoly, though geopoly depends on rtree.
      # The --geopoly flag, though, will automatically re-enable
      # --rtree, so --disable-rtree won't actually disable anything in
      # that case.
      foreach k $::sqlite__Config(all-flag-enables) {
        if {![proj-opt-was-provided $k]} {
          proj-opt-set $k 1
        }
      }
    }
    fts3         -DSQLITE_ENABLE_FTS3    {sqlite-affirm-have-math fts3}
    fts4         -DSQLITE_ENABLE_FTS4    {sqlite-affirm-have-math fts4}
    fts5         -DSQLITE_ENABLE_FTS5    {sqlite-affirm-have-math fts5}
    geopoly      -DSQLITE_ENABLE_GEOPOLY {proj-opt-set rtree}
    rtree        -DSQLITE_ENABLE_RTREE   {}
    session      {-DSQLITE_ENABLE_SESSION -DSQLITE_ENABLE_PREUPDATE_HOOK} {}
    update-limit -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT {}
    scanstatus     -DSQLITE_ENABLE_STMT_SCANSTATUS {}
  }] {
    if {$boolFlag ni $::autosetup(options)} {
      # Skip flags which are in the canonical build but not
      # the autoconf bundle.
      continue
    }
    proj-if-opt-truthy $boolFlag {
      sqlite-add-feature-flag $featureFlag
      if {0 != [eval $ifSetEvalThis] && "all" ne $boolFlag} {
        msg-result "  + $boolFlag"
      }
    } {
      if {"all" ne $boolFlag} {
        msg-result "  - $boolFlag"
      }
    }
  }
  #
  # Invert the above loop's logic for some SQLITE_OMIT_...  cases. If
  # config option $boolFlag is false, [sqlite-add-feature-flag
  # $featureFlag], where $featureFlag is intended to be
  # -DSQLITE_OMIT_...
  foreach {boolFlag featureFlag} {
    json        -DSQLITE_OMIT_JSON
  } {
    if {[proj-opt-truthy $boolFlag]} {
      msg-result "  + $boolFlag"
    } else {
      sqlite-add-feature-flag $featureFlag
      msg-result "  - $boolFlag"
    }
  }

  ##
  # Remove duplicates from the final feature flag sets and show them
  # to the user.
  set oFF [get-define OPT_FEATURE_FLAGS]
  if {"" ne $oFF} {
    define OPT_FEATURE_FLAGS [lsort -unique $oFF]
    msg-result "Library feature flags: [get-define OPT_FEATURE_FLAGS]"
  }
  if {[lsearch [get-define TARGET_DEBUG ""] -DSQLITE_DEBUG=1] > -1} {
    msg-result "Note: this is a debug build, so performance will suffer."
  }
  teaish-cflags-add -define OPT_FEATURE_FLAGS
}; # sqlite-handle-common-feature-flags

#
# If --enable-threadsafe is set, this adds -DSQLITE_THREADSAFE=1 to
# OPT_FEATURE_FLAGS and sets LDFLAGS_PTHREAD to the linker flags
# needed for linking pthread (possibly an empty string). If
# --enable-threadsafe is not set, adds -DSQLITE_THREADSAFE=0 to
# OPT_FEATURE_FLAGS and sets LDFLAGS_PTHREAD to an empty string.
#
# It prepends the flags to the global LDFLAGS.
proc sqlite-handle-threadsafe {} {
  msg-checking "Support threadsafe operation? "
  define LDFLAGS_PTHREAD ""
  set enable 0
  if {[proj-opt-was-provided threadsafe]} {
    proj-if-opt-truthy threadsafe {
      if {[proj-check-function-in-lib pthread_create pthread]
          && [proj-check-function-in-lib pthread_mutexattr_init pthread]} {
        incr enable
        set ldf [get-define lib_pthread_create]
        define LDFLAGS_PTHREAD $ldf
        teaish-ldflags-prepend $ldf
        undefine lib_pthread_create
        undefine lib_pthread_mutexattr_init
      } else {
        user-error "Missing required pthread libraries. Use --disable-threadsafe to disable this check."
      }
      # Recall that LDFLAGS_PTHREAD might be empty even if pthreads if
      # found because it's in -lc on some platforms.
    } {
      msg-result "Disabled using --disable-threadsafe"
    }
  } else {
    #
    # If user does not specify --[disable-]threadsafe then select a
    # default based on whether it looks like Tcl has threading
    # support.
    #
    catch {
      scan [exec echo {puts [tcl::pkgconfig get threaded]} | [get-define TCLSH_CMD]] \
        %d enable
    }
    if {$enable} {
      set flagName "--threadsafe"
      set lblAbled "enabled"
      msg-result yes
    } else {
      set flagName "--disable-threadsafe"
      set lblAbled "disabled"
      msg-result no
    }
    msg-result "Defaulting to ${flagName} because Tcl has threading ${lblAbled}."
    # ^^^ We (probably) don't need to link against -lpthread in the
    # is-enabled case. We might in the case of static linking. Unsure.
  }
  sqlite-add-feature-flag -DSQLITE_THREADSAFE=${enable}
  return $enable
}

#
# Handles the --enable-load-extension flag. Returns 1 if the support
# is enabled, else 0. If support for that feature is not found, a
# fatal error is triggered if --enable-load-extension is explicitly
# provided, else a loud warning is instead emitted. If
# --disable-load-extension is used, no check is performed.
#
# Makes the following environment changes:
#
# - defines LDFLAGS_DLOPEN to any linker flags needed for this
#   feature.  It may legally be empty on some systems where dlopen()
#   is in libc.
#
# - If the feature is not available, adds
#   -DSQLITE_OMIT_LOAD_EXTENSION=1 to the feature flags list.
proc sqlite-handle-load-extension {} {
  define LDFLAGS_DLOPEN ""
  set found 0
  proj-if-opt-truthy load-extension {
    set found [proj-check-function-in-lib dlopen dl]
    if {$found} {
      set ldf [get-define lib_dlopen]
      define LDFLAGS_DLOPEN $ldf
      teaish-ldflags-prepend $ldf
      undefine lib_dlopen
    } else {
      if {[proj-opt-was-provided load-extension]} {
        # Explicit --enable-load-extension: fail if not found
        proj-indented-notice -error {
          --enable-load-extension was provided but dlopen()
          not found. Use --disable-load-extension to bypass this
          check.
        }
      } else {
        # It was implicitly enabled: warn if not found
        proj-indented-notice {
          WARNING: dlopen() not found, so loadable module support will
          be disabled. Use --disable-load-extension to bypass this
          check.
        }
      }
    }
  }
  if {$found} {
    msg-result "Loadable extension support enabled."
  } else {
    msg-result "Disabling loadable extension support. Use --enable-load-extension to enable them."
    sqlite-add-feature-flag -DSQLITE_OMIT_LOAD_EXTENSION=1
  }
  return $found
}

#
# ICU - International Components for Unicode
#
# Handles these flags:
#
#  --with-icu-ldflags=LDFLAGS
#  --with-icu-cflags=CFLAGS
#  --with-icu-config[=auto | pkg-config | /path/to/icu-config]
#  --enable-icu-collations
#
# --with-icu-config values:
#
#   - auto: use the first one of (pkg-config, icu-config) found on the
#     system.
#   - pkg-config: use only pkg-config to determine flags
#   - /path/to/icu-config: use that to determine flags
#
# If --with-icu-config is used as neither pkg-config nor icu-config
# are found, fail fatally.
#
# If both --with-icu-ldflags and --with-icu-config are provided, they
# are cumulative.  If neither are provided, icu-collations is not
# honored and a warning is emitted if it is provided.
#
# Design note: though we could automatically enable ICU if the
# icu-config binary or (pkg-config icu-io) are found, we specifically
# do not. ICU is always an opt-in feature.
proc sqlite-handle-icu {} {
  define LDFLAGS_LIBICU [join [opt-val with-icu-ldflags ""]]
  define CFLAGS_LIBICU [join [opt-val with-icu-cflags ""]]
  if {[proj-opt-was-provided with-icu-config]} {
    msg-result "Checking for ICU support..."
    set icuConfigBin [opt-val with-icu-config]
    set tryIcuConfigBin 1; # set to 0 if we end up using pkg-config
    if {$icuConfigBin in {auto pkg-config}} {
      uplevel 3 { use pkg-config }
      if {[pkg-config-init 0] && [pkg-config icu-io]} {
        # Maintenance reminder: historical docs say to use both of
        # (icu-io, icu-uc). icu-uc lacks a required lib and icu-io has
        # all of them on tested OSes.
        set tryIcuConfigBin 0
        define LDFLAGS_LIBICU [get-define PKG_ICU_IO_LDFLAGS]
        define-append LDFLAGS_LIBICU [get-define PKG_ICU_IO_LIBS]
        define CFLAGS_LIBICU [get-define PKG_ICU_IO_CFLAGS]
      } elseif {"pkg-config" eq $icuConfigBin} {
        proj-fatal "pkg-config cannot find package icu-io"
      } else {
        proj-assert {"auto" eq $icuConfigBin}
      }
    }
    if {$tryIcuConfigBin} {
      if {"auto" eq $icuConfigBin} {
        set icuConfigBin [proj-first-bin-of \
                            /usr/local/bin/icu-config \
                            /usr/bin/icu-config]
        if {"" eq $icuConfigBin} {
          proj-indented-notice -error {
            --with-icu-config=auto cannot find (pkg-config icu-io) or icu-config binary.
            On Ubuntu-like systems try:
            --with-icu-ldflags='-licui18n -licuuc -licudata'
          }
        }
      }
      if {[file-isexec $icuConfigBin]} {
        set x [exec $icuConfigBin --ldflags]
        if {"" eq $x} {
          proj-indented-notice -error \
            [subst {
              $icuConfigBin --ldflags returned no data.
              On Ubuntu-like systems try:
              --with-icu-ldflags='-licui18n -licuuc -licudata'
            }]
        }
        define-append LDFLAGS_LIBICU $x
        set x [exec $icuConfigBin --cppflags]
        define-append CFLAGS_LIBICU $x
      } else {
        proj-fatal "--with-icu-config=$icuConfigBin does not refer to an executable"
      }
    }
  }
  set ldflags [define LDFLAGS_LIBICU [string trim [get-define LDFLAGS_LIBICU]]]
  set cflags [define CFLAGS_LIBICU [string trim [get-define CFLAGS_LIBICU]]]
  if {"" ne $ldflags} {
    sqlite-add-feature-flag -DSQLITE_ENABLE_ICU
    msg-result "Enabling ICU support with flags: $ldflags $cflags"
    if {[opt-bool icu-collations]} {
      msg-result "Enabling ICU collations."
      sqlite-add-feature-flag -DSQLITE_ENABLE_ICU_COLLATIONS
    }
    teaish-ldflags-prepend $ldflags
    teaish-cflags-add $cflags
  } elseif {[opt-bool icu-collations]} {
    proj-warn "ignoring --enable-icu-collations because neither --with-icu-ldflags nor --with-icu-config provided any linker flags"
  } else {
    msg-result "ICU support is disabled."
  }
}; # sqlite-handle-icu


#
# Handles the --with-tempstore flag.
#
# The test fixture likes to set SQLITE_TEMP_STORE on its own, so do
# not set that feature flag unless it was explicitly provided to the
# configure script.
proc sqlite-handle-tempstore {} {
  if {[proj-opt-was-provided with-tempstore]} {
    set ts [opt-val with-tempstore no]
    set tsn 1
    msg-checking "Use an in-RAM database for temporary tables? "
    switch -exact -- $ts {
      never  { set tsn 0 }
      no     { set tsn 1 }
      yes    { set tsn 2 }
      always { set tsn 3 }
      default {
        user-error "Invalid --with-tempstore value '$ts'. Use one of: never, no, yes, always"
      }
    }
    msg-result $ts
    sqlite-add-feature-flag -DSQLITE_TEMP_STORE=$tsn
  }
}

#
# Handles the --enable-math flag.
proc sqlite-handle-math {} {
  proj-if-opt-truthy math {
    if {![proj-check-function-in-lib ceil m]} {
      user-error "Cannot find libm functions. Use --disable-math to bypass this."
    }
    set lfl [get-define lib_ceil]
    undefine lib_ceil
    define LDFLAGS_MATH $lfl
    teaish-ldflags-prepend $lfl
    sqlite-add-feature-flag -DSQLITE_ENABLE_MATH_FUNCTIONS
    msg-result "Enabling math SQL functions"
  } {
    define LDFLAGS_MATH ""
    msg-result "Disabling math SQL functions"
  }
}

#
# Move -DSQLITE_OMIT... and -DSQLITE_ENABLE... flags from CFLAGS and
# CPPFLAGS to OPT_FEATURE_FLAGS and remove them from BUILD_CFLAGS.
proc sqlite-munge-cflags {} {
  # Move CFLAGS and CPPFLAGS entries matching -DSQLITE_OMIT* and
  # -DSQLITE_ENABLE* to OPT_FEATURE_FLAGS. This behavior is derived
  # from the pre-3.48 build.
  #
  # If any configure flags for features are in conflict with
  # CFLAGS/CPPFLAGS-specified feature flags, all bets are off.  There
  # are no guarantees about which one will take precedence.
  foreach flagDef {CFLAGS CPPFLAGS} {
    set tmp ""
    foreach cf [get-define $flagDef ""] {
      switch -glob -- $cf {
        -DSQLITE_OMIT* -
        -DSQLITE_ENABLE* {
          sqlite-add-feature-flag $cf
        }
        default {
          lappend tmp $cf
        }
      }
    }
    define $flagDef $tmp
  }
}