scons icon indicating copy to clipboard operation
scons copied to clipboard

Improve API to detect bad options

Open dmoody256 opened this issue 3 years ago • 1 comments

brought to discord: https://discord.com/channels/571796279483564041/571796280146133047/996112461126185053

Describe the Feature Mongodb uses an 'append' type option --variables-files to allow users to load preset custom files which setup variables. This is used for things like compiler setup, version setup, etc. There are certain configure checks that will only work if the correct variables are in place for example, the system compiler is too old, so the variables files load the mongodb custom toolchain (which our developers install on their systems).

Let say for example, when the user makes a typo, for example --variable-files is missing an s, the configure check will fail. The build will exit saying the configure check failed. The problem is, users expect use of any invalid options to display an error message saying they gave an option which is not valid, so they don't understand how the configure check could have failed.

Required information

  • Link to SCons Users thread discussing your issue. https://discord.com/channels/571796279483564041/571796280146133047/996112461126185053
  • Version of SCons 3.1.2, 4.3.0
  • Version of Python python 3.8
  • Which python distribution if applicable (python.org, cygwin, anaconda, macports, brew,etc) python.org
  • How you installed SCons python -m pip install scons
  • What Platform are you on? (Linux/Windows and which version) linux
  • How to reproduce your issue? Please include a small self contained reproducer. Likely a SConstruct should do for most issues.

vars1_file.vars:

TEST1 = 1

vars2_file.vars;

TEST2 = 1

SConstruct:

def add_option(name, **kwargs):

    if 'dest' not in kwargs:
        kwargs['dest'] = name

    if 'metavar' not in kwargs and kwargs.get('type', None) == 'choice':
        kwargs['metavar'] = '[' + '|'.join(kwargs['choices']) + ']'

    AddOption('--' + name, **kwargs)

add_option(
    'variables-files',
    default=[],
    action="append",
    help="Specify variables files to load.",
)
print(GetOption('variables-files'))
env_vars = Variables(
    files=GetOption('variables-files'),
    args=ARGUMENTS,
)

env_vars.Add("TEST1")
env_vars.Add("TEST2")

env = Environment()
env_vars.Update(env)

def CustomCheck(target, source, env):
    if env.get("TEST1") and env.get("TEST2"):
        # you may build
        with open(target, 'w') as f:
            f.write('success')
        return True
    else:
        return False

conf = env.Configure()
if conf.TryAction(CustomCheck, str(env.get('TEST1', '0')) + str(env.get('TEST2', '0')), ".test")[0]:
    print("CONFIGURE CHECK FAILED")
    exit(1)

with open('in.txt', 'w') as f:
    f.write('text')
env.Command('out.txt', 'in.txt', 'echo $SOURCE > $TARGET')
  • How you invoke scons (The command line you're using "scons --flags some_arguments") execute the commands:
# no typo passes:
scons --variables-files=vars1_file.vars --variables-files=vars2_file.vars --config=force
# vars2 missing s, fails configure check
scons --variables-files=vars1_file.vars --variable-files=vars2_file.vars --config=force

Additional context Mongodb is working around with a single self parsing check at a certain point in the build by digging down into scons argparse and getting the current valid options and comparing them to sys.argv.

dmoody256 avatar Jul 12 '22 03:07 dmoody256

How about something like AddOptionFinished() ? So you can tell SCons when you're done adding options and then it can do sanity checks?

bdbaddog avatar Jul 12 '22 11:07 bdbaddog