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 / mksqlite3c.tcl
| 1 | #!/usr/bin/tclsh |
| 2 | # |
| 3 | # To build a single huge source file holding all of SQLite (or at |
| 4 | # least the core components - the test harness, shell, and TCL |
| 5 | # interface are omitted.) first do |
| 6 | # |
| 7 | # make target_source |
| 8 | # |
| 9 | # The make target above moves all of the source code files into |
| 10 | # a subdirectory named "tsrc". (This script expects to find the files |
| 11 | # there and will not work if they are not found.) There are a few |
| 12 | # generated C code files that are also added to the tsrc directory. |
| 13 | # For example, the "parse.c" and "parse.h" files to implement the |
| 14 | # the parser are derived from "parse.y" using lemon. And the |
| 15 | # "keywordhash.h" files is generated by a program named "mkkeywordhash". |
| 16 | # |
| 17 | # After the "tsrc" directory has been created and populated, run |
| 18 | # this script: |
| 19 | # |
| 20 | # tclsh mksqlite3c.tcl [flags] [extra source files] |
| 21 | # |
| 22 | # The amalgamated SQLite code will be written into sqlite3.c |
| 23 | # |
| 24 | |
| 25 | set help {Usage: tclsh mksqlite3c.tcl <options> |
| 26 | where <options> is zero or more of the following with these effects: |
| 27 | --nostatic => Do not generate with compile-time modifiable linkage. |
| 28 | --linemacros=? => Emit #line directives into output or not. (? = 1 or 0) |
| 29 | --useapicall => Prepend functions with SQLITE_APICALL or SQLITE_CDECL. |
| 30 | --srcdir $SRC => Specify the directory containing constituent sources. |
| 31 | --help => See this. |
| 32 | The value setting options default to --linemacros=1 and '--srcdir tsrc' . |
| 33 | } |
| 34 | |
| 35 | # Begin by reading the "sqlite3.h" header file. Extract the version number |
| 36 | # from in this file. The version number is needed to generate the header |
| 37 | # comment of the amalgamation. |
| 38 | # |
| 39 | |
| 40 | set addstatic 1 |
| 41 | set linemacros 0 |
| 42 | set useapicall 0 |
| 43 | set enable_recover 0 |
| 44 | set srcdir tsrc |
| 45 | set extrasrc [list] |
| 46 | |
| 47 | for {set i 0} {$i<[llength $argv]} {incr i} { |
| 48 | set x [lindex $argv $i] |
| 49 | if {[regexp {^-?-enable-recover$} $x]} { |
| 50 | set enable_recover 1 |
| 51 | } elseif {[regexp {^-?-nostatic$} $x]} { |
| 52 | set addstatic 0 |
| 53 | } elseif {[regexp {^-?-linemacros(?:=([01]))?$} $x ma ulm]} { |
| 54 | if {$ulm == ""} {set ulm 1} |
| 55 | set linemacros $ulm |
| 56 | } elseif {[regexp {^-?-useapicall$} $x]} { |
| 57 | set useapicall 1 |
| 58 | } elseif {[regexp {^-?-srcdir$} $x]} { |
| 59 | incr i |
| 60 | if {$i==[llength $argv]} { |
| 61 | error "No argument following $x" |
| 62 | } |
| 63 | set srcdir [lindex $argv $i] |
| 64 | } elseif {[regexp {^-?-((help)|\?)$} $x]} { |
| 65 | puts $help |
| 66 | exit 0 |
| 67 | } elseif {[regexp {^-?-} $x]} { |
| 68 | error "unknown command-line option: $x" |
| 69 | } else { |
| 70 | lappend extrasrc $x |
| 71 | } |
| 72 | } |
| 73 | set in [open $srcdir/sqlite3.h rb] |
| 74 | set cnt 0 |
| 75 | set VERSION ????? |
| 76 | while {![eof $in]} { |
| 77 | set line [gets $in] |
| 78 | if {$line=="" && [eof $in]} break |
| 79 | incr cnt |
| 80 | regexp {#define\s+SQLITE_VERSION\s+"(.*)"} $line all VERSION |
| 81 | } |
| 82 | close $in |
| 83 | |
| 84 | # Open the output file and write a header comment at the beginning |
| 85 | # of the file. |
| 86 | # |
| 87 | set fname sqlite3.c |
| 88 | if {$enable_recover} { set fname sqlite3r.c } |
| 89 | set out [open $fname wb] |
| 90 | # Force the output to use unix line endings, even on Windows. |
| 91 | fconfigure $out -translation binary |
| 92 | set today [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S UTC" -gmt 1] |
| 93 | puts $out [subst \ |
| 94 | {/****************************************************************************** |
| 95 | ** This file is an amalgamation of many separate C source files from SQLite |
| 96 | ** version $VERSION. By combining all the individual C code files into this |
| 97 | ** single large file, the entire code can be compiled as a single translation |
| 98 | ** unit. This allows many compilers to do optimizations that would not be |
| 99 | ** possible if the files were compiled separately. Performance improvements |
| 100 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 101 | ** translation unit. |
| 102 | ** |
| 103 | ** This file is all you need to compile SQLite. To use SQLite in other |
| 104 | ** programs, you need this file and the "sqlite3.h" header file that defines |
| 105 | ** the programming interface to the SQLite library. (If you do not have |
| 106 | ** the "sqlite3.h" header file at hand, you will find a copy embedded within |
| 107 | ** the text of this file. Search for "Begin file sqlite3.h" to find the start |
| 108 | ** of the embedded sqlite3.h header file.) Additional code files may be needed |
| 109 | ** if you want a wrapper to interface SQLite with your choice of programming |
| 110 | ** language. The code for the "sqlite3" command-line shell is also in a |
| 111 | ** separate file. This file contains only code for the core SQLite library. |
| 112 | **}] |
| 113 | set srcroot [file dirname [file dirname [info script]]] |
| 114 | if {$tcl_platform(platform) eq "windows"} { |
| 115 | set vsrcprog src-verify.exe |
| 116 | } else { |
| 117 | set vsrcprog ./src-verify |
| 118 | } |
| 119 | if {[file executable $vsrcprog] && [file readable $srcroot/manifest]} { |
| 120 | set tmpfile tmp-[clock millisec]-[expr {int(rand()*100000000000)}].txt |
| 121 | exec $vsrcprog -x $srcroot > $tmpfile |
| 122 | set fd [open $tmpfile rb] |
| 123 | set res [string trim [split [read $fd] \n]] |
| 124 | close $fd |
| 125 | file delete -force $tmpfile |
| 126 | puts $out "** The content in this amalgamation comes from Fossil check-in" |
| 127 | puts -nonewline $out "** [string range [lindex $res 0] 0 35]" |
| 128 | if {[llength $res]==1} { |
| 129 | puts $out "." |
| 130 | } else { |
| 131 | puts $out " with changes in files:\n**" |
| 132 | foreach f [lrange $res 1 end] { |
| 133 | puts $out "** [string trim $f]" |
| 134 | } |
| 135 | } |
| 136 | } else { |
| 137 | puts $out "** The origin of the sources used to build this amalgamation" |
| 138 | puts $out "** is unknown." |
| 139 | } |
| 140 | puts $out [subst {*/ |
| 141 | #ifndef SQLITE_AMALGAMATION |
| 142 | #define SQLITE_CORE 1 |
| 143 | #define SQLITE_AMALGAMATION 1}] |
| 144 | if {$addstatic} { |
| 145 | puts $out \ |
| 146 | {#ifndef SQLITE_PRIVATE |
| 147 | # define SQLITE_PRIVATE static |
| 148 | #endif} |
| 149 | } |
| 150 | |
| 151 | # Examine the parse.c file. If it contains lines of the form: |
| 152 | # |
| 153 | # "#ifndef SQLITE_ENABLE_UPDATE_LIMIT |
| 154 | # |
| 155 | # then set the SQLITE_UDL_CAPABLE_PARSER flag in the amalgamation. |
| 156 | # |
| 157 | set in [open $srcdir/parse.c rb] |
| 158 | if {[regexp {ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT} [read $in]]} { |
| 159 | puts $out "#define SQLITE_UDL_CAPABLE_PARSER 1" |
| 160 | } |
| 161 | close $in |
| 162 | |
| 163 | # These are the header files used by SQLite. The first time any of these |
| 164 | # files are seen in a #include statement in the C code, include the complete |
| 165 | # text of the file in-line. The file only needs to be included once. |
| 166 | # |
| 167 | foreach hdr { |
| 168 | btree.h |
| 169 | btreeInt.h |
| 170 | fts3.h |
| 171 | fts3Int.h |
| 172 | fts3_hash.h |
| 173 | fts3_tokenizer.h |
| 174 | geopoly.c |
| 175 | hash.h |
| 176 | hwtime.h |
| 177 | keywordhash.h |
| 178 | msvc.h |
| 179 | mutex.h |
| 180 | opcodes.h |
| 181 | os_common.h |
| 182 | os_setup.h |
| 183 | os_win.h |
| 184 | os.h |
| 185 | pager.h |
| 186 | parse.h |
| 187 | pcache.h |
| 188 | pragma.h |
| 189 | rtree.h |
| 190 | sqlite3session.h |
| 191 | sqlite3.h |
| 192 | sqlite3ext.h |
| 193 | sqlite3rbu.h |
| 194 | sqliteicu.h |
| 195 | sqliteInt.h |
| 196 | sqliteLimit.h |
| 197 | vdbe.h |
| 198 | vdbeInt.h |
| 199 | vxworks.h |
| 200 | wal.h |
| 201 | whereInt.h |
| 202 | sqlite3recover.h |
| 203 | } { |
| 204 | set available_hdr($hdr) 1 |
| 205 | } |
| 206 | set available_hdr(sqliteInt.h) 0 |
| 207 | set available_hdr(os_common.h) 0 |
| 208 | set available_hdr(sqlite3session.h) 0 |
| 209 | |
| 210 | # These headers should be copied into the amalgamation without modifying any |
| 211 | # of their function declarations or definitions. |
| 212 | set varonly_hdr(sqlite3.h) 1 |
| 213 | |
| 214 | # These are the functions that accept a variable number of arguments. They |
| 215 | # always need to use the "cdecl" calling convention even when another calling |
| 216 | # convention (e.g. "stcall") is being used for the rest of the library. |
| 217 | set cdecllist { |
| 218 | sqlite3_config |
| 219 | sqlite3_db_config |
| 220 | sqlite3_log |
| 221 | sqlite3_mprintf |
| 222 | sqlite3_snprintf |
| 223 | sqlite3_str_appendf |
| 224 | sqlite3_test_control |
| 225 | sqlite3_vtab_config |
| 226 | } |
| 227 | |
| 228 | # 78 stars used for comment formatting. |
| 229 | set s78 \ |
| 230 | {*****************************************************************************} |
| 231 | |
| 232 | # Insert a comment into the code |
| 233 | # |
| 234 | proc section_comment {text} { |
| 235 | global out s78 |
| 236 | set n [string length $text] |
| 237 | set nstar [expr {60 - $n}] |
| 238 | set stars [string range $s78 0 $nstar] |
| 239 | puts $out "/************** $text $stars/" |
| 240 | } |
| 241 | |
| 242 | # Read the source file named $filename and write it into the |
| 243 | # sqlite3.c output file. If any #include statements are seen, |
| 244 | # process them appropriately. |
| 245 | # |
| 246 | proc copy_file {filename} { |
| 247 | global seen_hdr available_hdr varonly_hdr cdecllist out |
| 248 | global addstatic linemacros useapicall srcdir |
| 249 | set ln 0 |
| 250 | set tail [file tail $filename] |
| 251 | section_comment "Begin file $tail" |
| 252 | if {$linemacros} {puts $out "#line 1 \"$filename\""} |
| 253 | set in [open $filename rb] |
| 254 | set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+(sqlite3[_a-zA-Z0-9]+)(\[|;| =)} |
| 255 | set declpattern {([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3[_a-zA-Z0-9]+)(\(.*)} |
| 256 | if {[file extension $filename]==".h"} { |
| 257 | set declpattern " *$declpattern" |
| 258 | } |
| 259 | set declpattern ^$declpattern\$ |
| 260 | while {![eof $in]} { |
| 261 | set line [string trimright [gets $in]] |
| 262 | incr ln |
| 263 | if {[regexp {^\s*#\s*include\s+["<]([^">]+)[">]} $line all hdr]} { |
| 264 | if {[info exists available_hdr($hdr)]} { |
| 265 | if {$available_hdr($hdr)} { |
| 266 | set available_hdr($hdr) 0 |
| 267 | section_comment "Include $hdr in the middle of $tail" |
| 268 | copy_file $srcdir/$hdr |
| 269 | section_comment "Continuing where we left off in $tail" |
| 270 | if {$linemacros} {puts $out "#line [expr {$ln+1}] \"$filename\""} |
| 271 | } else { |
| 272 | # Comment out the entire line, replacing any nested comment |
| 273 | # begin/end markers with the harmless substring "**". |
| 274 | puts $out "/* [string map [list /* ** */ **] $line] */" |
| 275 | } |
| 276 | } elseif {![info exists seen_hdr($hdr)]} { |
| 277 | if {![regexp {/\*\s+amalgamator:\s+dontcache\s+\*/} $line]} { |
| 278 | set seen_hdr($hdr) 1 |
| 279 | } |
| 280 | puts $out $line |
| 281 | } elseif {[regexp {/\*\s+amalgamator:\s+keep\s+\*/} $line]} { |
| 282 | # This include file must be kept because there was a "keep" |
| 283 | # directive inside of a line comment. |
| 284 | puts $out $line |
| 285 | } else { |
| 286 | # Comment out the entire line, replacing any nested comment |
| 287 | # begin/end markers with the harmless substring "**". |
| 288 | puts $out "/* [string map [list /* ** */ **] $line] */" |
| 289 | } |
| 290 | } elseif {[regexp {^#ifdef __cplusplus} $line]} { |
| 291 | puts $out "#if 0" |
| 292 | } elseif {!$linemacros && [regexp {^#line} $line]} { |
| 293 | # Skip #line directives. |
| 294 | } elseif {$addstatic |
| 295 | && ![regexp {^(static|typedef|SQLITE_PRIVATE)} $line]} { |
| 296 | # Skip adding the SQLITE_PRIVATE or SQLITE_API keyword before |
| 297 | # functions if this header file does not need it. |
| 298 | if {![info exists varonly_hdr($tail)] |
| 299 | && [regexp $declpattern $line all rettype funcname rest]} { |
| 300 | regsub {^SQLITE_API } $line {} line |
| 301 | regsub {^SQLITE_API } $rettype {} rettype |
| 302 | |
| 303 | # Add the SQLITE_PRIVATE or SQLITE_API keyword before functions. |
| 304 | # so that linkage can be modified at compile-time. |
| 305 | if {[regexp {^sqlite3[a-z]*_} $funcname]} { |
| 306 | set line SQLITE_API |
| 307 | append line " " [string trim $rettype] |
| 308 | if {[string index $rettype end] ne "*"} { |
| 309 | append line " " |
| 310 | } |
| 311 | if {$useapicall} { |
| 312 | if {[lsearch -exact $cdecllist $funcname] >= 0} { |
| 313 | append line SQLITE_CDECL " " |
| 314 | } else { |
| 315 | append line SQLITE_APICALL " " |
| 316 | } |
| 317 | } |
| 318 | append line $funcname $rest |
| 319 | if {$funcname=="sqlite3_sourceid"} { |
| 320 | # The sqlite3_sourceid() routine is synthesized at the end of |
| 321 | # the amalgamation |
| 322 | puts $out "/* $line */" |
| 323 | } else { |
| 324 | puts $out $line |
| 325 | } |
| 326 | } else { |
| 327 | puts $out "SQLITE_PRIVATE $line" |
| 328 | } |
| 329 | } elseif {[regexp $varpattern $line all varname]} { |
| 330 | # Add the SQLITE_PRIVATE before variable declarations or |
| 331 | # definitions for internal use |
| 332 | regsub {^SQLITE_API } $line {} line |
| 333 | if {![regexp {^sqlite3_} $varname] |
| 334 | && ![regexp {^sqlite3Show[A-Z]} $varname]} { |
| 335 | regsub {^extern } $line {} line |
| 336 | puts $out "SQLITE_PRIVATE $line" |
| 337 | } else { |
| 338 | if {[regexp {const char sqlite3_version\[\];} $line]} { |
| 339 | set line {const char sqlite3_version[] = SQLITE_VERSION;} |
| 340 | } |
| 341 | regsub {^SQLITE_EXTERN } $line {} line |
| 342 | puts $out "SQLITE_API $line" |
| 343 | } |
| 344 | } elseif {[regexp {^(SQLITE_EXTERN )?void \(\*sqlite3IoTrace\)} $line]} { |
| 345 | regsub {^SQLITE_API } $line {} line |
| 346 | regsub {^SQLITE_EXTERN } $line {} line |
| 347 | puts $out $line |
| 348 | } elseif {[regexp {^void \(\*sqlite3Os} $line]} { |
| 349 | regsub {^SQLITE_API } $line {} line |
| 350 | puts $out "SQLITE_PRIVATE $line" |
| 351 | } else { |
| 352 | puts $out $line |
| 353 | } |
| 354 | } else { |
| 355 | puts $out $line |
| 356 | } |
| 357 | } |
| 358 | close $in |
| 359 | section_comment "End of $tail" |
| 360 | } |
| 361 | |
| 362 | # Read the source file named $filename and write it into the |
| 363 | # sqlite3.c output file. The only transformation is the trimming |
| 364 | # of EOL whitespace. |
| 365 | # |
| 366 | proc copy_file_verbatim {filename} { |
| 367 | global out |
| 368 | set in [open $filename rb] |
| 369 | set tail [file tail $filename] |
| 370 | section_comment "Begin EXTRA_SRC file $tail" |
| 371 | while {![eof $in]} { |
| 372 | set line [string trimright [gets $in]] |
| 373 | puts $out $line |
| 374 | } |
| 375 | section_comment "End of EXTRA_SRC $tail" |
| 376 | } |
| 377 | |
| 378 | # Process the source files. Process files containing commonly |
| 379 | # used subroutines first in order to help the compiler find |
| 380 | # inlining opportunities. |
| 381 | # |
| 382 | set flist { |
| 383 | sqliteInt.h |
| 384 | os_common.h |
| 385 | ctime.c |
| 386 | |
| 387 | global.c |
| 388 | status.c |
| 389 | date.c |
| 390 | os.c |
| 391 | |
| 392 | fault.c |
| 393 | mem0.c |
| 394 | mem1.c |
| 395 | mem2.c |
| 396 | mem3.c |
| 397 | mem5.c |
| 398 | mutex.c |
| 399 | mutex_noop.c |
| 400 | mutex_unix.c |
| 401 | mutex_w32.c |
| 402 | malloc.c |
| 403 | printf.c |
| 404 | treeview.c |
| 405 | random.c |
| 406 | threads.c |
| 407 | utf.c |
| 408 | util.c |
| 409 | hash.c |
| 410 | opcodes.c |
| 411 | |
| 412 | os_kv.c |
| 413 | os_unix.c |
| 414 | os_win.c |
| 415 | memdb.c |
| 416 | |
| 417 | bitvec.c |
| 418 | pcache.c |
| 419 | pcache1.c |
| 420 | rowset.c |
| 421 | pager.c |
| 422 | wal.c |
| 423 | |
| 424 | btmutex.c |
| 425 | btree.c |
| 426 | backup.c |
| 427 | |
| 428 | vdbemem.c |
| 429 | vdbeaux.c |
| 430 | vdbeapi.c |
| 431 | vdbetrace.c |
| 432 | vdbe.c |
| 433 | vdbeblob.c |
| 434 | vdbesort.c |
| 435 | vdbevtab.c |
| 436 | memjournal.c |
| 437 | |
| 438 | walker.c |
| 439 | resolve.c |
| 440 | expr.c |
| 441 | alter.c |
| 442 | analyze.c |
| 443 | attach.c |
| 444 | auth.c |
| 445 | build.c |
| 446 | callback.c |
| 447 | delete.c |
| 448 | func.c |
| 449 | fkey.c |
| 450 | insert.c |
| 451 | legacy.c |
| 452 | loadext.c |
| 453 | pragma.c |
| 454 | prepare.c |
| 455 | select.c |
| 456 | table.c |
| 457 | trigger.c |
| 458 | update.c |
| 459 | upsert.c |
| 460 | vacuum.c |
| 461 | vtab.c |
| 462 | wherecode.c |
| 463 | whereexpr.c |
| 464 | where.c |
| 465 | window.c |
| 466 | |
| 467 | parse.c |
| 468 | |
| 469 | tokenize.c |
| 470 | complete.c |
| 471 | |
| 472 | main.c |
| 473 | notify.c |
| 474 | |
| 475 | fts3.c |
| 476 | fts3_aux.c |
| 477 | fts3_expr.c |
| 478 | fts3_hash.c |
| 479 | fts3_porter.c |
| 480 | fts3_tokenizer.c |
| 481 | fts3_tokenizer1.c |
| 482 | fts3_tokenize_vtab.c |
| 483 | fts3_write.c |
| 484 | fts3_snippet.c |
| 485 | fts3_unicode.c |
| 486 | fts3_unicode2.c |
| 487 | |
| 488 | json.c |
| 489 | rtree.c |
| 490 | icu.c |
| 491 | fts3_icu.c |
| 492 | sqlite3rbu.c |
| 493 | dbstat.c |
| 494 | dbpage.c |
| 495 | carray.c |
| 496 | sqlite3session.c |
| 497 | fts5.c |
| 498 | stmt.c |
| 499 | } |
| 500 | if {$enable_recover} { |
| 501 | lappend flist sqlite3recover.c dbdata.c |
| 502 | } |
| 503 | foreach file $flist { |
| 504 | copy_file $srcdir/$file |
| 505 | } |
| 506 | foreach file $extrasrc { |
| 507 | copy_file_verbatim $file |
| 508 | } |
| 509 | |
| 510 | puts $out \ |
| 511 | "/* Return the source-id for this library */ |
| 512 | SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }" |
| 513 | |
| 514 | puts $out \ |
| 515 | "#endif /* SQLITE_AMALGAMATION */ |
| 516 | /************************** End of sqlite3.c ******************************/" |
| 517 | |
| 518 | close $out |