1#!/usr/bin/tclsh
2#
3# This script builds a single C code file holding all of FTS3 code.
4# The name of the output file is fts3amal.c.  To build this file,
5# first do:
6#
7#      make target_source
8#
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.)
12#
13# After the "tsrc" directory has been created and populated, run
14# this script:
15#
16#      tclsh mkfts3amal.tcl
17#
18# The amalgamated FTS3 code will be written into fts3amal.c
19#
20
21# Open the output file and write a header comment at the beginning
22# of the file.
23#
24set out [open fts3amal.c w]
25set today [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S UTC" -gmt 1]
26puts $out [subst \
27{/******************************************************************************
28** This file is an amalgamation of separate C source files from the SQLite
29** Full Text Search extension 2 (fts3).  By combining all the individual C
30** code  files into this single large file, the entire code can be compiled
31** as a one translation unit.  This allows many compilers to do optimizations
32** that would not be possible if the files were compiled separately.  It also
33** makes the code easier to import into other projects.
34**
35** This amalgamation was generated on $today.
36*/}]
37
38# These are the header files used by FTS3.  The first time any of these
39# files are seen in a #include statement in the C code, include the complete
40# text of the file in-line.  The file only needs to be included once.
41#
42foreach hdr {
43   fts3.h
44   fts3_hash.h
45   fts3_tokenizer.h
46   sqlite3.h
47   sqlite3ext.h
48} {
49  set available_hdr($hdr) 1
50}
51
52# 78 stars used for comment formatting.
53set s78 \
54{*****************************************************************************}
55
56# Insert a comment into the code
57#
58proc section_comment {text} {
59  global out s78
60  set n [string length $text]
61  set nstar [expr {60 - $n}]
62  set stars [string range $s78 0 $nstar]
63  puts $out "/************** $text $stars/"
64}
65
66# Read the source file named $filename and write it into the
67# sqlite3.c output file.  If any #include statements are seen,
68# process them approprately.
69#
70proc copy_file {filename} {
71  global seen_hdr available_hdr out
72  set tail [file tail $filename]
73  section_comment "Begin file $tail"
74  set in [open $filename r]
75  while {![eof $in]} {
76    set line [gets $in]
77    if {[regexp {^#\s*include\s+["<]([^">]+)[">]} $line all hdr]} {
78      if {[info exists available_hdr($hdr)]} {
79        if {$available_hdr($hdr)} {
80          section_comment "Include $hdr in the middle of $tail"
81          copy_file tsrc/$hdr
82          section_comment "Continuing where we left off in $tail"
83        }
84      } elseif {![info exists seen_hdr($hdr)]} {
85        set seen_hdr($hdr) 1
86        puts $out $line
87      }
88    } elseif {[regexp {^#ifdef __cplusplus} $line]} {
89      puts $out "#if 0"
90    } elseif {[regexp {^#line} $line]} {
91      # Skip #line directives.
92    } else {
93      puts $out $line
94    }
95  }
96  close $in
97  section_comment "End of $tail"
98}
99
100
101# Process the source files.  Process files containing commonly
102# used subroutines first in order to help the compiler find
103# inlining opportunities.
104#
105foreach file {
106   fts3.c
107   fts3_hash.c
108   fts3_porter.c
109   fts3_tokenizer.c
110   fts3_tokenizer1.c
111} {
112  copy_file tsrc/$file
113}
114
115close $out
116