1"""Copyright (C) 2022 The Android Open Source Project
2Licensed under the Apache License, Version 2.0 (the "License");
3you may not use this file except in compliance with the License.
4You may obtain a copy of the License at
5     http://www.apache.org/licenses/LICENSE-2.0
6Unless required by applicable law or agreed to in writing, software
7distributed under the License is distributed on an "AS IS" BASIS,
8WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9See the License for the specific language governing permissions and
10limitations under the License.
11"""
12
13load("@bazel_skylib//lib:paths.bzl", "paths")
14load("@soong_injection//cc_toolchain:config_constants.bzl", "constants")
15load("//build/bazel/platforms:platform_utils.bzl", "platforms")
16
17def rs_flags(ctx):
18    # TODO handle target api flags once there is support for sdk versions b/245584567
19    flags = ["-Wall", "-Werror"]
20
21    flags.extend(ctx.attr.flags)
22
23    arch = platforms.get_target_arch(ctx.attr._platform_utils)
24
25    if arch in ["x86", "arm"]:
26        flags.append("-m32")
27    elif arch in ["x86_64", "arm64"]:
28        flags.append("-m64")
29
30    for flag in constants.RsGlobalIncludes:
31        flags.extend(["-I", flag])
32
33    return flags
34
35def _rscript_to_cpp_impl(ctx):
36    rs_files = ctx.files.srcs
37
38    outputs = []
39
40    for f in rs_files:
41        out_file_base = "ScriptC_" + paths.replace_extension(f.basename, "")
42        outputs.append(ctx.actions.declare_file(out_file_base + ".cpp"))
43        outputs.append(ctx.actions.declare_file(out_file_base + ".h"))
44
45    args = ctx.actions.args()
46    output_path = paths.join(ctx.bin_dir.path, ctx.label.package)
47    args.add("-o", output_path)
48    args.add("-reflect-c++")
49    args.add_all(rs_flags(ctx))
50    args.add_all([f.path for f in rs_files])
51
52    ctx.actions.run(
53        outputs = outputs,
54        inputs = rs_files + ctx.files._rs_headers,
55        executable = ctx.executable._rs_to_cc_tool,
56        arguments = [args],
57    )
58
59    return [DefaultInfo(files = depset(outputs))]
60
61rscript_to_cpp = rule(
62    implementation = _rscript_to_cpp_impl,
63    doc = "Generate C/C++ langauge sources from renderscript files",
64    attrs = {
65        "srcs": attr.label_list(
66            allow_files = [".rscript", ".fs"],
67            mandatory = True,
68        ),
69        "flags": attr.string_list(
70            doc = "",
71        ),
72        "_rs_headers": attr.label_list(
73            default = [
74                "//external/clang/lib:rs_clang_headers",
75                "//frameworks/rs/script_api:rs_script_api",
76            ],
77        ),
78        "_rs_to_cc_tool": attr.label(
79            # TODO use non-prebuilt llvm-rs-cc b/245736162
80            default = "//prebuilts/sdk/tools:linux/bin/llvm-rs-cc",
81            allow_files = True,
82            cfg = "exec",
83            executable = True,
84        ),
85        "_platform_utils": attr.label(
86            default = Label("//build/bazel/platforms:platform_utils"),
87        ),
88    },
89)
90