blob: 9078a157535509e6c43bd7a859bf1ae21b1b170a [file] [log] [blame]
danielk19772d04deb2009-08-11 05:50:36 +00001#!/usr/bin/tclsh
2#
drh47baebc2009-08-14 16:01:24 +00003# 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.
mistachkin4ef916e2016-08-24 19:58:46 +000013#
danielk19772d04deb2009-08-11 05:50:36 +000014# 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,
mistachkin4ef916e2016-08-24 19:58:46 +000018# 3) Replaces the string --VERS-- with the current library version,
danielk19772d04deb2009-08-11 05:50:36 +000019# 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").
mistachkin4ef916e2016-08-24 19:58:46 +000022# 5) Replaces the string --SOURCE-ID-- with the date and time and sha1
drh47baebc2009-08-14 16:01:24 +000023# hash of the fossil-scm manifest for the source tree.
drh4194ff62016-07-28 15:09:02 +000024# 6) Adds the SQLITE_CALLBACK calling convention macro in front of all
25# callback declarations.
danielk19772d04deb2009-08-11 05:50:36 +000026#
drh47baebc2009-08-14 16:01:24 +000027# This script outputs to stdout.
danielk19772d04deb2009-08-11 05:50:36 +000028#
drh47baebc2009-08-14 16:01:24 +000029# Example usage:
30#
31# tclsh mksqlite3h.tcl ../sqlite >sqlite3.h
danielk19772d04deb2009-08-11 05:50:36 +000032#
33
drh47baebc2009-08-14 16:01:24 +000034
35# Get the source tree root directory from the command-line
36#
37set TOP [lindex $argv 0]
38
mistachkin4ef916e2016-08-24 19:58:46 +000039# Enable use of SQLITE_APICALL macros at the right points?
40#
41set useapicall 0
42
43if {[lsearch -regexp [lrange $argv 1 end] {^-+useapicall}] != -1} {
44 set useapicall 1
45}
46
drh47baebc2009-08-14 16:01:24 +000047# Get the SQLite version number (ex: 3.6.18) from the $TOP/VERSION file.
48#
49set in [open $TOP/VERSION]
50set zVersion [string trim [read $in]]
51close $in
danielk19772d04deb2009-08-11 05:50:36 +000052set nVersion [eval format "%d%03d%03d" [split $zVersion .]]
53
drhf39e0ed2017-08-22 19:19:00 +000054# Get the source-id
drh47baebc2009-08-14 16:01:24 +000055#
drha1722542017-08-22 19:43:41 +000056set PWD [pwd]
57cd $TOP
drhc9aed7f2017-08-22 19:49:34 +000058set zSourceId [exec $PWD/mksourceid manifest]
drha1722542017-08-22 19:43:41 +000059cd $PWD
drh47baebc2009-08-14 16:01:24 +000060
61# Set up patterns for recognizing API declarations.
62#
63set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+sqlite3_[_a-zA-Z0-9]+(\[|;| =)}
mistachkin953fbd12017-02-07 21:09:34 +000064set declpattern1 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3_[_a-zA-Z0-9]+)(\(.*)$}
65
66set declpattern2 \
67 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3session_[_a-zA-Z0-9]+)(\(.*)$}
68
69set declpattern3 \
70 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changeset_[_a-zA-Z0-9]+)(\(.*)$}
drh47baebc2009-08-14 16:01:24 +000071
mistachkinc8d3e832017-05-12 14:05:11 +000072set declpattern4 \
73 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changegroup_[_a-zA-Z0-9]+)(\(.*)$}
74
drhb873f4e2018-03-24 00:19:18 +000075set declpattern5 \
76 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3rebaser_[_a-zA-Z0-9]+)(\(.*)$}
77
shaneh5e0fb2c2011-06-17 15:54:59 +000078# Force the output to use unix line endings, even on Windows.
79fconfigure stdout -translation lf
80
drhe191e2c2013-03-19 15:23:18 +000081set filelist [subst {
82 $TOP/src/sqlite.h.in
83 $TOP/ext/rtree/sqlite3rtree.h
84 $TOP/ext/session/sqlite3session.h
drh50065652015-10-08 19:29:18 +000085 $TOP/ext/fts5/fts5.h
drhe191e2c2013-03-19 15:23:18 +000086}]
87
mistachkin59b9b022015-03-24 21:27:27 +000088# 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.
91set 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
drhe191e2c2013-03-19 15:23:18 +0000101# Process the source files.
drh47baebc2009-08-14 16:01:24 +0000102#
drhe191e2c2013-03-19 15:23:18 +0000103foreach file $filelist {
danc223b8f2010-08-30 18:39:49 +0000104 set in [open $file]
mistachkin73c40412013-04-11 00:45:28 +0000105 if {![regexp {sqlite\.h\.in} $file]} {
drheeeee7f2013-04-10 15:01:36 +0000106 puts "/******** Begin file [file tail $file] *********/"
107 }
danc223b8f2010-08-30 18:39:49 +0000108 while {![eof $in]} {
mistachkin4ef916e2016-08-24 19:58:46 +0000109
drh24aee8a2020-06-23 17:57:08 +0000110 set line [string trimright [gets $in]]
drh47baebc2009-08-14 16:01:24 +0000111
danc223b8f2010-08-30 18:39:49 +0000112 # File sqlite3rtree.h contains a line "#include <sqlite3.h>". Omit this
113 # line when copying sqlite3rtree.h into sqlite3.h.
114 #
drh0b086402015-10-16 15:56:27 +0000115 if {[string match {*#include*[<"]sqlite3.h[>"]*} $line]} continue
mistachkin4ef916e2016-08-24 19:58:46 +0000116
danc223b8f2010-08-30 18:39:49 +0000117 regsub -- --VERS-- $line $zVersion line
118 regsub -- --VERSION-NUMBER-- $line $nVersion line
drhf39e0ed2017-08-22 19:19:00 +0000119 regsub -- --SOURCE-ID-- $line "$zSourceId" line
drh790fa6e2015-03-24 21:54:42 +0000120
mistachkin59b9b022015-03-24 21:27:27 +0000121 if {[regexp $varpattern $line] && ![regexp {^ *typedef} $line]} {
danc223b8f2010-08-30 18:39:49 +0000122 set line "SQLITE_API $line"
mistachkin59b9b022015-03-24 21:27:27 +0000123 } else {
mistachkin953fbd12017-02-07 21:09:34 +0000124 if {[regexp $declpattern1 $line all rettype funcname rest] || \
125 [regexp $declpattern2 $line all rettype funcname rest] || \
mistachkinc8d3e832017-05-12 14:05:11 +0000126 [regexp $declpattern3 $line all rettype funcname rest] || \
drhb873f4e2018-03-24 00:19:18 +0000127 [regexp $declpattern4 $line all rettype funcname rest] || \
128 [regexp $declpattern5 $line all rettype funcname rest]} {
mistachkin59b9b022015-03-24 21:27:27 +0000129 set line SQLITE_API
130 append line " " [string trim $rettype]
131 if {[string index $rettype end] ne "*"} {
132 append line " "
133 }
mistachkin4ef916e2016-08-24 19:58:46 +0000134 if {$useapicall} {
135 if {[lsearch -exact $cdecllist $funcname] >= 0} {
mistachkin0e4125a2016-09-08 23:16:02 +0000136 append line SQLITE_CDECL " "
mistachkin4ef916e2016-08-24 19:58:46 +0000137 } else {
mistachkin0e4125a2016-09-08 23:16:02 +0000138 append line SQLITE_APICALL " "
mistachkin4ef916e2016-08-24 19:58:46 +0000139 }
mistachkin59b9b022015-03-24 21:27:27 +0000140 }
mistachkin0e4125a2016-09-08 23:16:02 +0000141 append line $funcname $rest
mistachkin59b9b022015-03-24 21:27:27 +0000142 }
danc223b8f2010-08-30 18:39:49 +0000143 }
mistachkin4ef916e2016-08-24 19:58:46 +0000144 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 }
danielk19772d04deb2009-08-11 05:50:36 +0000149 puts $line
danielk19772d04deb2009-08-11 05:50:36 +0000150 }
danc223b8f2010-08-30 18:39:49 +0000151 close $in
mistachkin73c40412013-04-11 00:45:28 +0000152 if {![regexp {sqlite\.h\.in} $file]} {
drheeeee7f2013-04-10 15:01:36 +0000153 puts "/******** End of [file tail $file] *********/"
154 }
danielk19772d04deb2009-08-11 05:50:36 +0000155}