def test_LinkBitcodeFilesToBytecode_empty_file(tempdir: pathlib.Path): """llvm-link with an empty file.""" input_path = tempdir / "empty.ll" output_path = tempdir / "linked.ll" fs.Write(input_path, "".encode("utf-8")) llvm_link.LinkBitcodeFilesToBytecode( [input_path], output_path, timeout_seconds=5 ) assert output_path.is_file()
def test_LinkBitcodeFilesToBytecode_syntax_error(tempdir: pathlib.Path): """llvm-link fails when a file contains invalid syntax.""" input_path = tempdir / "empty.ll" output_path = tempdir / "linked.ll" fs.Write(input_path, "syntax error!".encode("utf-8")) with test.Raises(ValueError) as e_ctx: llvm_link.LinkBitcodeFilesToBytecode( [input_path], output_path, timeout_seconds=5 ) assert str(e_ctx.value).startswith("Failed to link bytecode: ")
def ProduceBytecodeFromSources( input_paths: typing.List[pathlib.Path], output_path: pathlib.Path, copts: typing.Optional[typing.List[str]] = None, linkopts: typing.Optional[typing.List[str]] = None, timeout_seconds: int = 60, ) -> pathlib.Path: """Produce a single bytecode file for a set of sources. Args: input_paths: A list of input source files. output_path: The file to generate. copts: A list of additional flags to pass to clang. linkopts: A list of additional flags to pass to llvm-link. timeout_seconds: The number of seconds to allow clang to run for. Returns: The output_path. """ copts = copts or [] if output_path.is_file(): output_path.unlink() # Compile each input source to a bytecode file. with tempfile.TemporaryDirectory() as d: d = pathlib.Path(d) input_srcs = [ d / (crypto.sha256_str(str(src)) + ".l") for src in input_paths ] for src, input_src in zip(input_paths, input_srcs): clang.Compile( [src], input_src, copts=copts + ["-O0", "-emit-llvm", "-S", "-c"], timeout_seconds=timeout_seconds, ) # Link the separate bytecode files. llvm_link.LinkBitcodeFilesToBytecode(input_srcs, output_path, linkopts, timeout_seconds=timeout_seconds) return output_path