establish default sqlcipher log level and target upon first activation
[sqlcipher.git] / tool / mksqlite3h.tcl
blobbd579c28b0ee7df128adcc012ae7cd43f7891d97
1 #!/usr/bin/tclsh
3 # This script constructs the "sqlite3.h" header file from the following
4 # sources:
6 # 1) The src/sqlite.h.in source file. This is the template for sqlite3.h.
7 # 2) The VERSION file containing the current SQLite version number.
8 # 3) The manifest file from the fossil SCM. This gives use the date.
9 # 4) The manifest.uuid file from the fossil SCM. This gives the SHA1 hash.
11 # Run this script by specifying the root directory of the source tree
12 # on the command-line.
14 # This script performs processing on src/sqlite.h.in. It:
16 # 1) Adds SQLITE_EXTERN in front of the declaration of global variables,
17 # 2) Adds SQLITE_API in front of the declaration of API functions,
18 # 3) Replaces the string --VERS-- with the current library version,
19 # formatted as a string (e.g. "3.6.17"), and
20 # 4) Replaces the string --VERSION-NUMBER-- with current library version,
21 # formatted as an integer (e.g. "3006017").
22 # 5) Replaces the string --SOURCE-ID-- with the date and time and sha1
23 # hash of the fossil-scm manifest for the source tree.
24 # 6) Adds the SQLITE_CALLBACK calling convention macro in front of all
25 # callback declarations.
27 # This script outputs to stdout.
29 # Example usage:
31 # tclsh mksqlite3h.tcl ../sqlite >sqlite3.h
35 # Get the source tree root directory from the command-line
37 set TOP [lindex $argv 0]
39 # Enable use of SQLITE_APICALL macros at the right points?
41 set useapicall 0
43 # Include sqlite3recover.h?
45 set enable_recover 0
47 if {[lsearch -regexp [lrange $argv 1 end] {^-+useapicall}] != -1} {
48 set useapicall 1
50 if {[lsearch -regexp [lrange $argv 1 end] {^-+enable-recover}] != -1} {
51 set enable_recover 1
54 # Get the SQLite version number (ex: 3.6.18) from the $TOP/VERSION file.
56 set in [open $TOP/VERSION]
57 set zVersion [string trim [read $in]]
58 close $in
59 set nVersion [eval format "%d%03d%03d" [split $zVersion .]]
61 # Get the source-id
63 set PWD [pwd]
64 cd $TOP
65 set zSourceId [exec $PWD/mksourceid manifest]
66 cd $PWD
68 # Set up patterns for recognizing API declarations.
70 set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+sqlite3_[_a-zA-Z0-9]+(\[|;| =)}
71 set declpattern1 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3_[_a-zA-Z0-9]+)(\(.*)$}
73 set declpattern2 \
74 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3session_[_a-zA-Z0-9]+)(\(.*)$}
76 set declpattern3 \
77 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changeset_[_a-zA-Z0-9]+)(\(.*)$}
79 set declpattern4 \
80 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changegroup_[_a-zA-Z0-9]+)(\(.*)$}
82 set declpattern5 \
83 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3rebaser_[_a-zA-Z0-9]+)(\(.*)$}
85 # Force the output to use unix line endings, even on Windows.
86 fconfigure stdout -translation lf
88 set filelist [subst {
89 $TOP/src/sqlite.h.in
90 $TOP/ext/rtree/sqlite3rtree.h
91 $TOP/ext/session/sqlite3session.h
92 $TOP/ext/fts5/fts5.h
94 if {$enable_recover} {
95 lappend filelist "$TOP/ext/recover/sqlite3recover.h"
98 # These are the functions that accept a variable number of arguments. They
99 # always need to use the "cdecl" calling convention even when another calling
100 # convention (e.g. "stcall") is being used for the rest of the library.
101 set cdecllist {
102 sqlite3_config
103 sqlite3_db_config
104 sqlite3_log
105 sqlite3_mprintf
106 sqlite3_snprintf
107 sqlite3_test_control
108 sqlite3_vtab_config
111 # Process the source files.
113 foreach file $filelist {
114 set in [open $file]
115 if {![regexp {sqlite\.h\.in} $file]} {
116 puts "/******** Begin file [file tail $file] *********/"
118 while {![eof $in]} {
120 set line [string trimright [gets $in]]
122 # File sqlite3rtree.h contains a line "#include <sqlite3.h>". Omit this
123 # line when copying sqlite3rtree.h into sqlite3.h.
125 if {[string match {*#include*[<"]sqlite3.h[>"]*} $line]} continue
127 regsub -- --VERS-- $line $zVersion line
128 regsub -- --VERSION-NUMBER-- $line $nVersion line
129 regsub -- --SOURCE-ID-- $line "$zSourceId" line
131 if {[regexp $varpattern $line] && ![regexp {^ *typedef} $line]} {
132 set line "SQLITE_API $line"
133 } else {
134 if {[regexp $declpattern1 $line all rettype funcname rest] || \
135 [regexp $declpattern2 $line all rettype funcname rest] || \
136 [regexp $declpattern3 $line all rettype funcname rest] || \
137 [regexp $declpattern4 $line all rettype funcname rest] || \
138 [regexp $declpattern5 $line all rettype funcname rest]} {
139 set line SQLITE_API
140 append line " " [string trim $rettype]
141 if {[string index $rettype end] ne "*"} {
142 append line " "
144 if {$useapicall} {
145 if {[lsearch -exact $cdecllist $funcname] >= 0} {
146 append line SQLITE_CDECL " "
147 } else {
148 append line SQLITE_APICALL " "
151 append line $funcname $rest
154 if {$useapicall} {
155 set line [string map [list (*sqlite3_syscall_ptr) \
156 "(SQLITE_SYSAPI *sqlite3_syscall_ptr)"] $line]
157 regsub {\(\*} $line {(SQLITE_CALLBACK *} line
159 puts $line
161 close $in
162 if {![regexp {sqlite\.h\.in} $file]} {
163 puts "/******** End of [file tail $file] *********/"