Skip to content

Commit a99da7c

Browse files
authored
Merge pull request SCons#4553 from mwichmann/clone-variables
Teach Clone() to respect the variables= kwarg.
2 parents 2566498 + 68eb885 commit a99da7c

File tree

5 files changed

+109
-16
lines changed

5 files changed

+109
-16
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER
8585
Now matches the annotation and docstring (which were prematurely
8686
updated in 4.6). All SCons usage except unit test was already fully
8787
consistent with a bool.
88+
- The Clone() method now respects the variables argument (fixes #3590)
8889

8990

9091
RELEASE 4.7.0 - Sun, 17 Mar 2024 17:22:20 -0700

RELEASE.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ FIXES
5858
- Improved the conversion of a "foreign" exception from an action
5959
into BuildError by making sure our defaults get applied even in
6060
corner cases. Fixes Issue #4530
61+
- The Clone() method now respects the variables argument (fixes #3590)
6162

6263
IMPROVEMENTS
6364
------------

SCons/Environment.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,16 +1568,28 @@ def AppendUnique(self, delete_existing: bool=False, **kw) -> None:
15681568
self._dict[key] = dk + val
15691569
self.scanner_map_delete(kw)
15701570

1571-
def Clone(self, tools=[], toolpath=None, parse_flags = None, **kw):
1571+
def Clone(self, tools=[], toolpath=None, variables=None, parse_flags=None, **kw):
15721572
"""Return a copy of a construction Environment.
15731573
1574-
The copy is like a Python "deep copy"--that is, independent
1575-
copies are made recursively of each objects--except that
1576-
a reference is copied when an object is not deep-copyable
1577-
(like a function). There are no references to any mutable
1578-
objects in the original Environment.
1579-
"""
1574+
The copy is like a Python "deep copy": independent copies are made
1575+
recursively of each object, except that a reference is copied when
1576+
an object is not deep-copyable (like a function). There are no
1577+
references to any mutable objects in the original environment.
1578+
1579+
Unrecognized keyword arguments are taken as construction variable
1580+
assignments.
15801581
1582+
Arguments:
1583+
tools: list of tools to initialize.
1584+
toolpath: list of paths to search for tools.
1585+
variables: a :class:`~SCons.Variables.Variables` object to
1586+
use to populate construction variables from command-line
1587+
variables.
1588+
parse_flags: option strings to parse into construction variables.
1589+
1590+
.. versionadded:: 4.8.0
1591+
The optional *variables* parameter was added.
1592+
"""
15811593
builders = self._dict.get('BUILDERS', {})
15821594

15831595
clone = copy.copy(self)
@@ -1603,6 +1615,8 @@ def Clone(self, tools=[], toolpath=None, parse_flags = None, **kw):
16031615
for key, value in kw.items():
16041616
new[key] = SCons.Subst.scons_subst_once(value, self, key)
16051617
clone.Replace(**new)
1618+
if variables:
1619+
variables.Update(clone)
16061620

16071621
apply_tools(clone, tools, toolpath)
16081622

SCons/Environment.xml

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,11 +1079,12 @@ Clean(docdir, os.path.join(docdir, projectname))
10791079
</arguments>
10801080
<summary>
10811081
<para>
1082-
Returns a separate copy of a construction environment.
1083-
If there are any keyword arguments specified,
1084-
they are added to the returned copy,
1082+
Returns an independent copy of a &consenv;.
1083+
If there are any unrecognized keyword arguments specified,
1084+
they are added as &consvars; in the copy,
10851085
overwriting any existing values
1086-
for the keywords.
1086+
for those keywords.
1087+
See the manpage section "Construction Environments" for more details.
10871088
</para>
10881089

10891090
<para>
@@ -1096,8 +1097,9 @@ env3 = env.Clone(CCFLAGS='-g')
10961097
</example_commands>
10971098

10981099
<para>
1099-
Additionally, a list of tools and a toolpath may be specified, as in
1100-
the &f-link-Environment; constructor:
1100+
A list of <parameter>tools</parameter>
1101+
and a <parameter>toolpath</parameter> may be specified,
1102+
as in the &f-link-Environment; constructor:
11011103
</para>
11021104

11031105
<example_commands>
@@ -1110,7 +1112,7 @@ env4 = env.Clone(tools=['msvc', MyTool])
11101112
<para>
11111113
The
11121114
<parameter>parse_flags</parameter>
1113-
keyword argument is also recognized to allow merging command-line
1115+
keyword argument is also recognized, to allow merging command-line
11141116
style arguments into the appropriate construction
11151117
variables (see &f-link-env-MergeFlags;).
11161118
</para>
@@ -1119,6 +1121,17 @@ variables (see &f-link-env-MergeFlags;).
11191121
# create an environment for compiling programs that use wxWidgets
11201122
wx_env = env.Clone(parse_flags='!wx-config --cflags --cxxflags')
11211123
</example_commands>
1124+
1125+
<para>
1126+
The <parameter>variables</parameter>
1127+
keyword argument is also recognized, to allow (re)initializing
1128+
&consvars; from a <literal>Variables</literal> object.
1129+
</para>
1130+
1131+
<para>
1132+
<emphasis>Changed in version 4.8.0:</emphasis>
1133+
the <parameter>variables</parameter> parameter was added.
1134+
</para>
11221135
</summary>
11231136
</scons_function>
11241137

@@ -1760,7 +1773,7 @@ will print:
17601773
</arguments>
17611774
<summary>
17621775
<para>
1763-
Return a new construction environment
1776+
Return a new &consenv;
17641777
initialized with the specified
17651778
<parameter>key</parameter>=<replaceable>value</replaceable>
17661779
pairs.
@@ -1770,7 +1783,8 @@ The keyword arguments
17701783
<parameter>toolpath</parameter>,
17711784
<parameter>tools</parameter>
17721785
and <parameter>variables</parameter>
1773-
are also specially recognized.
1786+
are specially recognized and do not lead to
1787+
&consvar; creation.
17741788
See the manpage section "Construction Environments" for more details.
17751789
</para>
17761790
</summary>

test/Clone-Variables.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
#
3+
# MIT License
4+
#
5+
# Copyright The SCons Foundation
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining
8+
# a copy of this software and associated documentation files (the
9+
# "Software"), to deal in the Software without restriction, including
10+
# without limitation the rights to use, copy, modify, merge, publish,
11+
# distribute, sublicense, and/or sell copies of the Software, and to
12+
# permit persons to whom the Software is furnished to do so, subject to
13+
# the following conditions:
14+
#
15+
# The above copyright notice and this permission notice shall be included
16+
# in all copies or substantial portions of the Software.
17+
#
18+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19+
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20+
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25+
26+
27+
"""
28+
Verify that Clone() respects the variables kwarg.
29+
30+
"""
31+
32+
import TestSCons
33+
34+
test = TestSCons.TestSCons()
35+
36+
test.write('SConstruct', """\
37+
vars = Variables()
38+
vars.Add(BoolVariable('MYTEST', 'help', default=False))
39+
40+
_ = DefaultEnvironment(tools=[])
41+
env = Environment(variables=vars, tools=[])
42+
print(f"MYTEST={env.Dictionary('MYTEST')}")
43+
env.Replace(MYTEST=True)
44+
print(f"MYTEST={env.Dictionary('MYTEST')}")
45+
env1 = env.Clone(variables=vars)
46+
print(f"MYTEST={env1.Dictionary('MYTEST')}")
47+
""")
48+
49+
expect = """\
50+
MYTEST=False
51+
MYTEST=True
52+
MYTEST=False
53+
"""
54+
55+
test.run(arguments = '-q -Q', stdout=expect)
56+
57+
test.pass_test()
58+
59+
# Local Variables:
60+
# tab-width:4
61+
# indent-tabs-mode:nil
62+
# End:
63+
# vim: set expandtab tabstop=4 shiftwidth=4:

0 commit comments

Comments
 (0)