aboutsummaryrefslogtreecommitdiff
path: root/rules/filegroup.bzl
blob: f1ab826e4e3d1f7681d74bbeaa642fdfba40bf1d (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
# Copyright (C) 2021 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("//build/bazel/rules/cc:cc_constants.bzl", "constants")

def extension(f):
    return f.split(".")[-1]

def group_files_by_ext(files):
    cpp = []
    c = []
    asm = []

    # This for-loop iterator works because filegroups in Android don't use
    # configurable selects.
    for f in files:
        if extension(f) in constants.c_src_exts:
            c.append(f)
        elif extension(f) in constants.cpp_src_exts:
            cpp.append(f)
        elif extension(f) in constants.as_src_exts:
            asm.append(f)
        else:
            # not C based
            continue
    return cpp, c, asm

# Filegroup is a macro because it needs to expand to language specific source
# files for cc_library's srcs_as, srcs_c and srcs attributes.
def filegroup(name, srcs = [], **kwargs):
    native.filegroup(
        name = name,
        srcs = srcs,
        **kwargs
    )

    # These genrule prevent empty filegroups being used as deps to cc libraries,
    # avoiding the error:
    #
    # in srcs attribute of cc_library rule //foo/bar:baz:
    # '//foo/bar/some_other:baz2' does not produce any cc_library srcs files.
    native.genrule(
        name = name + "_null_cc",
        outs = [name + "_null.cc"],
        tags = ["manual"],
        cmd = "touch $@",
    )
    native.genrule(
        name = name + "_null_c",
        outs = [name + "_null.c"],
        tags = ["manual"],
        cmd = "touch $@",
    )
    native.genrule(
        name = name + "_null_s",
        outs = [name + "_null.S"],
        tags = ["manual"],
        cmd = "touch $@",
    )

    cpp_srcs, c_srcs, as_srcs = group_files_by_ext(srcs)
    native.filegroup(
        name = name + "_cpp_srcs",
        srcs = [name + "_null.cc"] + cpp_srcs,
        tags = ["manual"],
    )
    native.filegroup(
        name = name + "_c_srcs",
        srcs = [name + "_null.c"] + c_srcs,
        tags = ["manual"],
    )
    native.filegroup(
        name = name + "_as_srcs",
        srcs = [name + "_null.S"] + as_srcs,
        tags = ["manual"],
    )