'Finalize module and return the HSAIL code'

def finalize(self):

    assert (not self._finalized), 'Module    finalized    already'
    llvmfile = self._linkfiles[0]
    pre_builtin_path = self._track_temp_file('link-dep')
    libpaths = self._linkfiles[1:]
    self._cmd.link_libs(ipath=llvmfile, libpaths=libpaths, opath=pre_builtin_path)
    linked_path = self._track_temp_file('linked-path')
    self._cmd.link_builtins(ipath=pre_builtin_path, opath=linked_path)
    opt_path = self._track_temp_file('optimized-llvm-ir')
    self._cmd.optimize(ipath=linked_path, opath=opt_path)
    if config.DUMP_OPTIMIZED:
        with open(opt_path, 'rb') as fin:
            print(fin.read().decode('ascii'))
    hsail_path = self._track_temp_file('finalized-hsail')
    self._cmd.generate_hsail(ipath=opt_path, opath=hsail_path)
    brig_path = self._track_temp_file('finalized-brig')
    self._cmd.generate_brig(ipath=opt_path, opath=brig_path)
    self._finalized = True
    with open(hsail_path, 'rb') as fin:
        hsail = fin.read().decode('ascii')
    with open(brig_path, 'rb') as fin:
        brig = fin.read()
    if config.DUMP_ASSEMBLY:
        print(hsail)
    return namedtuple('FinalizerResult', ['hsail', 'brig'])(hsail, brig)
