| 1 |
# SPDX-License-Identifier: MIT |
| 2 |
# |
| 3 |
# Copyright The SCons Foundation |
| 4 |
|
| 5 |
"""Append/AppendUnique tests""" |
| 6 |
|
| 7 |
DefaultEnvironment(tools=[]) |
| 8 |
|
| 9 |
# Special cases: |
| 10 |
# https://github.com/SCons/scons/issues/1738 |
| 11 |
env_1738_2 = Environment(tools=['gcc'],CPPDEFPREFIX='-D') |
| 12 |
env_1738_2['CPPDEFINES'] = ['FOO'] |
| 13 |
env_1738_2.Append(CPPDEFINES={'value': '1'}) |
| 14 |
print(env_1738_2.subst('$_CPPDEFFLAGS')) |
| 15 |
# env_1738_2.Object('test_1738_2', 'main.c') |
| 16 |
|
| 17 |
# https://github.com/SCons/scons/issues/2300 |
| 18 |
env_2300_1 = Environment(tools=['gcc'],CPPDEFINES='foo', CPPDEFPREFIX='-D') |
| 19 |
env_2300_1.Append(CPPDEFINES='bar') |
| 20 |
print(env_2300_1.subst('$_CPPDEFFLAGS')) |
| 21 |
|
| 22 |
env_2300_2 = Environment(tools=['gcc'],CPPDEFINES=['foo'], CPPDEFPREFIX='-D') # note the list |
| 23 |
env_2300_2.Append(CPPDEFINES='bar') |
| 24 |
print(env_2300_2.subst('$_CPPDEFFLAGS')) |
| 25 |
|
| 26 |
# An initial space-separated string will be split, but not a string in a list. |
| 27 |
env_multi = Environment(tools=['gcc'],CPPDEFPREFIX='-D') |
| 28 |
env_multi['CPPDEFINES'] = "foo bar" |
| 29 |
env_multi.Append(CPPDEFINES="baz") |
| 30 |
print(env_multi.subst('$_CPPDEFFLAGS')) |
| 31 |
|
| 32 |
env_multi = Environment(tools=['gcc'],CPPDEFPREFIX='-D') |
| 33 |
env_multi['CPPDEFINES'] = ["foo bar"] |
| 34 |
env_multi.Append(CPPDEFINES="baz") |
| 35 |
print(env_multi.subst('$_CPPDEFFLAGS')) |
| 36 |
|
| 37 |
env_multi = Environment(tools=['gcc'],CPPDEFPREFIX='-D') |
| 38 |
env_multi['CPPDEFINES'] = "foo" |
| 39 |
env_multi.Append(CPPDEFINES=["bar baz"]) |
| 40 |
print(env_multi.subst('$_CPPDEFFLAGS')) |
| 41 |
|
| 42 |
env_multi = Environment(tools=['gcc'],CPPDEFPREFIX='-D') |
| 43 |
env_multi['CPPDEFINES'] = "foo" |
| 44 |
env_multi.Append(CPPDEFINES="bar baz") |
| 45 |
print(env_multi.subst('$_CPPDEFFLAGS')) |
| 46 |
|
| 47 |
# Check that AppendUnique(..., delete_existing=True) works as expected. |
| 48 |
# Each addition is in different but matching form, and different order |
| 49 |
# so we expect a reordered list, but with the same macro defines. |
| 50 |
env_multi = Environment(tools=['gcc'],CPPDEFPREFIX='-D') |
| 51 |
env_multi.Append(CPPDEFINES=["Macro1=Value1", ("Macro2", "Value2"), {"Macro3": "Value3"}, "Macro4"]) |
| 52 |
try: |
| 53 |
env_multi.AppendUnique(CPPDEFINES="Macro2=Value2", delete_existing=True) |
| 54 |
env_multi.AppendUnique(CPPDEFINES=[("Macro4", None)], delete_existing=True) |
| 55 |
env_multi.AppendUnique(CPPDEFINES=[("Macro3", "Value3")], delete_existing=True) |
| 56 |
env_multi.AppendUnique(CPPDEFINES={"Macro1": "Value1"}, delete_existing=True) |
| 57 |
except Exception as t: |
| 58 |
print(f"Prepend FAILED: {t}") |
| 59 |
else: |
| 60 |
print(env_multi.subst('$_CPPDEFFLAGS')) |
| 61 |
|
| 62 |
# A lone tuple handled differently than a lone list. |
| 63 |
env_multi = Environment(tools=['gcc'],CPPDEFPREFIX='-D', CPPDEFINES=("Macro1", "Value1")) |
| 64 |
print(env_multi.subst('$_CPPDEFFLAGS')) |
| 65 |
env_multi = Environment(tools=['gcc'],CPPDEFPREFIX='-D', CPPDEFINES=["Macro1", "Value1"]) |
| 66 |
print(env_multi.subst('$_CPPDEFFLAGS')) |
| 67 |
|
| 68 |
# https://github.com/SCons/scons/issues/1152 |
| 69 |
# https://github.com/SCons/scons/issues/2900 |
| 70 |
# Python3 dicts dont preserve order. Hence we supply subclass of OrderedDict |
| 71 |
# whose __str__ and __repr__ act like a normal dict. |
| 72 |
from collections import OrderedDict |
| 73 |
|
| 74 |
class OrderedPrintingDict(OrderedDict): |
| 75 |
def __repr__(self): |
| 76 |
return '{' + ', '.join([f'{k!r}: {v!r}' for (k, v) in self.items()]) + '}' |
| 77 |
|
| 78 |
__str__ = __repr__ |
| 79 |
|
| 80 |
# Because dict-like objects (except dict and UserDict) are not deep copied |
| 81 |
# directly when constructing Environment(CPPDEFINES=OrderedPrintingDict(...)) |
| 82 |
def __semi_deepcopy__(self): |
| 83 |
return self.copy() |
| 84 |
|
| 85 |
|
| 86 |
# each of these types will be appended to each of the others |
| 87 |
# the first item in each tuple is a label for the output |
| 88 |
cases = [ |
| 89 |
('string', 'FOO'), |
| 90 |
('valuestring', 'NAME1=VAL1'), |
| 91 |
('list', ['NAME1', 'NAME2', 'NAME3']), |
| 92 |
('tuple', ('NAME1', 'VAL1')), |
| 93 |
('list-of-2lists', [('NAME1', 'VAL1'), ['NAME2', 'VAL2']]), |
| 94 |
( |
| 95 |
'dict', # intentionally not sorted by key |
| 96 |
OrderedPrintingDict([('NAME2', 'VAL2'), ('NAME3', None), ('NAME1', 'VAL1')]), |
| 97 |
), |
| 98 |
] |
| 99 |
|
| 100 |
|
| 101 |
def dlist(coll): |
| 102 |
# if it's a deque, turn it into a list for display purposes |
| 103 |
from collections import deque |
| 104 |
|
| 105 |
if isinstance(coll, deque): |
| 106 |
return list(coll) |
| 107 |
return coll |
| 108 |
|
| 109 |
|
| 110 |
for (t1, c1) in cases: |
| 111 |
for (t2, c2) in cases: |
| 112 |
print(f"==== Testing CPPDEFINES, appending a {t2} to a {t1}") |
| 113 |
# string-like appearance if the value is a string |
| 114 |
orig = f"{c1!r}" if isinstance(c1, str) else c1 |
| 115 |
app = f"{c2!r}" if isinstance(c2, str) else c2 |
| 116 |
print(f" orig = {orig}, append = {app}") |
| 117 |
env = Environment(tools=['gcc'],CPPDEFINES=c1, CPPDEFPREFIX='-D') |
| 118 |
try: |
| 119 |
env.Append(CPPDEFINES=c2) |
| 120 |
final = env.subst('$_CPPDEFFLAGS', source="src", target="tgt") |
| 121 |
print(f"Append:\n result={dlist(env['CPPDEFINES'])}\n final={final}") |
| 122 |
except Exception as t: |
| 123 |
print(f"Append:\n FAILED: {t}") |
| 124 |
|
| 125 |
env = Environment(tools=['gcc'],CPPDEFINES=c1, CPPDEFPREFIX='-D') |
| 126 |
try: |
| 127 |
env.AppendUnique(CPPDEFINES=c2) |
| 128 |
final = env.subst('$_CPPDEFFLAGS', source="src", target="tgt") |
| 129 |
print( |
| 130 |
f"AppendUnique:\n result={dlist(env['CPPDEFINES'])}\n final={final}" |
| 131 |
) |
| 132 |
except Exception as t: |
| 133 |
print(f"AppendUnique:\n FAILED: {t}") |