aboutsummaryrefslogtreecommitdiff
path: root/rules/cc/yasm.bzl
blob: 5820784cd4d340015ce49a5f1f5d7ff5f59b8da9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Copyright (C) 2022 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("//build/bazel/platforms:platform_utils.bzl", "platforms")

"""Build rule for converting `.asm` files to `.o` files with yasm."""

def globalFlags(ctx):
    arch = platforms.get_target_arch(ctx.attr._platform_utils)
    linux = platforms.is_target_linux_or_android(ctx.attr._platform_utils)
    darwin = platforms.is_target_darwin(ctx.attr._platform_utils)

    if linux and arch == "x86_64":
        return ["-f", "elf64", "-m", "amd64"]
    if linux and arch == "x86":
        return ["-f", "elf32", "-m", "x86"]
    if linux and arch == "arm64":
        return ["-f", "elf64", "-m", "aarch64"]
    if linux and arch == "arm":
        return ["-f", "elf32", "-m", "arm"]
    if darwin:
        return ["-f", "macho", "-m", "amd64"]

    fail("Unable to detect target platform for compiling .asm files")

def _yasm_impl(ctx):
    common_args = (globalFlags(ctx) + ctx.attr.flags +
                   ["-I" + paths.join(ctx.label.package, d) for d in ctx.attr.include_dirs])

    outputs = [ctx.actions.declare_file(paths.replace_extension(src.path, ".o")) for src in ctx.files.srcs]
    for src, out in zip(ctx.files.srcs, outputs):
        ctx.actions.run(
            inputs = ctx.files.include_srcs,  # include_srcs will contain src
            outputs = [out],
            executable = ctx.executable._yasm,
            arguments = common_args + ["-o", out.path, src.path],
            mnemonic = "yasm",
        )

    return [DefaultInfo(files = depset(outputs))]

_yasm = rule(
    implementation = _yasm_impl,
    doc = "Generate object files from a .asm file using yasm.",
    attrs = {
        "srcs": attr.label_list(
            mandatory = True,
            allow_files = [".asm"],
            doc = "The asm source files for this rule",
        ),
        "include_srcs": attr.label_list(
            allow_files = [".inc", ".asm"],
            doc = "All files that could possibly be included from source files. " +
                  "This is necessary because starlark doesn't allow adding dependencies " +
                  "via .d files.",
        ),
        "include_dirs": attr.string_list(
            doc = "Include directories",
        ),
        "flags": attr.string_list(
            doc = "A list of options to be added to the yasm command line.",
        ),
        "_yasm": attr.label(
            default = "//prebuilts/misc:yasm",
            executable = True,
            cfg = "exec",
        ),
        "_platform_utils": attr.label(
            default = Label("//build/bazel/platforms:platform_utils"),
        ),
    },
)

def yasm(
        name,
        srcs,
        include_dirs = [],
        flags = [],
        target_compatible_with = [],
        tags = []):
    _yasm(
        name = name,
        srcs = srcs,
        flags = flags,
        include_dirs = include_dirs,
        include_srcs = native.glob(["**/*.inc", "**/*.asm"]),
        target_compatible_with = target_compatible_with,
        tags = tags,
    )