Esempio n. 1
0
    def test_set_ld_unknown(self):
        version = ('g++ (Ubuntu 5.4.0-6ubuntu1~16.04.6) 5.4.0 20160609\n' +
                   'Copyright (C) 2015 Free Software Foundation, Inc.')

        self.env.variables['LD'] = '/usr/bin/ld.goofy'
        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', mock_execute), \
             mock.patch('logging.log'):  # noqa
            cc = CcBuilder(self.env, known_langs['c++'], ['g++'], version)
        self.assertEqual(cc.linker('executable').command, ['g++'])
Esempio n. 2
0
    def test_properties(self):
        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
            cc = CcBuilder(self.env, known_langs['c++'], ['c++'], 'version')

        self.assertEqual(cc.flavor, 'cc')
        self.assertEqual(cc.compiler.flavor, 'cc')
        self.assertEqual(cc.pch_compiler.flavor, 'cc')
        self.assertEqual(cc.linker('executable').flavor, 'cc')
        self.assertEqual(cc.linker('shared_library').flavor, 'cc')
        self.assertEqual(cc.linker('raw').flavor, 'ld')

        self.assertEqual(cc.family, 'native')
        self.assertEqual(cc.auto_link, False)
        self.assertEqual(cc.can_dual_link, True)

        self.assertEqual(cc.compiler.num_outputs, 'all')
        self.assertEqual(cc.pch_compiler.num_outputs, 'all')
        self.assertEqual(cc.linker('executable').num_outputs, 'all')

        num_out = 2 if self.env.target_platform.has_import_library else 'all'
        self.assertEqual(cc.linker('shared_library').num_outputs, num_out)

        self.assertEqual(cc.compiler.deps_flavor, 'gcc')
        self.assertEqual(cc.pch_compiler.deps_flavor, 'gcc')

        self.assertEqual(cc.compiler.needs_libs, False)
        self.assertEqual(cc.pch_compiler.needs_libs, False)

        self.assertEqual(cc.compiler.accepts_pch, True)
        self.assertEqual(cc.pch_compiler.accepts_pch, False)

        self.assertRaises(KeyError, lambda: cc.linker('unknown'))
Esempio n. 3
0
class TestCcPackageResolver(CrossPlatformTestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(clear_variables=True, *args, **kwargs)

    def setUp(self):
        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
            self.packages = CcBuilder(self.env, known_langs['c++'], ['c++'],
                                      'version').packages

    def test_header_not_found(self):
        with mock.patch('bfg9000.tools.cc.exists', return_value=False):
            with self.assertRaises(PackageResolutionError):
                self.packages.header('foo.hpp')

    def test_header_relpath(self):
        with self.assertRaises(ValueError):
            self.packages.header('foo.hpp', [Path('dir', Root.srcdir)])

    def test_library_not_found(self):
        with mock.patch('bfg9000.tools.cc.exists', return_value=False):
            with self.assertRaises(PackageResolutionError):
                self.packages.library('foo')

    def test_library_relpath(self):
        with self.assertRaises(ValueError):
            p = Path('dir', Root.srcdir)
            self.packages.library('foo', search_dirs=[p])
Esempio n. 4
0
class TestCcPchCompiler(TestCcCompiler):
    def setUp(self):
        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
            self.compiler = CcBuilder(self.env, known_langs['c++'], ['c++'],
                                      'version').pch_compiler

    def test_default_name(self):
        hdr = HeaderFile(Path('file.hpp', Root.srcdir), 'c++')
        self.assertEqual(self.compiler.default_name(hdr, None), 'file.hpp')

    def test_output_file(self):
        ext = '.gch' if self.compiler.brand == 'gcc' else '.pch'
        self.assertEqual(self.compiler.output_file('file.h', None),
                         PrecompiledHeader(Path('file.h' + ext), 'c++'))
Esempio n. 5
0
    def test_cross_gcc(self):
        def mock_cross_exec(args, **kwargs):
            if args[-1] == '-dumpmachine':
                return self.env.host_platform.triplet
            return mock_execute(args, **kwargs)

        subtests = (
            ('x86_64', 'x86_64', []),
            ('x86_64', 'i686', ['-m32']),
            ('x86_64', 'i386', ['-m32']),
            ('i686', 'x86_64', ['-m64']),
            ('i686', 'i686', []),
            ('i686', 'i386', []),
            ('i386', 'x86_64', ['-m64']),
            ('i386', 'i686', []),
            ('i386', 'i386', []),
            ('x86_64', 'goofy', []),
        )
        for host, target, flags in subtests:
            self.env.host_platform = platforms.host.platform_info(
                self.env.host_platform.name, host)
            self.env.target_platform = platforms.target.platform_info(
                'linux', target)

            version = ('g++ (Ubuntu 5.4.0-6ubuntu1~16.04.6) 5.4.0 20160609\n' +
                       'Copyright (C) 2015 Free Software Foundation, Inc.')

            with mock.patch('bfg9000.shell.which', mock_which), \
                 mock.patch('bfg9000.shell.execute', mock_cross_exec):  # noqa
                cc = CcBuilder(self.env, known_langs['c++'], ['g++'], version)
            self.assertEqual(cc.compiler.global_flags, flags)
Esempio n. 6
0
    def test_cross_clang(self):
        self.env.target_platform = platforms.target.platform_info(
            'linux', self.env.target_platform.arch)

        version = 'clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)'
        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
            cc = CcBuilder(self.env, known_langs['c++'], ['g++'], version)
        self.assertEqual(cc.compiler.global_flags,
                         ['-target', self.env.target_platform.triplet]
                         if self.env.host_platform.name != 'linux' else [])
Esempio n. 7
0
    def test_execution_failure(self):
        def bad_execute(args, **kwargs):
            raise OSError()

        def weird_execute(args, **kwargs):
            if args[-1] == '-Wl,--version':
                return '', 'stderr\n'
            return mock_execute(args, **kwargs)

        version = ('g++ (Ubuntu 5.4.0-6ubuntu1~16.04.6) 5.4.0 20160609\n' +
                   'Copyright (C) 2015 Free Software Foundation, Inc.')

        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', bad_execute), \
             mock.patch('logging.log'):  # noqa
            cc = CcBuilder(self.env, known_langs['c++'], ['g++'], version)
        self.assertRaises(KeyError, cc.linker, 'raw')

        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', weird_execute), \
             mock.patch('logging.log'):  # noqa
            cc = CcBuilder(self.env, known_langs['c++'], ['g++'], version)
        self.assertRaises(KeyError, cc.linker, 'raw')
Esempio n. 8
0
    def test_clang(self):
        version = 'clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)'

        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
            cc = CcBuilder(self.env, known_langs['c++'], ['clang++'], version)

        self.assertEqual(cc.brand, 'clang')
        self.assertEqual(cc.compiler.brand, 'clang')
        self.assertEqual(cc.pch_compiler.brand, 'clang')
        self.assertEqual(cc.linker('executable').brand, 'clang')
        self.assertEqual(cc.linker('shared_library').brand, 'clang')

        self.assertEqual(cc.version, Version('3.8.0'))
        self.assertEqual(cc.compiler.version, Version('3.8.0'))
        self.assertEqual(cc.pch_compiler.version, Version('3.8.0'))
        self.assertEqual(cc.linker('executable').version, Version('3.8.0'))
        self.assertEqual(cc.linker('shared_library').version, Version('3.8.0'))
Esempio n. 9
0
    def test_unknown_brand(self):
        version = 'unknown'

        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
            cc = CcBuilder(self.env, known_langs['c++'], ['c++'], version)

        self.assertEqual(cc.brand, 'unknown')
        self.assertEqual(cc.compiler.brand, 'unknown')
        self.assertEqual(cc.pch_compiler.brand, 'unknown')
        self.assertEqual(cc.linker('executable').brand, 'unknown')
        self.assertEqual(cc.linker('shared_library').brand, 'unknown')

        self.assertEqual(cc.version, None)
        self.assertEqual(cc.compiler.version, None)
        self.assertEqual(cc.pch_compiler.version, None)
        self.assertEqual(cc.linker('executable').version, None)
        self.assertEqual(cc.linker('shared_library').version, None)
Esempio n. 10
0
    def test_gcc(self):
        version = ('g++ (Ubuntu 5.4.0-6ubuntu1~16.04.6) 5.4.0 20160609\n' +
                   'Copyright (C) 2015 Free Software Foundation, Inc.')

        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
            cc = CcBuilder(self.env, known_langs['c++'], ['g++'], version)

        self.assertEqual(cc.brand, 'gcc')
        self.assertEqual(cc.compiler.brand, 'gcc')
        self.assertEqual(cc.pch_compiler.brand, 'gcc')
        self.assertEqual(cc.linker('executable').brand, 'gcc')
        self.assertEqual(cc.linker('shared_library').brand, 'gcc')

        self.assertEqual(cc.version, Version('5.4.0'))
        self.assertEqual(cc.compiler.version, Version('5.4.0'))
        self.assertEqual(cc.pch_compiler.version, Version('5.4.0'))
        self.assertEqual(cc.linker('executable').version, Version('5.4.0'))
        self.assertEqual(cc.linker('shared_library').version, Version('5.4.0'))
Esempio n. 11
0
 def _get_linker(self, lang):
     with mock.patch('bfg9000.shell.which', mock_which), \
          mock.patch('bfg9000.shell.execute', mock_execute):
         return CcBuilder(self.env, known_langs[lang], ['c++'], True,
                          'version').linker('executable')
Esempio n. 12
0
 def setUp(self):
     with mock.patch('bfg9000.shell.which', mock_which), \
          mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
         self.packages = CcBuilder(self.env, known_langs['c++'], ['c++'],
                                   'version').packages
Esempio n. 13
0
 def setUp(self):
     self.env = make_env()
     with mock.patch('bfg9000.shell.which', mock_which), \
          mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
         self.compiler = CcBuilder(self.env, known_langs['c++'], ['c++'],
                                   'version').compiler
Esempio n. 14
0
 def _get_linker(self, lang):
     with mock.patch('bfg9000.shell.which', mock_which), \
          mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
         return CcBuilder(self.env, known_langs[lang], ['c++'],
                          'version').linker('shared_library')
Esempio n. 15
0
 def setUp(self):
     with mock.patch('bfg9000.shell.which', mock_which), \
          mock.patch('bfg9000.shell.execute', mock_execute):
         self.compiler = CcBuilder(self.env, known_langs['c++'], ['c++'],
                                   True, 'version').pch_compiler
Esempio n. 16
0
class TestCcCompiler(CrossPlatformTestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(clear_variables=True, *args, **kwargs)

    def setUp(self):
        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
            self.compiler = CcBuilder(self.env, known_langs['c++'], ['c++'],
                                      'version').compiler

    def test_call(self):
        extra = self.compiler._always_flags
        self.assertEqual(self.compiler('in', 'out'),
                         [self.compiler] + extra + ['-c', 'in', '-o', 'out'])
        self.assertEqual(self.compiler('in', 'out',
                                       flags=['flags']), [self.compiler] +
                         extra + ['flags', '-c', 'in', '-o', 'out'])

        self.assertEqual(self.compiler('in', 'out',
                                       'out.d'), [self.compiler] + extra +
                         ['-c', 'in', '-MMD', '-MF', 'out.d', '-o', 'out'])
        self.assertEqual(
            self.compiler('in', 'out', 'out.d', ['flags']), [self.compiler] +
            extra + ['flags', '-c', 'in', '-MMD', '-MF', 'out.d', '-o', 'out'])

    def test_default_name(self):
        src = SourceFile(Path('file.cpp', Root.srcdir), 'c++')
        self.assertEqual(self.compiler.default_name(src, None), 'file')

    def test_output_file(self):
        fmt = self.env.target_platform.object_format
        self.assertEqual(self.compiler.output_file('file', None),
                         ObjectFile(Path('file.o'), fmt, 'c++'))

    def test_flags_empty(self):
        self.assertEqual(self.compiler.flags(opts.option_list()), [])

    def test_flags_include_dir(self):
        p = self.Path('/path/to/include')
        self.assertEqual(
            self.compiler.flags(
                opts.option_list(opts.include_dir(HeaderDirectory(p)))),
            ['-I' + p])

        self.assertEqual(
            self.compiler.flags(
                opts.option_list(
                    opts.include_dir(HeaderDirectory(p, system=True)))),
            ['-isystem', p])

        if self.env.target_platform.genus == 'linux':
            p = self.Path('/usr/include')
            self.assertEqual(
                self.compiler.flags(
                    opts.option_list(
                        opts.include_dir(HeaderDirectory(p, system=True)))),
                ['-I' + p])

    def test_flags_define(self):
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.define('NAME'))),
            ['-DNAME'])
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.define('NAME',
                                                             'value'))),
            ['-DNAME=value'])

    def test_flags_warning(self):
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.warning('disable'))),
            ['-w'])
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.warning('all'))),
            ['-Wall'])
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.warning('extra'))),
            ['-Wextra'])
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.warning('error'))),
            ['-Werror'])

        self.assertEqual(
            self.compiler.flags(
                opts.option_list(opts.warning('all', 'extra', 'error'))),
            ['-Wall', '-Wextra', '-Werror'])

        with self.assertRaises(ValueError):
            self.compiler.flags(opts.option_list(opts.warning('unknown')))

    def test_flags_std(self):
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.std('c++14'))),
            ['-std=c++14'])

    def test_flags_debug(self):
        self.assertEqual(self.compiler.flags(opts.option_list(opts.debug())),
                         ['-g'])

    def test_flags_static(self):
        self.assertEqual(self.compiler.flags(opts.option_list(opts.static())),
                         [])

    def test_flags_optimize(self):
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.optimize('disable'))),
            ['-O0'])
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.optimize('size'))),
            ['-Osize'])
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.optimize('speed'))),
            ['-O3'])
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.optimize('linktime'))),
            ['-flto'])

        self.assertEqual(
            self.compiler.flags(
                opts.option_list(opts.optimize('speed', 'linktime'))),
            ['-O3', '-flto'])

    def test_flags_pthread(self):
        self.assertEqual(self.compiler.flags(opts.option_list(opts.pthread())),
                         ['-pthread'])

    def test_flags_pic(self):
        self.assertEqual(self.compiler.flags(opts.option_list(opts.pic())),
                         ['-fPIC'])

    def test_flags_include_pch(self):
        p = self.Path('/path/to/header.hpp')
        self.assertEqual(
            self.compiler.flags(
                opts.option_list(opts.pch(PrecompiledHeader(p, 'c++')))),
            ['-include', p.stripext()])

    def test_flags_sanitize(self):
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.sanitize())),
            ['-fsanitize=address'])

    def test_flags_string(self):
        self.assertEqual(self.compiler.flags(opts.option_list('-v')), ['-v'])

    def test_flags_invalid(self):
        with self.assertRaises(TypeError):
            self.compiler.flags(opts.option_list(123))
Esempio n. 17
0
class TestCcCompiler(unittest.TestCase):
    def setUp(self):
        self.env = make_env()
        with mock.patch('bfg9000.shell.which', mock_which), \
             mock.patch('bfg9000.shell.execute', mock_execute):  # noqa
            self.compiler = CcBuilder(self.env, known_langs['c++'], ['c++'],
                                      'version').compiler

    def test_flags_empty(self):
        self.assertEqual(self.compiler.flags(opts.option_list()), [])

    def test_flags_include_dir(self):
        p = Path('/path/to/include')
        self.assertEqual(
            self.compiler.flags(
                opts.option_list(
                    opts.include_dir(file_types.HeaderDirectory(p)))),
            ['-I' + p])

        self.assertEqual(
            self.compiler.flags(
                opts.option_list(
                    opts.include_dir(file_types.HeaderDirectory(
                        p, system=True)))), ['-isystem', p])

        if self.env.target_platform.name == 'linux':
            p = Path('/usr/include')
            self.assertEqual(
                self.compiler.flags(
                    opts.option_list(
                        opts.include_dir(
                            file_types.HeaderDirectory(p, system=True)))),
                ['-I' + p])

    def test_flags_define(self):
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.define('NAME'))),
            ['-DNAME'])
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.define('NAME',
                                                             'value'))),
            ['-DNAME=value'])

    def test_flags_std(self):
        self.assertEqual(
            self.compiler.flags(opts.option_list(opts.std('c++14'))),
            ['-std=c++14'])

    def test_flags_pthread(self):
        self.assertEqual(self.compiler.flags(opts.option_list(opts.pthread())),
                         ['-pthread'])

    def test_flags_pic(self):
        self.assertEqual(self.compiler.flags(opts.option_list(opts.pic())),
                         ['-fPIC'])

    def test_flags_include_pch(self):
        p = Path('/path/to/header.hpp')
        self.assertEqual(
            self.compiler.flags(
                opts.option_list(opts.pch(file_types.PrecompiledHeader(p)))),
            ['-include', p.stripext()])

    def test_flags_string(self):
        self.assertEqual(self.compiler.flags(opts.option_list('-v')), ['-v'])

    def test_flags_invalid(self):
        with self.assertRaises(TypeError):
            self.compiler.flags(opts.option_list(123))