xref: /sqlite-3.40.0/ext/fts5/tool/mkfts5c.tcl (revision 20de9f6c)
1#!/bin/sh
2# restart with tclsh \
3exec tclsh "$0" "$@"
4
5set srcdir [file dirname [file dirname [info script]]]
6set G(src) [string map [list %dir% $srcdir] {
7  %dir%/fts5.h
8  %dir%/fts5Int.h
9  fts5parse.h
10  fts5parse.c
11  %dir%/fts5_aux.c
12  %dir%/fts5_buffer.c
13  %dir%/fts5_config.c
14  %dir%/fts5_expr.c
15  %dir%/fts5_hash.c
16  %dir%/fts5_index.c
17  %dir%/fts5_main.c
18  %dir%/fts5_storage.c
19  %dir%/fts5_tokenize.c
20  %dir%/fts5_unicode2.c
21  %dir%/fts5_varint.c
22  %dir%/fts5_vocab.c
23}]
24
25set G(hdr) {
26
27#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5)
28
29#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
30# define NDEBUG 1
31#endif
32#if defined(NDEBUG) && defined(SQLITE_DEBUG)
33# undef NDEBUG
34#endif
35
36}
37
38set G(footer) {
39
40#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS5) */
41}
42
43#-------------------------------------------------------------------------
44# Read and return the entire contents of text file $zFile from disk.
45#
46proc readfile {zFile} {
47  set fd [open $zFile]
48  set data [read $fd]
49  close $fd
50  return $data
51}
52
53#-------------------------------------------------------------------------
54# This command returns a string identifying the current sqlite version -
55# the equivalent of the SQLITE_SOURCE_ID string.
56#
57proc fts5_source_id {zDir} {
58  set top [file dirname [file dirname $zDir]]
59  set uuid [string trim [readfile [file join $top manifest.uuid]]]
60
61  set L [split [readfile [file join $top manifest]]]
62  set date [lindex $L [expr [lsearch -exact $L D]+1]]
63  set idx [expr {[string last . $date]-1}]
64  set date [string range $date 0 $idx]
65  set date [string map {T { }} $date]
66
67  return "fts5: $date $uuid"
68}
69
70proc fts5c_init {zOut} {
71  global G
72  set G(fd) stdout
73  set G(fd) [open $zOut w]
74
75  puts -nonewline $G(fd) $G(hdr)
76}
77
78proc fts5c_printfile {zIn} {
79  global G
80  set data [readfile $zIn]
81  set zTail [file tail $zIn]
82  puts $G(fd) "#line 1 \"$zTail\""
83
84  set sub_map [list --FTS5-SOURCE-ID-- [fts5_source_id $::srcdir]]
85  if {$zTail=="fts5parse.c"} {
86    lappend sub_map yy fts5yy YY fts5YY TOKEN FTS5TOKEN
87  }
88
89  foreach line [split $data "\n"] {
90    if {[regexp {^#include.*fts5} $line]} {
91      set line "/* $line */"
92    } elseif {
93         ![regexp { sqlite3Fts5Init\(} $line]
94       && [regexp {^(const )?[a-zA-Z][a-zA-Z0-9]* [*]?sqlite3Fts5} $line]
95    } {
96      set line "static $line"
97    }
98    set line [string map $sub_map $line]
99    puts $G(fd) $line
100  }
101}
102
103proc fts5c_close {} {
104  global G
105  puts -nonewline $G(fd) $G(footer)
106  if {$G(fd)!="stdout"} {
107    close $G(fd)
108  }
109}
110
111
112fts5c_init fts5.c
113foreach f $G(src) { fts5c_printfile $f }
114fts5c_close
115