Tệp Run?

Runfile là một nhóm tệp được mục tiêu sử dụng trong thời gian chạy (khác với thời gian tạo bản dựng).

Không mã hoá cứng đường dẫn runfiles. Các tên này chứa tên kho lưu trữ chuẩn, nhưng định dạng tên kho lưu trữ chuẩn là một chi tiết triển khai có thể thay đổi bất cứ lúc nào.

Sử dụng một trong các thư viện runfile theo ngôn ngữ để truy cập vào các thư viện đó:

Runfile thường được tham chiếu bằng một rlocationpath ở dạng $REPO/package/file, trong đó $REPO phải là tên kho lưu trữ rõ ràng. Hầu hết các thư viện runfiles (xem bên dưới) đều hỗ trợ chức năng xác định kho lưu trữ của mục tiêu hiện đang được thực thi. Chức năng này rất hữu ích khi tham chiếu đến các tệp khác trong cùng một kho lưu trữ. Nhiều quy tắc Bazel hỗ trợ Make Variables (Biến tạo) để dịch từ đích đến rlocationpath bằng cách sử dụng ký hiệu $(rlocationpath //package:target).

Ví dụ:

C++

load("@rules_cc//cc:cc_binary.bzl", "cc_binary")

cc_binary(
    name = "runfile",
    srcs = ["runfile.cc"],
    data = ["//examples:runfile.txt"],
    deps = ["@rules_cc//cc/runfiles"],
)
    
#include 
#include 
#include 
#include 

#include "rules_cc/cc/runfiles/runfiles.h"

using rules_cc::cc::runfiles::Runfiles;

inline constexpr std::string_view someFile = "examples/runfile.txt";

int main(int argc, char **argv) {
  std::string error;
  const auto runfiles = Runfiles::Create(argv[0], BAZEL_CURRENT_REPOSITORY, &error);
  if (runfiles == nullptr) {
    std::cerr << "Failed to create Runfiles object" << error << "\n";
    return 1;
  }

  std::string root = BAZEL_CURRENT_REPOSITORY;
  if (root == "") {
    root = "_main";
  }
  const std::string realPathToSomeFile = runfiles->Rlocation(std::filesystem::path(root) / someFile);

  std::cout << "The content of the runfile is:\n";
  std::ifstream fin(realPathToSomeFile);
  std::string line;
  while (std::getline(fin, line)) {
    std::cout << line << "\n";
  }

  return 0;
}
    

Golang

load("@rules_go//go:def.bzl", "go_binary")

go_binary(
    name = "runfile",
    srcs = ["runfile.go"],
    data = ["//examples:runfile.txt"],
    deps = ["@rules_go//go/runfiles:go_default_library"],
)
    
package main

import (
    "fmt"
    "log"
    "os"
    "path/filepath"

    "github.com/bazelbuild/rules_go/go/runfiles"
)

const (
    someFile = "examples/runfile.txt"
)

func main() {
    r, err := runfiles.New()
    if err != nil {
        log.Fatalf("Failed to create Runfiles object: %v", err)
    }

    root := runfiles.CallerRepository()
    if root == "" {
        root = "_main"
    }

    path, err := r.Rlocation(filepath.Join(root, someFile))
    if err != nil {
        log.Fatalf("Failed to find rlocation: %v", err)
    }

    fmt.Println("The content of my runfile is:")
    data, err := os.ReadFile(path)
    if err != nil {
        log.Fatalf("Failed to read file: %v", err)
    }
    fmt.Print(string(data))
}
    

Python

load("@rules_python//python:defs.bzl", "py_binary")

py_binary(
    name = "runfile",
    srcs = ["runfile.py"],
    data = ["//examples:runfile.txt"],
    deps = ["@rules_python//python/runfiles"],
)
    
import pathlib

from python.runfiles import runfiles

SOME_FILE = pathlib.Path('examples/runfile.txt')

r = runfiles.Create()
root = r.CurrentRepository()
if root == "":
  root = "_main"
realPathToSomeFile = r.Rlocation(str(root / SOME_FILE))

print("The content of the runfile is:")
with open(realPathToSomeFile, 'r') as f:
    print(f.read())
    

Vỏ

load("@rules_shell//shell:sh_binary.bzl", "sh_binary")

sh_binary(
    name = "runfile",
    srcs = ["runfile.sh"],
    data = ["//examples:runfile.txt"],
    use_bash_launcher = True,
)
    
#!/bin/bash

SOME_FILE='examples/runfile.txt'

root="$(runfiles_current_repository)"
if [ -z "$root" ]; then
  root="_main"
fi
real_path_to_some_file="$(rlocation "${root}/${SOME_FILE}")"

echo "The content of the runfile is:"
cat "${real_path_to_some_file}"