-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.bzl
152 lines (123 loc) · 4.29 KB
/
utils.bzl
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def extract(rule, path):
name = 'extract-' + (
rule.replace(':','') + '-' +
path
.replace('/','-')
.replace('.','-'))
if not native.rule_exists(':'+name):
filename = path.split('/')[-1]
native.genrule(
name = name,
out = filename,
cmd = 'cp $(location '+rule+')/'+ path +' $OUT')
return ':'+name
def extractFolder(rule, path):
name = 'extract-folder-' + (
rule.replace(':','') + '-' +
path
.replace('/','-')
.replace('.','-'))
if not native.rule_exists(':'+name):
native.genrule(
name = name,
out = 'out',
cmd = 'mkdir $OUT && cd $OUT && cp -r $(location '+rule+')/'+ path +'/. .')
return ':'+name
def pkgconfig(name, find, search = None, visibility = []):
env = 'PKG_CONFIG_PATH=' + search + ' ' if search else ''
native.genrule(
name = name+'-flags',
out = 'out.txt',
cmd = env + 'pkg-config ' + find + ' --cflags > $OUT')
native.genrule(
name = name+'-linker',
out = 'out.txt',
cmd = env + 'pkg-config ' + find + ' --libs > $OUT')
native.prebuilt_cxx_library(
name = name,
header_namespace = '',
header_only = True,
exported_preprocessor_flags = [
'@$(location :'+name+'-flags)',
],
exported_linker_flags = [
'@$(location :'+name+'-linker)',
],
visibility = visibility)
def cmake(name, srcs = [], options = [], targets = [], out = 'build', prefix = 'ROOT', jobs = 1):
native.genrule(
name = name,
srcs = srcs,
out = out,
cmd = ' && '.join([
'mkdir $OUT $OUT/'+prefix,
'cd $OUT',
'cmake -DCMAKE_INSTALL_PREFIX:PATH=$OUT/' + prefix + ' '.join(options)+ ' $SRCDIR'] +
(['make -j'+ jobs + ' '.join(targets) ] if len(targets) else [])
))
"""Provides utility macros for working with globs."""
def _paths_join(path, *others):
"""Joins one or more path components."""
result = path
for p in others:
if p.startswith("/"): # absolute
result = p
elif not result or result.endswith("/"):
result += p
else:
result += "/" + p
return result
def subdir_glob(glob_specs, exclude = None, prefix = ""):
"""Returns a dict of sub-directory relative paths to full paths.
The subdir_glob() function is useful for defining header maps for C/C++
libraries which should be relative the given sub-directory.
Given a list of tuples, the form of (relative-sub-directory, glob-pattern),
it returns a dict of sub-directory relative paths to full paths.
Please refer to native.glob() for explanations and examples of the pattern.
Args:
glob_specs: The array of tuples in form of
(relative-sub-directory, glob-pattern inside relative-sub-directory).
type: List[Tuple[str, str]]
exclude: A list of patterns to identify files that should be removed
from the set specified by the first argument. Defaults to [].
type: Optional[List[str]]
prefix: If is not None, prepends it to each key in the dictionary.
Defaults to None.
type: Optional[str]
Returns:
A dict of sub-directory relative paths to full paths.
"""
if exclude == None:
exclude = []
results = []
for dirpath, glob_pattern in glob_specs:
results.append(
_single_subdir_glob(dirpath, glob_pattern, exclude, prefix),
)
return _merge_maps(*results)
def _merge_maps(*file_maps):
result = {}
for file_map in file_maps:
for key in file_map:
if key in result and result[key] != file_map[key]:
fail(
"Conflicting files in file search paths. " +
"\"%s\" maps to both \"%s\" and \"%s\"." %
(key, result[key], file_map[key]),
)
result[key] = file_map[key]
return result
def _single_subdir_glob(dirpath, glob_pattern, exclude = None, prefix = None):
if exclude == None:
exclude = []
results = {}
files = native.glob([_paths_join(dirpath, glob_pattern)], exclude = exclude)
for f in files:
if dirpath:
key = f[len(dirpath) + 1:]
else:
key = f
if prefix:
key = _paths_join(prefix, key)
results[key] = f
return results