• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Copyright (C) 2022 The Android Open Source Project
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7     http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14"""
15
16load("@bazel_skylib//lib:paths.bzl", "paths")
17load("//build/bazel/platforms:platform_utils.bzl", "platforms")
18
19"""Build rule for converting `.asm` files to `.o` files with yasm."""
20
21def globalFlags(ctx):
22    arch = platforms.get_target_arch(ctx.attr._platform_utils)
23    linux = platforms.is_target_linux_or_android(ctx.attr._platform_utils)
24    darwin = platforms.is_target_darwin(ctx.attr._platform_utils)
25
26    if linux and arch == "x86_64":
27        return ["-f", "elf64", "-m", "amd64"]
28    if linux and arch == "x86":
29        return ["-f", "elf32", "-m", "x86"]
30    if linux and arch == "arm64":
31        return ["-f", "elf64", "-m", "aarch64"]
32    if linux and arch == "arm":
33        return ["-f", "elf32", "-m", "arm"]
34    if darwin:
35        return ["-f", "macho", "-m", "amd64"]
36
37    fail("Unable to detect target platform for compiling .asm files")
38
39def _yasm_impl(ctx):
40    common_args = (globalFlags(ctx) + ctx.attr.flags +
41                   ["-I" + paths.join(ctx.label.package, d) for d in ctx.attr.include_dirs])
42
43    outputs = [ctx.actions.declare_file(paths.replace_extension(src.path, ".o")) for src in ctx.files.srcs]
44    for src, out in zip(ctx.files.srcs, outputs):
45        ctx.actions.run(
46            inputs = ctx.files.include_srcs,  # include_srcs will contain src
47            outputs = [out],
48            executable = ctx.executable._yasm,
49            arguments = common_args + ["-o", out.path, src.path],
50            mnemonic = "yasm",
51        )
52
53    return [DefaultInfo(files = depset(outputs))]
54
55_yasm = rule(
56    implementation = _yasm_impl,
57    doc = "Generate object files from a .asm file using yasm.",
58    attrs = {
59        "srcs": attr.label_list(
60            mandatory = True,
61            allow_files = [".asm"],
62            doc = "The asm source files for this rule",
63        ),
64        "include_srcs": attr.label_list(
65            allow_files = [".inc", ".asm"],
66            doc = "All files that could possibly be included from source files. " +
67                  "This is necessary because starlark doesn't allow adding dependencies " +
68                  "via .d files.",
69        ),
70        "include_dirs": attr.string_list(
71            doc = "Include directories",
72        ),
73        "flags": attr.string_list(
74            doc = "A list of options to be added to the yasm command line.",
75        ),
76        "_yasm": attr.label(
77            default = "//prebuilts/misc:yasm",
78            executable = True,
79            cfg = "exec",
80        ),
81        "_platform_utils": attr.label(
82            default = Label("//build/bazel/platforms:platform_utils"),
83        ),
84    },
85)
86
87def yasm(
88        name,
89        srcs,
90        include_dirs = [],
91        flags = [],
92        target_compatible_with = [],
93        tags = []):
94    _yasm(
95        name = name,
96        srcs = srcs,
97        flags = flags,
98        include_dirs = include_dirs,
99        include_srcs = native.glob(["**/*.inc", "**/*.asm"]),
100        target_compatible_with = target_compatible_with,
101        tags = tags,
102    )
103