'Finalize the library.  After this call, nothing can be added anymore.
Finalization involves various stages of code optimization and
linking.'

@llvmts.lock_llvm
def finalize(self):

    self._codegen._check_llvm_bugs()
    self._raise_if_finalized()
    if config.DUMP_FUNC_OPT:
        dump(('FUNCTION    OPTIMIZED    DUMP    %s' % self._name), self.get_llvm_str())
    for library in self._linking_libraries:
        self._final_module.link_in(library._get_module_for_linking(), preserve=True)
    for library in self._codegen._libraries:
        self._final_module.link_in(library._get_module_for_linking(), preserve=True)
    self._optimize_final_module()
    self._final_module.verify()
    self._finalize_final_module()
