sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / tool / buildtclext.tcl
| 1 | #!/usr/bin/tclsh |
| 2 | # |
| 3 | set help \ |
| 4 | {Run this TCL script to build and install the TCL interface library for |
| 5 | SQLite. Run the script with the specific "tclsh" for which the installation |
| 6 | should occur. |
| 7 | |
| 8 | There must be a valid "tclsqlite3.c" file in the working directory prior |
| 9 | to running this script. Use "make tclsqlite3.c" to generate that file. |
| 10 | |
| 11 | Options: |
| 12 | |
| 13 | --build-only Only build the extension, don't install it |
| 14 | --cc COMPILER Build using this compiler |
| 15 | --info Show info on existing SQLite TCL extension installs |
| 16 | --install-only Install an extension previously build |
| 17 | --uninstall Uninstall the extension |
| 18 | --version-check Check extension version against this source tree |
| 19 | --destdir DIR Installation root (used by "make install DESTDIR=...") |
| 20 | --tclConfig.sh FILE Use this tclConfig.sh instead of looking for one |
| 21 | --extlibs LDFLAGS required for external libs, e.g. ICU. |
| 22 | |
| 23 | Other options are retained and passed through into the compiler.} |
| 24 | |
| 25 | |
| 26 | set build 1 |
| 27 | set install 1 |
| 28 | set uninstall 0 |
| 29 | set infoonly 0 |
| 30 | set versioncheck 0 |
| 31 | set CC {} |
| 32 | set OPTS {} |
| 33 | set DESTDIR "" ; # --destdir "$(DESTDIR)" |
| 34 | set tclConfigSh ""; # --tclConfig.sh FILE |
| 35 | set LIBS {} ; # --extlibs "-lx -ly ..." |
| 36 | for {set ii 0} {$ii<[llength $argv]} {incr ii} { |
| 37 | set a0 [lindex $argv $ii] |
| 38 | if {$a0=="--install-only"} { |
| 39 | set build 0 |
| 40 | } elseif {$a0=="--build-only"} { |
| 41 | set install 0 |
| 42 | } elseif {$a0=="--uninstall"} { |
| 43 | set build 0 |
| 44 | set install 0 |
| 45 | set versioncheck 0 |
| 46 | set uninstall 1 |
| 47 | } elseif {$a0=="--info"} { |
| 48 | set build 0 |
| 49 | set install 0 |
| 50 | set versioncheck 0 |
| 51 | set infoonly 1 |
| 52 | } elseif {$a0=="--version-check"} { |
| 53 | set build 0 |
| 54 | set install 0 |
| 55 | set infoonly 0 |
| 56 | set versioncheck 1 |
| 57 | } elseif {$a0=="--cc" && $ii+1<[llength $argv]} { |
| 58 | incr ii |
| 59 | set CC [lindex $argv $ii] |
| 60 | } elseif {$a0=="--destdir" && $ii+1<[llength $argv]} { |
| 61 | incr ii |
| 62 | set DESTDIR [lindex $argv $ii] |
| 63 | } elseif {$a0=="--tclConfig.sh" && $ii+1<[llength $argv]} { |
| 64 | incr ii |
| 65 | set tclConfigSh [lindex $argv $ii] |
| 66 | } elseif {$a0=="--extlibs" && $ii+1<[llength $argv]} { |
| 67 | incr ii |
| 68 | lappend LIBS [lindex $argv $ii] |
| 69 | } elseif {[string match -* $a0]} { |
| 70 | append OPTS " $a0" |
| 71 | } else { |
| 72 | puts stderr "Unknown option: \"$a0\"\n" |
| 73 | puts stderr $help |
| 74 | exit 1 |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | # Find the root of the SQLite source tree |
| 79 | # |
| 80 | set srcdir [file normalize [file dir $argv0]/..] |
| 81 | |
| 82 | # Get the SQLite version number into $VERSION |
| 83 | # |
| 84 | set fd [open $srcdir/VERSION] |
| 85 | set VERSION [string trim [read $fd]] |
| 86 | close $fd |
| 87 | |
| 88 | if {$tcl_platform(platform) eq "windows"} { |
| 89 | # We are only able to install, uninstall, and list on Windows. |
| 90 | # The build process is handled by the Makefile.msc, specifically |
| 91 | # using "nmake /f Makefile.msc pkgIndex.tcl tclsqlite3.dll" |
| 92 | # |
| 93 | if {$build} { |
| 94 | puts "Unable to build on Windows using the builttclext.tcl script." |
| 95 | puts "To build, run\n" |
| 96 | puts " \"nmake /f Makefile.msc pkgIndex.tcl tclsqlite3.dll" |
| 97 | exit 1 |
| 98 | } |
| 99 | set OUT tclsqlite3.dll |
| 100 | } else { |
| 101 | # Read the tclConfig.sh file into the $tclConfig variable |
| 102 | # |
| 103 | if {"" eq $tclConfigSh} { |
| 104 | # Figure out the location of the tclConfig.sh file used by the |
| 105 | # tclsh that is executing this script. |
| 106 | # |
| 107 | if {[catch { |
| 108 | set LIBDIR [tcl::pkgconfig get libdir,install] |
| 109 | }]} { |
| 110 | puts stderr "$argv0: tclsh does not support tcl::pkgconfig." |
| 111 | exit 1 |
| 112 | } |
| 113 | if {![file exists $LIBDIR]} { |
| 114 | puts stderr "$argv0: cannot find the tclConfig.sh file." |
| 115 | puts stderr "$argv0: tclsh reported library directory \"$LIBDIR\"\ |
| 116 | does not exist." |
| 117 | exit 1 |
| 118 | } |
| 119 | if {![file exists $LIBDIR/tclConfig.sh] |
| 120 | || [file size $LIBDIR/tclConfig.sh]<5000} { |
| 121 | set n1 $LIBDIR/tcl$::tcl_version |
| 122 | if {[file exists $n1/tclConfig.sh] |
| 123 | && [file size $n1/tclConfig.sh]>5000} { |
| 124 | set LIBDIR $n1 |
| 125 | } else { |
| 126 | puts stderr "$argv0: cannot find tclConfig.sh in either $LIBDIR or $n1" |
| 127 | exit 1 |
| 128 | } |
| 129 | } |
| 130 | #puts "using $LIBDIR/tclConfig.sh" |
| 131 | set fd [open $LIBDIR/tclConfig.sh rb] |
| 132 | set tclConfig [read $fd] |
| 133 | close $fd |
| 134 | } else { |
| 135 | # User-provided tclConfig.sh |
| 136 | # |
| 137 | set fd [open $tclConfigSh rb] |
| 138 | set tclConfig [read $fd] |
| 139 | close $fd |
| 140 | } |
| 141 | |
| 142 | # Extract parameter we will need from the tclConfig.sh file |
| 143 | # |
| 144 | set TCLMAJOR 8 |
| 145 | regexp {TCL_MAJOR_VERSION='(\d)'} $tclConfig all TCLMAJOR |
| 146 | set SUFFIX so |
| 147 | regexp {TCL_SHLIB_SUFFIX='\.([^']+)'} $tclConfig all SUFFIX |
| 148 | if {$CC==""} { |
| 149 | set cc {} |
| 150 | regexp {TCL_CC='([^']+)'} $tclConfig all cc |
| 151 | if {$cc!=""} { |
| 152 | set CC $cc |
| 153 | } |
| 154 | } |
| 155 | if {$CC==""} { |
| 156 | set CC gcc |
| 157 | } |
| 158 | set CFLAGS -fPIC |
| 159 | regexp {TCL_SHLIB_CFLAGS='([^']+)'} $tclConfig all CFLAGS |
| 160 | regexp {TCL_STUB_LIB_SPEC='([^']+)'} $tclConfig all LIBSTUB |
| 161 | lappend LIBS $LIBSTUB |
| 162 | set INC "-I$srcdir/src" |
| 163 | set inc {} |
| 164 | regexp {TCL_INCLUDE_SPEC='([^']+)'} $tclConfig all inc |
| 165 | if {$inc!=""} { |
| 166 | append INC " $inc" |
| 167 | } |
| 168 | set cmd {${CC} ${CFLAGS} ${LDFLAGS} -shared} |
| 169 | regexp {TCL_SHLIB_LD='([^']+)(-Wl,--out-implib.*)?'} $tclConfig all cmd |
| 170 | |
| 171 | # TCL_SHLIB_LD is a shell command recorded from the Tcl build, so the names |
| 172 | # in it are Tcl's configure-time variables. We use every name that |
| 173 | # a tclConfig.sh is known to use so [subst] below doesn't fail. |
| 174 | set SHLIB_CFLAGS $CFLAGS |
| 175 | set LDFLAGS "" |
| 176 | |
| 177 | # Our own options get appended to the command directly. |
| 178 | |
| 179 | set EXTRA "$INC -DUSE_TCL_STUBS" |
| 180 | if {[string length $OPTS]>1} { |
| 181 | append EXTRA $OPTS |
| 182 | } |
| 183 | if {$tcl_platform(os) eq "Windows NT"} { |
| 184 | set OUT cyg |
| 185 | } else { |
| 186 | set OUT lib |
| 187 | } |
| 188 | if {$TCLMAJOR>8} { |
| 189 | set OUT ${OUT}tcl9sqlite$VERSION.$SUFFIX |
| 190 | } else { |
| 191 | set OUT ${OUT}sqlite$VERSION.$SUFFIX |
| 192 | } |
| 193 | set @ $OUT; # Workaround for https://sqlite.org/forum/forumpost/0683a49cb02f31a1 |
| 194 | # in which Gentoo edits their tclConfig.sh to include an soname |
| 195 | # linker flag which includes ${@} (the target file's name). |
| 196 | if {[catch {subst $cmd} CMD]} { |
| 197 | puts stderr "warning: cannot expand TCL_SHLIB_LD: $CMD" |
| 198 | set CMD "$CC $CFLAGS -shared" |
| 199 | } |
| 200 | append CMD " $EXTRA" |
| 201 | } |
| 202 | |
| 203 | # Check the SQLite TCL extension that is loaded by default by this running |
| 204 | # TCL interpreter to see if it has the same SQLITE_SOURCE_ID as the source |
| 205 | # code in the directory holding this script. |
| 206 | # |
| 207 | if {$versioncheck} { |
| 208 | if {[catch {package require sqlite3} msg]} { |
| 209 | puts stderr "No SQLite TCL extension available: $msg" |
| 210 | exit 1 |
| 211 | } |
| 212 | sqlite3 db :memory: |
| 213 | set extvers [db one {SELECT sqlite_source_id()}] |
| 214 | db close |
| 215 | set fd [open sqlite3.h rb] |
| 216 | set sqlite3h [read $fd] |
| 217 | close $fd |
| 218 | regexp {#define SQLITE_SOURCE_ID +"([^"]+)"} $sqlite3h all srcvers |
| 219 | set srcvers [string range $srcvers 0 78] |
| 220 | set extvers [string range $extvers 0 78] |
| 221 | if {$srcvers==$extvers} { |
| 222 | puts "source code and extension versions aligned:\n$extvers" |
| 223 | exit 0 |
| 224 | } |
| 225 | puts stderr "source code and extension versions differ" |
| 226 | puts stderr "source: $srcvers\nextension: $extvers" |
| 227 | exit 1 |
| 228 | } |
| 229 | |
| 230 | # Show information about prior installs |
| 231 | # |
| 232 | if {$infoonly} { |
| 233 | set cnt 0 |
| 234 | foreach dir $auto_path { |
| 235 | foreach subdir [glob -nocomplain -types d $dir/sqlite3*] { |
| 236 | if {[file exists $subdir/pkgIndex.tcl]} { |
| 237 | puts $subdir |
| 238 | incr cnt |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | if {$cnt==0} { |
| 243 | puts "no current installations of the SQLite TCL extension" |
| 244 | } |
| 245 | exit |
| 246 | } |
| 247 | |
| 248 | # Uninstall the extension |
| 249 | # |
| 250 | if {$uninstall} { |
| 251 | set cnt 0 |
| 252 | foreach dir $auto_path { |
| 253 | if {[file isdirectory $dir/sqlite$VERSION]} { |
| 254 | incr cnt |
| 255 | if {![file writable $dir] || ![file writable $dir/sqlite$VERSION]} { |
| 256 | puts "cannot uninstall $dir/sqlite$VERSION - permission denied" |
| 257 | } else { |
| 258 | puts "uninstalling $dir/sqlite$VERSION..." |
| 259 | file delete -force $dir/sqlite$VERSION |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | if {$cnt==0} { |
| 264 | puts "nothing to uninstall" |
| 265 | } |
| 266 | exit |
| 267 | } |
| 268 | |
| 269 | if {$install} { |
| 270 | # Figure out where the extension will be installed. Put the extension |
| 271 | # in the first writable directory on $auto_path. |
| 272 | # |
| 273 | set DEST {} |
| 274 | foreach dir $auto_path { |
| 275 | if {[string match //*:* $dir]} { |
| 276 | # We can't install to //zipfs: paths |
| 277 | continue |
| 278 | } elseif {"" ne $DESTDIR && ![file writable $DESTDIR]} { |
| 279 | # In the common case, ${DESTDIR}${dir} will not exist when we |
| 280 | # get to this point of the installation, and the "is writable?" |
| 281 | # check just below this will fail for that case. |
| 282 | # |
| 283 | # Assumption made for simplification's sake: if ${DESTDIR} is |
| 284 | # not writable, no part of the remaining path will |
| 285 | # be. ${DESTDIR} is typically used by OS package maintainers, |
| 286 | # not normal installations, and it "shouldn't" ever happen that |
| 287 | # the DESTDIR is read-only while the target ${DESTDIR}${prefix} |
| 288 | # is not, as it's typical for such installations to create |
| 289 | # ${prefix} on-demand under ${DESTDIR}. |
| 290 | break |
| 291 | } |
| 292 | set dir ${DESTDIR}$dir |
| 293 | if {[file writable $dir] || "" ne $DESTDIR} { |
| 294 | # the dir will be created later ^^^^^^^^ |
| 295 | set DEST $dir |
| 296 | break |
| 297 | } elseif {[glob -nocomplain $dir/sqlite3*/pkgIndex.tcl]!=""} { |
| 298 | set conflict [lindex [glob $dir/sqlite3*/pkgIndex.tcl] 0] |
| 299 | puts "Unable to install. There is already a conflicting version" |
| 300 | puts "of the SQLite TCL Extension that cannot be overwritten at\n" |
| 301 | puts " [file dirname $conflict]\n" |
| 302 | puts "Consider running using sudo to work around this problem." |
| 303 | exit 1 |
| 304 | } |
| 305 | } |
| 306 | if {$DEST==""} { |
| 307 | puts "None of the directories on \$auto_path are writable by this process," |
| 308 | puts "so the installation cannot take place. Consider running using sudo" |
| 309 | puts "to work around this problem.\n" |
| 310 | puts "These are the (unwritable) \$auto_path directories:\n" |
| 311 | foreach dir $auto_path { |
| 312 | puts " * ${DESTDIR}$dir" |
| 313 | } |
| 314 | exit 1 |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if {$build} { |
| 319 | # Generate the pkgIndex.tcl file |
| 320 | # |
| 321 | puts "generating pkgIndex.tcl..." |
| 322 | set fd [open pkgIndex.tcl w] |
| 323 | puts $fd [subst -nocommands {# -*- tcl -*- |
| 324 | # Tcl package index file, version ??? |
| 325 | # |
| 326 | package ifneeded sqlite3 $VERSION \\ |
| 327 | [list load [file join \$dir $OUT] Sqlite3] |
| 328 | }] |
| 329 | close $fd |
| 330 | |
| 331 | # Generate and execute the command with which to do the compilation. |
| 332 | # |
| 333 | set cmd "$CMD -DUSE_TCL_STUBS tclsqlite3.c -o $OUT [join $LIBS { }]" |
| 334 | puts $cmd |
| 335 | file delete -force $OUT |
| 336 | catch {exec {*}$cmd} errmsg |
| 337 | if {$errmsg!="" && ![file exists $OUT]} { |
| 338 | puts $errmsg |
| 339 | exit 1 |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | |
| 344 | if {$install} { |
| 345 | # Install the extension |
| 346 | set DEST2 $DEST/sqlite$VERSION |
| 347 | file mkdir $DEST2 |
| 348 | puts "installing $DEST2/pkgIndex.tcl" |
| 349 | file copy -force pkgIndex.tcl $DEST2 |
| 350 | puts "installing $DEST2/$OUT" |
| 351 | file copy -force $OUT $DEST2 |
| 352 | } |