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
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
20 # tclsh mksqlite3c.tcl --srcdir $SRC
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 version number is needed to generate the header
27 # comment of the amalgamation.
32 for {set i
0} {$i<[llength $argv]} {incr i
} {
33 set x
[lindex $argv $i]
34 if {[regexp {^
-+nostatic
$} $x]} {
36 } elseif
{[regexp {^
-+linemacros
} $x]} {
38 } elseif
{[regexp {^
-+useapicall
} $x]} {
41 error "unknown command-line option: $x"
44 set in
[open tsrc
/sqlite3.h
]
49 if {$line=="" && [eof $in]} break
51 regexp {#define\s+SQLITE_VERSION\s+"(.*)"} $line all VERSION
55 # Open the output file and write a header comment at the beginning
58 set out
[open sqlite3.c w
]
59 # Force the output to use unix line endings, even on Windows.
60 fconfigure $out -translation lf
61 set today
[clock format [clock seconds
] -format "%Y-%m-%d %H:%M:%S UTC" -gmt 1]
63 {/******************************************************************************
64 ** This
file is an amalgamation of many separate C
source files from SQLite
65 ** version
$VERSION. By combining all the individual C code files into this
66 ** single large
file, the entire code can be compiled as a single translation
67 ** unit. This allows many compilers to do optimizations that would not be
68 ** possible
if the files were compiled separately. Performance improvements
69 ** of
5% or more are commonly seen when SQLite is compiled as a single
72 ** This
file is all you need to compile SQLite. To use SQLite in other
73 ** programs
, you need this
file and the
"sqlite3.h" header
file that defines
74 ** the programming interface to the SQLite library.
(If you do not have
75 ** the
"sqlite3.h" header
file at hand
, you will find a copy embedded within
76 ** the
text of this
file. Search
for "Begin file sqlite3.h" to find the start
77 ** of the embedded sqlite3.h header
file.
) Additional code files may be needed
78 ** if you want a wrapper to interface SQLite with your choice of programming
79 ** language. The code
for the
"sqlite3" command-line shell is also in a
80 ** separate
file. This
file contains only code
for the core SQLite library.
83 #define SQLITE_AMALGAMATION 1}]
86 {#ifndef SQLITE_PRIVATE
87 # define SQLITE_PRIVATE static
91 # Examine the parse.c file. If it contains lines of the form:
93 # "#ifndef SQLITE_ENABLE_UPDATE_LIMIT
95 # then set the SQLITE_UDL_CAPABLE_PARSER flag in the amalgamation.
97 set in
[open tsrc
/parse.c
]
98 if {[regexp {ifndef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
} [read $in]]} {
99 puts $out "#define SQLITE_UDL_CAPABLE_PARSER 1"
103 # These are the header files used by SQLite. The first time any of these
104 # files are seen in a #include statement in the C code, include the complete
105 # text of the file in-line. The file only needs to be included once.
145 set available_hdr
($hdr) 1
147 set available_hdr
(sqliteInt.h
) 0
148 set available_hdr
(sqlite3session.h
) 0
150 # These headers should be copied into the amalgamation without modifying any
151 # of their function declarations or definitions.
152 set varonly_hdr
(sqlite3.h
) 1
154 # These are the functions that accept a variable number of arguments. They
155 # always need to use the "cdecl" calling convention even when another calling
156 # convention (e.g. "stcall") is being used for the rest of the library.
167 # 78 stars used for comment formatting.
169 {*****************************************************************************}
171 # Insert a comment into the code
173 proc section_comment
{text} {
175 set n
[string length
$text]
176 set nstar
[expr {60 - $n}]
177 set stars
[string range
$s78 0 $nstar]
178 puts $out "/************** $text $stars/"
181 # Read the source file named $filename and write it into the
182 # sqlite3.c output file. If any #include statements are seen,
183 # process them appropriately.
185 proc copy_file
{filename} {
186 global seen_hdr available_hdr varonly_hdr cdecllist out
187 global addstatic linemacros useapicall
189 set tail
[file tail
$filename]
190 section_comment
"Begin file $tail"
191 if {$linemacros} {puts $out "#line 1 \"$filename\""}
192 set in
[open $filename r
]
193 set varpattern
{^
[a-zA-Z
][a-zA-Z_0-9
*]+(sqlite3
[_a-zA-Z0-9
]+)(\[|
;|
=)}
194 set declpattern
{([a-zA-Z
][a-zA-Z_0-9
]+ \**)(sqlite3
[_a-zA-Z0-9
]+)(\(.
*)}
195 if {[file extension
$filename]==".h"} {
196 set declpattern
" *$declpattern"
198 set declpattern ^
$declpattern\$
200 set line
[string trimright
[gets $in]]
202 if {[regexp {^
\s
*#\s*include\s+["<]([^">]+)[">]} $line all hdr]} {
203 if {[info exists available_hdr
($hdr)]} {
204 if {$available_hdr($hdr)} {
205 if {$hdr!="os_common.h" && $hdr!="hwtime.h"} {
206 set available_hdr
($hdr) 0
208 section_comment
"Include $hdr in the middle of $tail"
210 section_comment
"Continuing where we left off in $tail"
211 if {$linemacros} {puts $out "#line [expr {$ln+1}] \"$filename\""}
213 # Comment out the entire line, replacing any nested comment
214 # begin/end markers with the harmless substring "**".
215 puts $out "/* [string map [list /* ** */ **] $line] */"
217 } elseif
{![info exists seen_hdr
($hdr)]} {
218 if {![regexp {/\*\s
+amalgamator
:\s
+dontcache
\s
+\*/} $line]} {
222 } elseif
{[regexp {/\*\s
+amalgamator
:\s
+keep
\s
+\*/} $line]} {
223 # This include file must be kept because there was a "keep"
224 # directive inside of a line comment.
227 # Comment out the entire line, replacing any nested comment
228 # begin/end markers with the harmless substring "**".
229 puts $out "/* [string map [list /* ** */ **] $line] */"
231 } elseif
{[regexp {^
#ifdef __cplusplus} $line]} {
233 } elseif
{!$linemacros && [regexp {^
#line} $line]} {
234 # Skip #line directives.
236 && ![regexp {^
(static|typedef|SQLITE_PRIVATE
)} $line]} {
237 # Skip adding the SQLITE_PRIVATE or SQLITE_API keyword before
238 # functions if this header file does not need it.
239 if {![info exists varonly_hdr
($tail)]
240 && [regexp $declpattern $line all rettype funcname rest
]} {
241 regsub {^SQLITE_API
} $line {} line
242 regsub {^SQLITE_API
} $rettype {} rettype
244 # Add the SQLITE_PRIVATE or SQLITE_API keyword before functions.
245 # so that linkage can be modified at compile-time.
246 if {[regexp {^sqlite3
[a-z
]*_
} $funcname]} {
248 append line
" " [string trim
$rettype]
249 if {[string index
$rettype end
] ne
"*"} {
253 if {[lsearch -exact $cdecllist $funcname] >= 0} {
254 append line SQLITE_CDECL
" "
256 append line SQLITE_APICALL
" "
259 append line
$funcname $rest
260 if {$funcname=="sqlite3_sourceid" && !$linemacros} {
261 # The sqlite3_sourceid() routine is synthesized at the end of
263 puts $out "/* $line */"
268 puts $out "SQLITE_PRIVATE $line"
270 } elseif
{[regexp $varpattern $line all varname
]} {
271 # Add the SQLITE_PRIVATE before variable declarations or
272 # definitions for internal use
273 regsub {^SQLITE_API
} $line {} line
274 if {![regexp {^sqlite3_
} $varname]} {
275 regsub {^extern
} $line {} line
276 puts $out "SQLITE_PRIVATE $line"
278 if {[regexp {const char sqlite3_version
\[\];} $line]} {
279 set line
{const char sqlite3_version
[] = SQLITE_VERSION
;}
281 regsub {^SQLITE_EXTERN
} $line {} line
282 puts $out "SQLITE_API $line"
284 } elseif
{[regexp {^
(SQLITE_EXTERN
)?void
\(\*sqlite3IoTrace
\)} $line]} {
285 regsub {^SQLITE_API
} $line {} line
286 regsub {^SQLITE_EXTERN
} $line {} line
288 } elseif
{[regexp {^void
\(\*sqlite3Os
} $line]} {
289 regsub {^SQLITE_API
} $line {} line
290 puts $out "SQLITE_PRIVATE $line"
299 section_comment
"End of $tail"
303 # Process the source files. Process files containing commonly
304 # used subroutines first in order to help the compiler find
305 # inlining opportunities.
434 # Synthesize an alternative sqlite3_sourceid() implementation that
435 # that tries to detects changes in the amalgamation source text
436 # and modify returns a modified source-id if changes are detected.
438 # The only detection mechanism we have is the __LINE__ macro. So only
439 # edits that changes the number of lines of source code are detected.
443 set in2
[open sqlite3.c
]
446 while {![eof $in2]} {
449 if {[regexp {^
#define SQLITE_SOURCE_ID } $line]} {set oldsrcid $line}
452 regsub {[0-9a-flt
]{4}"} $oldsrcid {alt2"} oldsrcid
454 "#if __LINE__!=[expr {$cnt+0}]
455 #undef SQLITE_SOURCE_ID
458 /* Return the source-id for this library */
459 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }"
462 "/************************** End of sqlite3.c ******************************/"