• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Copyright (C) 2021 The Android Open Source Project
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15"""
16
17load("//build/bazel/rules/cc:cc_constants.bzl", "constants")
18
19def extension(f):
20    return f.split(".")[-1]
21
22def group_files_by_ext(files):
23    cpp = []
24    c = []
25    asm = []
26
27    # This for-loop iterator works because filegroups in Android don't use
28    # configurable selects.
29    for f in files:
30        if extension(f) in constants.c_src_exts:
31            c += [f]
32        elif extension(f) in constants.cpp_src_exts:
33            cpp += [f]
34        elif extension(f) in constants.as_src_exts:
35            asm += [f]
36        else:
37            # not C based
38            continue
39    return cpp, c, asm
40
41# Filegroup is a macro because it needs to expand to language specific source
42# files for cc_library's srcs_as, srcs_c and srcs attributes.
43def filegroup(name, srcs = [], **kwargs):
44    native.filegroup(
45        name = name,
46        srcs = srcs,
47        **kwargs
48    )
49
50    # These genrule prevent empty filegroups being used as deps to cc libraries,
51    # avoiding the error:
52    #
53    # in srcs attribute of cc_library rule //foo/bar:baz:
54    # '//foo/bar/some_other:baz2' does not produce any cc_library srcs files.
55    native.genrule(
56        name = name + "_null_cc",
57        outs = [name + "_null.cc"],
58        cmd = "touch $@",
59    )
60    native.genrule(
61        name = name + "_null_c",
62        outs = [name + "_null.c"],
63        cmd = "touch $@",
64    )
65    native.genrule(
66        name = name + "_null_s",
67        outs = [name + "_null.S"],
68        cmd = "touch $@",
69    )
70
71    cpp_srcs, c_srcs, as_srcs = group_files_by_ext(srcs)
72    native.filegroup(
73        name = name + "_cpp_srcs",
74        srcs = [name + "_null.cc"] + cpp_srcs,
75    )
76    native.filegroup(
77        name = name + "_c_srcs",
78        srcs = [name + "_null.c"] + c_srcs,
79    )
80    native.filegroup(
81        name = name + "_as_srcs",
82        srcs = [name + "_null.S"] + as_srcs,
83    )
84