danielk1977 | 2d04deb | 2009-08-11 05:50:36 +0000 | [diff] [blame] | 1 | #!/usr/bin/tclsh |
| 2 | # |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 3 | # This script constructs the "sqlite3.h" header file from the following |
| 4 | # sources: |
| 5 | # |
| 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. |
| 10 | # |
| 11 | # Run this script by specifying the root directory of the source tree |
| 12 | # on the command-line. |
mistachkin | 4ef916e | 2016-08-24 19:58:46 +0000 | [diff] [blame] | 13 | # |
danielk1977 | 2d04deb | 2009-08-11 05:50:36 +0000 | [diff] [blame] | 14 | # This script performs processing on src/sqlite.h.in. It: |
| 15 | # |
| 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, |
mistachkin | 4ef916e | 2016-08-24 19:58:46 +0000 | [diff] [blame] | 18 | # 3) Replaces the string --VERS-- with the current library version, |
danielk1977 | 2d04deb | 2009-08-11 05:50:36 +0000 | [diff] [blame] | 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"). |
mistachkin | 4ef916e | 2016-08-24 19:58:46 +0000 | [diff] [blame] | 22 | # 5) Replaces the string --SOURCE-ID-- with the date and time and sha1 |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 23 | # hash of the fossil-scm manifest for the source tree. |
drh | 4194ff6 | 2016-07-28 15:09:02 +0000 | [diff] [blame] | 24 | # 6) Adds the SQLITE_CALLBACK calling convention macro in front of all |
| 25 | # callback declarations. |
danielk1977 | 2d04deb | 2009-08-11 05:50:36 +0000 | [diff] [blame] | 26 | # |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 27 | # This script outputs to stdout. |
danielk1977 | 2d04deb | 2009-08-11 05:50:36 +0000 | [diff] [blame] | 28 | # |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 29 | # Example usage: |
| 30 | # |
| 31 | # tclsh mksqlite3h.tcl ../sqlite >sqlite3.h |
danielk1977 | 2d04deb | 2009-08-11 05:50:36 +0000 | [diff] [blame] | 32 | # |
| 33 | |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 34 | |
| 35 | # Get the source tree root directory from the command-line |
| 36 | # |
| 37 | set TOP [lindex $argv 0] |
| 38 | |
mistachkin | 4ef916e | 2016-08-24 19:58:46 +0000 | [diff] [blame] | 39 | # Enable use of SQLITE_APICALL macros at the right points? |
| 40 | # |
| 41 | set useapicall 0 |
| 42 | |
| 43 | if {[lsearch -regexp [lrange $argv 1 end] {^-+useapicall}] != -1} { |
| 44 | set useapicall 1 |
| 45 | } |
| 46 | |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 47 | # Get the SQLite version number (ex: 3.6.18) from the $TOP/VERSION file. |
| 48 | # |
| 49 | set in [open $TOP/VERSION] |
| 50 | set zVersion [string trim [read $in]] |
| 51 | close $in |
danielk1977 | 2d04deb | 2009-08-11 05:50:36 +0000 | [diff] [blame] | 52 | set nVersion [eval format "%d%03d%03d" [split $zVersion .]] |
| 53 | |
drh | f39e0ed | 2017-08-22 19:19:00 +0000 | [diff] [blame] | 54 | # Get the source-id |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 55 | # |
drh | a172254 | 2017-08-22 19:43:41 +0000 | [diff] [blame] | 56 | set PWD [pwd] |
| 57 | cd $TOP |
drh | c9aed7f | 2017-08-22 19:49:34 +0000 | [diff] [blame] | 58 | set zSourceId [exec $PWD/mksourceid manifest] |
drh | a172254 | 2017-08-22 19:43:41 +0000 | [diff] [blame] | 59 | cd $PWD |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 60 | |
| 61 | # Set up patterns for recognizing API declarations. |
| 62 | # |
| 63 | set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+sqlite3_[_a-zA-Z0-9]+(\[|;| =)} |
mistachkin | 953fbd1 | 2017-02-07 21:09:34 +0000 | [diff] [blame] | 64 | set declpattern1 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3_[_a-zA-Z0-9]+)(\(.*)$} |
| 65 | |
| 66 | set declpattern2 \ |
| 67 | {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3session_[_a-zA-Z0-9]+)(\(.*)$} |
| 68 | |
| 69 | set declpattern3 \ |
| 70 | {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changeset_[_a-zA-Z0-9]+)(\(.*)$} |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 71 | |
mistachkin | c8d3e83 | 2017-05-12 14:05:11 +0000 | [diff] [blame] | 72 | set declpattern4 \ |
| 73 | {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changegroup_[_a-zA-Z0-9]+)(\(.*)$} |
| 74 | |
drh | b873f4e | 2018-03-24 00:19:18 +0000 | [diff] [blame] | 75 | set declpattern5 \ |
| 76 | {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3rebaser_[_a-zA-Z0-9]+)(\(.*)$} |
| 77 | |
shaneh | 5e0fb2c | 2011-06-17 15:54:59 +0000 | [diff] [blame] | 78 | # Force the output to use unix line endings, even on Windows. |
| 79 | fconfigure stdout -translation lf |
| 80 | |
drh | e191e2c | 2013-03-19 15:23:18 +0000 | [diff] [blame] | 81 | set filelist [subst { |
| 82 | $TOP/src/sqlite.h.in |
| 83 | $TOP/ext/rtree/sqlite3rtree.h |
| 84 | $TOP/ext/session/sqlite3session.h |
drh | 5006565 | 2015-10-08 19:29:18 +0000 | [diff] [blame] | 85 | $TOP/ext/fts5/fts5.h |
drh | e191e2c | 2013-03-19 15:23:18 +0000 | [diff] [blame] | 86 | }] |
| 87 | |
mistachkin | 59b9b02 | 2015-03-24 21:27:27 +0000 | [diff] [blame] | 88 | # These are the functions that accept a variable number of arguments. They |
| 89 | # always need to use the "cdecl" calling convention even when another calling |
| 90 | # convention (e.g. "stcall") is being used for the rest of the library. |
| 91 | set cdecllist { |
| 92 | sqlite3_config |
| 93 | sqlite3_db_config |
| 94 | sqlite3_log |
| 95 | sqlite3_mprintf |
| 96 | sqlite3_snprintf |
| 97 | sqlite3_test_control |
| 98 | sqlite3_vtab_config |
| 99 | } |
| 100 | |
drh | e191e2c | 2013-03-19 15:23:18 +0000 | [diff] [blame] | 101 | # Process the source files. |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 102 | # |
drh | e191e2c | 2013-03-19 15:23:18 +0000 | [diff] [blame] | 103 | foreach file $filelist { |
dan | c223b8f | 2010-08-30 18:39:49 +0000 | [diff] [blame] | 104 | set in [open $file] |
mistachkin | 73c4041 | 2013-04-11 00:45:28 +0000 | [diff] [blame] | 105 | if {![regexp {sqlite\.h\.in} $file]} { |
drh | eeeee7f | 2013-04-10 15:01:36 +0000 | [diff] [blame] | 106 | puts "/******** Begin file [file tail $file] *********/" |
| 107 | } |
dan | c223b8f | 2010-08-30 18:39:49 +0000 | [diff] [blame] | 108 | while {![eof $in]} { |
mistachkin | 4ef916e | 2016-08-24 19:58:46 +0000 | [diff] [blame] | 109 | |
drh | 24aee8a | 2020-06-23 17:57:08 +0000 | [diff] [blame] | 110 | set line [string trimright [gets $in]] |
drh | 47baebc | 2009-08-14 16:01:24 +0000 | [diff] [blame] | 111 | |
dan | c223b8f | 2010-08-30 18:39:49 +0000 | [diff] [blame] | 112 | # File sqlite3rtree.h contains a line "#include <sqlite3.h>". Omit this |
| 113 | # line when copying sqlite3rtree.h into sqlite3.h. |
| 114 | # |
drh | 0b08640 | 2015-10-16 15:56:27 +0000 | [diff] [blame] | 115 | if {[string match {*#include*[<"]sqlite3.h[>"]*} $line]} continue |
mistachkin | 4ef916e | 2016-08-24 19:58:46 +0000 | [diff] [blame] | 116 | |
dan | c223b8f | 2010-08-30 18:39:49 +0000 | [diff] [blame] | 117 | regsub -- --VERS-- $line $zVersion line |
| 118 | regsub -- --VERSION-NUMBER-- $line $nVersion line |
drh | f39e0ed | 2017-08-22 19:19:00 +0000 | [diff] [blame] | 119 | regsub -- --SOURCE-ID-- $line "$zSourceId" line |
drh | 790fa6e | 2015-03-24 21:54:42 +0000 | [diff] [blame] | 120 | |
mistachkin | 59b9b02 | 2015-03-24 21:27:27 +0000 | [diff] [blame] | 121 | if {[regexp $varpattern $line] && ![regexp {^ *typedef} $line]} { |
dan | c223b8f | 2010-08-30 18:39:49 +0000 | [diff] [blame] | 122 | set line "SQLITE_API $line" |
mistachkin | 59b9b02 | 2015-03-24 21:27:27 +0000 | [diff] [blame] | 123 | } else { |
mistachkin | 953fbd1 | 2017-02-07 21:09:34 +0000 | [diff] [blame] | 124 | if {[regexp $declpattern1 $line all rettype funcname rest] || \ |
| 125 | [regexp $declpattern2 $line all rettype funcname rest] || \ |
mistachkin | c8d3e83 | 2017-05-12 14:05:11 +0000 | [diff] [blame] | 126 | [regexp $declpattern3 $line all rettype funcname rest] || \ |
drh | b873f4e | 2018-03-24 00:19:18 +0000 | [diff] [blame] | 127 | [regexp $declpattern4 $line all rettype funcname rest] || \ |
| 128 | [regexp $declpattern5 $line all rettype funcname rest]} { |
mistachkin | 59b9b02 | 2015-03-24 21:27:27 +0000 | [diff] [blame] | 129 | set line SQLITE_API |
| 130 | append line " " [string trim $rettype] |
| 131 | if {[string index $rettype end] ne "*"} { |
| 132 | append line " " |
| 133 | } |
mistachkin | 4ef916e | 2016-08-24 19:58:46 +0000 | [diff] [blame] | 134 | if {$useapicall} { |
| 135 | if {[lsearch -exact $cdecllist $funcname] >= 0} { |
mistachkin | 0e4125a | 2016-09-08 23:16:02 +0000 | [diff] [blame] | 136 | append line SQLITE_CDECL " " |
mistachkin | 4ef916e | 2016-08-24 19:58:46 +0000 | [diff] [blame] | 137 | } else { |
mistachkin | 0e4125a | 2016-09-08 23:16:02 +0000 | [diff] [blame] | 138 | append line SQLITE_APICALL " " |
mistachkin | 4ef916e | 2016-08-24 19:58:46 +0000 | [diff] [blame] | 139 | } |
mistachkin | 59b9b02 | 2015-03-24 21:27:27 +0000 | [diff] [blame] | 140 | } |
mistachkin | 0e4125a | 2016-09-08 23:16:02 +0000 | [diff] [blame] | 141 | append line $funcname $rest |
mistachkin | 59b9b02 | 2015-03-24 21:27:27 +0000 | [diff] [blame] | 142 | } |
dan | c223b8f | 2010-08-30 18:39:49 +0000 | [diff] [blame] | 143 | } |
mistachkin | 4ef916e | 2016-08-24 19:58:46 +0000 | [diff] [blame] | 144 | if {$useapicall} { |
| 145 | set line [string map [list (*sqlite3_syscall_ptr) \ |
| 146 | "(SQLITE_SYSAPI *sqlite3_syscall_ptr)"] $line] |
| 147 | regsub {\(\*} $line {(SQLITE_CALLBACK *} line |
| 148 | } |
danielk1977 | 2d04deb | 2009-08-11 05:50:36 +0000 | [diff] [blame] | 149 | puts $line |
danielk1977 | 2d04deb | 2009-08-11 05:50:36 +0000 | [diff] [blame] | 150 | } |
dan | c223b8f | 2010-08-30 18:39:49 +0000 | [diff] [blame] | 151 | close $in |
mistachkin | 73c4041 | 2013-04-11 00:45:28 +0000 | [diff] [blame] | 152 | if {![regexp {sqlite\.h\.in} $file]} { |
drh | eeeee7f | 2013-04-10 15:01:36 +0000 | [diff] [blame] | 153 | puts "/******** End of [file tail $file] *********/" |
| 154 | } |
danielk1977 | 2d04deb | 2009-08-11 05:50:36 +0000 | [diff] [blame] | 155 | } |