Runfile은 빌드 시간과 달리 런타임에 타겟에서 사용하는 파일 집합입니다.
실행 파일 경로를 하드코딩하지 마세요. 여기에는 정규 저장소 이름이 포함되지만 정규 저장소 이름 형식은 언제든지 변경될 수 있는 구현 세부정보입니다.
언어별 runfiles 라이브러리 중 하나를 사용하여 액세스합니다.
런파일은 일반적으로 rlocationpath에서 $REPO/package/file 형식으로 참조됩니다. 여기서 $REPO은 명백한 저장소 이름이어야 합니다.
대부분의 실행 파일 라이브러리 (아래 참고)는 현재 실행된 타겟의 저장소를 확인하는 기능을 지원하며, 이는 동일한 저장소의 다른 파일을 참조하는 데 유용합니다. 많은 Bazel 규칙이 $(rlocationpath //package:target) 표기법을 사용하여 타겟에서 rlocationpath로 변환하는 Make Variables를 지원합니다.
예:
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())
Shell
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}"