From 8c2cf586b6cad4d028838c54d1a25e3320585da7 Mon Sep 17 00:00:00 2001 From: Joseph Brill <48932340+jcbrill@users.noreply.github.com> Date: Mon, 21 Mar 2022 12:31:29 -0400 Subject: Bypass msvc detection and environment processing with a user-provided dictionary via the MSVC_USE_SETTINGS variable. MSVC_USE_SETTINGS is ignored when MSVC_USE_SCRIPT is defined or MSVC_USE_SETTINGS is None. Raise an MSVCUseSettingsError exception if MSVC_USE_SETTINGS is not a dictionary upon usage. --- SCons/Tool/MSCommon/vc.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/SCons/Tool/MSCommon/vc.py b/SCons/Tool/MSCommon/vc.py index fe31cb3..860ffdd 100644 --- a/SCons/Tool/MSCommon/vc.py +++ b/SCons/Tool/MSCommon/vc.py @@ -79,6 +79,9 @@ class BatchFileExecutionError(VisualCException): class MSVCScriptNotFound(VisualCException): pass +class MSVCUseSettingsError(VisualCException): + pass + # Dict to 'canonalize' the arch _ARCH_TO_CANONICAL = { "amd64" : "amd64", @@ -920,6 +923,39 @@ def msvc_find_valid_batch_script(env, version): return d +_undefined = None + +def get_use_script_use_settings(env): + global _undefined + + if _undefined is None: + _undefined = object() + + # use_script use_settings return values + # value n/a (value, None) + # undefined undefined (True, None) + # undefined None (True, None) + # undefined not None (False, value) + + use_script = env.get('MSVC_USE_SCRIPT', _undefined) + use_settings = env.get('MSVC_USE_SETTINGS', _undefined) + + if use_script != _undefined: + # use_script defined, use_settings ignored + use_settings = None + elif use_settings == _undefined: + # use_script undefined, use_settings undefined + use_script = True + use_settings = None + elif use_settings is None: + # use script undefined, use_settings defined (None) + use_script = True + else: + # use script undefined, use_settings defined (not None) + use_script = False + + return use_script, use_settings + def msvc_setup_env(env): debug('called') @@ -937,7 +973,7 @@ def msvc_setup_env(env): env['MSVS'] = {} - use_script = env.get('MSVC_USE_SCRIPT', True) + use_script, use_settings = get_use_script_use_settings(env) if SCons.Util.is_String(use_script): use_script = use_script.strip() if not os.path.exists(use_script): @@ -950,6 +986,12 @@ def msvc_setup_env(env): debug('use_script 2 %s', d) if not d: return d + elif use_settings is not None: + if not SCons.Util.is_Dict(use_settings): + error_msg = 'MSVC_USE_SETTINGS type error: expected a dictionary, found {}'.format(type(use_settings).__name__) + raise MSVCUseSettingsError(error_msg) + d = use_settings + debug('use_settings %s', d) else: debug('MSVC_USE_SCRIPT set to False') warn_msg = "MSVC_USE_SCRIPT set to False, assuming environment " \ -- cgit v0.12 From 253a06db97698f0da1abab5d69807dc6faac1001 Mon Sep 17 00:00:00 2001 From: Joseph Brill <48932340+jcbrill@users.noreply.github.com> Date: Tue, 22 Mar 2022 10:08:24 -0400 Subject: Combine MSVC_USE_SETTINGS undefined and None. Additional comments describing behavior. --- SCons/Tool/MSCommon/vc.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/SCons/Tool/MSCommon/vc.py b/SCons/Tool/MSCommon/vc.py index 860ffdd..e5f229d 100644 --- a/SCons/Tool/MSCommon/vc.py +++ b/SCons/Tool/MSCommon/vc.py @@ -931,27 +931,26 @@ def get_use_script_use_settings(env): if _undefined is None: _undefined = object() - # use_script use_settings return values - # value n/a (value, None) - # undefined undefined (True, None) - # undefined None (True, None) - # undefined not None (False, value) + # use_script use_settings return values + # value ignore (value, None) + # undefined undefined/None (True, None) + # undefined value not None (False, value) + # None (documentation) or evaluates False (code): bypass detection + # distinguish between undefined and defined as evaluates False use_script = env.get('MSVC_USE_SCRIPT', _undefined) - use_settings = env.get('MSVC_USE_SETTINGS', _undefined) + + # undefined or None: use settings ignored + use_settings = env.get('MSVC_USE_SETTINGS', None) if use_script != _undefined: - # use_script defined, use_settings ignored - use_settings = None - elif use_settings == _undefined: - # use_script undefined, use_settings undefined - use_script = True + # use_script defined, use_settings ignored (not type checked) use_settings = None elif use_settings is None: - # use script undefined, use_settings defined (None) + # use script undefined, use_settings undefined or None use_script = True else: - # use script undefined, use_settings defined (not None) + # use script undefined, use_settings defined and not None (type checked) use_script = False return use_script, use_settings -- cgit v0.12 From 39fa3174f45a03f34c3ff6137f709de5084ab2cd Mon Sep 17 00:00:00 2001 From: Joseph Brill <48932340+jcbrill@users.noreply.github.com> Date: Fri, 25 Mar 2022 09:04:07 -0400 Subject: Add documentation for MSVC_USE_SETTINGS. --- SCons/Tool/msvc.xml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/SCons/Tool/msvc.xml b/SCons/Tool/msvc.xml index 2b2b070..0dad6eb 100644 --- a/SCons/Tool/msvc.xml +++ b/SCons/Tool/msvc.xml @@ -422,6 +422,31 @@ Provides arguments passed to the script &cv-link-MSVC_USE_SCRIPT;. + + + +Use a dictionary to set up the Microsoft Visual C++ compiler. + + +&cv-MSVC_USE_SETTINGS; is ignored when &cv-link-MSVC_USE_SCRIPT; is defined +and/or when &cv-MSVC_USE_SETTINGS; is set to None. + + +The dictionary is used to populate the environment with the relevant variables +(typically %INCLUDE%, %LIB%, and %PATH%) +for supplying to the build. This can be useful to force the use of a compiler environment +that &SCons; does not configure correctly. This is an alternative to manually configuring +the environment when bypassing Visual Studio autodetection entirely by setting +&cv-link-MSVC_USE_SCRIPT; to None. + + +Note: the dictionary content requirements are based on the internal msvc implementation and +therefore may change at any time. The burden is on the user to ensure the dictionary contents +are minimally sufficient to ensure successful builds. + + + +