관련성이 있음

규칙 또는 측면의 구현 함수에 전달되는 컨텍스트 객체입니다. 현재 타겟을 분석하는 데 필요한 정보와 메서드에 대한 액세스를 제공합니다.

특히 구현 함수가 현재 타겟의 라벨, 속성, 구성, 종속 항목의 제공자에 액세스할 수 있습니다. 출력 파일과 이를 생성하는 작업을 선언하는 메서드가 있습니다.

컨텍스트 객체는 기본적으로 구현 함수 호출 기간 동안 유지됩니다. 연결된 함수 외부에서 이러한 객체에 액세스하는 것은 유용하지 않습니다. 자세한 내용은 규칙 페이지를 참고하세요.

회원

작업

actions ctx.actions

출력 파일과 이를 생성하는 작업을 선언하는 메서드를 포함합니다.

aspect_ids

list ctx.aspect_ids

타겟에 적용된 모든 측면의 ID 목록입니다. 측면 구현 함수에서만 사용할 수 있습니다.

attr

struct ctx.attr

attributes의 값에 액세스하는 구조체입니다. 값은 사용자가 제공합니다 (제공하지 않으면 기본값이 사용됨). 구조체의 속성과 값의 유형은 rule 함수에 제공된 attrs dict의 키와 값에 해당합니다. 사용 예시 보기

bin_dir

root ctx.bin_dir

bin 디렉터리에 해당하는 루트입니다.

build_file_path

string ctx.build_file_path

지원 중단됨: ctx.label.package + '/BUILD'를 사용하세요. 소스 루트를 기준으로 하는 이 규칙의 BUILD 파일 경로입니다.

build_setting_value

unknown ctx.build_setting_value

현재 타겟으로 표시되는 빌드 설정의 값입니다. build_setting 속성을 설정하는 규칙 인스턴스의 컨텍스트가 아닌 경우 이를 읽으면 오류가 발생합니다.

구성

configuration ctx.configuration

현재 타겟의 빌드 구성입니다. 자세한 내용은 Starlark 구성 유형구성 문서를 참고하세요.

coverage_instrumented

bool ctx.coverage_instrumented(target=None)

이 규칙 또는 target가 제공된 경우 해당 타겟으로 지정된 규칙의 컴파일 작업을 실행할 때 코드 커버리지 계측을 생성해야 하는지 여부를 반환합니다. (규칙이 아닌 타겟이나 Starlark 규칙 타겟이 제공되면 False가 반환됩니다.) --instrumentation_filter 및 --instrument_test_targets 구성 설정에 따라 현재 규칙의 소스 (타겟이 제공되지 않은 경우) 또는 타겟의 소스를 계측해야 하는지 확인합니다. 이는 구성coverage_enabled와는 다릅니다. coverage_enabled는 전체 실행에 대해 커버리지 데이터 수집이 사용 설정되었는지 여부를 나타내지만 특정 타겟이 계측되어야 하는지 여부는 나타내지 않습니다.

매개변수

매개변수 설명
target 타겟 또는 None. 기본값은 None
입니다. 규칙을 지정하는 타겟입니다. 제공되지 않으면 기본값은 현재 규칙입니다.

created_actions

StarlarkValue ctx.created_actions()

_skylark_testableTrue로 설정된 규칙의 경우 현재 규칙에 대해 지금까지 생성된 모든 작업을 나타내는 Actions 제공자를 반환합니다. 다른 모든 규칙의 경우 None을 반환합니다. 후속 작업을 생성할 때는 제공자가 업데이트되지 않으므로 검사하려면 이 함수를 다시 호출해야 합니다.

이는 ctx 객체를 가져와 객체에 작업을 생성할 수 있는 규칙 구현 도우미 함수의 테스트를 작성하는 데 도움이 됩니다.

disabled_features

list ctx.disabled_features

이 규칙에 대해 사용자가 명시적으로 사용 중지한 기능 집합입니다.

exec_groups

ExecGroupCollection ctx.exec_groups

이 규칙에 사용할 수 있는 실행 그룹의 컬렉션으로, 이름으로 색인이 생성됩니다. ctx.exec_groups[name_of_group]로 액세스

실행 파일

struct ctx.executable

executable=True로 표시된 라벨 유형 속성에 정의된 실행 파일이 포함된 struct 구조체 필드는 속성 이름에 해당합니다. 구조체의 각 값은 File 또는 None입니다. 선택적 속성이 규칙에 지정되지 않으면 해당 구조체 값은 None입니다. 라벨 유형이 executable=True로 표시되지 않으면 해당 구조체 필드가 생성되지 않습니다. 사용 예시 보기

expand_location

string ctx.expand_location(input, targets=[])

$(location //x)을 타겟 //x의 출력 파일 경로로 대체하여 지정된 문자열의 모든 $(location ...) 템플릿을 확장합니다. 확장은 이 규칙의 직접 종속 항목을 가리키거나 선택적 인수 targets에 명시적으로 나열된 라벨에만 작동합니다. 참조된 타겟에 출력이 여러 개 있으면

$(location ...)로 인해 오류가 발생합니다. 이 경우 출력 경로의 공백으로 구분된 목록을 생성하는 $(locations ...)를 사용하세요. 단일 출력 파일에도 안전하게 사용할 수 있습니다.

이 함수는 사용자가 BUILD 파일에서 명령어를 지정하도록 하는 데 유용합니다 (예: genrule). 다른 경우에는 라벨을 직접 조작하는 것이 더 나은 경우가 많습니다.

매개변수

매개변수 설명
input string; required
확장할 문자열입니다.
targets Targetsequence입니다. 기본값은 []
입니다. 추가 조회 정보를 위한 타겟 목록입니다. 이는 다음과 같이 확장됩니다. DefaultInfo.files에 단일 파일이 있는 타겟은 해당 파일로 확장됩니다. 다른 타겟은 설정되어 있고 --incompatible_locations_prefers_executable이 사용 설정된 경우 DefaultInfo.executable 파일로 확장되고, 그렇지 않으면 DefaultInfo.files로 확장됩니다.
None을 반환할 수 있습니다.

expand_make_variables

string ctx.expand_make_variables(attribute_name, command, additional_substitutions)

지원 중단되었습니다. 대신 ctx.var를 사용하여 변수에 액세스하세요.
'변수 만들기'에 대한 모든 참조를 확장한 후 문자열을 반환합니다. 변수는 $(VAR_NAME) 형식이어야 합니다. 또한 $$VAR_NAME expands to $VAR_NAME. Examples:
ctx.expand_make_variables("cmd", "$(MY_VAR)", {"MY_VAR": "Hi"})  # == "Hi"
ctx.expand_make_variables("cmd", "$$PWD", {})  # == "$PWD"
Additional variables may come from other places, such as configurations. Note that this function is experimental.
                  
      

Parameters

Parameter Description
attribute_name string; required
The attribute name. Used for error reporting.
command string; required
The expression to expand. It can contain references to "Make variables".
additional_substitutions dict; required
Additional substitutions to make beyond the default make variables.

features

list ctx.features

The set of features that are explicitly enabled by the user for this rule. See example of use.

file

struct ctx.file

A struct containing files defined in label type attributes marked as allow_single_file. The struct fields correspond to the attribute names. The struct value is always a File or None. If an optional attribute is not specified in the rule then the corresponding struct value is None. If a label type is not marked as allow_single_file, no corresponding struct field is generated. It is a shortcut for:
list(ctx.attr.<ATTR>.files)[0]
In other words, use file to access the (singular) default output of a dependency. See example of use.

files

struct ctx.files

A struct containing files defined in label or label list type attributes. The struct fields correspond to the attribute names. The struct values are list of Files. It is a shortcut for:
[f for t in ctx.attr.<ATTR> for f in t.files]
In other words, use files to access the default outputs of a dependency. See example of use.

fragments

fragments ctx.fragments

Allows access to configuration fragments in target configuration.

genfiles_dir

root ctx.genfiles_dir

The root corresponding to genfiles directory.

info_file

File ctx.info_file

The file that is used to hold the non-volatile workspace status for the current build request. See documentation for --workspace_status_command for more information.

label

Label ctx.label

The label of the target currently being analyzed.

outputs

structure ctx.outputs

A pseudo-struct containing all the predeclared output files, represented by File objects. See the Rules page for more information and examples.

This field does not exist on aspect contexts, since aspects do not have predeclared outputs.

The fields of this object are defined as follows. It is an error if two outputs produce the same field name or have the same label.

  • If the rule declares an outputs dict, then for every entry in the dict, there is a field whose name is the key and whose value is the corresponding File.
  • For every attribute of type attr.output that the rule declares, there is a field whose name is the attribute's name. If the target specified a label for that attribute, then the field value is the corresponding File; otherwise the field value is None.
  • For every attribute of type attr.output_list that the rule declares, there is a field whose name is the attribute's name. The field value is a list of File objects corresponding to the labels given for that attribute in the target, or an empty list if the attribute was not specified in the target.
  • (Deprecated) If the rule is marked executable or test, there is a field named "executable", which is the default executable. It is recommended that instead of using this, you pass another file (either predeclared or not) to the executable arg of DefaultInfo.

package_relative_label

Label ctx.package_relative_label(input)

Converts the input string into a Label object, in the context of the package of the target currently being analyzed. If the input is already a Label, it is returned unchanged.

The result of this function is the same Label value as would be produced by passing the given string to a label-valued attribute of the rule and accessing the corresponding Label()Label()가 현재 분석 중인 타겟의 패키지가 아닌 이를 호출한 .bzl 파일의 패키지 컨텍스트를 사용한다는 것입니다. 이 함수는 규칙이나 측면 구현 함수에서 사용할 수 없는 native.package_relative_label()와 동일한 동작을 합니다.

매개변수

매개변수 설명
input string 또는 Label; required
입력 라벨 문자열 또는 Label 객체입니다. Label 객체가 전달되면 그대로 반환됩니다.

resolve_command

tuple ctx.resolve_command(*, command='', attribute=None, expand_locations=False, make_variables=None, tools=[], label_dict={}, execution_requirements={})

(실험적) 해결된 입력 목록과 해결된 명령의 argv 목록으로 구성된 튜플 (inputs, command, empty list)을 반환합니다. 둘 다 ctx.action 메서드의 이름이 같은 인수로 전달하는 데 적합합니다.
Windows 사용자 참고사항: 이 메서드에는 Bash (MSYS2)가 필요합니다. 필요한 경우 resolve_tools()을 대신 사용하는 것이 좋습니다. 빈 목록은 하위 호환성을 위해 튜플의 세 번째 멤버로 반환됩니다.

매개변수

매개변수 설명
command string; default is ''
Command to resolve.
attribute string 또는 None. 기본값은 None
입니다. 오류를 발생시킬 연결된 속성의 이름 또는 None입니다.
expand_locations bool; default is False
$(location) 변수를 확장해야 하나요? 자세한 내용은 ctx.expand_location()을 참고하세요.
make_variables dict 또는 None. 기본값은 None
입니다. 확장할 변수 또는 None입니다.
tools Targetsequence. 기본값은 []
입니다. 도구 목록 (타겟 목록)입니다.
label_dict dict; default is {}
해결된 라벨과 해당 파일 목록의 사전입니다 (라벨 : 파일 목록의 사전).
execution_requirements dict; default is {}
이 명령어를 해결하기 위한 작업을 예약하는 데 필요한 정보입니다. 유용한 키는 태그를 참고하세요.

resolve_tools

tuple ctx.resolve_tools(*, tools=[])

도구를 실행하는 데 필요한 확인된 입력의 depset을 반환합니다. 이는 ctx.actions.runctx.actions.run_shell 메서드의 동일한 이름의 인수로 전달하는 데 적합합니다.(inputs, empty list)

ctx.resolve_command과 달리 이 메서드는 머신에 Bash가 설치되어 있지 않아도 되므로 Windows에서 빌드된 규칙에 적합합니다. 빈 목록은 하위 호환성을 위해 튜플의 일부로 반환됩니다.

매개변수

매개변수 설명
tools Targetsequence. 기본값은 []
입니다. 도구 목록 (타겟 목록)입니다.

규칙

rule_attributes ctx.rule

측면이 적용되는 규칙의 규칙 속성 설명자입니다. 측면 구현 함수에서만 사용할 수 있습니다.

runfiles

runfiles ctx.runfiles(files=[], transitive_files=None, collect_data=False, collect_default=False, symlinks={}, root_symlinks={})

runfiles 객체를 만듭니다.

매개변수

매개변수 설명
files Filesequence; default is []
runfiles에 추가할 파일 목록입니다.
transitive_files Filedepset 또는 None입니다. 기본값은 None
입니다. 런파일에 추가할 파일의 (전이적) 집합입니다. deps는 default 순서 (이름에서 알 수 있듯이 기본값)를 사용해야 합니다.
collect_data bool; default is False
이 매개변수는 사용하지 않는 것이 좋습니다. runfiles 가이드를 참고하세요.

srcs, data, deps 속성의 종속 항목에서 데이터 실행 파일을 수집할지 여부입니다.

collect_default bool; default is False
이 매개변수는 사용하지 않는 것이 좋습니다. runfiles 가이드를 참고하세요.

srcs, data, deps 속성의 종속 항목에서 기본 실행 파일을 수집할지 여부입니다.

dict 또는 SymlinkEntrydepset입니다. 기본값은 {}
입니다. SymlinkEntry depset 또는 실행 파일에 추가할 심볼릭 링크의 맵입니다. 심볼릭 링크는 항상 기본 워크스페이스의 runfiles 디렉터리 (예: 현재 타겟의 저장소에 해당하는 디렉터리가 아님) 아래에 추가됩니다.<runfiles_root>/_main/<symlink_path> 규칙 가이드의 Runfiles 심볼릭 링크를 참고하세요.
dict 또는 SymlinkEntrydepset. 기본값은 {}
입니다. 런파일에 추가할 심볼릭 링크의 SymlinkEntry depset 또는 맵입니다. 규칙 가이드의 Runfiles 심볼릭 링크를 참고하세요.

split_attr

struct ctx.split_attr

분할된 구성이 있는 속성의 값에 액세스하는 구조체입니다. 속성이 라벨 목록인 경우 split_attr의 값은 분할의 키 (문자열)에서 분할의 해당 브랜치에 있는 ConfiguredTarget 목록으로의 dict입니다. 속성이 라벨인 경우 split_attr의 값은 분할의 키 (문자열)에서 단일 ConfiguredTarget으로의 dict입니다. 분할 구성이 있는 속성은 여전히 attr 구조체에 표시되지만 값은 분할된 모든 브랜치가 병합된 단일 목록이 됩니다.

super

unknown ctx.super()

실험적: 상위 구현 함수를 호출하고 제공자를 반환합니다.

target_platform_has_constraint

bool ctx.target_platform_has_constraint(constraintValue)

지정된 제약 조건 값이 현재 타겟 플랫폼에 포함되어 있으면 true를 반환합니다.

매개변수

매개변수 설명
constraintValue ConstraintValueInfo; required
타겟 플랫폼을 확인할 제약조건 값입니다.

도구 모음으로의 통합

ToolchainContext ctx.toolchains

이 규칙의 기본 실행 그룹의 도구 모음입니다.

var

dict ctx.var

구성 변수의 사전 (문자열에서 문자열로)입니다.

version_file

File ctx.version_file

현재 빌드 요청의 휘발성 작업공간 상태를 저장하는 데 사용되는 파일입니다. 자세한 내용은 --workspace_status_command 문서를 참고하세요.

workspace_name

string ctx.workspace_name

작업공간의 이름입니다. 이는 기본 저장소의 실행 루트 이름 및 runfiles 접두사와 같습니다. --enable_bzlmod가 사용 설정된 경우 고정된 문자열 _main입니다. 그렇지 않으면 WORKSPACE 파일에 정의된 작업공간 이름입니다.