pytorch
91 строка · 3.0 Кб
1# Only used for PyTorch open source BUCK build
2
3"""Provides utility macros for working with globs."""
4
5load("@bazel_skylib//lib:paths.bzl", "paths")
6
7def subdir_glob(glob_specs, exclude = None, prefix = ""):
8"""Returns a dict of sub-directory relative paths to full paths.
9
10The subdir_glob() function is useful for defining header maps for C/C++
11libraries which should be relative the given sub-directory.
12Given a list of tuples, the form of (relative-sub-directory, glob-pattern),
13it returns a dict of sub-directory relative paths to full paths.
14
15Please refer to native.glob() for explanations and examples of the pattern.
16
17Args:
18glob_specs: The array of tuples in form of
19(relative-sub-directory, glob-pattern inside relative-sub-directory).
20type: List[Tuple[str, str]]
21exclude: A list of patterns to identify files that should be removed
22from the set specified by the first argument. Defaults to [].
23type: Optional[List[str]]
24prefix: If is not None, prepends it to each key in the dictionary.
25Defaults to None.
26type: Optional[str]
27
28Returns:
29A dict of sub-directory relative paths to full paths.
30"""
31if exclude == None:
32exclude = []
33
34results = []
35
36for dirpath, glob_pattern in glob_specs:
37results.append(
38_single_subdir_glob(dirpath, glob_pattern, exclude, prefix),
39)
40
41return _merge_maps(*results)
42
43def _merge_maps(*file_maps):
44result = {}
45for file_map in file_maps:
46for key in file_map:
47if key in result and result[key] != file_map[key]:
48fail(
49"Conflicting files in file search paths. " +
50"\"%s\" maps to both \"%s\" and \"%s\"." %
51(key, result[key], file_map[key]),
52)
53
54result[key] = file_map[key]
55
56return result
57
58def _single_subdir_glob(dirpath, glob_pattern, exclude = None, prefix = None):
59if exclude == None:
60exclude = []
61results = {}
62files = native.glob([paths.join(dirpath, glob_pattern)], exclude = exclude)
63for f in files:
64if dirpath:
65key = f[len(dirpath) + 1:]
66else:
67key = f
68if prefix:
69key = paths.join(prefix, key)
70results[key] = f
71
72return results
73
74# Using a flat list will trigger build errors on Android.
75# cxx_library will generate an apple_library on iOS, a cxx_library on Android.
76# Those rules have different behaviors. Using a map will make the behavior consistent.
77#
78def glob_private_headers(glob_patterns, exclude = []):
79result = {}
80headers = native.glob(glob_patterns, exclude = exclude)
81for header in headers:
82result[paths.basename(header)] = header
83return result
84
85def glob(include, exclude = (), **kwargs):
86buildfile = native.read_config("buildfile", "name", "BUCK")
87subpkgs = [
88target[:-len(buildfile)] + "**/*"
89for target in native.glob(["*/**/" + buildfile])
90]
91return native.glob(include, exclude = list(exclude) + subpkgs, **kwargs)
92