mksqlite3h.tcl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/tclsh
  2. #
  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.
  13. #
  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,
  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.
  26. #
  27. # This script outputs to stdout.
  28. #
  29. # Example usage:
  30. #
  31. # tclsh mksqlite3h.tcl ../sqlite >sqlite3.h
  32. #
  33. # Get the source tree root directory from the command-line
  34. #
  35. set TOP [lindex $argv 0]
  36. # Enable use of SQLITE_APICALL macros at the right points?
  37. #
  38. set useapicall 0
  39. # Include sqlite3recover.h?
  40. #
  41. set enable_recover 0
  42. if {[lsearch -regexp [lrange $argv 1 end] {^-+useapicall}] != -1} {
  43. set useapicall 1
  44. }
  45. if {[lsearch -regexp [lrange $argv 1 end] {^-+enable-recover}] != -1} {
  46. set enable_recover 1
  47. }
  48. # Get the SQLite version number (ex: 3.6.18) from the $TOP/VERSION file.
  49. #
  50. set in [open [file normalize $TOP/VERSION] rb]
  51. set zVersion [string trim [read $in]]
  52. close $in
  53. set nVersion [eval format "%d%03d%03d" [split $zVersion .]]
  54. # Get the source-id
  55. #
  56. set PWD [pwd]
  57. cd $TOP
  58. set tmpfile tmp-[clock millisec]-[expr {int(rand()*100000000000)}].txt
  59. exec $PWD/mksourceid manifest > $tmpfile
  60. set fd [open $tmpfile rb]
  61. set zSourceId [string trim [read $fd]]
  62. close $fd
  63. file delete -force $tmpfile
  64. cd $PWD
  65. # Set up patterns for recognizing API declarations.
  66. #
  67. set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+sqlite3_[_a-zA-Z0-9]+(\[|;| =)}
  68. set declpattern1 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3_[_a-zA-Z0-9]+)(\(.*)$}
  69. set declpattern2 \
  70. {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3session_[_a-zA-Z0-9]+)(\(.*)$}
  71. set declpattern3 \
  72. {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changeset_[_a-zA-Z0-9]+)(\(.*)$}
  73. set declpattern4 \
  74. {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changegroup_[_a-zA-Z0-9]+)(\(.*)$}
  75. set declpattern5 \
  76. {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3rebaser_[_a-zA-Z0-9]+)(\(.*)$}
  77. # Force the output to use unix line endings, even on Windows.
  78. fconfigure stdout -translation lf
  79. set filelist [subst {
  80. $TOP/src/sqlite.h.in
  81. $TOP/ext/rtree/sqlite3rtree.h
  82. $TOP/ext/session/sqlite3session.h
  83. $TOP/ext/fts5/fts5.h
  84. }]
  85. if {$enable_recover} {
  86. lappend filelist "$TOP/ext/recover/sqlite3recover.h"
  87. }
  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. # Process the source files.
  101. #
  102. foreach file $filelist {
  103. set in [open $file rb]
  104. if {![regexp {sqlite\.h\.in} $file]} {
  105. puts "/******** Begin file [file tail $file] *********/"
  106. }
  107. while {![eof $in]} {
  108. set line [string trimright [gets $in]]
  109. # File sqlite3rtree.h contains a line "#include <sqlite3.h>". Omit this
  110. # line when copying sqlite3rtree.h into sqlite3.h.
  111. #
  112. if {[string match {*#include*[<"]sqlite3.h[>"]*} $line]} continue
  113. regsub -- --VERS-- $line $zVersion line
  114. regsub -- --VERSION-NUMBER-- $line $nVersion line
  115. regsub -- --SOURCE-ID-- $line "$zSourceId" line
  116. if {[regexp $varpattern $line] && ![regexp {^ *typedef} $line]} {
  117. set line "SQLITE_API $line"
  118. } else {
  119. if {[regexp $declpattern1 $line all rettype funcname rest] || \
  120. [regexp $declpattern2 $line all rettype funcname rest] || \
  121. [regexp $declpattern3 $line all rettype funcname rest] || \
  122. [regexp $declpattern4 $line all rettype funcname rest] || \
  123. [regexp $declpattern5 $line all rettype funcname rest]} {
  124. set line SQLITE_API
  125. append line " " [string trim $rettype]
  126. if {[string index $rettype end] ne "*"} {
  127. append line " "
  128. }
  129. if {$useapicall} {
  130. if {[lsearch -exact $cdecllist $funcname] >= 0} {
  131. append line SQLITE_CDECL " "
  132. } else {
  133. append line SQLITE_APICALL " "
  134. }
  135. }
  136. append line $funcname $rest
  137. }
  138. }
  139. if {$useapicall} {
  140. set line [string map [list (*sqlite3_syscall_ptr) \
  141. "(SQLITE_SYSAPI *sqlite3_syscall_ptr)"] $line]
  142. regsub {\(\*} $line {(SQLITE_CALLBACK *} line
  143. }
  144. puts $line
  145. }
  146. close $in
  147. if {![regexp {sqlite\.h\.in} $file]} {
  148. puts "/******** End of [file tail $file] *********/"
  149. }
  150. }
  151. puts "#endif /* SQLITE3_H */"