# -*- python -*-
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__doc__ = """
Textfile/Substfile builder for SCons.

    Create file 'target' which typically is a textfile.  The 'source'
    may be any combination of strings, Nodes, or lists of same.  A
    'linesep' will be put between any part written and defaults to
    os.linesep.

    The only difference between the Textfile builder and the Substfile
    builder is that strings are converted to Value() nodes for the
    former and File() nodes for the latter.  To insert files in the
    former or strings in the latter, wrap them in a File() or Value(),
    respectively.

    The values of SUBST_DICT first have any construction variables
    expanded (its keys are not expanded).  If a value of SUBST_DICT is
    a python callable function, it is called and the result is expanded
    as the value.  Values are substituted in a "random" order; if any
    substitution could be further expanded by another substitution, it
    is unpredictable whether the expansion will occur.
"""

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import SCons

import os
import re

from SCons.Node import Node
from SCons.Node.Python import Value
from SCons.Util import is_String, is_Sequence, is_Dict, to_bytes


TEXTFILE_FILE_WRITE_MODE = 'w'

LINESEP = '\n'

def _do_subst(node, subs):
    """
    Fetch the node contents and replace all instances of the keys with
    their values.  For example, if subs is
        {'%VERSION%': '1.2345', '%BASE%': 'MyProg', '%prefix%': '/bin'},
    then all instances of %VERSION% in the file will be replaced with
    1.2345 and so forth.
    """
    contents = node.get_text_contents()
    if subs:
        for (k, val) in subs:
            contents = contents.replace(k, val)

    if 'b' in TEXTFILE_FILE_WRITE_MODE:
        try:
            contents = bytearray(contents, 'utf-8')
        except UnicodeDecodeError:
            # contents is already utf-8 encoded python 2 str i.e. a byte array
            contents = bytearray(contents)

    return contents


def _action(target, source, env):

    # prepare the line separator
    linesep = env['LINESEPARATOR']
    if linesep is None:
        linesep = LINESEP # os.linesep
    elif is_String(linesep):
        pass
    elif isinstance(linesep, Value):
        linesep = linesep.get_text_contents()
    else:
        raise SCons.Errors.UserError('unexpected type/class for LINESEPARATOR: %s'
                                     % repr(linesep), None)

    if 'b' in TEXTFILE_FILE_WRITE_MODE:
        linesep = to_bytes(linesep)

    # create a dictionary to use for the substitutions
    if 'SUBST_DICT' not in env:
        subs = None    # no substitutions
    else:
        subst_dict = env['SUBST_DICT']
        if is_Dict(subst_dict):
            subst_dict = list(subst_dict.items())
        elif is_Sequence(subst_dict):
            pass
        else:
            raise SCons.Errors.UserError('SUBST_DICT must be dict or sequence')
        subs = []
        for (k, value) in subst_dict:
            if callable(value):
                value = value()
            if is_String(value):
                value = env.subst(value)
            else:
                value = str(value)
            subs.append((k, value))

    # write the file
    try:
        target_file = open(target[0].get_path(), TEXTFILE_FILE_WRITE_MODE, newline='')
    except (OSError, IOError):
        raise SCons.Errors.UserError("Can't write target file %s" % target[0])

    # separate lines by 'linesep' only if linesep is not empty
    lsep = None
    for line in source:
        if lsep:
            target_file.write(lsep)

        target_file.write(_do_subst(line, subs))
        lsep = linesep
    target_file.close()


def _strfunc(target, source, env):
    return "Creating '%s'" % target[0]


def _convert_list_R(newlist, sources):
    for elem in sources:
        if is_Sequence(elem):
            _convert_list_R(newlist, elem)
        elif isinstance(elem, Node):
            newlist.append(elem)
        else:
            newlist.append(Value(elem))


def _convert_list(target, source, env):
    if len(target) != 1:
        raise SCons.Errors.UserError("Only one target file allowed")
    newlist = []
    _convert_list_R(newlist, source)
    return target, newlist


_common_varlist = ['SUBST_DICT', 'LINESEPARATOR']

_text_varlist = _common_varlist + ['TEXTFILEPREFIX', 'TEXTFILESUFFIX']
_text_builder = SCons.Builder.Builder(
    action=SCons.Action.Action(_action, _strfunc, varlist=_text_varlist),
    source_factory=Value,
    emitter=_convert_list,
    prefix='$TEXTFILEPREFIX',
    suffix='$TEXTFILESUFFIX',
)

_subst_varlist = _common_varlist + ['SUBSTFILEPREFIX', 'TEXTFILESUFFIX']
_subst_builder = SCons.Builder.Builder(
    action=SCons.Action.Action(_action, _strfunc, varlist=_subst_varlist),
    source_factory=SCons.Node.FS.File,
    emitter=_convert_list,
    prefix='$SUBSTFILEPREFIX',
    suffix='$SUBSTFILESUFFIX',
    src_suffix=['.in'],
)


def generate(env):
    env['LINESEPARATOR'] = LINESEP # os.linesep
    env['BUILDERS']['Textfile'] = _text_builder
    env['TEXTFILEPREFIX'] = ''
    env['TEXTFILESUFFIX'] = '.txt'
    env['BUILDERS']['Substfile'] = _subst_builder
    env['SUBSTFILEPREFIX'] = ''
    env['SUBSTFILESUFFIX'] = ''


def exists(env):
    return 1

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4:

src/engine/SCons/Tool/textfile.py
# -*- python -*-
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__doc__ = """
Textfile/Substfile builder for SCons.

    Create file 'target' which typically is a textfile.  The 'source'
    may be any combination of strings, Nodes, or lists of same.  A
    'linesep' will be put between any part written and defaults to
    os.linesep.

    The only difference between the Textfile builder and the Substfile
    builder is that strings are converted to Value() nodes for the
    former and File() nodes for the latter.  To insert files in the
    former or strings in the latter, wrap them in a File() or Value(),
    respectively.

    The values of SUBST_DICT first have any construction variables
    expanded (its keys are not expanded).  If a value of SUBST_DICT is
    a python callable function, it is called and the result is expanded
    as the value.  Values are substituted in a "random" order; if any
    substitution could be further expanded by another substitution, it
    is unpredictable whether the expansion will occur.
"""

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import SCons

import os
import re

from SCons.Node import Node
from SCons.Node.Python import Value
from SCons.Util import is_String, is_Sequence, is_Dict, to_bytes


TEXTFILE_FILE_WRITE_MODE = 'w'

LINESEP = '\n'

def _do_subst(node, subs):
    """
    Fetch the node contents and replace all instances of the keys with
    their values.  For example, if subs is
        {'%VERSION%': '1.2345', '%BASE%': 'MyProg', '%prefix%': '/bin'},
    then all instances of %VERSION% in the file will be replaced with
    1.2345 and so forth.
    """
    contents = node.get_text_contents()
    if subs:
        for (k, val) in subs:
            contents = contents.replace(k, val)

    if 'b' in TEXTFILE_FILE_WRITE_MODE:
        try:
            contents = bytearray(contents, 'utf-8')
        except UnicodeDecodeError:
            # contents is already utf-8 encoded python 2 str i.e. a byte array
            contents = bytearray(contents)

    return contents


def _action(target, source, env):

    # prepare the line separator
    linesep = env['LINESEPARATOR']
    if linesep is None:
        linesep = LINESEP # os.linesep
    elif is_String(linesep):
        pass
    elif isinstance(linesep, Value):
        linesep = linesep.get_text_contents()
    else:
        raise SCons.Errors.UserError('unexpected type/class for LINESEPARATOR: %s'
                                     % repr(linesep), None)

    if 'b' in TEXTFILE_FILE_WRITE_MODE:
        linesep = to_bytes(linesep)

    # create a dictionary to use for the substitutions
    if 'SUBST_DICT' not in env:
        subs = None    # no substitutions
    else:
        subst_dict = env['SUBST_DICT']
        if is_Dict(subst_dict):
            subst_dict = list(subst_dict.items())
        elif is_Sequence(subst_dict):
            pass
        else:
            raise SCons.Errors.UserError('SUBST_DICT must be dict or sequence')
        subs = []
        for (k, value) in subst_dict:
            if callable(value):
                value = value()
            if is_String(value):
                value = env.subst(value)
            else:
                value = str(value)
            subs.append((k, value))

    # write the file
    try:
        target_file = open(target[0].get_path(), TEXTFILE_FILE_WRITE_MODE, newline='')
    except (OSError, IOError):
        raise SCons.Errors.UserError("Can't write target file %s" % target[0])

    # separate lines by 'linesep' only if linesep is not empty
    lsep = None
    for line in source:
        if lsep:
            target_file.write(lsep)

        target_file.write(_do_subst(line, subs))
        lsep = linesep
    target_file.close()


def _strfunc(target, source, env):
    return "Creating '%s'" % target[0]


def _convert_list_R(newlist, sources):
    for elem in sources:
        if is_Sequence(elem):
            _convert_list_R(newlist, elem)
        elif isinstance(elem, Node):
            newlist.append(elem)
        else:
            newlist.append(Value(elem))


def _convert_list(target, source, env):
    if len(target) != 1:
        raise SCons.Errors.UserError("Only one target file allowed")
    newlist = []
    _convert_list_R(newlist, source)
    return target, newlist


_common_varlist = ['SUBST_DICT', 'LINESEPARATOR']

_text_varlist = _common_varlist + ['TEXTFILEPREFIX', 'TEXTFILESUFFIX']
_text_builder = SCons.Builder.Builder(
    action=SCons.Action.Action(_action, _strfunc, varlist=_text_varlist),
    source_factory=Value,
    emitter=_convert_list,
    prefix='$TEXTFILEPREFIX',
    suffix='$TEXTFILESUFFIX',
)

_subst_varlist = _common_varlist + ['SUBSTFILEPREFIX', 'SUBSTFILESUFFIX']
_subst_builder = SCons.Builder.Builder(
    action=SCons.Action.Action(_action, _strfunc, varlist=_subst_varlist),
    source_factory=SCons.Node.FS.File,
    emitter=_convert_list,
    prefix='$SUBSTFILEPREFIX',
    suffix='$SUBSTFILESUFFIX',
    src_suffix=['.in'],
)


def generate(env):
    env['LINESEPARATOR'] = LINESEP # os.linesep
    env['BUILDERS']['Textfile'] = _text_builder
    env['TEXTFILEPREFIX'] = ''
    env['TEXTFILESUFFIX'] = '.txt'
    env['BUILDERS']['Substfile'] = _subst_builder
    env['SUBSTFILEPREFIX'] = ''
    env['SUBSTFILESUFFIX'] = ''


def exists(env):
    return 1

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4:

src/engine/SCons/Tool/textfile.py
diff --git a/src/CHANGES.txt b/src/CHANGES.txt
index 025e046a66..574f64bd81 100755
--- a/src/CHANGES.txt
+++ b/src/CHANGES.txt
@@ -74,6 +74,7 @@ RELEASE  VERSION/DATE TO BE FILLED IN LATER
     - Add an alternate warning message cl.exe is not found and msvc config
       cache is in use (SCONS_CACHE_MSVC_CONFIG was given) - config cache
       may be out of date.
+    - Fixed bug where changing TEXTFILESUFFIX would cause Substfile() to rebuild. (Github Issue #3540)
     - Script/Main.py now uses importlib instead of imp module.
 
 
diff --git a/src/engine/SCons/Tool/textfile.py b/src/engine/SCons/Tool/textfile.py
index c233658180..b404304aca 100644
--- a/src/engine/SCons/Tool/textfile.py
+++ b/src/engine/SCons/Tool/textfile.py
@@ -171,7 +171,7 @@ def _convert_list(target, source, env):
     suffix='$TEXTFILESUFFIX',
 )
 
-_subst_varlist = _common_varlist + ['SUBSTFILEPREFIX', 'TEXTFILESUFFIX']
+_subst_varlist = _common_varlist + ['SUBSTFILEPREFIX', 'SUBSTFILESUFFIX']
 _subst_builder = SCons.Builder.Builder(
     action=SCons.Action.Action(_action, _strfunc, varlist=_subst_varlist),
     source_factory=SCons.Node.FS.File,
diff --git a/test/textfile/fixture/SConstruct.issue-3540 b/test/textfile/fixture/SConstruct.issue-3540
new file mode 100644
index 0000000000..021689a258
--- /dev/null
+++ b/test/textfile/fixture/SConstruct.issue-3540
@@ -0,0 +1,23 @@
+"""
+Test for GH Issue 3540
+
+textfile()'s action is not sensitive to changes in TEXTFILESUFFIX (rather was sensitive to SUBSTFILESUFFIX)
+
+"""
+
+DefaultEnvironment(tools=[])
+
+text_file_suffix = ARGUMENTS.get('text_file_suffix', 'DEFAULTSUFFIX')
+
+env = Environment(tools=['textfile'],
+                  TEXTFILESUFFIX=text_file_suffix)
+
+env['FOO_PATH'] = "test-value-1"
+
+foo = env.Substfile(
+    target="substfile",
+    source="substfile.in",
+    SUBST_DICT={
+        "@foo_path@": "$FOO_PATH",
+    }
+)
diff --git a/test/textfile/fixture/SConstruct.issue-3550 b/test/textfile/fixture/SConstruct.issue-3550
index 8502717bb8..0239ade045 100644
--- a/test/textfile/fixture/SConstruct.issue-3550
+++ b/test/textfile/fixture/SConstruct.issue-3550
@@ -4,8 +4,8 @@ env = Environment(tools=['textfile'])
 env['FOO_PATH'] = r'Z:\mongo\build\install\bin'
 
 foo = env.Substfile(
-    target="foo",
-    source="foo-3550.in",
+    target="substfile",
+    source="substfile.in",
     SUBST_DICT={
         "@foo_path@": "$FOO_PATH",
     }
diff --git a/test/textfile/fixture/foo-3550.in b/test/textfile/fixture/substfile.in
similarity index 100%
rename from test/textfile/fixture/foo-3550.in
rename to test/textfile/fixture/substfile.in
diff --git a/test/textfile/issue-3540.py b/test/textfile/issue-3540.py
new file mode 100644
index 0000000000..b53f06a24b
--- /dev/null
+++ b/test/textfile/issue-3540.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+#
+# __COPYRIGHT__
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+
+__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+"""
+Test for GH Issue 3540
+
+textfile()'s action is not sensitive to changes in TEXTFILEPREFIX (rather was sensitive to SUBSTFILEPREFIX)
+
+"""
+
+import TestSCons
+
+test = TestSCons.TestSCons()
+
+# test.verbose_set(1)
+
+test.file_fixture('fixture/substfile.in', 'substfile.in')
+test.file_fixture('fixture/SConstruct.issue-3540', 'SConstruct')
+
+test.run()
+test.must_exist('substfile')
+
+test.up_to_date(options='text_file_suffix=BLAH')
+
+
+test.pass_test()
diff --git a/test/textfile/issue-3550.py b/test/textfile/issue-3550.py
index 60430edede..5951d1ba54 100644
--- a/test/textfile/issue-3550.py
+++ b/test/textfile/issue-3550.py
@@ -23,21 +23,27 @@
 #
 
 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+"""
+Test for GH Issue 3550
+
+ You can't pass a Windows path in a value to be interpolated, 
+ because SCons will try to pass it through re.sub which rejects certain character sequences.
+"""
 
 import TestSCons
 
 test = TestSCons.TestSCons()
 
-test.verbose_set(1)
+# test.verbose_set(1)
 
 match_mode = 'r'
 
 test.file_fixture('fixture/SConstruct.issue-3550', 'SConstruct')
-test.file_fixture('fixture/foo-3550.in', 'foo-3550.in')
+test.file_fixture('fixture/substfile.in', 'substfile.in')
 
 test.run(arguments='.')
 
-test.must_match('foo',
+test.must_match('substfile',
                 r'''foo_path: Z:\mongo\build\install\bin
 ''', mode=match_mode)
 

['SUBSTFILEPREFIX', 'TEXTFILESUFFIX']

**Describe the bug**
From git head, just now.  
File: src/engine/SCons/Tool/textfile.py
Line 165:
_text_varlist = _common_varlist + ['TEXTFILEPREFIX', 'TEXTFILESUFFIX']

Line 174:
_subst_varlist = _common_varlist + ['SUBSTFILEPREFIX', 'TEXTFILESUFFIX']

Looks like a cur/paste/edit error.  My guess, from the rest of the code, is that
 'TEXTFILESUFFIX' should be  'SUBSTFILESUFFIX' on line 174

