aboutsummaryrefslogtreecommitdiffstats
path: root/bazel/functions.bzl
blob: bc90287f92a8226dffa88a360e1d227b6be4341b (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
# Helper functions for implementing Bazel rules.

def create_option_file(ctx, name, content):
    """ Create the command line options file """
    options_file = ctx.actions.declare_file(name)
    ctx.actions.write(output = options_file, content = content)
    return options_file

def create_java_compiler_args_srcs(ctx, srcs, path, deps):
    return create_java_compiler_args_srcs_deps(
        ctx,
        srcs,
        path,
        ":".join([dep.path for dep in deps]),
    )

def create_java_compiler_args_srcs_deps(ctx, srcs, jar, deps):
    args = []
    option_files = []

    # Classpath
    if deps:
        cp_file = create_option_file(ctx, jar.basename + ".cp", deps)
        option_files += [cp_file]
        args += ["-cp", "@" + cp_file.path]

    # Output
    args += ["-o", jar.path]

    # Source files
    srcs_lines = "\n".join(srcs)
    source_file = create_option_file(ctx, jar.basename + ".lst", srcs_lines)
    option_files += [source_file]
    args += ["@" + source_file.path]

    return (args, option_files)

def create_java_compiler_args(ctx, path, deps):
    return create_java_compiler_args_srcs(
        ctx,
        [src.path for src in ctx.files.srcs],
        path,
        deps,
    )

# Adds an explict target-name part if label doesn't have it.
def explicit_target(label):
    return label if ":" in label else label + ":" + label.rsplit("/", 1)[-1]

# Converts label package to a path relative to the execroot.
def label_workspace_path(label):
    if label.workspace_root != "":
        return label.workspace_root + "/" + label.package
    return label.package

# Converts a relative path to be relative to the execroot.
def workspace_path(path):
    return label_workspace_path(Label("//" + path, relative_to_caller_repository = True))