blob: 21b3226d151c91a487aab6a49bc37f256a62a6cc [file]
{{- /* Collect all the conditionals used by all targets in this directory */}}
{{- $AllConditionals := $.DecomposedConditionals}}
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
{{- if $.Project.Target $ "proto"}}
load("@com_google_protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
{{- end}}
load("//src/tint:flags.bzl", "COPTS")
load("@bazel_skylib//lib:selects.bzl", "selects")
exports_files(glob(["*.def", "*.tmpl"], allow_empty = True))
{{/* newline */}}
{{- Eval "TargetIfNotEmpty" ($.Project.Target $ "lib")}}
{{- Eval "TargetIfNotEmpty" ($.Project.Target $ "proto")}}
{{- Eval "TargetIfNotEmpty" ($.Project.Target $ "test")}}
{{- Eval "TargetIfNotEmpty" ($.Project.Target $ "bench")}}
{{- Eval "TargetIfNotEmpty" ($.Project.Target $ "cmd")}}
{{- Eval "TargetIfNotEmpty" ($.Project.Target $ "test_cmd")}}
{{- Eval "TargetIfNotEmpty" ($.Project.Target $ "bench_cmd")}}
{{- Eval "ConditionalRules" $AllConditionals}}
{{- /*
--------------------------------------------------------------------------------
-- Emits a Bazel target if it contains any files
--------------------------------------------------------------------------------
*/ -}}
{{- define "TargetIfNotEmpty"}}
{{- if $}}
{{- if and $.Condition (Contains $.Condition.String "tint_build_glsl_validator")}}
# GLSL validator is not supported by the bazel build configuration.
{{- else if and $.Condition (Contains $.Condition.String "tint_build_tintd")}}
# WGSL Language Server (tintd) is not supported by the bazel build configuration.
{{- else}}
{{- if or (len $.SourceFiles) (len $.GeneratedSourcePaths.List)}}{{Eval "Target" $}}{{end}}
{{- end}}
{{- end}}
{{- end}}
{{- /*
--------------------------------------------------------------------------------
-- Emits a Bazel target
--------------------------------------------------------------------------------
*/ -}}
{{- define "Target"}}
{{- $Conditionals := $.Conditionals}}
{{- if $.Kind.IsProto -}}
proto_library(
name = "{{$.Directory.Name}}_proto",
srcs = [
{{- range $File := $.UnconditionalSourceFiles}}
"{{TrimPrefix $File.Name $.Directory.Path}}",
{{- end}}
],
deps = [
{{- range $Dep := $.UnconditionalInternalDependencies}}
"//src/tint/{{$Dep.Directory.Path}}:{{$Dep.Directory.Name}}_proto",
{{- end}}
{{- range $Dep := $.UnconditionalExternalDependencies}}
{{- $Target := Eval "ExternalDependencyTarget" $Dep}}
{{- if $Target}}
{{$Target}}
{{- end}}
{{- end}}
],
visibility = ["//visibility:public"],
)
cc_proto_library(
name = "proto",
deps = [":{{$.Directory.Name}}_proto"],
visibility = ["//visibility:public"],
)
{{- else -}}
{{- if $.Kind.IsLib -}}
cc_library(
name = "{{$.Directory.Name}}",
{{- else if $.Kind.IsProto -}}
cc_library(
name = "proto",
{{- else if $.Kind.IsTest -}}
cc_library(
name = "test",
alwayslink = True,
{{- else if $.Kind.IsBench -}}
cc_library(
name = "bench",
alwayslink = True,
{{- else if $.Kind.IsCmd -}}
cc_binary(
name = "cmd",
{{- else if $.Kind.IsTestCmd -}}
cc_binary(
name = "test_cmd",
{{- else if $.Kind.IsBenchCmd -}}
cc_binary(
name = "bench_cmd",
{{- end}}
srcs = [
{{- if $.Kind.IsProto}}
{{- range $File := $.GeneratedProtobufSources.List}}
{{- if HasSuffix $File ".cc"}}
"{{TrimPrefix $File (printf "%s/" $.Directory.Path)}}",
{{- end}}
{{- end}}
{{- else}}
{{- range $File := $.UnconditionalSourceFiles}}
{{- if or (not $.Kind.IsLib) (HasSuffix $File.Name ".cc")}}
{{- if not ($.IsGeneratedSource $File.Path)}}
"{{TrimPrefix $File.Name $.Directory.Path}}",
{{- end}}
{{- end}}
{{- end}}
{{- range $Path := $.GeneratedSourcePaths.List}}
"//src/tint:gen/src/tint/{{$Path}}",
{{- end}}
{{- end}}
]
{{- if $Conditionals.HasSourceFiles}}
{{- range $Cond := $Conditionals}} + select({
{{- if $Cond.SourceFiles}}
":{{Eval "ConditionTarget" $Cond.Condition}}": [
{{- range $File := $Cond.SourceFiles}}
"{{TrimPrefix $File.Name $.Directory.Path}}",
{{- end}}
],
{{- end}}
"//conditions:default": [],
})
{{- end}}
{{- end}},
{{- if or $.Kind.IsLib $.Kind.IsProto}}
hdrs = [
{{- if $.Kind.IsProto}}
{{- range $File := $.GeneratedProtobufSources.List}}
{{- if not (HasSuffix $File ".cc")}}
"{{TrimPrefix $File (printf "%s/" $.Directory.Path)}}",
{{- end}}
{{- end}}
{{- else}}
{{- range $File := $.UnconditionalSourceFiles}}
{{- if not (HasSuffix $File.Name ".cc")}}
{{- if not ($.IsGeneratedSource $File.Path)}}
"{{TrimPrefix $File.Name $.Directory.Path}}",
{{- end}}
{{- end}}
{{- end}}
{{- range $Path := $.GeneratedSourcePaths.List}}
{{- if not (HasSuffix $Path ".cc")}}
"//src/tint:gen/src/tint/{{$Path}}",
{{- end}}
{{- end}}
{{- end}}
],
{{- end}}
deps = [
{{- /* Emit unconditional internal dependencies */}}
{{- range $Dep := $.UnconditionalInternalDependencies}}
"{{Eval "Dependency" $Dep}}",
{{- end}}
{{- if and (not $.Kind.IsProto) $.GeneratedSourcePaths.List}}
"//src/tint:generated_tint_headers",
{{- end}}
{{- /* Emit unconditional external dependencies */}}
{{- range $Dep := $.UnconditionalExternalDependencies}}
{{- $Target := Eval "ExternalDependencyTarget" $Dep}}
{{- if $Target}}
{{$Target}}
{{- end}}
{{- end}}
]
{{- if $Conditionals.HasDependencies}}
{{- range $Cond := $Conditionals}} + select({
{{- if or $Cond.InternalDependencies $Cond.ExternalDependencies}}
":{{Eval "ConditionTarget" $Cond.Condition}}": [
{{- range $Dep := $Cond.InternalDependencies}}
"{{Eval "Dependency" $Dep}}",
{{- end}}
{{- range $Dep := $Cond.ExternalDependencies}}
{{- $Target := Eval "ExternalDependencyTarget" $Dep}}
{{- if $Target}}
{{$Target}}
{{- end}}
{{- end}}
],
{{- end}}
"//conditions:default": [],
})
{{- end}}
{{- end}},
copts = COPTS,
visibility = ["//visibility:public"],
)
{{- end}}
{{/* newline */}}
{{- end}}
{{- /*
--------------------------------------------------------------------------------
-- Emits a dependency target name
--------------------------------------------------------------------------------
*/ -}}
{{- define "Dependency" -}}
{{- if $.Kind.IsLib -}}//src/tint/{{$.Directory.Path}}
{{- else if $.Kind.IsProto -}}//src/tint/{{$.Directory.Path}}:proto
{{- else if $.Kind.IsTest -}}//src/tint/{{$.Directory.Path}}:test
{{- else if $.Kind.IsBench -}}//src/tint/{{$.Directory.Path}}:bench
{{- else if $.Kind.IsCmd -}}//src/tint/{{$.Directory.Path}}:cmd
{{- else if $.Kind.IsTestCmd -}}//src/tint/{{$.Directory.Path}}:test_cmd
{{- else if $.Kind.IsBenchCmd -}}//src/tint/{{$.Directory.Path}}:bench_cmd
{{- else if $.Kind.IsFuzz -}}//src/tint/{{$.Directory.Path}}
{{- else if $.Kind.IsFuzzCmd -}}//src/tint/{{$.Directory.Path}}:fuzz_cmd
{{- end}}
{{- end}}
{{- /*
--------------------------------------------------------------------------------
-- Emits comma separated, quoted external dependency target name(s) with a trailing comma
-- External target names are declared in tools/src/cmd/gen/build/externals.json
--------------------------------------------------------------------------------
*/ -}}
{{- define "ExternalDependencyTarget"}}
{{- if eq $.Name "abseil" -}}"@abseil_cpp//absl/strings",
{{- else if eq $.Name "dl" -}}{{/* unsupported */}}
{{- else if eq $.Name "dxc-include" -}}"//third_party/directx-shader-compiler:dxc_headers",
{{- else if eq $.Name "dxcompiler-for-fuzzer" -}}{{/* unsupported */}}
{{- else if eq $.Name "glslang-res-limits" -}}{{/* unsupported */}}
{{- else if eq $.Name "glslang" -}}{{/* unsupported */}}
{{- else if eq $.Name "gmock" -}}"@gtest",
{{- else if eq $.Name "google-benchmark" -}}"@benchmark",
{{- else if eq $.Name "gtest" -}}"@gtest",
{{- else if eq $.Name "jsoncpp" -}}{{/* unsupported */}}
{{- else if eq $.Name "langsvr" -}}{{/* unsupported */}}
{{- else if eq $.Name "mesa" -}}{{/* unsupported */}}
{{- else if eq $.Name "metal" -}}{{/* unsupported */}}
{{- else if eq $.Name "spirv-headers" -}}"@spirv_headers//:spirv_cpp11_headers", "@spirv_headers//:spirv_c_headers",
{{- else if eq $.Name "spirv-opt-internal" -}}"@spirv_tools//:spirv_tools_opt",
{{- else if eq $.Name "spirv-tools" -}}"@spirv_tools",
{{- else if eq $.Name "thread" -}}{{/* unsupported */}}
{{- else if eq $.Name "winsock" -}}{{/* unsupported */}}
{{- else if eq $.Name "src_utils_crash_handler" -}}"//src/utils:crash_handler",
{{- else if eq $.Name "src_utils" -}}"//src/utils",
{{- else if eq $.Name "src_utils_chromium_test_compat" -}}"//src/utils/chromium_test_compat",
{{- else -}}{{Error (printf "unhandled external dependency '%v'" $.Name)}}
{{- end}}
{{- end}}
{{- /*
--------------------------------------------------------------------------------
-- Emits all the conditional rules required by the targets
--------------------------------------------------------------------------------
*/ -}}
{{- define "ConditionalRules"}}
{{- if $.Unarys}}
{{- range $Unary := $.Unarys}}
alias(
name = "{{Eval "ConditionTarget" $Unary}}",
actual = "//src/tint:{{$Unary.Var}}_{{not $Unary.Negate}}",
)
{{/* newline */}}
{{- end}}
{{- end}}
{{- if $.Ors}}
{{- range $Ors := $.Ors}}
selects.config_setting_group(
name = "{{Eval "ConditionTarget" $Ors}}",
match_any = [
{{- range $Unary := $Ors}}
"{{Eval "ConditionTarget" $Unary}}",
{{- end}}
],
)
{{/* newline */}}
{{- end}}
{{- end}}
{{- if $.Ands}}
{{- range $Ands := $.Ands}}
selects.config_setting_group(
name = "{{Eval "ConditionTarget" $Ands}}",
match_all = [
{{- range $Ors := $Ands}}
":{{Eval "ConditionTarget" $Ors}}",
{{- end}}
],
)
{{- end}}
{{/* newline */}}
{{- end}}
{{- end}}
{{- /*
--------------------------------------------------------------------------------
-- Emits a target name for a condition expression
--------------------------------------------------------------------------------
*/ -}}
{{- define "ConditionTarget" -}}
{{- $C := $.String}}
{{- $C := Replace $C " && " "_and_"}}
{{- $C := Replace $C " || " "_or_"}}
{{- $C := Replace $C "!" "not_"}}
{{- $C := Replace $C "(" "_"}}
{{- $C := Replace $C ")" "_"}}
{{- $C := Replace $C " " "_"}}
{{- $C}}
{{- end}}