Commit 6eee113e authored by Mike Hommey's avatar Mike Hommey
Browse files

Bug 1259960 - Make check_prog more flexible about the list of programs it will...

Bug 1259960 - Make check_prog more flexible about the list of programs it will check. r=chmanchester
parent 028ebf69
Loading
Loading
Loading
Loading
+17 −10
Original line number Diff line number Diff line
@@ -72,6 +72,8 @@ def checking(what, callback=None):
# - `var` is the name of the variable that will be set with `set_config` when
#   the program is found.
# - `progs` is a list (or tuple) of program names that will be searched for.
#   It can also be a reference to a @depends function that returns such a
#   list. If the list is empty and there is no input, the check is skipped.
# - `what` is a human readable description of what is being looked for. It
#   defaults to the lowercase version of `var`.
# - `input` is a string reference to an existing option or a reference to a
@@ -90,6 +92,7 @@ def checking(what, callback=None):
@advanced
def check_prog(var, progs, what=None, input=None, allow_missing=False):
    from mozbuild.shellutil import quote
    from mozbuild.configure import DependsFunction

    if input:
        # Wrap input with type checking and normalization.
@@ -109,16 +112,20 @@ def check_prog(var, progs, what=None, input=None, allow_missing=False):
        input = var
    what = what or var.lower()

    if not isinstance(progs, (tuple, list)):
        configure_error('progs should be a list or tuple!')
    progs = list(progs)
    if not isinstance(progs, DependsFunction):
        # Trick to make a @depends function out of an immediate value.
        progs = depends('--help')(lambda h: progs)

    @depends(input)
    @depends_if(input, progs)
    @checking('for %s' % what, lambda x: quote(x) if x else 'not found')
    def check(value):
        if value:
            progs[:] = value
        for prog in progs:
    def check(value, progs):
        if progs is None:
            progs = ()

        if not isinstance(progs, (tuple, list)):
            configure_error('progs must resolve to a list or tuple!')

        for prog in value or progs:
            log.debug('%s: Trying %s', var.lower(), quote(prog))
            result = find_program(prog)
            if result:
@@ -127,8 +134,8 @@ def check_prog(var, progs, what=None, input=None, allow_missing=False):
        if not allow_missing or value:
            raise FatalCheckError('Cannot find %s' % what)

    @depends(check)
    def normalized_for_config(value):
    @depends_if(check, progs)
    def normalized_for_config(value, progs):
        return ':' if value is None else value

    set_config(var, normalized_for_config)
+66 −1
Original line number Diff line number Diff line
@@ -201,12 +201,77 @@ class TestChecksConfigure(unittest.TestCase):
        self.assertEqual(config, {'CC': '/usr/local/bin/known-b'})
        self.assertEqual(out, 'checking for cc... /usr/local/bin/known-b\n')

    def test_check_prog_progs(self):
        config, out, status = self.get_result(
            'check_prog("FOO", ())')
        self.assertEqual(status, 0)
        self.assertEqual(config, {})
        self.assertEqual(out, '')

        config, out, status = self.get_result(
            'check_prog("FOO", ())', ['FOO=known-a'])
        self.assertEqual(status, 0)
        self.assertEqual(config, {'FOO': '/usr/bin/known-a'})
        self.assertEqual(out, 'checking for foo... /usr/bin/known-a\n')

        script = (
            'option(env="TARGET", nargs=1, default="linux", help="target")\n'
            '@depends("TARGET")\n'
            'def compiler(value):\n'
            '    if value:\n'
            '        if value[0] == "linux":\n'
            '            return ("gcc", "clang")\n'
            '        if value[0] == "winnt":\n'
            '            return ("cl", "clang-cl")\n'
            'check_prog("CC", compiler)'
        )
        config, out, status = self.get_result(script)
        self.assertEqual(status, 1)
        self.assertEqual(config, {})
        self.assertEqual(out, 'checking for cc... not found\n'
                              'DEBUG: cc: Trying gcc\n'
                              'DEBUG: cc: Trying clang\n'
                              'ERROR: Cannot find cc\n')

        config, out, status = self.get_result(script, ['TARGET=linux'])
        self.assertEqual(status, 1)
        self.assertEqual(config, {})
        self.assertEqual(out, 'checking for cc... not found\n'
                              'DEBUG: cc: Trying gcc\n'
                              'DEBUG: cc: Trying clang\n'
                              'ERROR: Cannot find cc\n')

        config, out, status = self.get_result(script, ['TARGET=winnt'])
        self.assertEqual(status, 1)
        self.assertEqual(config, {})
        self.assertEqual(out, 'checking for cc... not found\n'
                              'DEBUG: cc: Trying cl\n'
                              'DEBUG: cc: Trying clang-cl\n'
                              'ERROR: Cannot find cc\n')

        config, out, status = self.get_result(script, ['TARGET=none'])
        self.assertEqual(status, 0)
        self.assertEqual(config, {})
        self.assertEqual(out, '')

        config, out, status = self.get_result(script, ['TARGET=winnt',
                                                       'CC=known-a'])
        self.assertEqual(status, 0)
        self.assertEqual(config, {'CC': '/usr/bin/known-a'})
        self.assertEqual(out, 'checking for cc... /usr/bin/known-a\n')

        config, out, status = self.get_result(script, ['TARGET=none',
                                                       'CC=known-a'])
        self.assertEqual(status, 0)
        self.assertEqual(config, {'CC': '/usr/bin/known-a'})
        self.assertEqual(out, 'checking for cc... /usr/bin/known-a\n')

    def test_check_prog_configure_error(self):
        with self.assertRaises(ConfigureError) as e:
            self.get_result('check_prog("FOO", "foo")')

        self.assertEqual(e.exception.message,
                         'progs should be a list or tuple!')
                         'progs must resolve to a list or tuple!')

        with self.assertRaises(ConfigureError) as e:
            self.get_result(