|
| 1 | +"""Module for building and extracting bitcode from applications using Portage""" |
| 2 | + |
| 3 | +import subprocess |
| 4 | +import os |
| 5 | +import glob |
| 6 | +import tempfile |
| 7 | +import logging |
| 8 | +import pathlib |
| 9 | +import shutil |
| 10 | +import re |
| 11 | +import getpass |
| 12 | +import ray |
| 13 | + |
| 14 | +from mlgo.corpus import extract_ir_lib |
| 15 | + |
| 16 | +from llvm_ir_dataset_utils.util import file |
| 17 | +from llvm_ir_dataset_utils.util import portage as portage_utils |
| 18 | +from llvm_ir_dataset_utils.util import extract_source_lib |
| 19 | + |
| 20 | +BUILD_LOG_NAME = './portage_build.log' |
| 21 | + |
| 22 | + |
| 23 | +def get_spec_command_vector_section(spec): |
| 24 | + return spec.split(' ') |
| 25 | + |
| 26 | + |
| 27 | +def generate_emerge_command(package_to_build, threads, build_dir): |
| 28 | + command_vector = [ |
| 29 | + 'emerge', # Portage package management command |
| 30 | + '--jobs={}'.format( |
| 31 | + threads), # Set the number of jobs for parallel building |
| 32 | + '--load-average={}'.format( |
| 33 | + threads), # Set the maximum load average for parallel builds |
| 34 | + '--config-root={}'.format( |
| 35 | + build_dir), # Set the configuration root directory |
| 36 | + '--buildpkg', # Build binary packages, similar to Spack's build cache |
| 37 | + '--usepkg', # Use binary packages if available |
| 38 | + '--binpkg-respect-use=y', # Ensure that binary package installations respect USE flag settings |
| 39 | + '--quiet-build=y', # Reduce output during the build process |
| 40 | + package_to_build # The package to install |
| 41 | + ] |
| 42 | + |
| 43 | + # Portage does not support setting the build directory directly in the command, |
| 44 | + # but this can be controlled with the PORTAGE_TMPDIR environment variable |
| 45 | + # This environment variable needs to be set when calling subprocess, not here directly |
| 46 | + return command_vector |
| 47 | + |
| 48 | + |
| 49 | +def perform_build(package_name, assembled_build_command, corpus_dir, build_dir): |
| 50 | + logging.info(f"Portage building package {package_name}") |
| 51 | + environment = os.environ.copy() |
| 52 | + # Set DISTDIR and PORTAGE_TMPDIR to set the build directory for Portage |
| 53 | + environment['DISTDIR'] = build_dir |
| 54 | + environment['PORTAGE_TMPDIR'] = build_dir |
| 55 | + build_log_path = os.path.join(corpus_dir, BUILD_LOG_NAME) |
| 56 | + try: |
| 57 | + with open(build_log_path, 'w') as build_log_file: |
| 58 | + subprocess.run( |
| 59 | + assembled_build_command, |
| 60 | + stdout=build_log_file, |
| 61 | + stderr=build_log_file, |
| 62 | + check=True, |
| 63 | + env=environment) |
| 64 | + except subprocess.SubprocessError: |
| 65 | + logging.warn(f"Failed to build portage package {package_name}") |
| 66 | + return False |
| 67 | + logging.info(f"Finished build portage package {package_name}") |
| 68 | + return True |
| 69 | + |
| 70 | + |
| 71 | +def extract_ir(package_spec, corpus_dir, build_dir, threads): |
| 72 | + build_directory = build_dir + "/portage/" |
| 73 | + package_spec = package_spec + "*" |
| 74 | + match = glob.glob(os.path.join(build_directory, package_spec)) |
| 75 | + assert (len(match) == 1) |
| 76 | + package_name_with_version = os.path.basename(match[0]) |
| 77 | + build_directory = match[0] + "/work/" + package_name_with_version |
| 78 | + if build_directory is not None: |
| 79 | + objects = extract_ir_lib.load_from_directory(build_directory, corpus_dir) |
| 80 | + relative_output_paths = extract_ir_lib.run_extraction( |
| 81 | + objects, threads, "llvm-objcopy", None, None, ".llvmcmd", ".llvmbc") |
| 82 | + extract_ir_lib.write_corpus_manifest(None, relative_output_paths, |
| 83 | + corpus_dir) |
| 84 | + extract_source_lib.copy_source(build_directory, corpus_dir) |
| 85 | + |
| 86 | + |
| 87 | +def cleanup(package_name, package_spec, corpus_dir, uninstall=True): |
| 88 | + #TODO: Implement cleanup |
| 89 | + return |
| 90 | + |
| 91 | + |
| 92 | +def construct_build_log(build_success, package_name): |
| 93 | + return { |
| 94 | + 'targets': [{ |
| 95 | + 'name': package_name, |
| 96 | + 'build_log': BUILD_LOG_NAME, |
| 97 | + 'success': build_success |
| 98 | + }] |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | +def build_package(dependency_futures, |
| 103 | + package_name, |
| 104 | + package_spec, |
| 105 | + corpus_dir, |
| 106 | + threads, |
| 107 | + buildcache_dir, |
| 108 | + build_dir, |
| 109 | + cleanup_build=False): |
| 110 | + dependency_futures = ray.get(dependency_futures) |
| 111 | + for dependency_future in dependency_futures: |
| 112 | + if not dependency_future['targets'][0]['success']: |
| 113 | + logging.warning( |
| 114 | + f"Dependency {dependency_future['targets'][0]['name']} failed to build " |
| 115 | + f"for package {package_name}, not building.") |
| 116 | + if cleanup_build: |
| 117 | + cleanup(package_name, package_spec, corpus_dir, uninstall=False) |
| 118 | + return construct_build_log(False, package_name, None) |
| 119 | + portage_utils.portage_setup_compiler(build_dir) |
| 120 | + portage_utils.clean_binpkg(package_spec) |
| 121 | + build_command = generate_emerge_command(package_spec, threads, build_dir) |
| 122 | + build_result = perform_build(package_name, build_command, corpus_dir, |
| 123 | + build_dir) |
| 124 | + if build_result: |
| 125 | + extract_ir(package_spec, corpus_dir, build_dir, threads) |
| 126 | + logging.warning(f'Finished building {package_name}') |
| 127 | + if cleanup_build: |
| 128 | + if build_result: |
| 129 | + cleanup(package_name, package_spec, corpus_dir) |
| 130 | + else: |
| 131 | + cleanup(package_name, package_spec, corpus_dir, uninstall=False) |
| 132 | + return construct_build_log(build_result, package_name) |
0 commit comments