xref: /sqlite-3.40.0/tool/split-sqlite3c.tcl (revision 067b92ba)
1#!/usr/bin/tclsh
2#
3# This script splits the sqlite3.c amalgamated source code files into
4# several smaller files such that no single files is more than a fixed
5# number of lines in length (32k or 64k).  Each of the split out files
6# is #include-ed by the master file.
7#
8# Splitting files up this way allows them to be used with older compilers
9# that cannot handle really long source files.
10#
11set MAX 32768    ;# Maximum number of lines per file.
12
13set BEGIN {^/\*+ Begin file ([a-zA-Z0-9_.]+) \*+/}
14set END   {^/\*+ End of %s \*+/}
15
16set in [open sqlite3.c]
17set out1 [open sqlite3-all.c w]
18fconfigure $out1 -translation lf
19
20# Copy the header from sqlite3.c into sqlite3-all.c
21#
22while {[gets $in line]} {
23  if {[regexp $BEGIN $line]} break
24  puts $out1 $line
25}
26
27# Gather the complete content of a file into memory.  Store the
28# content in $bufout.  Store the number of lines is $nout
29#
30proc gather_one_file {firstline bufout nout} {
31  regexp $::BEGIN $firstline all filename
32  set end [format $::END $filename]
33  upvar $bufout buf $nout n
34  set buf $firstline\n
35  global in
36  set n 0
37  while {[gets $in line]>=0} {
38    incr n
39    append buf $line\n
40    if {[regexp $end $line]} break
41  }
42}
43
44# Write a big chunk of text in to an auxiliary file "sqlite3-NNN.c".
45# Also add an appropriate #include to sqlite3-all.c
46#
47set filecnt 0
48proc write_one_file {content} {
49  global filecnt
50  incr filecnt
51  set out [open sqlite3-$filecnt.c w]
52  fconfigure $out -translation lf
53  puts -nonewline $out $content
54  close $out
55  puts $::out1 "#include \"sqlite3-$filecnt.c\""
56}
57
58# Continue reading input.  Store chunks in separate files and add
59# the #includes to the main sqlite3-all.c file as necessary to reference
60# the extra chunks.
61#
62set all {}
63set N 0
64while {[regexp $BEGIN $line]} {
65  set buf {}
66  set n 0
67  gather_one_file $line buf n
68  if {$n+$N>=$MAX} {
69    write_one_file $all
70    set all {}
71    set N 0
72  }
73  append all $buf
74  incr N $n
75  while {[gets $in line]>=0} {
76    if {[regexp $BEGIN $line]} break
77    puts $out1 $line
78  }
79}
80if {$N>0} {
81  write_one_file $all
82}
83close $out1
84close $in
85