1313# limitations under the License.
1414"""Rules for making .tar files."""
1515
16- load ("//pkg:path.bzl" , "compute_data_path" , "dest_path" )
1716load ("//pkg:providers.bzl" , "PackageVariablesInfo" )
1817load (
1918 "//pkg/private:pkg_files.bzl" ,
2019 "add_directory" ,
2120 "add_empty_file" ,
21+ "add_label_list" ,
2222 "add_single_file" ,
2323 "add_symlink" ,
24- "add_tree_artifact" ,
2524 "create_mapping_context_from_ctx" ,
26- "process_src" ,
2725 "write_manifest" ,
2826)
2927load ("//pkg/private:util.bzl" , "setup_output_files" , "substitute_package_variables" )
@@ -58,16 +56,8 @@ def _pkg_tar_impl(ctx):
5856
5957 # Files needed by rule implementation at runtime
6058 files = []
61-
6259 outputs , output_file , _ = setup_output_files (ctx )
6360
64- # Compute the relative path
65- data_path = compute_data_path (ctx .label , ctx .attr .strip_prefix )
66- data_path_without_prefix = compute_data_path (ctx .label , "." )
67-
68- # Find a list of path remappings to apply.
69- remap_paths = ctx .attr .remap_paths
70-
7161 # Start building the arguments.
7262 args = ctx .actions .args ()
7363 args .add ("--output" , output_file .path )
@@ -112,53 +102,36 @@ def _pkg_tar_impl(ctx):
112102 args .add ("--mtime" , "%d" % ctx .attr .mtime )
113103 if ctx .attr .portable_mtime :
114104 args .add ("--mtime" , "portable" )
105+ if ctx .attr .modes :
106+ for key in ctx .attr .modes :
107+ args .add ("--modes" , "%s=%s" % (_quote (key ), ctx .attr .modes [key ]))
108+ if ctx .attr .owners :
109+ for key in ctx .attr .owners :
110+ args .add ("--owners" , "%s=%s" % (_quote (key ), ctx .attr .owners [key ]))
111+ if ctx .attr .ownernames :
112+ for key in ctx .attr .ownernames :
113+ args .add (
114+ "--owner_names" ,
115+ "%s=%s" % (_quote (key ), ctx .attr .ownernames [key ]),
116+ )
115117
116118 # Now we begin processing the files.
119+ path_mapper = None
120+ if ctx .attr .remap_paths :
121+ path_mapper = lambda path : _remap (ctx .attr .remap_paths , path )
122+
117123 mapping_context = create_mapping_context_from_ctx (
118124 ctx ,
119125 label = ctx .label ,
120- include_runfiles = False , # TODO(aiuto): ctx.attr.include_runfiles,
126+ include_runfiles = ctx .attr .include_runfiles ,
121127 strip_prefix = ctx .attr .strip_prefix ,
122- default_mode = ctx .attr .mode ,
128+ # build_tar does the default modes. Consider moving attribute mapping
129+ # into mapping_context.
130+ default_mode = None ,
131+ path_mapper = path_mapper ,
123132 )
124133
125- # Start with all the pkg_* inputs
126- for src in ctx .attr .srcs :
127- if not process_src (
128- mapping_context ,
129- src = src ,
130- origin = src .label ,
131- ):
132- src_files = src [DefaultInfo ].files .to_list ()
133- if ctx .attr .include_runfiles :
134- runfiles = src [DefaultInfo ].default_runfiles
135- if runfiles :
136- mapping_context .file_deps .append (runfiles .files )
137- src_files .extend (runfiles .files .to_list ())
138-
139- # Add in the files of srcs which are not pkg_* types
140- for f in src_files :
141- d_path = dest_path (f , data_path , data_path_without_prefix )
142-
143- # Note: This extra remap is the bottleneck preventing this
144- # large block from being a utility method as shown below.
145- # Should we disallow mixing pkg_files in srcs with remap?
146- # I am fine with that if it makes the code more readable.
147- dest = _remap (remap_paths , d_path )
148- if f .is_directory :
149- add_tree_artifact (mapping_context .content_map , dest , f , src .label )
150- else :
151- # Note: This extra remap is the bottleneck preventing this
152- # large block from being a utility method as shown below.
153- # Should we disallow mixing pkg_files in srcs with remap?
154- # I am fine with that if it makes the code more readable.
155- dest = _remap (remap_paths , d_path )
156- add_single_file (mapping_context , dest , f , src .label )
157-
158- # TODO(aiuto): I want the code to look like this, but we don't have lambdas.
159- # transform_path = lambda f: _remap(
160- # remap_paths, dest_path(f, data_path, data_path_without_prefix))
161- # add_label_list(mapping_context, ctx.attr.srcs, transform_path)
134+ add_label_list (mapping_context , srcs = ctx .attr .srcs )
162135
163136 # The files attribute is a map of labels to destinations. We can add them
164137 # directly to the content map.
@@ -174,18 +147,6 @@ def _pkg_tar_impl(ctx):
174147 target .label ,
175148 )
176149
177- if ctx .attr .modes :
178- for key in ctx .attr .modes :
179- args .add ("--modes" , "%s=%s" % (_quote (key ), ctx .attr .modes [key ]))
180- if ctx .attr .owners :
181- for key in ctx .attr .owners :
182- args .add ("--owners" , "%s=%s" % (_quote (key ), ctx .attr .owners [key ]))
183- if ctx .attr .ownernames :
184- for key in ctx .attr .ownernames :
185- args .add (
186- "--owner_names" ,
187- "%s=%s" % (_quote (key ), ctx .attr .ownernames [key ]),
188- )
189150 for empty_file in ctx .attr .empty_files :
190151 add_empty_file (mapping_context , empty_file , ctx .label )
191152 for empty_dir in ctx .attr .empty_dirs or []:
@@ -289,7 +250,10 @@ pkg_tar_impl = rule(
289250 "extension" : attr .string (default = "tar" ),
290251 "symlinks" : attr .string_dict (),
291252 "empty_files" : attr .string_list (),
292- "include_runfiles" : attr .bool (),
253+ "include_runfiles" : attr .bool (
254+ doc = ("""Include runfiles for executables. These appear as they would in bazel-bin."""
255+ + """For example: 'path/to/myprog.runfiles/path/to/my_data.txt'.""" ),
256+ ),
293257 "empty_dirs" : attr .string_list (),
294258 "remap_paths" : attr .string_dict (),
295259 "compressor" : attr .label (
0 commit comments