Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/tool/mkccode.tcl # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / tool / mkccode.tcl` 166 lines ยท 4683 bytes Raw Blame History 1 #!/bin/env tclsh 2 # 3 # This script is used to amalgamate C source code files into a single 4 # unit. 5 # 6 # Usage example: 7 # 8 # tclsh mkccode.tcl -DENABLE_FEATURE_XYZ demoapp.c.in >demoapp.c 9 # 10 # The demoapp.c.in file contains a mixture of C code, TCL script, and 11 # processing directives used by mkccode.tcl to build the final C-code 12 # output file. Most lines of demoapp.c.in are copied straight through 13 # into the output. The following control directives are recognized: 14 # 15 # BEGIN_STRING 16 # 17 # This marks the beginning of large string literal - usually a TCL 18 # script of some kind. Subsequent lines of text through the first 19 # line that begins with END_STRING are converted into a C-language 20 # string literal. 21 # 22 # INCLUDE path 23 # 24 # The path argument is the name of a file to be inserted in place of 25 # the INCLUDE line. The path can begin with $ROOT to signify the 26 # root of the SQLite source tree, or $HOME to signify the directory 27 # that contains the demoapp.c.in input script itself. If the path does 28 # not begin with either $ROOT or $HOME, then it is interpreted relative 29 # to the current working directory. 30 # 31 # If the INCLUDE occurs in the middle of BEGIN_STRING...END_STRING 32 # then all of the text in the input file is converted into C-language 33 # string literals. 34 # 35 # IFDEF macro 36 # IFNDEF macro 37 # ELSE 38 # ENDIF 39 # 40 # The text from "IFDEF macro" down to the next ELSE or ENDIF is 41 # included only if -Dmacro appears as a command-line argument. 42 # The "IFNDEF macro" simply inverts the initial test. 43 # 44 # None of the control directives described above will nest. Only the 45 # top-level input file ("demoapp.c.in" in the example) is interpreted. 46 # referenced files are copied verbatim. 47 # 48 proc usage {} { 49 puts stderr "Usage: $::argv0 \[OPTIONS\] TEMPLATE >OUTPUT" 50 exit 1 51 } 52 set infile {} 53 foreach ax $argv { 54 if {[string match -D* $ax]} { 55 if {[string match *=* $ax]} { 56 regexp -- {-D([^=]+)=(.*)} $ax all name value 57 set DEF($name) $value 58 } else { 59 set DEF([string range $ax 2 end]) 1 60 } 61 continue 62 } 63 if {[string match -* $ax]} { 64 puts stderr "$::argv0: Unknown option \"$ax\"" 65 usage 66 } 67 if {$infile!=""} { 68 puts stderr "$::argv0: Surplus argument: \"$ax\"" 69 usage 70 } 71 set infile $ax 72 } 73 set ROOT [file normalize [file dir $argv0]/..] 74 set HOME [file normalize [file dir $infile]] 75 set in [open $infile rb] 76 puts [subst {/* DO NOT EDIT 77 ** 78 ** This file was generated by \"$argv0 $argv\". 79 ** To make changes, edit $infile then rerun the generator 80 ** command. 81 */}] 82 set instr 0 83 set omit {} 84 set nomit 0 85 set ln 0 86 while {1} { 87 set line [gets $in] 88 incr ln 89 if {[eof $in]} break 90 if {[regexp {^INCLUDE (.*)} $line all path]} { 91 if {$nomit>0 && [string match *1* $omit]} continue 92 if {0} { 93 # https://github.com/msteveb/jimtcl/issues/320 94 regsub {^\$ROOT\y} $path $ROOT path 95 regsub {^\$HOME\y} $path $HOME path 96 } else { 97 set path [string map "\$ROOT $ROOT" $path] 98 set path [string map "\$HOME $HOME" $path] 99 # or: set path [string map "\$HOME $HOME \$ROOT $ROOT" $path] 100 } 101 set in2 [open $path rb] 102 puts "/* INCLUDE $path */" 103 if {$instr} { 104 while {1} { 105 set line [gets $in2] 106 if {[eof $in2]} break 107 set x [string map "\\\\ \\\\\\\\ \\\" \\\\\"" $line] 108 puts "\"$x\\n\"" 109 } 110 } else { 111 puts [read $in2] 112 } 113 puts "/* END $path */" 114 close $in2 115 continue 116 } 117 if {[regexp {^BEGIN_STRING} $line]} { 118 set instr 1 119 puts "/* BEGIN_STRING */" 120 continue 121 } 122 if {[regexp {^END_STRING} $line]} { 123 set instr 0 124 puts "/* END_STRING */" 125 continue 126 } 127 if {[regexp {^IFNDEF +([A-Za-z_0-9]+)} $line all name]} { 128 set omit $omit[info exists DEF($name)] 129 incr nomit 130 continue 131 } 132 if {[regexp {^IFDEF +([A-Za-z_0-9]+)} $line all name]} { 133 set omit $omit[expr {![info exists DEF($name)]}] 134 incr nomit 135 continue 136 } 137 if {[regexp {^ELSE} $line]} { 138 if {!$nomit} { 139 puts stderr "$infile:$ln: ELSE without a prior IFDEF" 140 exit 1 141 } 142 set omit [string range $omit 0 end-1][expr {![string index $omit end]}] 143 continue 144 } 145 if {[regexp {^ENDIF} $line]} { 146 if {!$nomit} { 147 puts stderr "$infile:$ln: ENDIF without a prior IFDEF" 148 exit 1 149 } 150 incr nomit -1 151 set omit [string range $omit 0 [expr {$nomit-1}]] 152 continue 153 } 154 if {$nomit>0 && [string match *1* $omit]} { 155 # noop 156 } elseif {$instr} { 157 set x [string map "\\\\ \\\\\\\\ \\\" \\\\\"" $line] 158 puts "\"$x\\n\"" 159 } else { 160 puts $line 161 } 162 } 163 if {$nomit} { 164 puts stderr "$infile:$ln: One or more unterminated IFDEFs" 165 exit 1 166 }