#!/usr/bin/env python3
import os

import common
import thread_pool
from shell_helpers import LF

class Main(common.BuildCliFunction):
    def __init__(self):
        super().__init__(
            defaults={
                'gcc_which': 'crosstool-ng',
                'mode': 'baremetal',
            },
            description='''\
Build the baremetal examples with crosstool-NG.
''',
        )
        self._add_argument('--ccflags')
        self._add_argument('--force-rebuild')
        self._add_argument('--optimization-level')
        self.add_argument(
            'targets',
            default=[],
            help='Analogous to ./build-userland target selection',
            nargs='*',
        )

    def build(self):
        build_dir = self.get_build_dir()
        cc_flags = [
            '-I', self.env['root_dir'], LF,
        ]
        if self.env['emulator'] == 'gem5':
            cc_flags.extend([
                '-DLKMC_GEM5=1', LF,
                '-DLKMC_M5OPS_ENABLE=1', LF,
            ])
        else:
            cc_flags.extend(['-D', 'LKMC_QEMU=1', LF])
        cc_flags.extend(['-D', 'LKMC_UART0_ADDR={:#x}'.format(self.env['uart_address']), LF])
        cc_flags.extend(self.env['ccflags'])
        bootloader_src = os.path.join(
            self.env['baremetal_source_lib_dir'],
            '{}{}'.format(
                self.env['arch'],
                self.env['asm_ext']
            )
        )
        for in_path, out_path in [
            (bootloader_src, self.env['baremetal_extra_obj_bootloader']),
            (self.env['common_c'], self.env['baremetal_extra_obj_lkmc_common']),
            (
                self.env['baremetal_syscalls_src'],
                self.env['baremetal_syscalls_obj']
            ),
            (
                self.env['baremetal_syscalls_asm_src'],
                self.env['baremetal_syscalls_asm_obj']
            ),
        ]:
            self._build_one(
                in_path=in_path,
                out_path=out_path,
                cc_flags=cc_flags,
                extra_deps=[self.env['common_h']],
                link=False,
            )
        cc_flags.extend(self.env['ldflags'])
        with thread_pool.ThreadPool(
            self._build_one,
            nthreads=self.env['nproc'],
            submit_raise_exit=self.env['quit_on_fail'],
        ) as my_thread_pool:
            for target in self.env['targets']:
                for path, in_dirnames, in_filenames in self.sh.walk(target):
                    for in_filename in in_filenames:
                        in_ext = os.path.splitext(in_filename)[1]
                        if not in_ext in self.env['baremetal_build_in_exts']:
                            continue
                        in_path = os.path.join(path, in_filename)
                        my_thread_pool.submit({
                            'cc_flags': cc_flags,
                            'extra_deps': [
                                self.env['baremetal_link_script'],
                                self.env['common_h']
                            ],
                            'extra_objs': [
                                self.env['baremetal_syscalls_obj'],
                                self.env['baremetal_syscalls_asm_obj']
                            ],
                            'extra_objs_baremetal_bootloader': [self.env['baremetal_extra_obj_bootloader']],
                            'extra_objs_lkmc_common': [self.env['baremetal_extra_obj_lkmc_common']],
                            'in_path': in_path,
                            'out_path': self.resolve_baremetal_executable(in_path),
                        })
        return self._handle_thread_pool_errors(my_thread_pool)

    def get_build_dir(self):
        return self.env['baremetal_build_dir']

    def setup_one_build(self):
        self.env['targets'] = self.resolve_targets(
            [
                self.env['baremetal_source_dir'],
                self.env['userland_source_dir']
            ],
            self.env['targets']
        )

if __name__ == '__main__':
    Main().cli()
