Merge branch 'sqlite-release' into prerelease-integration
[sqlcipher.git] / tool / mksqlite3c.tcl
blob2e059b0c7b373949ae19324844eb28a3d7c67fee
1 #!/usr/bin/tclsh
3 # To build a single huge source file holding all of SQLite (or at
4 # least the core components - the test harness, shell, and TCL
5 # interface are omitted.) first do
7 # make target_source
9 # The make target above moves all of the source code files into
10 # a subdirectory named "tsrc". (This script expects to find the files
11 # there and will not work if they are not found.) There are a few
12 # generated C code files that are also added to the tsrc directory.
13 # For example, the "parse.c" and "parse.h" files to implement the
14 # the parser are derived from "parse.y" using lemon. And the
15 # "keywordhash.h" files is generated by a program named "mkkeywordhash".
17 # After the "tsrc" directory has been created and populated, run
18 # this script:
20 # tclsh mksqlite3c.tcl
22 # The amalgamated SQLite code will be written into sqlite3.c
25 # Begin by reading the "sqlite3.h" header file. Extract the version number
26 # from in this file. The versioon number is needed to generate the header
27 # comment of the amalgamation.
29 if {[lsearch $argv --nostatic]>=0} {
30 set addstatic 0
31 } else {
32 set addstatic 1
34 if {[lsearch $argv --linemacros]>=0} {
35 set linemacros 1
36 } else {
37 set linemacros 0
39 set in [open tsrc/sqlite3.h]
40 set cnt 0
41 set VERSION ?????
42 while {![eof $in]} {
43 set line [gets $in]
44 if {$line=="" && [eof $in]} break
45 incr cnt
46 regexp {#define\s+SQLITE_VERSION\s+"(.*)"} $line all VERSION
48 close $in
50 # Open the output file and write a header comment at the beginning
51 # of the file.
53 set out [open sqlite3.c w]
54 # Force the output to use unix line endings, even on Windows.
55 fconfigure $out -translation lf
56 set today [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S UTC" -gmt 1]
57 puts $out [subst \
58 {/******************************************************************************
59 ** This file is an amalgamation of many separate C source files from SQLite
60 ** version $VERSION. By combining all the individual C code files into this
61 ** single large file, the entire code can be compiled as a single translation
62 ** unit. This allows many compilers to do optimizations that would not be
63 ** possible if the files were compiled separately. Performance improvements
64 ** of 5% or more are commonly seen when SQLite is compiled as a single
65 ** translation unit.
67 ** This file is all you need to compile SQLite. To use SQLite in other
68 ** programs, you need this file and the "sqlite3.h" header file that defines
69 ** the programming interface to the SQLite library. (If you do not have
70 ** the "sqlite3.h" header file at hand, you will find a copy embedded within
71 ** the text of this file. Search for "Begin file sqlite3.h" to find the start
72 ** of the embedded sqlite3.h header file.) Additional code files may be needed
73 ** if you want a wrapper to interface SQLite with your choice of programming
74 ** language. The code for the "sqlite3" command-line shell is also in a
75 ** separate file. This file contains only code for the core SQLite library.
77 #define SQLITE_CORE 1
78 #define SQLITE_AMALGAMATION 1}]
79 if {$addstatic} {
80 puts $out \
81 {#ifndef SQLITE_PRIVATE
82 # define SQLITE_PRIVATE static
83 #endif
84 #ifndef SQLITE_API
85 # define SQLITE_API
86 #endif}
89 # These are the header files used by SQLite. The first time any of these
90 # files are seen in a #include statement in the C code, include the complete
91 # text of the file in-line. The file only needs to be included once.
93 foreach hdr {
94 crypto.h
95 btree.h
96 btreeInt.h
97 fts3.h
98 fts3Int.h
99 fts3_hash.h
100 fts3_tokenizer.h
101 hash.h
102 hwtime.h
103 keywordhash.h
104 mutex.h
105 opcodes.h
106 os_common.h
107 os.h
108 os_os2.h
109 pager.h
110 parse.h
111 pcache.h
112 rtree.h
113 sqlite3ext.h
114 sqlite3.h
115 sqliteicu.h
116 sqliteInt.h
117 sqliteLimit.h
118 vdbe.h
119 vdbeInt.h
120 wal.h
122 set available_hdr($hdr) 1
124 set available_hdr(sqliteInt.h) 0
126 # 78 stars used for comment formatting.
127 set s78 \
128 {*****************************************************************************}
130 # Insert a comment into the code
132 proc section_comment {text} {
133 global out s78
134 set n [string length $text]
135 set nstar [expr {60 - $n}]
136 set stars [string range $s78 0 $nstar]
137 puts $out "/************** $text $stars/"
140 # Read the source file named $filename and write it into the
141 # sqlite3.c output file. If any #include statements are seen,
142 # process them approprately.
144 proc copy_file {filename} {
145 global seen_hdr available_hdr out addstatic linemacros
146 set ln 0
147 set tail [file tail $filename]
148 section_comment "Begin file $tail"
149 if {$linemacros} {puts $out "#line 1 \"$filename\""}
150 set in [open $filename r]
151 set varpattern {^[a-zA-Z][a-zA-Z_0-9 *]+(sqlite3[_a-zA-Z0-9]+)(\[|;| =)}
152 set declpattern {[a-zA-Z][a-zA-Z_0-9 ]+ \**(sqlite3[_a-zA-Z0-9]+)\(}
153 if {[file extension $filename]==".h"} {
154 set declpattern " *$declpattern"
156 set declpattern ^$declpattern
157 while {![eof $in]} {
158 set line [gets $in]
159 incr ln
160 if {[regexp {^\s*#\s*include\s+["<]([^">]+)[">]} $line all hdr]} {
161 if {[info exists available_hdr($hdr)]} {
162 if {$available_hdr($hdr)} {
163 if {$hdr!="os_common.h" && $hdr!="hwtime.h"} {
164 set available_hdr($hdr) 0
166 section_comment "Include $hdr in the middle of $tail"
167 copy_file tsrc/$hdr
168 section_comment "Continuing where we left off in $tail"
169 if {$linemacros} {puts $out "#line [expr {$ln+1}] \"$filename\""}
171 } elseif {![info exists seen_hdr($hdr)]} {
172 set seen_hdr($hdr) 1
173 puts $out $line
174 } else {
175 puts $out "/* $line */"
177 } elseif {[regexp {^#ifdef __cplusplus} $line]} {
178 puts $out "#if 0"
179 } elseif {!$linemacros && [regexp {^#line} $line]} {
180 # Skip #line directives.
181 } elseif {$addstatic && ![regexp {^(static|typedef)} $line]} {
182 regsub {^SQLITE_API } $line {} line
183 if {[regexp $declpattern $line all funcname]} {
184 # Add the SQLITE_PRIVATE or SQLITE_API keyword before functions.
185 # so that linkage can be modified at compile-time.
186 if {[regexp {^sqlite3_} $funcname]} {
187 puts $out "SQLITE_API $line"
188 } else {
189 puts $out "SQLITE_PRIVATE $line"
191 } elseif {[regexp $varpattern $line all varname]} {
192 # Add the SQLITE_PRIVATE before variable declarations or
193 # definitions for internal use
194 if {![regexp {^sqlite3_} $varname]} {
195 regsub {^extern } $line {} line
196 puts $out "SQLITE_PRIVATE $line"
197 } else {
198 if {[regexp {const char sqlite3_version\[\];} $line]} {
199 set line {const char sqlite3_version[] = SQLITE_VERSION;}
201 regsub {^SQLITE_EXTERN } $line {} line
202 puts $out "SQLITE_API $line"
204 } elseif {[regexp {^(SQLITE_EXTERN )?void \(\*sqlite3IoTrace\)} $line]} {
205 regsub {^SQLITE_EXTERN } $line {} line
206 puts $out "SQLITE_PRIVATE $line"
207 } elseif {[regexp {^void \(\*sqlite3Os} $line]} {
208 puts $out "SQLITE_PRIVATE $line"
209 } else {
210 puts $out $line
212 } else {
213 puts $out $line
216 close $in
217 section_comment "End of $tail"
221 # Process the source files. Process files containing commonly
222 # used subroutines first in order to help the compiler find
223 # inlining opportunities.
225 foreach file {
226 sqliteInt.h
228 crypto.c
229 crypto_impl.c
231 global.c
232 ctime.c
233 status.c
234 date.c
235 os.c
237 fault.c
238 mem0.c
239 mem1.c
240 mem2.c
241 mem3.c
242 mem5.c
243 mutex.c
244 mutex_noop.c
245 mutex_os2.c
246 mutex_unix.c
247 mutex_w32.c
248 malloc.c
249 printf.c
250 random.c
251 utf.c
252 util.c
253 hash.c
254 opcodes.c
256 os_os2.c
257 os_unix.c
258 os_win.c
260 bitvec.c
261 pcache.c
262 pcache1.c
263 rowset.c
264 pager.c
265 wal.c
267 btmutex.c
268 btree.c
269 backup.c
271 vdbemem.c
272 vdbeaux.c
273 vdbeapi.c
274 vdbetrace.c
275 vdbe.c
276 vdbeblob.c
277 vdbesort.c
278 journal.c
279 memjournal.c
281 walker.c
282 resolve.c
283 expr.c
284 alter.c
285 analyze.c
286 attach.c
287 auth.c
288 build.c
289 callback.c
290 delete.c
291 func.c
292 fkey.c
293 insert.c
294 legacy.c
295 loadext.c
296 pragma.c
297 prepare.c
298 select.c
299 table.c
300 trigger.c
301 update.c
302 vacuum.c
303 vtab.c
304 where.c
306 parse.c
308 tokenize.c
309 complete.c
311 main.c
312 notify.c
314 fts3.c
315 fts3_aux.c
316 fts3_expr.c
317 fts3_hash.c
318 fts3_porter.c
319 fts3_tokenizer.c
320 fts3_tokenizer1.c
321 fts3_write.c
322 fts3_snippet.c
323 fts3_unicode.c
324 fts3_unicode2.c
326 rtree.c
327 icu.c
329 fts3_icu.c
332 copy_file tsrc/$file
335 close $out