Skip to content

Commit e48e447

Browse files
authored
Merge pull request SCons#4470 from mwichmann/doc/scanner-examples
Fix scanner examples in User Guide
2 parents e7b62a7 + a30f12f commit e48e447

File tree

7 files changed

+280
-111
lines changed

7 files changed

+280
-111
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
7272
- Be more cautious about encodings fetching command output on Windows.
7373
Problem occurs in piped-spawn scenario, used by Configure tests.
7474
Fixes #3529.
75+
- Clarify/fix documentation of Scanners in User Guide and Manpage.
76+
Fixes #4468.
7577

7678

7779
RELEASE 4.6.0 - Sun, 19 Nov 2023 17:22:20 -0700

RELEASE.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ PACKAGING
7373
DOCUMENTATION
7474
-------------
7575

76-
- List any significant changes to the documentation (not individual
77-
typo fixes, even if they're mentioned in src/CHANGES.txt to give
78-
the contributor credit)
76+
- Fixed the Scanner examples in the User Guide to be runnable and added
77+
some more explantion. Clarified discussion of the scanner function in
78+
the Scanner Objects section of the manpage.
7979

8080
DEVELOPMENT
8181
-----------
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<screen xmlns="http://www.scons.org/dbxsd/v1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.scons.org/dbxsd/v1.0 http://www.scons.org/dbxsd/v1.0/scons.xsd">% <userinput>scons -Q</userinput>
2+
DEBUG: scan of 'file.input' found ['other_file']
3+
DEBUG: scanned dependencies found: ['inc/other_file']
4+
build_function(["file.k"], ["file.input"])
5+
</screen>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<screen xmlns="http://www.scons.org/dbxsd/v1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.scons.org/dbxsd/v1.0 http://www.scons.org/dbxsd/v1.0/scons.xsd">% <userinput>scons -Q</userinput>
2+
scons: *** [foo] Implicit dependency `other_file' not found, needed by target `foo'.
3+
</screen>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
some initial text
3+
include other_file
4+
some other text
5+

doc/man/scons.xml

Lines changed: 103 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5634,7 +5634,7 @@ may be a string or a list of strings.</para>
56345634
</listitem>
56355635
</varlistentry>
56365636

5637-
<varlistentry>
5637+
<varlistentry id="target_scanner">
56385638
<term><parameter>target_scanner</parameter></term>
56395639
<listitem>
56405640
<para>A Scanner object that
@@ -5652,7 +5652,7 @@ for information about creating Scanner objects.</para>
56525652
</listitem>
56535653
</varlistentry>
56545654

5655-
<varlistentry>
5655+
<varlistentry id="source_scanner">
56565656
<term><parameter>source_scanner</parameter></term>
56575657
<listitem>
56585658
<para>A Scanner object that
@@ -7243,11 +7243,12 @@ the rest are optional:
72437243
<term><parameter>function</parameter></term>
72447244
<listitem>
72457245
<para>
7246-
A scanner function to call to process
7246+
A function which can process ("scan")
72477247
a given Node (usually a file)
72487248
and return a list of Nodes
7249-
representing the implicit
7250-
dependencies (usually files) found in the contents.
7249+
representing any implicit
7250+
dependencies (usually files) which will be tracked
7251+
for the Node.
72517252
The function must accept three required arguments,
72527253
<parameter>node</parameter>,
72537254
<parameter>env</parameter> and
@@ -7260,59 +7261,98 @@ the internal &SCons; node representing the file to scan,
72607261
the scan, and <parameter>path</parameter> is a tuple
72617262
of directories that can be searched for files,
72627263
as generated by the optional scanner
7263-
<parameter>path_function</parameter> (see below).
7264-
If <parameter>argument</parameter> was supplied when the Scanner
7265-
object was created, it is given as <parameter>arg</parameter>
7266-
when the scanner function is called; since <parameter>argument</parameter>
7267-
is optional, the default is no <parameter>arg</parameter>.
7264+
<xref linkend="path_function"/>.
7265+
If the <xref linkend="scanner-argument"/>
7266+
parameter was supplied when the Scanner object was created,
7267+
it is passed as the <parameter>arg</parameter> parameter
7268+
to the scanner function when it is called.
7269+
Since <parameter>argument</parameter> is optional,
7270+
the scanner function <emphasis>may</emphasis> be
7271+
called without an <parameter>arg</parameter> parameter.
72687272
</para>
7273+
72697274
<para>
7270-
The function can use use
7275+
The scanner function can make use of
72717276
<function>str</function>(<parameter>node</parameter>)
72727277
to fetch the name of the file,
7273-
<replaceable>node</replaceable>.<function>dir</function>
7278+
<parameter>node</parameter>.<methodname>dir</methodname>
72747279
to fetch the directory the file is in,
7275-
<replaceable>node</replaceable>.<function>get_contents</function>()
7280+
<parameter>node</parameter>.<methodname>get_contents</methodname>()
72767281
to fetch the contents of the file as bytes or
7277-
<replaceable>node</replaceable>.<function>get_text_contents</function>()
7282+
<parameter>node</parameter>.<methodname>get_text_contents</methodname>()
72787283
to fetch the contents of the file as text.
72797284
</para>
7285+
72807286
<para>
7281-
The function must take into account the <parameter>path</parameter>
7282-
directories when generating the dependency Nodes. To illustrate this,
7283-
a C language source file may contain a line like
7284-
<literal>#include "foo.h"</literal>. However, there is no guarantee
7285-
that <filename>foo.h</filename> exists in the current directory:
7286-
the contents of &cv-link-CPPPATH; is passed to the C preprocessor which
7287-
will look in those places for the header,
7288-
so the scanner function needs to look in those places as well
7289-
in order to build Nodes with correct paths.
7290-
Using &f-link-FindPathDirs; with an argument of <literal>CPPPATH</literal>
7291-
as the <parameter>path_function</parameter> in the &f-Scanner; call
7292-
means the scanner function will be called with the paths extracted
7293-
from &cv-CPPPATH; in the environment <parameter>env</parameter>
7294-
passed as the <parameter>paths</parameter> parameter.
7295-
</para>
7296-
<para>
7297-
Note that the file to scan is
7298-
<emphasis>not</emphasis>
7299-
guaranteed to exist at the time the scanner is called -
7300-
it could be a generated file which has not been generated yet -
7301-
so the scanner function must be tolerant of that.
7287+
The scanner function should account for any directories
7288+
listed in the <parameter>path</parameter> parameter
7289+
when determining the existence of possible dependencies.
7290+
External tools such as the C/C++ preprocessor are given
7291+
lists of directories to search for source file inclusion directives
7292+
(e.g. <literal>#include "myheader.h"</literal>).
7293+
That list is generated from the relevant path variable
7294+
(e.g. &cv-link-CPPPATH; for C/C++). The Scanner can be
7295+
directed to pass the same list on to the scanner function
7296+
via the <parameter>path</parameter> parameter so it can
7297+
search in the same places.
7298+
The Scanner is enabled to pass this list via the
7299+
<xref linkend="path_function"/> argument at Scanner creation time.
73027300
</para>
7301+
73037302
<para>
7304-
Alternatively, you can supply a dictionary as the
7305-
<parameter>function</parameter> parameter,
7306-
to map keys (such as file suffixes) to other Scanner objects.
7303+
Instead of a scanner function, you can supply a dictionary as the
7304+
<parameter>function</parameter> parameter.
7305+
The dictionary must map keys (such as file suffixes)
7306+
to other Scanner objects.
73077307
A Scanner created this way serves as a dispatcher:
7308-
the Scanner's <parameter>skeys</parameter> parameter is
7308+
the Scanner's <xref linkend="skeys"/> parameter is
73097309
automatically populated with the dictionary's keys,
73107310
indicating that the Scanner handles Nodes which would be
73117311
selected by those keys; the mapping is then used to pass
73127312
the file on to a different Scanner that would not have been
73137313
selected to handle that Node based on its
73147314
own <parameter>skeys</parameter>.
73157315
</para>
7316+
7317+
<para>
7318+
Note that the file to scan is
7319+
<emphasis>not</emphasis>
7320+
guaranteed to exist at the time the scanner is called -
7321+
it could be a generated file which has not been generated yet -
7322+
so the scanner function must be tolerant of that.
7323+
</para>
7324+
7325+
<para>
7326+
While many scanner functions operate on source code files by
7327+
looking for known patterns in the code, they can really
7328+
do anything they need to.
7329+
For example, the &b-link-Program; Builder is assigned a
7330+
<xref linkend="target_scanner"/> which examines the
7331+
list of libraries supplied for the build (&cv-link-LIBS;)
7332+
and decides whether to add them as dependencies,
7333+
it does not look <emphasis>inside</emphasis> the built binary.
7334+
</para>
7335+
7336+
<para>
7337+
It is up to the scanner function to decide whether or not to
7338+
generate an &SCons; dependency for candidates identified by scanning.
7339+
Dependencies are a key part of &SCons; operation,
7340+
enabling both rebuild determination and correct ordering of builds.
7341+
It is particularly important that generated files which are
7342+
dependencies are added into the Node graph,
7343+
or use-before-create failures are likely.
7344+
However, not everything may need to be tracked as a dependency.
7345+
In some cases, implementation-provided header files change
7346+
infrequently but are included very widely,
7347+
so tracking them in the &SCons; node graph could become quite
7348+
expensive for limited benefit -
7349+
consider for example the C standard header file
7350+
<filename>string.h</filename>.
7351+
The scanner function is not passed any special information
7352+
to help make this choice, so the decision making encoded
7353+
in the scanner function must be carefully considered.
7354+
</para>
7355+
73167356
</listitem>
73177357
</varlistentry>
73187358

@@ -7325,7 +7365,7 @@ The default value is <literal>"NONE"</literal>.</para>
73257365
</listitem>
73267366
</varlistentry>
73277367

7328-
<varlistentry>
7368+
<varlistentry id="scanner-argument">
73297369
<term><parameter>argument</parameter></term>
73307370
<listitem>
73317371
<para>If specified,
@@ -7339,7 +7379,7 @@ as the optional parameter each of those functions takes.
73397379
</listitem>
73407380
</varlistentry>
73417381

7342-
<varlistentry>
7382+
<varlistentry id="skeys">
73437383
<term><parameter>skeys</parameter></term>
73447384
<listitem>
73457385
<para>Scanner key(s) indicating the file types
@@ -7355,27 +7395,42 @@ it will be expanded into a list by the current environment.
73557395
</listitem>
73567396
</varlistentry>
73577397

7358-
<varlistentry>
7398+
<varlistentry id="path_function">
73597399
<term><parameter>path_function</parameter></term>
73607400
<listitem>
7361-
<para>A Python function that takes four or five arguments:
7401+
<para>
7402+
If specified, a function to generate paths to pass to
7403+
the scanner function to search while generating dependencies.
7404+
The function must take five arguments:
73627405
a &consenv;,
73637406
a Node for the directory containing
73647407
the &SConscript; file in which
73657408
the first target was defined,
73667409
a list of target nodes,
73677410
a list of source nodes,
73687411
and the value of <parameter>argument</parameter>
7369-
if it was supplied when the Scanner was created.
7412+
if it was supplied when the Scanner was created
7413+
(since <parameter>argument</parameter> is optional,
7414+
the function may be called without this argument,
7415+
the <parameter>path_function</parameter>
7416+
should be prepared for this).
73707417
Must return a tuple of directories
73717418
that can be searched for files to be returned
73727419
by this Scanner object.
7373-
(Note that the
7374-
&f-link-FindPathDirs;
7375-
function can be used to return a ready-made
7420+
</para>
7421+
7422+
<para>
7423+
The &f-link-FindPathDirs;
7424+
function can be called to return a ready-made
73767425
<parameter>path_function</parameter>
73777426
for a given &consvar; name,
7378-
instead of having to write your own function from scratch.)
7427+
which is often easier than writing your own function from scratch.
7428+
For example,
7429+
<userinput>path_function=FindPathDirs('CPPPATH')</userinput>
7430+
means the scanner function will be called with the paths extracted
7431+
from &cv-CPPPATH; in the &consenv; <parameter>env</parameter>,
7432+
and passed as the <parameter>path</parameter> parameter
7433+
to the scanner function.
73797434
</para>
73807435
</listitem>
73817436
</varlistentry>

0 commit comments

Comments
 (0)