mksqlite3h.tcl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 $TOP/VERSION]
  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 zSourceId [exec $PWD/mksourceid manifest]
  59. cd $PWD
  60. # Set up patterns for recognizing API declarations.
  61. #
  62. set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+sqlite3_[_a-zA-Z0-9]+(\[|;| =)}
  63. set declpattern1 {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3_[_a-zA-Z0-9]+)(\(.*)$}
  64. set declpattern2 \
  65. {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3session_[_a-zA-Z0-9]+)(\(.*)$}
  66. set declpattern3 \
  67. {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changeset_[_a-zA-Z0-9]+)(\(.*)$}
  68. set declpattern4 \
  69. {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3changegroup_[_a-zA-Z0-9]+)(\(.*)$}
  70. set declpattern5 \
  71. {^ *([a-zA-Z][a-zA-Z_0-9 ]+ \**)(sqlite3rebaser_[_a-zA-Z0-9]+)(\(.*)$}
  72. # Force the output to use unix line endings, even on Windows.
  73. fconfigure stdout -translation lf
  74. set filelist [subst {
  75. $TOP/src/sqlite.h.in
  76. $TOP/ext/rtree/sqlite3rtree.h
  77. $TOP/ext/session/sqlite3session.h
  78. $TOP/ext/fts5/fts5.h
  79. }]
  80. if {$enable_recover} {
  81. lappend filelist "$TOP/ext/recover/sqlite3recover.h"
  82. }
  83. # These are the functions that accept a variable number of arguments. They
  84. # always need to use the "cdecl" calling convention even when another calling
  85. # convention (e.g. "stcall") is being used for the rest of the library.
  86. set cdecllist {
  87. sqlite3_config
  88. sqlite3_db_config
  89. sqlite3_log
  90. sqlite3_mprintf
  91. sqlite3_snprintf
  92. sqlite3_test_control
  93. sqlite3_vtab_config
  94. }
  95. # Process the source files.
  96. #
  97. foreach file $filelist {
  98. set in [open $file]
  99. if {![regexp {sqlite\.h\.in} $file]} {
  100. puts "/******** Begin file [file tail $file] *********/"
  101. }
  102. while {![eof $in]} {
  103. set line [string trimright [gets $in]]
  104. # File sqlite3rtree.h contains a line "#include <sqlite3.h>". Omit this
  105. # line when copying sqlite3rtree.h into sqlite3.h.
  106. #
  107. if {[string match {*#include*[<"]sqlite3.h[>"]*} $line]} continue
  108. regsub -- --VERS-- $line $zVersion line
  109. regsub -- --VERSION-NUMBER-- $line $nVersion line
  110. regsub -- --SOURCE-ID-- $line "$zSourceId" line
  111. if {[regexp $varpattern $line] && ![regexp {^ *typedef} $line]} {
  112. set line "SQLITE_API $line"
  113. } else {
  114. if {[regexp $declpattern1 $line all rettype funcname rest] || \
  115. [regexp $declpattern2 $line all rettype funcname rest] || \
  116. [regexp $declpattern3 $line all rettype funcname rest] || \
  117. [regexp $declpattern4 $line all rettype funcname rest] || \
  118. [regexp $declpattern5 $line all rettype funcname rest]} {
  119. set line SQLITE_API
  120. append line " " [string trim $rettype]
  121. if {[string index $rettype end] ne "*"} {
  122. append line " "
  123. }
  124. if {$useapicall} {
  125. if {[lsearch -exact $cdecllist $funcname] >= 0} {
  126. append line SQLITE_CDECL " "
  127. } else {
  128. append line SQLITE_APICALL " "
  129. }
  130. }
  131. append line $funcname $rest
  132. }
  133. }
  134. if {$useapicall} {
  135. set line [string map [list (*sqlite3_syscall_ptr) \
  136. "(SQLITE_SYSAPI *sqlite3_syscall_ptr)"] $line]
  137. regsub {\(\*} $line {(SQLITE_CALLBACK *} line
  138. }
  139. puts $line
  140. }
  141. close $in
  142. if {![regexp {sqlite\.h\.in} $file]} {
  143. puts "/******** End of [file tail $file] *********/"
  144. }
  145. }