forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_to_dist.bzl
117 lines (102 loc) · 3.85 KB
/
copy_to_dist.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
# Copyright 2021 Google LLC. All Rights Reserved.
#
# 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("@bazel_skylib//lib:paths.bzl", "paths")
def _copy_to_dist_impl(ctx):
if ctx.attr.global_root and ctx.attr.root:
fail("Both 'root' and 'global_root' were passed " +
"but only one is expected")
files = [f for s in ctx.attr.srcs for f in s.files.to_list()]
if ctx.attr.global_root:
root_dir = ctx.attr.global_root
else:
root_dir = paths.join(paths.dirname(ctx.build_file_path), ctx.attr.root)
outputs = []
for f in files:
dest_path = paths.join(ctx.attr.dest_dir, paths.relativize(f.short_path, root_dir))
if ctx.attr.extension:
dest_path = paths.replace_extension(dest_path, ctx.attr.extension)
out = ctx.actions.declare_file(dest_path)
outputs.append(out)
ctx.actions.symlink(
output = out,
target_file = f,
)
return [DefaultInfo(files = depset(outputs))]
copy_to_dist = rule(
implementation = _copy_to_dist_impl,
attrs = {
"dest_dir": attr.string(
default = "dist",
doc = "Destination directory to copy the source file tree to",
),
"extension": attr.string(
doc = "New file extension to use for each file",
),
"global_root": attr.string(
default = "",
doc = "Root path to remove when symlinking. Relative to the repository root",
),
"root": attr.string(
default = "",
doc = "Common root path to remove when symlinking. Relative to the build file's directory",
),
"srcs": attr.label_list(
doc = "Files to create symlinks of",
),
},
doc = """Creates symlinks in 'dest_dir' for each file in 'srcs'
Preserves relative paths between linked files. A common root path of the
input files can be removed via the 'root' arg.
This rule is used to 'copy' the results of compiling a tfjs package's
sources located in 'src' to the output directory 'dist' while preserving
the filetree's relative paths. The benifit of using this rule is that Bazel
is aware of the copied files and can use them in other rules like 'pkg_npm'.
It is also used for copying tfjs bundles and miniprogram outputs.
""",
)
def copy_ts_library_to_dist(name, srcs, root = "", dest_dir = "dist"):
# Declaration (.d.ts) files
declaration = name + "_declaration"
native.filegroup(
name = declaration,
srcs = srcs,
)
# ES Module es2017 compilation results. This is configured in
# tools/defaults.bzl. Outputs '.mjs' files.
esm = name + "_esm_sources"
native.filegroup(
name = esm,
srcs = srcs,
output_group = "es6_sources",
)
copy_esm = name + "_copy_esm"
copy_to_dist(
name = copy_esm,
srcs = [esm],
root = root,
dest_dir = dest_dir,
extension = ".js", # Rewrite '.mjs' extension to '.js'
)
copy_esm_declaration = name + "_copy_esm_declaration"
copy_to_dist(
name = copy_esm_declaration,
srcs = [declaration],
root = root,
dest_dir = dest_dir,
)
native.filegroup(
name = name,
srcs = [copy_esm, copy_esm_declaration],
)