3 SPDX-FileCopyrightText: Copyright The SCons Foundation (https://scons.org)
4 SPDX-License-Identifier: MIT
5 SPDX-FileType: DOCUMENTATION
7 This file is processed by the bin/SConsDoc.py module.
11 <!ENTITY % scons SYSTEM '../../doc/scons.mod'>
13 <!ENTITY % builders-mod SYSTEM '../../doc/generated/builders.mod'>
15 <!ENTITY % functions-mod SYSTEM '../../doc/generated/functions.mod'>
17 <!ENTITY % tools-mod SYSTEM '../../doc/generated/tools.mod'>
19 <!ENTITY % variables-mod SYSTEM '../../doc/generated/variables.mod'>
23 <sconsdoc xmlns="http://www.scons.org/dbxsd/v1.0"
24 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
25 xsi:schemaLocation="http://www.scons.org/dbxsd/v1.0 http://www.scons.org/dbxsd/v1.0/scons.xsd">
27 <tool name="textfile">
30 Set &consvars; for the &b-link-Textfile; and &b-link-Substfile; builders.
34 <item>LINESEPARATOR</item>
35 <item>SUBSTFILEPREFIX</item>
36 <item>SUBSTFILESUFFIX</item>
37 <item>TEXTFILEPREFIX</item>
38 <item>TEXTFILESUFFIX</item>
39 <item>FILE_ENCODING</item>
42 <item>SUBST_DICT</item>
46 <builder name="Textfile">
49 The &b-Textfile; builder generates a single text file from
50 a template consisting of a list of strings, replacing text
51 using the &cv-link-SUBST_DICT; &consvar; (if set) -
52 see &b-link-Substfile; for a description of replacement.
53 The strings will be separated in the target file using the
55 &cv-link-LINESEPARATOR; &consvar;;
56 the line separator is not emitted after the last string.
57 Nested lists of source strings
59 Source strings need not literally be Python strings:
60 they can be Nodes or Python objects that convert cleanly
61 to &f-link-Value; nodes.
65 The prefix and suffix specified by the &cv-link-TEXTFILEPREFIX;
66 and &cv-link-TEXTFILESUFFIX; &consvars;
67 (by default an empty string and <filename>.txt</filename>, respectively)
68 are automatically added to the target if they are not already present.
71 By default, the target file encoding is "utf-8" and can be changed by &cv-link-FILE_ENCODING;
76 # builds/writes foo.txt
77 env.Textfile(target='foo.txt', source=['Goethe', 42, 'Schiller'])
79 # builds/writes bar.txt
80 env.Textfile(target='bar', source=['lalala', 'tanteratei'], LINESEPARATOR='|*')
82 # nested lists are flattened automatically
83 env.Textfile(target='blob', source=['lalala', ['Goethe', 42, 'Schiller'], 'tanteratei'])
85 # files may be used as input by wrapping them in File()
87 target='concat', # concatenate files with a marker between
88 source=[File('concat1'), File('concat2')],
89 LINESEPARATOR='====================\n',
95 <para><filename>foo.txt</filename></para>
102 <para><filename>bar.txt</filename></para>
107 <para><filename>blob.txt</filename></para>
119 <builder name="Substfile">
122 The &b-Substfile; builder creates a single text file from
123 a template consisting of a file or set of files (or nodes),
124 replacing text using the &cv-link-SUBST_DICT; &consvar; (if set).
125 If a set, they are concatenated into the target file
126 using the value of the
127 &cv-link-LINESEPARATOR; &consvar; as a separator between contents;
128 the separator is not emitted after the contents of the last file.
129 Nested lists of source files
130 are flattened. See also &b-link-Textfile;.
134 By default, the target file encoding is "utf-8" and can be changed by &cv-link-FILE_ENCODING;
139 If a single source file name is specified and has a <filename>.in</filename> suffix,
140 the suffix is stripped and the remainder of the name is used as the default target name.
144 The prefix and suffix specified by the &cv-link-SUBSTFILEPREFIX;
145 and &cv-link-SUBSTFILESUFFIX; &consvars;
146 (an empty string by default in both cases)
147 are automatically added to the target if they are not already present.
151 If a construction variable named &cv-link-SUBST_DICT; is present,
152 it may be either a Python dictionary or a sequence of
153 (<replaceable>key</replaceable>, <replaceable>value</replaceable>) tuples.
154 If it is a dictionary it is converted into a list of tuples
155 with unspecified order,
156 so if one key is a prefix of another key
157 or if one substitution could be further expanded by another substitution,
158 it is unpredictable whether the expansion will occur.
162 Any occurrences of a key in the source
163 are replaced by the corresponding value,
164 which may be a Python callable function or a string.
165 If the value is a callable, it is called with no arguments to get a string.
166 Strings are <emphasis>subst</emphasis>-expanded
167 and the result replaces the key.
171 env = Environment(tools=['default'])
173 env['prefix'] = '/usr/bin'
174 script_dict = {'@prefix@': '/bin', '@exec_prefix@': '$prefix'}
175 env.Substfile('script.in', SUBST_DICT=script_dict)
177 conf_dict = {'%VERSION%': '1.2.3', '%BASE%': 'MyProg'}
178 env.Substfile('config.h.in', conf_dict, SUBST_DICT=conf_dict)
180 # UNPREDICTABLE - one key is a prefix of another
181 bad_foo = {'$foo': '$foo', '$foobar': '$foobar'}
182 env.Substfile('foo.in', SUBST_DICT=bad_foo)
184 # PREDICTABLE - keys are applied longest first
185 good_foo = [('$foobar', '$foobar'), ('$foo', '$foo')]
186 env.Substfile('foo.in', SUBST_DICT=good_foo)
188 # UNPREDICTABLE - one substitution could be further expanded
189 bad_bar = {'@bar@': '@soap@', '@soap@': 'lye'}
190 env.Substfile('bar.in', SUBST_DICT=bad_bar)
192 # PREDICTABLE - substitutions are expanded in order
193 good_bar = (('@bar@', '@soap@'), ('@soap@', 'lye'))
194 env.Substfile('bar.in', SUBST_DICT=good_bar)
196 # the SUBST_DICT may be in common (and not an override)
198 subst = Environment(tools=['textfile'], SUBST_DICT=substitutions)
199 substitutions['@foo@'] = 'foo'
200 subst['SUBST_DICT']['@bar@'] = 'bar'
203 [Value('#include "@foo@.h"'), Value('#include "@bar@.h"'), "common.in", "pgm1.in"],
207 [Value('#include "@foo@.h"'), Value('#include "@bar@.h"'), "common.in", "pgm2.in"],
214 <cvar name="LINESEPARATOR">
217 The separator used by the &b-link-Substfile; and &b-link-Textfile; builders.
218 This value is used between sources when constructing the target.
219 It defaults to the current system line separator.
224 <cvar name="SUBST_DICT">
227 The dictionary used by the &b-link-Substfile; or &b-link-Textfile; builders
228 for substitution values.
229 It can be anything acceptable to the <function>dict()</function> constructor,
230 so in addition to a dictionary,
231 lists of tuples are also acceptable.
236 <cvar name="SUBSTFILEPREFIX">
239 The prefix used for &b-link-Substfile; file names,
240 an empty string by default.
245 <cvar name="SUBSTFILESUFFIX">
248 The suffix used for &b-link-Substfile; file names,
249 an empty string by default.
254 <cvar name="TEXTFILEPREFIX">
257 The prefix used for &b-link-Textfile; file names,
258 an empty string by default.
263 <cvar name="TEXTFILESUFFIX">
266 The suffix used for &b-link-Textfile; file names;
267 <filename>.txt</filename> by default.
272 <cvar name="FILE_ENCODING">
275 File encoding used for files written by &b-link-Textfile; and &b-link-Substfile;.
276 Set to "utf-8" by default.
279 <emphasis>New in version 4.5.0.</emphasis>