Example #1
0
def create_targets(container_configs_list, bazel_version):
    """Creates the new docker_toolchain_autoconfig target if not exists.

  An example target located in configs/ubuntu16_04_clang/BUILD is:
  //configs/ubuntu16_04_clang:msan-ubuntu16_04-clang-1.0-bazel_0.15.0-autoconfig

  There is one target per container per Bazel version per config type.

  The script only creates new targets in the BUILD file if they do not exist,
  i.e. if a target for the given version of Bazel, type and config version
  already exists, then the script does not re-create it.

  Args:
    container_configs_list: list of ContainerConfigs, the list of
      ContainerConfigs to generate configs for.
    bazel_version: string, the version of Bazel used to generate the configs.

  """

    container_sha_map = imp.load_source("toolchain_containers", SHA_MAP_FILE)
    clang_revision_map = imp.load_source("clang_revision", CLANG_REVISION_FILE)
    clang_revision = clang_revision_map.CLANG_REVISION

    for container_configs in container_configs_list:

        # Get the sha256 value of the container used to generate the configs.
        sha = container_sha_map.toolchain_container_sha256s()[
            "%s_clang" % container_configs.distro]

        for config in container_configs.configs:

            # Get target basename from config definitions.
            target = get_autoconfig_target_name(
                config_type=config.config_type,
                distro=container_configs.distro,
                config_version=container_configs.version,
                bazel_version=bazel_version)

            with open(container_configs.get_target_build_path(),
                      "a+") as build_file:
                if target not in build_file.read():
                    tpl_file_path = os.path.join(GIT_ROOT, "release", "cc",
                                                 "%s.tpl" % config.config_type)
                    with open(tpl_file_path, "r") as tpl_file:
                        tpl = Template(tpl_file.read()).substitute(
                            DATE=get_date(),
                            DISTRO=container_configs.distro,
                            CONFIG_VERSION=container_configs.version,
                            BAZEL_VERSION=bazel_version,
                            NAME=container_configs.image,
                            SHA=sha,
                            CLANG_REVISION=clang_revision)

                        build_file.write(tpl)
def execute_and_extract_configs(container_configs_list, bazel_version,
                                buildifier):
    """Executes the docker_toolchain_autoconfig targets and extract configs.

  If configs already exist in this repo, the script will delete them and
  generate new ones.

  It generate cc configs which currently includes: CROSSTOOL, BUILD,
  cc_wrapper.sh and dummy_toolchain.bzl. Examples can be found in
  configs/ubuntu16_04_clang/1.0/bazel_0.15.0/default/.

  There is one such set of cc configs per container per Bazel version per config
  type.

  Args:
    container_configs_list: list of ContainerConfigs, the list of
      ContainerConfigs to generate configs for.
    bazel_version: string, the version of Bazel used to generate the configs.
  """

    atexit.register(_cleanup)

    # Create temporary directory to store generated tarballs of configs.
    if os.path.exists(TMP_DIR):
        shutil.rmtree(TMP_DIR)
    os.makedirs(TMP_DIR)

    for container_configs in container_configs_list:
        for config in container_configs.configs:

            # Get target basename from config definitions.
            target = get_autoconfig_target_name(
                config_type=config.config_type,
                distro=container_configs.distro,
                config_version=container_configs.version,
                bazel_version=bazel_version)

            # Remove old config dir if exists.
            if os.path.exists(config.get_config_dir()):
                print(
                    "\nOld version of toolchain configs for {target} already exists. "
                    "Deleting and generating again.".format(target=target))
                shutil.rmtree(config.get_config_dir())

            # Generate config directory.
            os.makedirs(config.get_config_dir())

            command = ("bazel build //{PACKAGE}:{TARGET}").format(
                PACKAGE=container_configs.package, TARGET=target)
            print("\nExecuting command: %s\n" % command)
            subprocess.check_call(shlex.split(command))

            command = (
                "cp "
                "{GIT_ROOT}/bazel-out/k8-fastbuild/bin/{PACKAGE}/{TARGET}_outputs.tar"
                " {OUTPUT_DIR}/").format(GIT_ROOT=GIT_ROOT,
                                         OUTPUT_DIR=TMP_DIR,
                                         PACKAGE=container_configs.package,
                                         TARGET=target)
            print("\nExecuting command: %s\n" % command)
            subprocess.check_call(shlex.split(command))

            # Extract toolchain configs.
            tar_path = os.path.join(TMP_DIR, "%s_outputs.tar" % target)
            tar = tarfile.open(tar_path)

            for config_file in CONFIG_FILES:
                # Extract toolchain config without the CONFIG_REPO name.
                member = tar.getmember(os.path.join(CONFIG_REPO, config_file))
                member.name = os.path.basename(member.name)
                tar.extract(member, config.get_config_dir())
                if config_file == "BUILD" or config_file.endswith("bzl"):
                    subprocess.check_call(
                        shlex.split("%s --lint=fix %s" %
                                    (buildifier,
                                     os.path.join(config.get_config_dir(),
                                                  config_file))))